php — сохранять в cookie gclid, utm-метки и параметр ga_cid из cookie _ga
Также в случае отсутствия gclid и utm определять источник по $_SERVER[‘HTTP_REFERER’] учитывая домены поисковых систем.
$seo_params=array('ga_cid','gclid','utm_source','utm_medium','utm_campaign','utm_content','utm_term'); $seo_cookies=$seo_cookies_default=array(); foreach($seo_params as $sp){ switch($sp){ case 'gclid': $seo_cookies_default[$sp]='NULL'; break; case 'utm_source': $seo_cookies_default[$sp]='direct'; break; default: $seo_cookies_default[$sp]='none'; break; } } $update_cookies=false; if(isset($_GET['gclid'])&&$_GET['gclid']!=''){ $update_cookies=true; foreach($seo_params as $sp){ switch($sp){ case 'gclid': $seo_cookies[$sp]=$_GET[$sp]; break; case 'utm_source': $seo_cookies[$sp]='google'; break; case 'utm_medium': $seo_cookies[$sp]='cpc'; break; default: if(stristr($sp,'utm_')&&isset($_GET[$sp])&&$_GET[$sp]!=''){ $seo_cookies[$sp]=$_GET[$sp]; } else{ $seo_cookies[$sp]='none'; } break; } } } else{ $is_utm=false; foreach($seo_params as $sp){ if(stristr($sp,'utm_')&&isset($_GET[$sp])&&$_GET[$sp]!=''){ $seo_cookies[$sp]=$_GET[$sp]; $is_utm=true; $update_cookies=true; } else{ $seo_cookies[$sp]='none'; } } if(!$is_utm){ $referer=$_SERVER['HTTP_REFERER']; if($referer!=''){ $parse_referer=parse_url($referer); $referer_domain=$parse_referer['host']; if($referer_domain!=''&&$referer_domain!=$_SERVER['HTTP_HOST']){ $update_cookies=true; $organics=array('google','yandex','bing'); $is_organic=false; foreach($organics as $organic){ if(stristr($referer_domain,$organic)){ $is_organic=true; foreach($seo_params as $sp){ switch($sp){ case 'utm_source': $seo_cookies[$sp]=$organic; break; case 'utm_medium': $seo_cookies[$sp]='organic'; break; default: if(stristr($sp,'utm_')){ $seo_cookies[$sp]='none'; } break; } } } } if(!$is_organic){ foreach($seo_params as $sp){ switch($sp){ case 'utm_source': $seo_cookies[$sp]=$referer_domain; break; case 'utm_medium': $seo_cookies[$sp]='referral'; break; default: if(stristr($sp,'utm_')){ $seo_cookies[$sp]='none'; } break; } } } } } } } if($_COOKIE['_ga']!=''){ $ga_cookie_split=explode('.',$_COOKIE['_ga']); if($ga_cookie_split[2]&&$ga_cookie_split[3]){ $ga_cid=$ga_cookie_split[2].'.'.$ga_cookie_split[3]; $seo_cookies['ga_cid']=$ga_cid; } } foreach($seo_params as $sp){ if($update_cookies||!isset($_COOKIE['local_'.$sp])||$sp=='ga_cid'){ $cv=($seo_cookies[$sp]!='')?$seo_cookies[$sp]:$seo_cookies_default[$sp]; setcookie('local_'.$sp,$cv,time()+31536000,'/',$_SERVER['HTTP_HOST']); $_COOKIE['local_'.$sp]=$cv; } } function add_seo_params_to_letter(){ global $seo_params; $text='<hr/>'; foreach($seo_params as $sp){ $text.=$sp.': '.$_COOKIE['local_'.$sp]."<br/>"; } return $text; }
Функция add_seo_params_to_letter
используется для добавления в письма сохраненных данных.
Например в modx:
// /core/components/formit/model/formit/fihooks.class.php public function email(array $fields=array()){ // ... $message.=add_seo_params_to_letter(); // ... }