-
Notifications
You must be signed in to change notification settings - Fork 7.6k
AB Testing With CodeIgniter
A/B Market Testing with CodeIgniter
I had a recent project that required me to develop a method that would randomly assign one of three versions of a site to a visitor and then tracked whether the visitor purchased an item from the ecommerce site. The requirements also called for the visitor to always see the same version of the site on each visit. This system was to allow the client's company to test public response and sells conversions one to different site designs and pricing. The market research was then to be used to find the proper approach to motivate customers to purchase and find the price point with the highest purchase rate. This type of market testing is known as A/B testing. In my case it was really A/B/C testing.
When I laid out the requirements I realized that there were two possible approaches to solving this problem. The first would be to have one site and multiple versions of the same view to display. This could easily be accomplished using a session variable to store a randomly assigned value indicating which of the three views to display. The other approach and the one I chose to implement, was two use CodeIgniter’s front controller to assign one of three application folders to the user. I chose this approach for two primary reasons. First, in my case I had multiple complete site layouts that were being tested. Second, I needed multiple pricing for the same products. The best solution for my requirements was to serve up different sites with different databases and then collect the data from each site.
Serving up two different versions of the site from two different application folders was easy. CodeIgniter was designed to allow you to move or change the name of your application folders. The application folder name is stored in a global variable set in the site’s front controller (index.php) file. The variable $application_folder holds your application folder path referenced from the document root of your site. Here is the portion of the index.php file that does this:
/*
|---------------------------------------------------------------
| APPLICATION FOLDER NAME
|---------------------------------------------------------------
|
| If you want this front controller to use a different "application"
| folder then the default one you can set its name here. The folder
| can also be renamed or relocated anywhere on your server.
| For more info please see the user guide:
| http://codeigniter.com/user_guide/general/managing_apps.html
|
|
| NO TRAILING SLASH!
|
*/
$application_folder = "application";
To assign an application folder to CodeIgniter I simply had to set this variable to the folder name I desired. I also needed to store the site selected so the user would always see the same version of the site. To do this I simply set a session variable. Since CodeIgniter is not fully configured at this point in the front controller, I decided the route of least resistance was to use PHPs native sessions. My simple solution follows:
/*
|---------------------------------------------------------------
| APPLICATION FOLDER NAME
|---------------------------------------------------------------
|
| If you want this front controller to use a different "application"
| folder then the default one you can set its name here. The folder
| can also be renamed or relocated anywhere on your server.
| For more info please see the user guide:
| http://codeigniter.com/user_guide/general/managing_apps.html
|
|
| NO TRAILING SLASH!
|
*/
// Comment out the following line for code below
//$application_folder = “application”;
/*
|--------------------------------------------------------------
| A/B TESTING APPLICATION FOLDER NAME
| ASSIGNMENT
|--------------------------------------------------------------
|
| The code below randomly assigns one of two
| application folders to a visitor and records the
| assignment in the session variable ‘subsite’ for
| use on future visits. This insures that the visitor
| sees the same version of the site on later visits
| from the same machine.
|
*/
if(isset($_SESSION['subsite']))
{
$application_folder = "application_".$_SESSION['subsite'];
}
elseif(rand(1,2) % 2)
{
$application_folder = "application_a";
$_SESSION['subsite'] = 'a';
}
else
{
$application_folder = "application_b";
$_SESSION['subsite'] = 'b';
}
This little snippet of code is all that is needed to server up multiple sites from different application folders. Next you will need to create to complete versions of the application and add one to the application_a folder and one to the application_b folder. If you need more than two versions of the site simply create application folders _c, _d, etc. and add then in an if or switch statement above.