create and install a Google Analytics experiment

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

// first should always be 'original'
    var button_copy_variations = ['Click Me', 'Click Me Please', 'Click ME NOW'];
    // 2. choose variation for user
    var button_copy_variation = cxApi.chooseVariation('#{ExperimentButton[:id]}');
    $(function() {
        // load chosen variations

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