Friday, March 22, 2013

Super Simple (aka Ghetto) A/B Testing

Looking to test a couple variants of your landing page?  Sure there are a bunch of great tools out there, but that takes time/effort to find, register, and install on your site. 

If you’re just at the point (like I am) where you want an easy free way to test landing pages quickly.  This little PHP A/B tester is the simplest way to compare two versions of a webpage.  Here’s how it works:

  1. Save on your php enabled web server as something like test.php
  2. A user will go to yourserver.com/test.php
  3. test.php will randomly pick one of two webpages to send them to (ideally a form/google survey where you can collect output)
  4. test.php will record where the user was sent in data.txt
  5. At the conclusion of the test you can easy grab the resulting text file (in this case called data.txt).  Count the number of visits and compared to the number of conversions to get your rate.  The higher number wins!

Code below.  Happy Testing!

<?php

$filename = 'data.txt';
$somecontent = "Placeholder";

//this will pick randomly spit out 0 or 1
$path = mt_rand(0,1);

// Let's make sure the file exists and is writable first.
if (is_writable($filename)) {

    // In our example we're opening $filename in append mode.
    // The file pointer is at the bottom of the file hence
    // that's where $somecontent will go when we fwrite() it.
    if (!$handle = fopen($filename, 'a')) {
         echo "Cannot open file ($filename)";
         exit;
    }
   

/*for the purpose of this test we’re just sending folks to google.com or microsoft.com.  Replace those URLs with desired content */


    if($path>0){
        //Write $somecontent to our opened file.
        if (fwrite($handle, "Google") === FALSE) {
             echo "Cannot write to file ($filename)";
            exit;
        }
        header("Location:
http://www.google.com");
    }else{
        if (fwrite($handle, "Microsoft") === FALSE) {
            echo "Cannot write to file ($filename)";
        exit;
    }
    header("Location:
http://www.microsoft.com");
}   
echo "Success, wrote ($somecontent) to file ($filename)";

fclose($handle);

} else {
    echo "The file $filename is not writable";
}
 
?>

7 comments:

  1. header() function sends a redirect to the browser. User has to wait for 2 round trips to server rather than the 1 they would get by going directly to google.com/microsoft.com

    ReplyDelete
  2. True. Is there a way to make it more efficient? This is basically a demux that takes one input (yoursite.com/test.php) and pumps it out to another (two trips). Is there a redirect that can do this in 1 trip?

    ReplyDelete
  3. In the real world, you prolly won't be redirecting to external URLs, but rather to PHP pages that you own. So if you have two landing pages, page1.php and page2.php, I'd replace your "header" calls with "include(page1.php)" / "include(page2.php)" calls.

    ReplyDelete
  4. Good call! In this case I'm using it to reference google docs, but you're right for the other 99% of cases.

    ReplyDelete
  5. Some who circumvent the regulation select to keep a low profile at home and only visit offshore sites. If you do not hassle to examine whether or not or not the casino you might be} enjoying in} at is licenced by the relevant authorities earlier than you begin to gamble, then you definitely open your self as much as} all kinds of issues. You may be unwittingly serving to to fund organised crime simply by enjoying in} there. Outside of the big cities, South Korea remains to be a pleasantly rural nation. English just isn't always broadly spoken and it's possible to dafabet get a real flavour of how folks reside here simply by walking round.

    ReplyDelete