A Poor-Man's A/B Testing in PHP

Recently at work the new person in charge of metrics and tracking came and asked me if we could set up a simple A/B test (things are always call things simple until someone slaps a level of effort on them) for one of our partners. This test would allow for us to try out two different calling plans on a site, all of five dollars in price difference, but with 400 minutes in calling plan difference.

Now I know that this can be done, but I wasn’t sure quite how to track the variable since we have multiple points of entry. Session variable seemed to be the trick, but the book I have was from before superglobals came into play (e.g. $_SESSION), and therefore, not very secure.

So I started off with a way of randomly assigning a variable to a user, that variable being a value of 0 or 1.

<?php 	
	# This is a session tracker to provide for A/B testing on the website.	<a href="http://us3.php.net/manual/en/function.session-start.php">session_start();</a>	

	# Initialize a random number	
	srand ((float) microtime() * 100003);		

	# Define the <a href="http://us3.php.net/manual/en/ref.array.php">array</a>	$whichdeal[0] = '600';
	$whichdeal[1] = '1000';	

	# Choose the poison
	$random_key = array_rand($whichdeal);	

	# Check to see if the session info is not set, if it's not
	# the assign the array value to the session variable "dealinfo"	if (!isset($_SESSION['dealinfo'])) {
		$_SESSION['dealinfo'] = $random_key;
	} else {
	}
?&gt;

I then created the code for the page itself. The included file above is called by a require function at the top of the page.

&lt;? require "includes/session.inc.php"; ?&gt;

And then the code that generates the deal links itself:

&lt;?php	
	# This code does a/b testing on price points
	if($_SESSION['dealinfo'] == "0") {
		$planlink = 'Get+More';	
		$plantitle = ' 600 Anytime MInutes $39.99';
	} elseif ($_SESSION['dealinfo'] == "1") {
		$planlink = 'Get+More+1000+With+Unlimited+Nights+%26+Weekends';
		$plantitle = '1000 Anytime MInutes $45.99';
	} else {
		$planlink = 'Get+More';	
		$plantitle = ' 600 Anytime MInutes $39.99';
	}		

	# session_unset();		// for testing
	# session_destroy();	// for testing

?&gt;

So if the value of $_SESSION[‘dealinfo’] is 0, you get one plan, if it’s 1, you get a different plan. If for some reason the whole thing chokes and it can’t get a variable, you get the good old generic plan. I could have also consolidated this into a smaller array, but since this is only for specific pages, I wanted to be able to define the deal on a page by page basis (e.g. for different carriers).

These variables were called in the code as strings. Since the plan link was the url fragment that followed a link variable with “&rp=” so it only had to be the actual value of the plan that was plugged into a larger URL structure.

You could do this on a larger scale with pretty much anything, including products, product images, stylesheets, body text, etc. The variables in the last piece can be changed to whatever you want, and you could have 30 of them as opposed to just two. The initial session file takes care of saying 0 or 1, and then it’s up to you.

You could also add more than two variables, and create multiple tests (a/b/c/d/e etc). by adding additional numbers to the initial random array generator and more elseifs to the page code.

Technorati Tags: | | | |
  1. Joel says:

    ummmmmmmm....yeah.....you lost me coach.....can we go over that play again?

  2. daniel says:

    Look at you with the PHP.

    I know what A/B testing is, but I still don't understand this particular instance of it given the context. Care to elaborate what the biz prob was?

  3. John Athayde says:

    the particular instance (and ongoing long term instance) is to provide a simple and flexible system that I can roll out on a variety of properties and can easily affect anything from a simple image call to completely replacing entire portions of the site code, style sheets, or even phone offers/orders.

    It's not a massive system that controls it, just some simple hand coding that's a quick fix.

  4. Kathryn says:

    You probably already caught this, but the only thing I could make sense of in all that code was that you have the "i" in minutes incorrectly capitalized. Just so's you know.

    Your old friend and grammar nerd,
    Kathryn

  5. John Athayde says:

    Ah yes, the sticky shift key strikes again. Luckily, it was only variable text. I do have to fix that on the live site tho. Yay for small sans-serif text that kind of just looks like a bad font issue. Boo for lazy me that hasn't fixed this already.

  6. Vineet says:

    The style can be edited thugroh CSS in your theme. You're talking about functionality here. The example that you pointed to will most likely be appropriate only in your particular case.If you want custom coding, just send me an email.

Post a comment

Name or OpenID (required)


(lesstile enabled - surround code blocks with ---)