Skip to content

Instantly share code, notes, and snippets.

@christopherhill
Last active December 19, 2015 08:49
Show Gist options
  • Save christopherhill/5928421 to your computer and use it in GitHub Desktop.
Save christopherhill/5928421 to your computer and use it in GitHub Desktop.
Script for injecting Google A/B testing into multiple pages. Could be extended in any number of ways.
<script type="text/javascript">
var abtestValue = "", runTest = false;
var windowLocation = window.location.pathname;
var mapArr = new Array();
mapArr.push({ "url" : "/url.html", "id" : "99999999-99" });
mapArr.push({ "url" : "/url.html", "id" : "99999999-99" });
mapArr.push({ "url" : "/url.html", "id" : "99999999-99" });
mapArr.push({ "url" : "/url.html", "id" : "99999999-99" });
mapArr.push({ "url" : "/url.html", "id" : "99999999-99" });
mapArr.push({ "url" : "/url.html", "id" : "99999999-99" });
mapArr.push({ "url" : "/url.html", "id" : "99999999-99" });
$.each(mapArr, function(i) {
if (this.url === windowLocation) {
abtestValue = this.id;
runTest = true;
console.log('Running Google Analytics A/B Test Using ID: ' + abtestValue);
}
});
if (runTest === true) {
var _udn = "domain.com";
<!-- Google Analytics Content Experiment code -->
function utmx_section(){}function utmx(){}(function(){var
k=abtestValue,d=document,l=d.location,c=d.cookie;
if(l.search.indexOf('utm_expid='+k)>0)return;
function f(n){if(c){var i=c.indexOf(n+'=');if(i>-1){var j=c.
indexOf(';',i);return escape(c.substring(i+n.length+1,j<0?c.
length:j))}}}var x=f('__utmx'),xx=f('__utmxx'),h=l.hash;d.write(
'<sc'+'ript src="'+'http'+(l.protocol=='https:'?'s://ssl':
'://www')+'.google-analytics.com/ga_exp.js?'+'utmxkey='+k+
'&utmx='+(x?x:'')+'&utmxx='+(xx?xx:'')+'&utmxtime='+new Date().
valueOf()+(h?'&utmxhash='+escape(h.substr(1)):'')+
'" type="text/javascript" charset="utf-8"><\/sc'+'ript>')})();
}
</script>
<script type="text/javascript">
utmx('url','A/B');
</script>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment