Create a Basic A/B Test Tool with Zend Framework

Below is a A/B testing Zend Framework View Helper that will allow you to easily create a custom A/B test in the event you either don’t want to pay for a tool, don’t want to share your data with Google or some other need that other tools can’t fulfill.

Using the A/B Test View Helper

This tool uses cookies to designates which version of a test they should see. To setup a test, call the helper at the top of your control layout with the path to the variant you wish to test passed in as an argument.

View helper call on your control:

$this->ABTest('/path/to/variant-2');

As with any test, drive traffic to your control (where you placed the call to the helper). The helper will then randomly place the visitor in either the control or the variant you passed it and set a cookie so that they always get redirected to the right variant in the future.

Don’t forget to put a call to the helper on all of the variant layouts or views, otherwise the user won’t be redirected to the control if the user went to the variant directly and have a control cookie.

View helper call on your variant:

$this->ABTest();

This is a quick and dirty solution. If you wanted to build a more robust tool, you might want to consider adding the following features:

  • Consider adding a registerVars method that lets you pass in multiple variants for you test
  • Support the ability to only put a percentage of traffic into the test (the one below puts 100% of traffic into the test)
  • Add support for test start and expire date

Tracking
A major piece of A/B testing is tracking the visitors to each test, conversions and statistical significance between each test. This helper does not include anything related to tracking. We use our own custom QuickBase-driven tracking mechanism that doesn’t require integration into the helper, but depending on your needs, you may want to modify the helper to integrate with whatever tracking mechanism you’re using.

Here’s the class for the helper:

/**
* This is a tool for creating A/B Tests
*
* @author: Joshua McGinnis <joshua_mcginnis@intuit.com>
*/

class App_View_Helper_ABTest extends Zend_View_Helper_Abstract {

protected $currentUrl = ”;
protected $protocol = ”;

/**
* Main view helper.
*
* Call this helper at the top of a layout and pass in the variants urls
* or leave empty for variant pages that are part of the test.
* Send all traffic to the control and this script will randomly
* place the visitor in one version of the page or the other.
*
* @param string $variantUrl
*/
public function ABTest($variantUrl = “”)
{
$this->setCurrentUrl();

/**
* Check to see if the user is a new visitor, if so, give them a test
* cookie and redirect them if put into something other than the
* control.
*
* The following needs to go at the top of each variant to ensure
* visitors always end up on the right variant:
*
*/
if($this->newTestVisitor() && isset($variantUrl))
{
echo ‘set cookies’;
$url = ($this->isValidUrl($variantUrl)) ? $variantUrl : $this->protocol . $_SERVER[‘SERVER_NAME’] . $variantUrl;

$rand = rand(0, 1);
echo $rand;

if ($rand == 0)
{
setcookie(‘abTest’, $url, time()+60*60*24*90);
header(“Location: ” . $url);
}
else
setcookie(‘abTest’, $this->currentUrl, time()+60*60*24*90);
}
}

private function setCurrentUrl()
{
$this->protocol = (isset($_SERVER[‘HTTPS’])) ? ‘https://’ : ‘http://’;
$this->currentUrl = $this->protocol . $_SERVER[‘SERVER_NAME’] . $_SERVER[‘REQUEST_URI’];
}

public function newTestVisitor()
{
if(isset($_COOKIE[‘abTest’]))
{
if($_COOKIE[‘abTest’] !== $this->currentUrl)
Header(“Location: “. $_COOKIE[‘abTest’]);
}
else
return true;
}

private function isValidURL($url)
{
return preg_match(‘|^http(s)?://[a-z0-9-]+(.[a-z0-9-]+)*(:[0-9]+)?(/.*)?$|i’, $url);
}
}

Published byJosh McGinnis

Josh is a software engineer, leader, startup advisor for the LA Chamber of Commerce and consultant residing in the Los Angeles area. Josh is passionate about helping entrepreneurs and businesses reach their maximum potential.

No Comments

Post a Comment