AB Email Testing

Today I learned some nifty tricks for AB email testing for ruby on rails applications. What is AB Email Testing you ask? Thats a good place to start at. When you own an application that has many users you are going to want to email them information. Whether its to tell them of a new offer, or update them on something, your ultimate goal is to get that reader engaged in your email and follow the links you offer them. AB testing is a simple test of sending out multiple versions on an email to all your users and seeing which version gets the highest amount of users following the link you supplied them. This is a very important test for marketing your emails. You want to use the email with the greatest success rate. So now we have a few things we need to do to get the to work. 1) we need a easy way to send emails to all your users. 2) we need a system of sending different versions of that email to each user. 3) we need a way to store and keep track of how many users clicked each link and figure out which one has the higest success rate.

Hopefully after this blog entry you will be able to create your own AB test system. There are many programs and companies that will sell you this service, which would probably be much easier, but sometimes the easiest route isn’t the smartest. By following this blog you will be able to fully create and control your own AB email tesing system, making it as specific and customized for your app as possible.

For this blog I am going to assume that you have an ActionMailer set up and working for your application. The ActionMailer I will be referring to in my blog is called SignupMailer. Yours may be called anything as long as it inherits the ActionMailer class. Now lets begin…

The first thing we are going to want to do is create a table in our database that will store all the information we desire. Lets create a model for our tracking system… I called my model “Click”. Everytime a user hits the link button a Click will be created and added to the database, and that will hold all the information we will find useful. Lets start this now shall we?

go ahead and -> ruby script/generate model Click

now go into your newly created migration file called. ####_create_clicks. Lets add some useful information that it shoul dbe holding. For mine I woul dlike to keep track of a few things. 1) The User who clicked the link. 2) The name of the email being sent. 3) what version of the email it is. 4) the location you want to send the user too after they click the link. Here is my migration file

class CreateClicks < ActiveRecord::Migration
def self.up
create_table :clicks do |t|
t.column :user_id, :integer # user id
t.column :email_name, :string #email name
t.column :variation, :integer #version number
t.column :link, :string # link we want to send our users to
t.column :created_at, :timestamp # information on when the link was pressed
end
end

def self.down
drop_table :clicks
end
end

now you can rake db:migrate the newly made migration. So now we got our table which we can create Clicks and add them to the database. How and when do we create these clicks? and how do we send the emails? There are many different ways to go about doing this, but I will do what works well for me. I am going to create a rake file that by simply executing from the command line will send all of your users emails of different versions with the ability to track whether or not they have been clicked.
The next thing we need to do is create a controller for our Clicks. Go ahead and
ruby script/generate controller Clicks
inside make a function link.
def link
end

we will fill that in later. Now lets begin with our rake file. Move into your lib/tasks folder and create our rakefile. If you do not have this directory simply create one. Now lets assume we have a new promotional email we want to send out and we want to find out which of the 3 versions we have is most efficient at drawing users. We will want to create a rakefill called promo1 (assuming you may have other promos later we will want to start with 1 and work our way up after that. so create your promo1.rake file inside lib/tasks and open it up. inside of it put this.

# Provide tasks to load and delete user data
require 'active_record'
require 'active_record/fixtures'

namespace :db do
namespace :promo1 do

desc "Send The Emails!"
task :load => :environment do |t|
send_emails
end
end
end

def send_emails
end

when you run this rake file it will load and run the send_email functions which we will now create.


def send_emails
users = User.find :all # gets all users in database

users.each { |user| # goes through each user

variation = rand(2)+1 # picks a random number from 1 - 3
email_name = "promo_" + variation.to_s # our promo name with the variation at the end
redirect_control = "login" # the controller we want to call
redirect_action = "login" # the action we want to call from that controller

# now we will check which of the 3 versions we will send to the user
if variation == 1
#we must send a bunch of these params to the ActionMailer so it can use it later
SignupMailer::deliver_promo_1(user, email_name, variation, redirect_control, redirect_action)

elsif variation == 2
SignupMailer::deliver_promo_2(user, email_name, variation, redirect_control, redirect_action)

elsif variation == 3
SignupMailer::deliver_promo_3(user, email_name, variation, redirect_control, redirect_action)

end
}

end

and thats it. the rakefile is done. But we are not quite done yet. Lets now go back to our clicks controller. Now lets fill in what we want in the link function.


def link
# all of these params we will specify in our link we create for our emails
user_id = params[:id]
email_name = params[:email]
variation = params[:variation]
control = params[:redirect_control]
action = params[:redirect_action]

link = "http://www.yoururl.com/#{control}/#{action}"

click = Click.new(:user_id => user_id,
:email_name => email_name,
:variation => variation,
:link => link,
:created_at => DateTime.now)

click.save

redirect_to :controller => control, :action => action
end

pretty straight forward huh? we just send it all the info we desire from the email. Save it into a new click object. and voila! its stored in our database. Now lets create our promo_(1-3) emails. Lets open our ActionMailer model and create the proper function calls.

SignupMailer.rb (model file)

def promo_1(user, email_name, variation, redirect_control, redirect_action)
from "admin@standardissimo.com"
recipients user.email
subject email_name
content_type "text/html"
body( :recipient => user,
:email_name => email_name,
:variation => variation,
:redirect_control => redirect_control,
:redirect_action => redirect_action)
end

def promo_2(user, email_name, variation, redirect_control, redirect_action)
from "admin@standardissimo.com"
recipients user.email
subject email_name
content_type "text/html"
body( :recipient => user,
:email_name => email_name,
:variation => variation,
:redirect_control => redirect_control,
:redirect_action => redirect_action)
end

def promo_3(user, email_name, variation, redirect_control, redirect_action)
from "admin@standardissimo.com"
recipients user.email
subject email_name
content_type "text/html"
body( :recipient => user,
:email_name => email_name,
:variation => variation,
:redirect_control => redirect_control,
:redirect_action => redirect_action)
end

so here we are sending all of our given params to the BODY which is very important. The body is what gets sent to the email .rhtml file to be used as a local variable. Now lets create our new SignupMailer .rhtml email files. Move into your views/SignupMailer folder and create promo_1 – promo_3. Now lets add the actual code to this .rhtml file.

views/signup_mailer/promo_1.rhtml

Promo 1

<%="" %>Click here four our promo!

now here is the true beauty of all this. we are actually sending our users who click on this link directly to our click controllers link action while sending with it the necessary parms such as user_id, email, variation, control and action. Once you copy this for your other 2 promos, changing the wording and varying the way it feels you are DONE. thats right. ready to test now? all you have to do for this to work is go to your console, move into your application and run
rake db:promo1:load
and shabam! you should have a bunch of emails all of different random variations being sent out, with the capability of tracking which emails were clicked with detailed information about them including when exactly it was clicked. Thanks for reading my tutorial and I hoped this helped some AB email testers out there. I do realize that there are probably other ways to go about doing this. I have found that this is the simplest and efficient way to create your own working AB email testing system. Thanks all!

Advertisement

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Connecting to %s

Follow

Get every new post delivered to your Inbox.