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', git: 'https://github.com/hlidacky/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 %>
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
show_delay: 0.3 # flickering prevention if google choose variant. you can increase or decrease this number (seconds)
})
%>
<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