Last active
July 11, 2016 10:22
-
-
Save svensson-david/ec8305a9a89d4a50f7cd to your computer and use it in GitHub Desktop.
Tweaked utm-alternative that adds each visit to the cookie. Automatically removes referrals from your own site. Skips addition if new hit is same as previous. Original here: https://github.com/dm-guy/utm-alternative
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(function(cookieName, domain){ | |
var traffic_source_COOKIE_TOKEN_SEPARATOR = ">>"; //separating between concatenated lead source | |
var traffic_source_meta_SEPARATOR = ">"; | |
var traffic_source_date_SEPARATOR = "|"; | |
var NONE = "(none)"; | |
domain = domain || window.location.hostname; | |
function getCookie(cookieName){ | |
var name = cookieName + "="; | |
var cookieArray = document.cookie.split(';'); //break cookie into array | |
for(var i = 0; i < cookieArray.length; i++){ | |
var cookie = cookieArray[i].replace(/^\s+|\s+$/g, ''); //replace all space with '' = delete it | |
if (cookie.indexOf(name)==0){ | |
return cookie.substring(name.length,cookie.length); // | |
} | |
} | |
return null; | |
} | |
/* | |
* Takes the current time of the session, and optional addition to the traffic source. | |
* The output will be of the format: >>{traffic source}|>DD-MM-YY HH:MM AM/PM>> | |
* | |
*/ | |
/* | |
* Uncomment currTime() in order to activate. You should also uncomment the lines in the code that call this function (all marked with a comment). If you do so, uncomment the line above them which does not employ currTime() | |
function currTime() { | |
var date = new Date(); | |
var currDate = date.getDate(); | |
var hours = date.getHours(); | |
var minutes = date.getMinutes(); | |
var month = date.getMonth() + 1; | |
var year = date.getFullYear(); | |
var ampm = hours >= 12 ? 'pm' : 'am'; | |
hours = hours % 12; | |
hours = hours ? hours : 12; // the hour '0' makes '12' | |
minutes = minutes < 10 ? '0' + minutes : minutes; | |
var strTime = traffic_source_date_SEPARATOR + currDate + '-' + month + '-' + year + ' ' + hours + ':' + minutes + ' ' + ampm + ')'; | |
return strTime; | |
} | |
*/ | |
/* | |
Checks whether a certain parameter exist in the current browser URL. If it does, it returns its name. | |
It will receive "src" later in the main function | |
*/ | |
function getURLParameter(param){ | |
var pageURL = window.location.search.substring(1); //get the query string parameters without the "?" | |
var URLVariables = pageURL.split('&'); //break the parameters and values attached together to an array | |
for (var i = 0; i < URLVariables.length; i++) { | |
var parameterName = URLVariables[i].split('='); //break the parameters from the values | |
if (parameterName[0] == param) { | |
return parameterName[1]; | |
} | |
} | |
return null; | |
} | |
/* | |
Gets the first (latest) token from a cookie: THIS__ONE>>NOT__THIS__ONE>>AND__NOT__THIS__ONE | |
*/ | |
function getFirstTokenFromCookie(cookie){ | |
var result = ""; | |
var firstSeparatorIndex = cookie.indexOf(traffic_source_COOKIE_TOKEN_SEPARATOR); | |
result = firstSeparatorIndex !== -1 ? cookie.substring(0, firstSeparatorIndex) : cookie; //if there is a separator, provide the newest value no the cookie | |
return result; | |
} | |
/* | |
Set the cookie if it doesn't exist. | |
*/ | |
function setCookie(cookie, value){ | |
var expires = new Date(); | |
expires.setTime(expires.getTime() + 62208000000); //1000*60*60*24*30*24 (2 years) | |
document.cookie = cookie + "=" + value + "; expires=" + expires.toGMTString() + "; domain=" + domain + "; path=/"; | |
} | |
/* | |
Boolean, whether or not it's not NULL or Empty | |
*/ | |
function isNotNullOrEmpty(string){ | |
return string !== null && string !== ""; | |
} | |
/* | |
Remove the protocol for the referral token | |
*/ | |
function removeProtocol(href) { | |
return href.replace(/.*?:\/\//g, ""); | |
} | |
/* | |
Add the UMTZ cookie as base for the cookie | |
*/ | |
function setUMTZdefault(){ | |
var utmzCookie = getCookie("__utmz"); //get GA cookie | |
var cookieCampaignName = ""; //reset lead source value | |
if(utmzCookie != null) { | |
var UTMSRC = "utmccn="; | |
var start = utmzCookie.indexOf(UTMSRC); | |
var end = utmzCookie.indexOf("|", start); | |
if(start > -1) { | |
if(end === -1) { | |
end = utmzCookie.length; | |
} | |
cookieCampaignName = "utmz" + traffic_source_meta_SEPARATOR + utmzCookie.substring((start + UTMSRC.length), end); //get the value of the UTMZ, without the parameter name | |
setCookie(cookieName, cookieCampaignName); //set the default cookie | |
} | |
} | |
} | |
function isRealReferrer(){ | |
return (document.referrer != "" && document.referrer.indexOf(location.protocol + "//" + location.host) !== 0); | |
} | |
/* | |
Find lead source by checking cookie, url param "src" or referrer(that order) and set new cookie traffic_source. | |
Do this only if cookie do not exist. This value will be used for contact form. | |
If cookie exist add url "src" param on the beginning of the cookie if it exists and it is not already at the beginning of the cookie. | |
This way "tokens" are made in traffic_source cookie. Token separator is ">>" (see above traffic_source_COOKIE_TOKEN_SEPARATOR) | |
*/ | |
var traffic_source = ""; //reset lead source value | |
if(document.cookie.indexOf(cookieName) === -1) { // if there is no cookie traffic_source set yet, check if there is an existing UTMZ campaign name. | |
setUMTZdefault(); | |
} | |
var urlParamSRC = getURLParameter("src"); //take the SRC value | |
if(isNotNullOrEmpty(urlParamSRC)) { //if there is not campaign name but we have SRC | |
traffic_source = decodeURI(urlParamSRC); // decode URI of the SRC parameter | |
// traffic_source = decodeURI(urlParamSRC) + currTime(); // Optional: add the session time | |
} | |
else if(isRealReferrer()){ //if there is not campaign, and no src, check if there is a referrer we want to keep | |
var referrerHostName = removeProtocol(document.referrer); | |
var GOOGLE = /www.google/; | |
var YAHOO = /search.yahoo/; | |
var BING = /www.bing/; | |
if(GOOGLE.test(referrerHostName)){ | |
traffic_source = "Google Search"; | |
//traffic_source = "Google Search" + currTime(); // + Optional: Add the current session time. Choose one of them. | |
} else if(YAHOO.test(referrerHostName)){ | |
traffic_source = "Yahoo Search"; | |
//traffic_source = "Yahoo Search" + currTime(); | |
} else if(BING.test(referrerHostName)){ | |
traffic_source = "Bing Search"; | |
//traffic_source = "Bing Search" + currTime(); | |
} else { | |
traffic_source = referrerHostName; | |
//traffic_source = referrerHostName + currTime(); | |
} | |
traffic_source = "referral" + traffic_source_meta_SEPARATOR + traffic_source; | |
} else if (document.referrer == "") { | |
traffic_source = NONE; | |
} | |
var firstToken = getFirstTokenFromCookie(getCookie(cookieName)); | |
if(traffic_source != "" && traffic_source !== firstToken) { | |
var newTrafficSourceCookie = traffic_source + traffic_source_COOKIE_TOKEN_SEPARATOR + getCookie(cookieName); //concetenate new SRC with old cookie values | |
// var newTrafficSourceCookie = urlParamSRC + currTime() + traffic_source_COOKIE_TOKEN_SEPARATOR + getCookie(cookieName); // Optional: add the time of current session | |
setCookie(cookieName, newTrafficSourceCookie); | |
} | |
})("traffic_source", ".YOURDOMAIN.COM"); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
I am getting Uncaught TypeError: Cannot read property 'indexOf' of null when using this. Most probably when the cookie is not yet set (user is visiting for the first time) cookie is null in this line of code -> cookie.indexOf(traffic_source_COOKIE_TOKEN_SEPARATOR);