about

create and install a Google Analytics experiment - 08 May 2016

search

Google Analytics offers an amazing service for free called Experiments. Say I want to figure out which button copy leads to a better conversion? The logic to choose options and track which one converts best is all hosted by Google Analytics.

First read the crash course on setting up one of these experiments for serving up variations with JavaScript: Content Experiments

I ran through these steps twice, once for our dev/staging property and once for our production property. In analytics.rb I then have a switch:

ExperimentButton = {
  id: Rails.env.production? ? 'prod_exp_id' : 'dev_exp_id',
}

To load the experiment in button.html.haml:

- # 1. load particular experiment - allow use of chooseVariation for this experiment
%script(src="//www.google-analytics.com/cx/api.js?experiment=#{ExperimentButton[:id]}")

// first should always be 'original'
:javascript
    var button_copy_variations = ['Click Me', 'Click Me Please', 'Click ME NOW'];
    
    // 2. choose variation for user
    // https://developers.google.com/analytics/devguides/collection/gajs/experiments#cxjs-choose
    var button_copy_variation = cxApi.chooseVariation('#{ExperimentButton[:id]}');
    
    $(function() {
        // load chosen variations
        $('#button_call_to_action').text(button_copy_variations[button_copy_variation]);
    });

= link_to 'Click Me', button_submission_path, class: 'btn btn-primary', id: 'button_call_to_action'

175 words. Post tags: user experience, Google Analytics, experiment, and JavaScript.

Post content is written by Jason Zerbe and licensed CC BY-NC 3.0.