Skip to content

Instantly share code, notes, and snippets.

@harrisonkrat
Forked from Nils-van-Kleef/redirectReferrerGA.js
Last active January 10, 2021 06:38
  • Star 4 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
Star You must be signed in to star a gist
Save harrisonkrat/6519d49b7102f361c992 to your computer and use it in GitHub Desktop.
/**
* redirectReferrer.js
* Setting the correct referrer in a redirect experiment page for CA, UA, or AA
* See ECO-878 for context
*
* THIS APPLIES TO OPTIMIZELY CLASSIC ONLY
*
* @install: before the Optimizely snippet on your site
* @install: before the Google Analytics or Adobe Analytics snippet on your site
* @install: this can be done via Project JS, a tag manager, or natively on your site
* @install: ensure this code runs on each pre- and post-redirect page, and all other pages where you want Optimizely to set the referrer
* @require: change "DOMAIN" to the top-level domain of your website (string)
* @require: value for scope - "page" or "session" (string)
* @require: value for alx (analytics platform) - "ca" (Google Classic Analytics), "ua" (Google Universal Analytics), or "aa" (Adobe Analytics) (string)
*/
var domain = "DOMAIN", scope = "page", alx = "ua";
/***DO NOT EDIT ANYTHING BELOW THIS LINE***/
var cookieDomain = "." + domain, optlyCookie="";
if (scope == "page") {
optlyCookie = "optimizelyRedirect"; //persist the original referrer from before the redirect for the page only
}
else {
optlyCookie = "optimizelyReferrer"; //persist the original referrer from before the redirect for the session
}
window.getCookie = function(name) {
var match = document.cookie.match(name+'=([^;]*)');
return match ? match[1] : undefined;
};
window.setCookie = function(c_name,value,c_domain) {
c_domain = (typeof c_domain === "undefined") ? "" : "domain=" + c_domain + ";";
document.cookie=c_name + "=" + escape(value) + ";" + c_domain + "path=/";
};
window.checkReferrer = decodeURIComponent(getCookie("session_referrer"));
window.optlyReferrer = decodeURIComponent(getCookie(optlyCookie));
if (checkReferrer == "undefined" || document.referrer.indexOf(domain)==-1) {
setCookie('session_referrer',document.referrer,cookieDomain);
}
else {
setCookie('session_referrer',checkReferrer,cookieDomain);
}
window.whatReferrer = decodeURIComponent(getCookie("session_referrer"));
function check_ga() {
if (typeof ga === 'function') {
ga('set','referrer',window.whatReferrer);
} else {
setTimeout(check_ga, 500);
}
}
function check_s() {
if (typeof s === 'object') {
s.referrer = window.whatReferrer;
} else {
setTimeout(check_s, 500);
}
}
if(window.optlyReferrer !== "undefined") {
if (alx == "ua") {
check_ga();
}
else if (alx == "ca") {
window['_gaq'] = window['_gaq'] || [];
window['_gaq'].push(['_setReferrerOverride', window.whatReferrer]);
}
else if (alx == "aa") {
check_s();
}
else {
console.log("referrer unchanged");
}
}
@JeroenSi
Copy link

Hi Mr. Harrison,

Thank you for this code!
Do you know how this would work with Optimizely X
The same way?

Greets,
Jeroen

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment