javascript — сохранять gclid и utm-метки при переходе между страницами и добавлять ко всем ссылкам на сайте get параметр ga_cid из cookie _ga
Также в случае отсутствия gclid и utm определять источник по document.referrer
учитывая домены поисковых систем.
document.onreadystatechange=function(){ 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_')!=-1&¶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); }