The ab-experiments-rails is wrap for google ab experiments. Work with partials instead of javascript fighting.


Add gem to your gemfile.

gem 'ab-experiments-rails', require: 'ab_experiments_rails'

Run bundle.


Run generator.

rails generate ab_experiments_rails

Put this content into head tag before google analytics load. (usually application layout file)

<%# load before google analytics %>
<%= yield :google_ab %>

Setup google experiments

Urls doesn't matters

Urls doesn't matters

For easy way testing enable - Distribute traffic evenly across all variants

Distribute traffic evenly

Create your first ab test

Let's apply ab testing on file like this app/views/pages/landing_page.html.erb

<h1>Welcome and buy our services</h1>

Copy and paste this file app/views/ab_experiments_rails/_example.html.erb into landing_page.html.erb and modify it like this

  settings ={
    test_name: :landing_page_test, # your choice
    experiment_id: 'EXPERIMENT_ID', # copy/paste from google ab experiments
    experiment_enabled: true, # on/off your experiment. display only original content when false
    spinner_starts_after: 1, # spinner loader start after 1s
    spinner_max_time: 5 # show original content after 5s seconds if javascript fails

<%= render "ab_experiments_rails/loading" if settings.experiment_enabled? %>
<div class="<%= "#{settings.original_class}" %> google_ab_show_later">
  <h1>Welcome and buy our services</h1>
<% if settings.experiment_enabled? %>
  <div class="<%= "#{settings.variant_class} "%> google_ab_hidden">
    <h1>Welcome and buy our services for half prices</h1>
  <%= render "ab_experiments_rails/header_content", settings: settings %>
<% end %>

Don't forget setup experiment id from your google analytics. Your ab test is done!

How to force display test variant

You can pass parameter into url to display test variant.

If url of your page is

You can force display original

or variant

This is useful for development