Reputation: 846
We use the analytics api to download the experiments and variations, and do the variation selection for our visitors on our end (i.e. Server-side experiments as described here: https://developers.google.com/analytics/solutions/experiments-server-side)
When a visitor visits a url that is under experimentation and they have a selected variation, they get the javascript as described like:
<script>
cxApi.setChosenVariation(1, 'a9BcDEFgHijKl8mON-Opqw');
</script>
This is working fine. We would like to have multiple experiments running(For example, a site-wide experiment involving the menu, and a page-specific experiment), the variation selection and everything works fine on our end. To the user, when they are part of multiple experiments, they get multiple calls of setChosenVariation like so:
<script>
cxApi.setChosenVariation(1, 'a1BcDEFgHijKl2mON-3pqr');
cxApi.setChosenVariation(1, 'z9YxWVVuTsrPl8oNM-7lkj');
</script>
I can't find any reason why this shouldn't work, but in the results from the experiments, when this happens, we see all the users being assigned only to one experiment, though both experiments have results (creating conversion rates of >100%).
Is there an explanation of this behaviour (I feel like perhaps the second call is overriding the first call?) and/or a correct way to do this?
Thanks very much
Upvotes: 12
Views: 2048
Reputation: 418
I realise this is a bit of an old question but I recently had to come up with a solution for this problem also and thought I would share my approach with a couple of real-world restraints thrown in for good measure.
At a high level my approach works the following way:
Unfortunately, I had to take this approach as the platform I was working with only allowed me to make edits to the global header rather than any kind of page specific basis. So as I mentioned it just checks the canonical URL tag against the URL I am running a test on.
<!-- 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>
Upvotes: 1
Reputation: 1567
Ease answer didn't work for me. Thanks to Google Analytics Debugger extension I could see that both those ga('send','pageview')
were sending data about second experiment. Using synchronous call worked and I ended up with something like:
var sendExperiment = function(tracker, experimentVar, experimentId) {
cxApi.setChosenVariation(experimentVar, experimentId);
tracker.send('event', 'experiment', 'view',{'nonInteraction': 1});
}
ga(function(tracker) {
sendExperiment(tracker, 1, 'a1BcDEFgHijKl2mON-3pqr');
sendExperiment(tracker, 2, 'z9YxWVVuTsrPl8oNM-7lkj');
});
Upvotes: 6
Reputation: 11
I think you should send experiment values to Google Analytics when you set a chosen variation under each experiment. Code Like this:
cxApi.setChosenVariation(1, 'a1BcDEFgHijKl2mON-3pqr');
ga('send', 'pageview');
cxApi.setChosenVariation(1, 'z9YxWVVuTsrPl8oNM-7lkj');
ga('send', 'pageview');
Upvotes: 1