Adsense testing with PHP

One advice people always give for webmasters in the Google Adsense program is to test ad placement, color scheme, etc. A common test is the A/B test. There are several articles talking about how to set up such a test using either Javascript or PHP, and they invariably involve the use of a random number generator to determine whether a visitor would fall under the control group or the test group. Even Google’s official Adsense blog talks about it.

If you want to make sure visitors to your site have a consistent user experience (for example, if you are testing whether it’s better to use a 728×90 Leaderboard or a 468×60 Banner, you want to ensure that users who see the Leaderboard ad initially will always see a Leaderboard ad as they navigate through the site), the random number method alone won’t work because it’s not possible to ensure all pages for that particular user fall under the same test bucket.

How can this be accomplished? The one constant during a visitor’s visit to a site is his/her IP address. So, we can use this information to determine which bucket a particular visitor falls under. To do this, include the following code at the top of every PHP file:

<?php

$ip =$_SERVER[‘REMOTE_ADDR’];
$last_ip = explode(“.”, $ip);
$even_odd = $last_ip[3] % 2;

?>

The above code divides all visitor into two equal buckets ($even_odd = 0 and $even_odd = 1) based on the last number of visitor’s IP address. This determination will last through user’s visit to the site.

At the place where Adsense code would be inserted, use the following code:

<?php
if ($even_odd > 0) {
?>
## First Adsense code
<?php
} else {
?>
## Second Adsense code
<?php
}
?>

The above assumes that the testing occurs in a single place on the page. However, even if this is not the case, you can still use the same principle.

Remember to use different channels to track the performance of the two blocks.