Chris Nizzardini

Salt Lake City, Utah Developer / Human / Blogger

Using Google Analytics Custom Variables for AB Testing

Setting up AB Tests using Google Analytics is a simple and free alternative to incredibly expensive solutions like Omniture AB Testing. I had actually used Omniture Test and Target with a company I had previously worked with. The results the Omnitures solution gave were very suspect when compared to our own internal tracking stored in a MySQL database on a per order basis. I decided to setup some simple AB Testing on a new project I am working on and was pleased with how easy it was to do using Google Analytics.

Setting up the custom variables

In your sites header file or whatever controller is called on each page load you will want to write a simple “coin flipper” to randomly determine which test a visitor gets.

    if(!isset($_SESSION['AB_Test'])){
        switch(rand(1,2)){
            case 1: // control - current form
                $abtest = 'Test';
                break;
            case 2: // test - single long form
            default:
                $abtest = 'Control';
                break;
        }
        $_SESSION['AB_Test'] = $abtest;
    }
    else{
        $abtest = $_SESSION['AB_Test'];
    }

Add tests to the switch statement as need be. Now you’ll want to define this test as a custom variable in Google Analytics:

    var _gaq = _gaq || [];
    <? if($abtest): ?>
    _gaq.push(['_setCustomVar',
        2,                   // This custom var is set to slot #1.  Required parameter.
        'AB Test',     // The name acts as a kind of category for the user activity.  Required parameter.
        '<?=$abtest?>',               // This value of the custom variable.  Required parameter.
        2                    // Sets the scope to session-level.  Optional parameter.
    ]);
    <? endif; ?>
    _gaq.push(['_setAccount', 'UA-xxxxxxx-x']);
    _gaq.push(['_trackPageview']);
    (function() {
        var ga = document.createElement('script'); ga.type = 'text/javascript'; ga.async = true;
        ga.src = ('https:' == document.location.protocol ? 'https://ssl' : 'http://www') + '.google-analytics.com/ga.js';
        var s = document.getElementsByTagName('script')[0]; s.parentNode.insertBefore(ga, s);
    })();

Building the custom report

Now we just need to build a custom report:

Google Analytics Custom Report - AB Testing

 

Sit back

Now just sit back as the results come in and hopefully you have a winner! You’ll want the test to run for at least 2 weeks before you can begin to get a true picture of how the test is performing. Don’t be tempted to nominate a winner after a few days as the results can be very skewed early on.

AB Testing Report

Chris Nizzardini has been developing web applications since 2006. He lives and works in beautiful Salt Lake City, Utah. If you’re interested in hiring me for contract work please visit IO Spring LLC.

Twitter Google+ 

, , ,

One thought on “Using Google Analytics Custom Variables for AB Testing

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>