Skip to content

Instantly share code, notes, and snippets.

@Padilo300
Last active September 30, 2020 11:53
Show Gist options
  • Save Padilo300/478c4069046a0446fe0902bed4456efc to your computer and use it in GitHub Desktop.
Save Padilo300/478c4069046a0446fe0902bed4456efc to your computer and use it in GitHub Desktop.
WP Bitrix интеграция utm
<?php
/*
Plugin Name: B24IntegrationForm
Author: @bobmv & @xxxnonsense & @padilo300
Description: Интеграция ContactForm7 и CRM Bitrix24. Генерация лидов с привязкой UTM-меток
Version: 1.0.1
Author URI: http://padilo.pro
*/
define('B24_WEBHOOK','https://bitrix24.ua/rest/1/4yuq0m4u3h3eq73g/');
add_action('init', 'save_utm');
add_action('wpcf7_before_send_mail', 'connect_b24');
function save_utm(){
if( isset( $_GET['utm_source'] ) ) {
setcookie('utm_source',$_GET['utm_source'], time()+ (90*86400));
if($_GET['utm_source'] === 'googleads'){
setcookie('referer','8', time()+ (90*86400));
}elseif ($_GET['utm_source'] === 'facebook'){
setcookie('referer','4', time()+ (90*86400));
}
}else{
if (stripos($_SERVER['HTTP_REFERER'],'google') ){
setcookie('referer','11', time()+ (90*86400));
}else if(!array_key_exists('utm_source',$_COOKIE)){
setcookie('referer','WEB', time()+ (90*86400));
}
}
if( isset( $_GET['utm_medium'] ) ) {
setcookie('utm_medium',$_GET['utm_medium'], time()+ (90*86400));
}
if( isset( $_GET['utm_campaign'] ) ) {
setcookie('utm_campaign',$_GET['utm_campaign'], time()+ (90*86400));
}
if( isset( $_GET['utm_content'] ) ) {
setcookie('utm_content',$_GET['utm_content'], time()+ (90*86400));
}
if( isset( $_GET['utm_term'] ) ) {
setcookie('utm_term',$_GET['utm_term'], time()+ (90*86400));
}
}
save_utm();
function toTelegram($messaggio = 'Аргумент не переданн', $chatID = 544625714){
$token = '860948571:AAGHoIqIHlHt8UVpa87UZJPnAlBC6JGAoc4';
$url = "https://api.telegram.org/bot" . $token . "/sendMessage?chat_id=" . $chatID;
$url = $url . "&text=" . urlencode($messaggio);
$ch = curl_init();
$optArray = array(
CURLOPT_URL => $url,
CURLOPT_RETURNTRANSFER => true
);
curl_setopt_array($ch, $optArray);
$result = curl_exec($ch);
curl_close($ch);
return $result;
}
function BX($method, $var)
{
$queryUrl = B24_WEBHOOK.$method.'.json';
$queryData = http_build_query($var);
$curl = curl_init();
curl_setopt_array($curl, array(
CURLOPT_SSL_VERIFYPEER => 0,
CURLOPT_POST => 1,
CURLOPT_HEADER => 0,
CURLOPT_RETURNTRANSFER => 1,
CURLOPT_URL => $queryUrl,
CURLOPT_POSTFIELDS => $queryData,
));
return json_decode(curl_exec($curl), true);
}
function connect_b24()
{
$submission = WPCF7_Submission::get_instance();
if ($submission) {
$posted_data = $submission->get_posted_data();
switch ($posted_data['_wpcf7'])
{
case '99':
$form_name = 'Контактна форма 1';
break;
case '105':
$form_name = 'Контактная форма';
break;
case '272':
$form_name = 'Контактная форма в блоге';
break;
case '369':
$form_name = 'Контактная форма на русском';
break;
default:
$form_name = 'Заявка с сайта';
}
$EMAIL_by_find_contact = FALSE;
$PHONE_by_find_contact = FALSE;
if(array_key_exists('your-tel',$posted_data) &&!empty($posted_data['your-tel'])){
$PHONE_by_find_contact = [
$posted_data['your-tel'],
'+' .$posted_data['your-tel'],
'+3' .$posted_data['your-tel'],
'+38' .$posted_data['your-tel'],
'+380' .$posted_data['your-tel'],
];
}
if(array_key_exists('your-email',$posted_data) && !empty($posted_data['your-email'])){
$EMAIL_by_find_contact = $posted_data['your-email'];
}
$contactDataPhone = [
'type' => 'PHONE',
'values' => $PHONE_by_find_contact,
'entity_type' => 'CONTACT',
];
$contactDataEmail = [
'type' => 'EMAIL',
'values' => $EMAIL_by_find_contact,
'entity_type' => 'CONTACT',
];
$contactId = null;
$duplicateByPhone = BX('crm.duplicate.findbycomm',$contactDataPhone);
$duplicateByEmail = BX('crm.duplicate.findbycomm',$contactDataEmail);
toTelegram("\$duplicateByPhone \n " . json_encode($duplicateByPhone));
toTelegram("\$duplicateByEmail \n " . json_encode($duplicateByEmail));
if(array_key_exists('CONTACT',$duplicateByPhone['result']) && $duplicateByPhone['result']['CONTACT'][0] > 0){
$contactId = $duplicateByPhone['result']['CONTACT'][0];
}else if (array_key_exists('CONTACT',$duplicateByEmail['result']) && $duplicateByEmail['result']['CONTACT'][0] > 0){
$contactId = $duplicateByEmail['result']['CONTACT'][0];
}else{
$contactData = [
'fields' => [
"NAME" => $posted_data['your-name'],
"OPENED" => "Y",
"ASSIGNED_BY_ID"=> 1,
"TYPE_ID" => "CLIENT",
"SOURCE_ID" => $_COOKIE['referer'],
"PHONE" => $posted_data['your-tel']==null?:[['VALUE'=>$posted_data['your-tel'],'VALUE_TYPE'=>"WORK"]],
'EMAIL' => $posted_data['your-email']==null?:[['VALUE'=>$posted_data['your-email'],'VALUE_TYPE'=>"WORK"]],
],
'params' => [ "REGISTER_SONET_EVENT" => "Y" ]
];
$contactId = BX('crm.contact.add',$contactData)['result'];
toTelegram("\$contactId \n " . json_encode($contactId));
}
$data = [
'fields'=>[
'TITLE' => $form_name,
'NAME' => $posted_data['your-name'],
'PHONE' => $posted_data['your-tel']==null?:[['VALUE'=>$posted_data['your-tel'],'VALUE_TYPE'=>"WORK"]],
'EMAIL' => $posted_data['your-email']==null?:[['VALUE'=>$posted_data['your-email'],'VALUE_TYPE'=>"WORK"]],
'COMMENTS' => $posted_data['your-subject'] . " \n". $posted_data['your-message'] . " \n" . $posted_data['your-text'] ,
'CONTACT_ID' => $contactId,
'UTM_CAMPAIGN' => $_COOKIE['utm_campaign'] ,
'UTM_CONTENT' => $_COOKIE['utm_content'] ,
'UTM_MEDIUM' => $_COOKIE['utm_medium'] ,
'UTM_SOURCE' => $_COOKIE['utm_source'] ,
'UTM_TERM' => $_COOKIE['utm_term'] ,
'SOURCE_ID' => $_COOKIE['referer'] ,
'params' => ['REGISTER_SONET_EVENT'=>'Y'],
]
];
toTelegram("\$data \n " . json_encode($data));
BX('crm.deal.add', $data);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment