Wednesday, March 28, 2007

AB split test in PHP

If you have a web site, and you run ads on it, you probably want to make the most money you can. This means testing out different sizes, layouts, colors, and even types of ad to see which ones make the most money. One of the easiest ways to find the most profitable ad, is to run a split test.

An AB split test runs one ad, the "A" ad, part of the time, and shows a second, the "B" ad, the other part of the time. After they've each been displayed many times, the results of the two different ads can be compared. Then, replace the less profitable ad with a new format/color/etc. and run the test again. This way you'll refine the ads over time, and always be lookng to improve your revenue.

If you host your site somewhere that supports PHP, you can set up an AB split test using this code:

<?php
if (rand(1, 100) > 50) { // display this 50% of the time
echo "A ad's code is here";
}
else { // display this the other 50% of the time
echo "B ad's code is here";
}
?>


You can change the code to display each ad whatever percentage of the time you choose. Just change the last number in this line:

if (rand(1, 100) > 50)

For example, using the number 80 (instead of 50) displays ad A %20 of the time, and the other 80% of the time, the B ad is displayed.

Note: If your ad code has a quote characters in it, you must escape them by putting a backslash \ before the quote character so php will not see them as the end of the echo string.

2 comments:

Soccerwidow said...

Hi David,

I have been using you code for a while until I had to run an ABCD test.

Here the code I came up with. It works!

$abctest = mt_rand(1, 1000);
switch ($abctest) {
case ($abctest < 250):
echo "A ad's code is here";
break;
case ($abctest < 500):
echo "B ad's code is here";
break;
case ($abctest < 750):
echo "C ad's code is here";
break;
default:
echo "D ad's code is here";
break;

Thanks for your article!!!! The information is very helpful indeed.

David August said...

That's great, I'm glad it works!