:chart_with_upwards_trend: The Rack Based A/B testing framework
:chart_with_upwards_trend: The Rack Based A/B testing framework
The Rack Based A/B testing framework https://libraries.io/rubygems/split
Split is a rack based A/B testing framework designed to work with Rails, Sinatra or any other rack based app.
Split is heavily inspired by the Abingo and Vanity Rails A/B testing plugins and Resque in its use of Redis.
Split is designed to be hacker friendly, allowing for maximum customisation and extensibility.
Split v4.0+ is currently tested with Ruby >= 2.5 and Rails >= 5.2.
If your project requires compatibility with Ruby 2.4.x or older Rails versions. You can try v3.0 or v0.8.0(for Ruby 1.9.3)
Split uses Redis as a datastore.
Split only supports Redis 4.0 or greater.
If you're on OS X, Homebrew is the simplest way to install Redis:
brew install redis
redis-server /usr/local/etc/redis.confYou now have a Redis daemon running on port 6379.
gem install splitAdding gem 'split' to your Gemfile will autoload it when rails starts up, as long as you've configured Redis it will 'just work'.
To configure Sinatra with Split you need to enable sessions and mix in the helper methods. Add the following lines at the top of your Sinatra app:
require 'split'
class MySinatraApp < Sinatra::Base
enable :sessions
helpers Split::Helper
get '/' do
...
endTo begin your A/B test use the ab_test method, naming your experiment with the first argument and then the different alternatives which you wish to test on as the other arguments.
ab_test returns one of the alternatives, if a user has already seen that test they will get the same alternative as before, which you can use to split your code on.
It can be used to render different templates, show different text or any other case based logic.
ab_finished is used to make a completion of an experiment, or conversion.
Example: View
<% ab_test(:login_button, "/images/button1.jpg", "/images/button2.jpg") do |button_file| %>
<%= image_tag(button_file, alt: "Login!") %>
<% end %>Example: Controller
def register_new_user
# See what level of free points maximizes users' decision to buy replacement points.
@starter_points = ab_test(:new_user_free_points, '100', '200', '300')
endExample: Conversion tracking (in a controller!)
def buy_new_points
# some business logic
ab_finished(:new_user_free_points)
endExample: Conversion tracking (in a view)
Thanks for signing up, dude! <% ab_finished(:signup_page_redesign) %>You can find more examples, tutorials and guides on the wiki.
Split has two options for you to use to determine which alternative is the best.
The first option (default on the dashboard) uses a z test (n>30) for the difference between your control and alternative conversion rates to calculate statistical significance. This test will tell you whether an alternative is better or worse than your control, but it will not distinguish between which alternative is the best in an experiment with multiple alternatives. Split will only tell you if your experiment is 90%, 95%, or 99% significant, and this test only works if you have more than 30 participants and 5 conversions for each branch.
As per this blog post on the pitfalls of A/B testing, it is highly recommended that you determine your requisite sample size for each branch before running the experiment. Otherwise, you'll have an increased rate of false positives (experiments which show a significant effect where really there is none).
Here is a sample size calculator for your convenience.
The second option uses simulations from a beta distribution to determine the probability that the given alternative is the winner compared to all other alternatives. You can view these probabilities by clicking on the drop-down menu labeled "Confidence." This option should be used when the experiment has more than just 1 control and 1 alternative. It can also be used for a simple, 2-alternative A/B test.
Calculating the beta-distribution simulations for a large number of experiments can be slow, so the results are cached. You can specify how often they should be recalculated (the default is once per day).
Split.configure do |config|
config.winning_alternative_recalculation_interval = 3600 # 1 hour
endThe recalculation is triggered while rendering the dashboard, which can make it
slow when many experiments are due for recalculation at once. On busy
installations you can turn the on-dashboard calculation off and instead run it
from a background job (e.g. Split::ExperimentCatalog.all.each(&:calc_winning_alternatives)):
Split.configure do |config|
config.dashboard_calculate_winning_alternatives = false
endThe dashboard will then display the most recently calculated probabilities.
Perhaps you only want to show an alternative to 10% of your visitors because it is very experimental or not yet fully load tested.
To do this you can pass a weight with each alternative in the following ways:
ab_test(:homepage_design, {'Old' => 18}, {'New' => 2})
ab_test(:homepage_design, 'Old', {'New' => 1.0/9})
ab_test(:homepage_design, {'Old' => 9}, 'New')This will only show the new alternative to visitors 1 in 10 times, the default weight for an alternative is 1.
For development and testing, you may wish to force your app to always return an alternative. You can do this by passing it as a parameter in the url.
If you have an experiment called button_color with alternatives called red and blue used on your homepage, a url such as:
http://myawesomesite.com?ab_test[button_color]=redwill always have red buttons. This won't be stored in your session or count towards to results, unless you set the store_override configuration option.
The dashboard's "Force for current user" button does the same thing for your browser, by setting a split_override cookie. Because an override is not counted, an experiment you have pinned can keep reading zero participants. The dashboard lists any overrides active in your browser at the top of the page, and lets you clear them individually or all at once.
In the event you want to disable all tests without having to know the individual experiment names, add a SPLIT_DISABLE query parameter.
http://myawesomesite.com?SPLIT_DISABLE=trueIt is not required to send SPLIT_DISABLE=false to activate Split.
To aid testing with RSpec, write spec/support/split_helper.rb and call use_ab_test(alternatives_by_experiment) in your specs as instructed below:
…Now you can call use_ab_test(alternatives_by_experiment) in your specs, for example:
it "registers using experimental signup" do
use_ab_test experiment_name: "alternative_name"
post "/signups"
...
endBy default new A/B tests will be active right after deployment. In case you would like to start new test a while after
the deploy, you can do it by setting the start_manually configuration option to true.
After choosing this option tests won't be started right after deploy, but after pressing the Start button in Split admin dashboard. If a test is deleted from the Split dashboard, then it can only be started after pressing the Start button whenever being re-initialized.
When a user completes a test their session is reset so that they may start the test again in the future.
To stop this behaviour you can pass the following option to the ab_finished method:
ab_finished(:experiment_name, reset: false)The user will then always see the alternative they started with.
Any old unfinished experiment key will be deleted from the user's data storage if the experiment had been removed or is over and a winner had been chosen. This allows a user to enroll into any new experiment in cases when the allow_multiple_experiments config option is set to false.
By default Split automatically resets the experiment whenever it detects the configuration for an experiment has changed (e.g. you call ab_test with different alternatives). You can prevent this by setting the option reset_manually to true.
You may want to do this when you want to change something, like the variants' names, the metadata about an experiment, etc. without resetting everything.
By default Split will avoid users participating in multiple experiments at once. This means you are less likely to skew results by adding in more variation to your tests.
To stop this behaviour and allow users to participate in multiple experiments at once set the allow_multiple_experiments config option to true like so:
Split.configure do |config|
config.allow_multiple_experiments = true
endThis will allow the user to participate in any number of experiments and belong to any alternative in each experiment. This has the possible downside of a variation in one experiment influencing the outcome of another.
To address this, setting the allow_multiple_experiments config option to 'control' like so:
Split.configure do |config|
config.allow_multiple_experiments = 'control'
endFor this to work, each and every experiment you define must have an alternative named 'control'. This will allow the user to participate in multiple experiments as long as the user belongs to the alternative 'control' in each experiment. As soon as the user belongs to an alternative named something other than 'control' the user may not participate in any more experiments. Calling ab_test() will always return the first alternative without adding the user to that experiment.
Split comes with three built-in persistence adapters for storing users and the alternatives they've been given for each experiment.
By default Split will store the tests for each user in the session.
You can optionally configure Split to use a cookie, Redis, or any custom adapter of your choosing.
Split.configure do |config|
config.persistence = :cookie
endWhen using the cookie persistence, Split stores data into an anonymous tracking cookie named 'split', which expires in 1 year. To change that, set the persistence_cookie_length in the configuration (unit of time in seconds).
Split.configure do |config|
config.persistence = :cookie
config.persistence_cookie_length = 2592000 # 30 days
endThe data stored consists of the experiment name and the variants the user is in. Example: { "experiment_name" => "variant_a" }
Note: Using cookies depends on ActionDispatch::Cookies or any identical API
Using Redis will allow ab_users to persist across sessions or machines.
Split.configure do |config|
config.persistence = Split::Persistence::RedisAdapter.with_config(lookup_by: -> (context) { context.current_user_id })
# Equivalent
# config.persistence = Split::Persistence::RedisAdapter.with_config(lookup_by: :current_user_id)
endOptions:
lookup_by: method to invoke per request for uniquely identifying ab_users (mandatory configuration)namespace: separate namespace to store these persisted values (default "persistence")expire_seconds: sets TTL for user key. (if a user is in multiple experiments most recent update will reset TTL for all their assignments)The Dual Adapter allows the use of different persistence adapters for l
No open issues yet, or sync has not completed.