Suggest a feature
×

Welcome to TagMyCode

Please login or create account to add a snippet.
0
0
 
0
Language: Javascript
Posted by: Mikael Malmgren
Added: Aug 2, 2018 1:09 PM
Views: 4
Tags: no tags
  1. <script>
  2. //get referrer info and shorten it
  3.   var ref = {{Referrer}}
  4.   function extractDomain(url) {
  5.     var domain;
  6.     //find & remove protocol (http, ftp, etc.) and get domain
  7.     if (url.indexOf("://") > -1) {
  8.         domain = url.split('/')[2];
  9.     }
  10.     else {
  11.         domain = url.split('/')[0];
  12.     }
  13.  
  14.     //find & remove port number
  15.     domain = domain.split(':')[0];
  16.  
  17.     return domain;
  18. }
  19.   ref = extractDomain(ref);
  20.  
  21. //create cookie  
  22.  
  23.     function createCookie(name,value,days) {
  24.                 if (days) {
  25.                 var date = new Date();
  26.                 date.setTime(date.getTime()+(days*24*60*60*1000));
  27.                 var expires = "; expires="+date.toGMTString();
  28.                 }
  29.                 else var expires = "";
  30.                 document.cookie = name+"="+value+expires+"; path=/";
  31.     }
  32.  
  33.   var cookie = "";
  34.   //check if the source cookie is already present and append |
  35.   if({{sourceCookie}}){cookie = {{sourceCookie}} + "|"};
  36.  
  37.   //check if UTMs are present and set cookie content to the source utm
  38.   if({{utm_source}}){
  39.   createCookie("source",cookie+{{utm_source}},1000)
  40.   //check if gclid is present and set cookie content to AdWords
  41.   }else if({{gclid}}){
  42.     createCookie("source",cookie+"AdWords",1000)
  43.   //check if iz query parameter is present set cookie content to affiliate
  44.   }else if({{URL Masking - Source and Medium}}){
  45.     createCookie("source",cookie+"AffiliateProgram",1000)
  46.   //check if referrer is present and set cookie content to the referrer
  47.   }else if(cookie && ref){
  48.     createCookie("source",cookie+ref,1000)
  49.   }else{
  50.     createCookie("source",ref,1000)
  51.   };
  52.   </script>