Также в случае отсутствия 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();
// ...
}
Также в случае отсутствия gclid и utm определять источник по document.referrer
учитывая домены поисковых систем.
document.addEventListener("DOMContentLoaded",function(event){
var seo_params=new Array('ga_cid','gclid','utm_source','utm_medium','utm_campaign','utm_content','utm_term');
var seo_cookies=new Array();
var seo_cookies_default=new Array();
var params=new URLSearchParams(location.search);
seo_params.forEach((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;
}
});
var update_cookies=false;
if(params.has('gclid')&¶ms.get('gclid')!=''&¶ms.get('gclid')!='none'){
update_cookies=true;
seo_params.forEach((sp)=>{
switch(sp){
case 'gclid':
seo_cookies[sp]=params.get(sp);
break;
case 'utm_source':
seo_cookies[sp]='google';
break;
case 'utm_medium':
seo_cookies[sp]='cpc';
break;
default:
if(sp.indexOf('utm_')!=-1&¶ms.has(sp)&¶ms.get(sp)!=''){
seo_cookies[sp]=params.get(sp);
}
else{
seo_cookies[sp]='none';
}
break;
}
});
}
else{
var is_utm=false;
seo_params.forEach((sp)=>{
if(sp.indexOf('utm_')&¶ms.has(sp)&¶ms.get(sp)!=''){
seo_cookies[sp]=params.get(sp);
is_utm=true;
update_cookies=true;
}
else{
seo_cookies[sp]='none';
}
});
if(!is_utm){
var referer=document.referrer;
if(referer!=''){
var referer_domain=referer.split('/')[2];
if(referer_domain!=''&&referer_domain!=location.hostname){
update_cookies=true;
var organics=new Array('google','yandex','ya.','bing');
var is_organic=false;
organics.forEach((organic)=>{
if(referer_domain.indexOf(organic)!=-1){
if(organic=='ya.'){
organic='yandex'
}
is_organic=true;
seo_params.forEach((sp)=>{
switch(sp){
case 'utm_source':
seo_cookies[sp]=organic;
break;
case 'utm_medium':
seo_cookies[sp]='organic';
break;
default:
if(sp.indexOf('utm_')){
seo_cookies[sp]='none';
}
break;
}
});
}
});
if(!is_organic){
seo_params.forEach((sp)=>{
switch(sp){
case 'utm_source':
seo_cookies[sp]=referer_domain;
break;
case 'utm_medium':
seo_cookies[sp]='referral';
break;
default:
if(sp.indexOf('utm_')!=-1){
seo_cookies[sp]='none';
}
break;
}
});
}
}
}
}
}
var ga_cookie=document.cookie.match(new RegExp('(^| )_ga=([^;]+)'));
if(ga_cookie[2]){
var ga_cookie_split=ga_cookie[2].split(".");
var ga_cid=ga_cookie_split[2]+'.'+ga_cookie_split[3];
seo_cookies['ga_cid']=ga_cid;
}
seo_params.forEach((sp)=>{
var sp_cookie=document.cookie.match(new RegExp('(^| )local_'+sp+'=([^;]+)'));
if(update_cookies||!sp_cookie||sp=='ga_cid'){
var cv=(seo_cookies[sp]!='')?seo_cookies[sp]:seo_cookies_default[sp];
document.cookie=encodeURIComponent('local_'+sp)+'='+encodeURIComponent(cv);
}
});
var links=document.getElementsByTagName("a");
for(i=0;i<links.length;i++){
var href=links[i].href;
if(href.includes(document.domain)&&!(href.includes('#'))){
seo_params.forEach((sp)=>{
var sp_cookie=document.cookie.match(new RegExp('(^| )local_'+sp+'=([^;]+)'));
if(sp_cookie[2]){
href+=(href.includes('?'))?"&":"?";
href+=sp+"="+sp_cookie[2];
}
})
links[i].href=href;
}
}
const url=new URL(window.location.href);
seo_params.forEach((sp)=>{
var sp_cookie=document.cookie.match(new RegExp('(^| )local_'+sp+'=([^;]+)'));
if(sp_cookie[2]&&!params.has(sp)){
url.searchParams.append(sp,sp_cookie[2]);
}
});
window.history.pushState(null,null,url);
});
ios webview pull to refresh
override func viewDidLoad() {
// after webView.load
let refreshControl = UIRefreshControl()
refreshControl.addTarget(self, action: #selector(reloadWebView(_:)), for: .valueChanged)
webView.scrollView.addSubview(refreshControl)
}
@objc func reloadWebView(_ sender: UIRefreshControl){
webView.reload()
sender.endRefreshing()
}