google-analyticsgoogle-analytics-apigoogle-experiments

cxApi.chooseVariation() always returning 0


My site is a single page webapp. I'm loading the experiment code with my experiment id:

<script src="//www.google-analytics.com/cx/api.js?experiment={@ js_settings.analytics.experimentid @}"></script>

After all is loaded, at the moment I display the page, I select the right template by calling a function which does the following:

ga('set', 'expId', experiment_id);
var variation = cxApi.getChosenVariation(experiment_id) || cxApi.chooseVariation();
cxApi.setChosenVariation(variation, experiment_id);
ga('set', 'expVar', variation);
ga('set', 'dimension1', variation);
ga('send', 'pageview');

Now, it seems like cxApi.chooseVariation() always returns 0. 100% of the 205 sessions so far were given the default variation (0). Why?


Solution

  • The problem seems to have been that the experiment javascript code is required to be present on the page even if you manually use cxApi. You have to modify the code such that it doesn't redirect the user and then you can call chooseVariation() and it will return an actual variation, instead of always 0.

    Surely this API should be improved because in its current state it's mediocre at best.