<!-- Load the regular Content Experiments JS API without any ID parameters -->
<script src="//www.google-analytics.com/cx/api.js"></script>
<!-- Setup Google Analytics -->
<script>
(function(i,s,o,g,r,a,m){i['GoogleAnalyticsObject']=r;i[r]=i[r]||function(){
(i[r].q=i[r].q||[]).push(arguments)},i[r].l=1*new Date();a=s.createElement(o),
m=s.getElementsByTagName(o)[0];a.async=1;a.src=g;m.parentNode.insertBefore(a,m)
})(window,document,'script','//www.google-analytics.com/analytics.js','ga');
// Create the first regular tracker like you normaly would
ga('create', 'UA-XXXXXXXX-1', 'auto', {'allowLinker': true, 'siteSpeedSampleRate': 90});
// Create the second named tracker just for experiments
ga('create', 'UA-XXXXXXXX-1', 'auto', 'experimentTracker');
//Setup all of the regular customizations for the regular tracker if you need to
ga('require', 'linker');
ga('require', 'linkid', 'linkid.js');
ga('require', 'ec');
ga('require', 'displayfeatures');
ga('linker:autoLink', ['example.com','domain2.com'], false, true);
// Send the pageview like normal for the regular tracker
ga('send', 'pageview');
</script>
<script>
// Define the different experiments you wish to run and the page
var experimentOneID = "a1BcDEFgHijKl2mON-3pqr";
var experimentTwoID = "z9YxWVVuTsrPl8oNM-7lkj";
var experimentOneURL = "http://www.example.com/experiment-1/";
var experimentTwoURL = "http://www.example.com/experiment-2/";
var runContentExperiment = function(experimentID) {
// Ask Google Analytics which variation to show the user and specify the experiment ID.
var chosenVariation = cxApi.chooseVariation(experimentID);
// Set the chosen variation for GA
cxApi.setChosenVariation(chosenVariation, experimentID);
// Here is where we have the page specific code changes you might want to make
if (experimentID === experimentOneID) {
var pageVariations = [
function() {}, // Original: Do nothing. This will render the default HTML.
function() { // Variation 1 of Experiment 1
// Do Something here in experiment 1
}
];
pageVariations[chosenVariation]
ga('send', 'event', 'Content Experiment', 'View', experimentID, { 'nonInteraction': 1 });
}
else if (experimentID === experimentTwoID) {
var pageVariations = [
function() {}, // Original: Do nothing. This will render the default HTML.
function() { // Variation 1 of Experiment 2
// Do Something here in experiment 2
},
function() { // Variation 2 of Experiment 2
}
];
pageVariations[chosenVariation]
ga('experimentTracker.send', 'event', 'Content Experiment', 'View', experimentID, { 'nonInteraction': 1 });
}
}
// Check the canonical URL of the page and make sure it matches the one we want
var canonical = document.querySelector("link[rel='canonical']").href;
if (canonical === experimentOneURL) {
$(function() {
runContentExperiment(experimentOneID);
});
}
else if (canonical === experimentTwoURL) {
$(function() {
runContentExperiment(experimentTwoID);
});
}
</script>