Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP

Simple client-side A/B testing in pure javascript

branch: master

Merge pull request #6 from optikfluffel/fix-ie8-bug

check for undefined in getElementsByClassName to work with IE8
latest commit 0ff0402b39
Daniele Mazzini authored
Octocat-spinner-32 example Example text changed to English October 13, 2012
Octocat-spinner-32 screenshots GA example screenshot added October 10, 2012
Octocat-spinner-32 abalytics.js forgot a '(' June 13, 2013
Octocat-spinner-32 readme.md Updated code attribution May 19, 2013
readme.md

ABalytics.js: pure js + Google Analytics A/B testing

This pure javascript library allows you to do simple A/B testing working only on your client-side code. All data is registered as custom variables in your website Google Analytics profile, so you don't need to configure anything server side.

Features

  • Easy to set up
    • You just list the possible variants, the randomization is handled automatically
    • You just mark the html elements you want to test on with a class, the substitution is automatic
    • No need to set anything up server side, the data is stored on Google Analytics
  • Consistent user experience: The selected variant is stored in a cookie, so the user will see the same one when coming back
  • No external dependencies: Pure javascript, you just need to include GA
  • Flexible: You can conduct multiple, independent experiments at the same time. Each experiment will use a custom variable slot

Usage

1. Include abalytics.js

<script type="text/javascript" src="abalytics.js"></script>

2. Define your tests and configure GA

var _gaq = _gaq || [];
      _gaq.push(['_setAccount', 'UA-XXXXXXXX-XX']);

      ABalytics.init({
              experiment1_name: [
                {
                  name: 'variant1_name',
                  "experiment1_class1_name": "<strong>Html content for variant 1 class 1</strong>",
                  "experiment1_class2_name": "Html content for variant 1 class 2"
                },
                {
                  name: 'variant2_name',
                  "experiment1_class1_name": "<strong>Html content for variant 2 class 1</strong>",
                  "experiment1_class2_name": "Html content for variant 2 class 2"
                }
              ],
              experiment2_name: [ ...
            }, _gaq);

      _gaq.push(['_trackPageview']);

3. Apply the experiment classes to your html content

<div class="experiment1_class1_name">
  This content will be replaced by ABalytics
</div>

<span class="experiment1_class2_name">This text will be replaced too</span>

4. Call applyHtml() on load

<script type="text/javascript">
  window.onload = function() {
    ABalytics.applyHtml();
  };
</script>

NB: If you're using jQuery in your website, it's even better to call applyHtml in the $(document).ready(...) handler

5. Run your experiment

Publish your code, wait for some visitors to come...

6. Analyze your data

ABalytics works best if you defined some goals on Google Analytics.

In GA, go to Audience->Custom->Custom Variables. You will find your experiements names in the available slots.

Click on the experiment name you want to analyze to see how many visits where recorded for each variant. Then click on "Goal set 1" to see the goal conversion rate for each variant. Simple and easy!

GA screenshot

Advanced options

If you are already using some custom variables, you can have ABalytics start from a slot > 1 using

ABalytics.init({...}, _gaq, YOUR_FIRST_FREE_SLOT);

License

The MIT License (MIT) Copyright (c) 2012 Daniele Mazzini

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Softwareis furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

Donated by hashtagify.me

Something went wrong with that request. Please try again.