Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kjprince/16dd72bc4f33a2c749a45db78fb49a88 to your computer and use it in GitHub Desktop.
Save kjprince/16dd72bc4f33a2c749a45db78fb49a88 to your computer and use it in GitHub Desktop.
With this script you are good to go for client-site A/B/n testing with Content Experiments. Add ?var=1 to preview the variation of the A/B test.
<script src="//www.google-analytics.com/cx/api.js?experiment=YOUR_EXPERIMENT_ID"></script>
<script>
function getParameterByName(name) {
name = name.replace(/[\[]/, "\\[").replace(/[\]]/, "\\]");
var regex = new RegExp("[\\?&]" + name + "=([^&#]*)"),
results = regex.exec(location.search);
return results === null ? "" : decodeURIComponent(results[1].replace(/\+/g, " "));
}
var varID = getParameterByName('var');
if(! varID){ var chosenVariation = cxApi.chooseVariation(); }
else { var chosenVariation = varID; };
var pageVariations = [
function(){
ga("set","dimensionX","GTM-001_Control"),
ga("send","event","AB-test","GTM-001 Name of the test","Control",{nonInteraction:1});
},
function(){
ga("set","dimensionX","GTM-001_Variation_1"),
ga("send","event","AB-test","GTM-001 Name of the test","Variation 1",{nonInteraction:1});
}
];
$(document).ready(
pageVariations[chosenVariation]
);
</script>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment