Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save vhsu/26f888d0032d9926446a to your computer and use it in GitHub Desktop.
Save vhsu/26f888d0032d9926446a to your computer and use it in GitHub Desktop.
Google Analytics - Sitewide Content Experiment With Get Var as Trigger
// D'ont forget to change suisseo.ch en your own domain
// 12036452-2 in the expid of your experiment
// test = 1 to the variable that will trigger the change
// Author : Vincent Hsu twitter.com/suisseo
<script>
_udn ="suisseo.ch";
</script>
<script>
function getUrlVars(url) {
var vars = {};
if(url){
var parts = url.replace(/[?&]+([^=&]+)=([^&]*)/gi, function(m,key,value) {
vars[key] = value;
});
}
else{
var parts = window.location.href.replace(/[?&]+([^=&]+)=([^&]*)/gi, function(m,key,value) {
vars[key] = value;
});
}
return vars;
}
</script>
<!-- Google Analytics Content Experiment code -->
<script>function utmx_section(){}function utmx(){}(function(){
var k='12036452-2',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>')})();
<!-- End of Google Analytics Content Experiment code -->
</script>
<script>
var b = utmx('variation_content', 'A/B');
function filter(v) {
var u = v;
if(b && u.substr(0,7) == 'http://' && b.substr(0, 7) != 'http://') {
u = u.substr(7);
}
var l = document.location.href;
var prefix = 'www.suisseo.ch';
var i = l.indexOf(prefix);
j= l.length ;
u = l.substring(i + prefix.length, j);
if(u.indexOf('?') == -1){
u ='' + u + '?test=1&utm_expid=12036452-2';
}
else{
u ='' + u + '&test=1&utm_expid=12036452-2';
}
var bVars = getUrlVars(u);
//alert(bVars)
var aVars = getUrlVars();
for(param in aVars){
if(!bVars[param]){
u = '' + u + '&' + param + '=' + aVars[param];
}
}
if(document.referrer){
u = /*'societe' +*/ u + '&utm_referrer=' + encodeURIComponent(document.referrer);
}
return u;
}
if(b){ window.location = filter(b);}
else{utmx('url','A/B');}
</script>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment