Multiple experiments with google analytics api
Asked Answered
P

3

12

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%).

conversion rate > 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

Poplin answered 16/7, 2014 at 9:1 Comment(0)
B
6

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');
});
Badillo answered 14/2, 2015 at 16:24 Comment(3)
We've moved away from GA now (and Ease's answer didn't work for us either). This answer looks good, but I'm not able to test itPoplin
thanks! I know this is an really old question, but I spent so much time searching for answer with no luck that I decided to post my result anyway.Badillo
I don't think that is actually called sync. What you do get is actually two async calls with the experiment parameters set differently.Ontina
E
1

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');
Erastatus answered 23/7, 2014 at 3:1 Comment(0)
P
1

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:

  1. Load the Google Content Experiments API code
  2. Setup Analytics and create a second tracker (for the same profile) that will only be used to track the second concurrent experiment I am running.
  3. Do any customization of my regular GA code.
  4. Create a function to check if the page the user is currently on is the one that I am running a given test on and if so find the chosen variation for the user for that particular experiment, execute it and send the information back into the relevant GA tracker with a custom event.

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>
Projectionist answered 9/6, 2016 at 5:41 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.