The ab-experiments-rails is wrap for google ab experiments. Work with partials instead of javascript fighting.
Instalation
Add gem to your gemfile.
gem 'ab-experiments-rails', require: 'ab_experiments_rails'
Run bundle.
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
For easy way testing enable - Distribute traffic evenly across all variants
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>
<button>
Registration...
</button>
Copy and paste this file app/views/ab_experiments_rails/_example.html.erb into landing_page.html.erb and modify it like this
<%
settings = AbExperimentsRails::Settings.new({
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>
<button>
Registration...
</button>
</div>
<% if settings.experiment_enabled? %>
<div class="<%= "#{settings.variant_class} "%> google_ab_hidden">
<h1>Welcome and buy our services for half prices</h1>
<button>
Registration...
</button>
</div>
<%= 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
www.example.com/landing_page
You can force display original
www.example.com/landing_page?landing_page_test_ab=0
or variant
www.example.com/landing_page?landing_page_test_ab=1
This is useful for development