Clive Portman: code

Split A/B testing using Google Analytics Experiments

In the past I've used Clicky for this, but it's just as easy using Google Analytics. The difficulty is in wading through the documentation and useless tutorials.

Start be setting up the experiement in Google Analytics (Reports > Behaviour > Experiments). Enter any old urls as theu aren't used and ignore the warnings. Get the experiment ID though, as you'll need that. Now, whack this in the top of your page BEFORE the analytics code:

<script>
    head.load("http://www.google-analytics.com/cx/api.js", function() {
        variationNumber = cxApi.getChosenVariation('GGObdMgqRS67prxsH_MzLw');
        if (variationNumber == -1) {
            variationNumber = Math.random() < 0.5 ? 1 : 2;
            cxApi.setChosenVariation(variationNumber, 'GGObdMgqRS67prxsH_MzLw');
        }
    });
</script>

The 1 : 2 refers to the two variation ids setup in the experiment. Then use something like this in your regular javascript file (I'm just changing link text):

$(function() {
    if (variationNumber == 1) $('#experimentallink').html('Advice');
    if (variationNumber == 2) $('#experimentallink').html('Consultancy');
});

Links:
https://developers.google.com/analytics/solutions/experiments-client-side
https://developers.google.com/analytics/devguides/collection/gajs/experiments