Data Driven Decisions using A/B tests in Rails

Intro

When building marketing pages we want to make sure that we are converting the maximum number of users who view the page. It is important that we find the best taglines, copy and images to ensure this happens and this is where A/B testing can be a useful tool. It allows you to test multiple variants and see what converts the best. In this video I will show you how to install the "split" gem in a rails app and setup a simple A/B test on a tagline.

https://github.com/splitrb/split

Install Redis

On mac we can simply run:

brew install redis

Setup the Gem

Add the gem to your GemFile. We want to use the dashboard as well so we will require it.

# Split - A/B Testing
gem "split", require: 'split/dashboard'

Install the gem

> bundle install

Once you have added the gem let's create the initializer config/initializers/split.rb.

Split.configure do |config|
  config.persistence = :cookie
end

We will use the :cookie adapter as we want to do testing with users who are not logged in.

Now we will add the dashboard to config/routes.rb:

# Split Dashboard - A/B Testing
Split::Dashboard.use Rack::Auth::Basic do |username, password|
  # Protect against timing attacks:
  # - Use & (do not use &&) so that it doesn't short circuit.
  # - Use digests to stop length information leaking
  ActiveSupport::SecurityUtils.secure_compare(::Digest::SHA256.hexdigest(username), ::Digest::SHA256.hexdigest(Rails.application.credentials.dig(:split, :username))) &
    ActiveSupport::SecurityUtils.secure_compare(::Digest::SHA256.hexdigest(password), ::Digest::SHA256.hexdigest(Rails.application.credentials.dig(:split, :password)))
end

mount Split::Dashboard, at: 'split'

Finally, let's add the username and password to our credentials file:

> EDITOR="code --wait" bin/rails credentials:edit --environment=development

Generate a secure password and add the details like below. Remember to use your own data here.

split:
  username: "admin@website.com"
  password: "k9SYb4s$WJF#JsI^SH"

Now we can run an experiment!

Run an Experiment

In our view we can setup an experiment like this:

title = ab_test(:landing_heading, "Welcome!", "Hello")

To finish the experiment once the user has completed the desired outcome e.g. sign up we can use:

ab_finished(:landing_heading)

We can force the test to a specific value by setting a query param:

?ab_test[landing_heading]=max_effort

Viewing our Results

We can view our results by navigating to http://localhost:3000/split/ to view the dashboard.

--

Follow along to watch us build a Rails / SaaS / B2C app from the ground up.

Clipflow.co https://www.clipflow.co

Twitter Darrell - https://twitter.com/craftbuildcut Ken - https://twitter.com/kennnG

View More Posts

Portfolio built using the open source project by

Initium Studio

Create your own Next.js Portfolio with Contentful CMS by forking this repo onGitHub

Blog

|

Projects

|

Health

|

Recipes

|

About

|

Contact

2024 Greeff Consulting Pty Ltd