07 Feb 2006
Selective Google Adsense and Firefox Ads
I’m still running my AdSense experiment (update: experiment over), and even the meagre earnings so far have produced some interesting results. I’ve been only showing ads to visitors arriving from search engines, then a few days back tweaked the code to show ads for Firefox with the Google Toolbar to those using Internet Explorer for Windows. Yeah, I know, I’m cruelly harassing poor IE users, but the idea intrigued me.
Here’s the simple PHP used to selectively display the ads, in case anyone finds it useful:
Show ads only to search engine referrals
<?
if (preg_match('/^http:\/\/(\w+\.)?(google|msn|yahoo|aol)\./',$_SERVER['HTTP_REFERER']) == 1)
{
?>
<!-- Google AdSense HTML -->
<?
}
?>
The expression used doesn’t bother to fully check the referring URL to see if it’s a known search results page, it’s enough to look for domains that appear to be part of Google, MSN, Yahoo! or AOL.
Show ad only to Windows Internet Explorer users
<?
$userAgent = $_SERVER['HTTP_USER_AGENT'];
if ( (strpos($userAgent,'Windows') !== false) && (strpos($userAgent,'MSIE') !== false) && (strpos($userAgent,'Opera') === false) )
{
?>
<!-- Firefox ad HTML -->
<?
}
?>
Browsers with ‘Windows’ and ‘MSIE’ in their descriptions are our targets, but there’s an extra check to make sure Opera users aren’t nagged.
Both combined
The code I’m currently using only displays ads for search engine referrals, showing Firefox ads to IE users and AdSense to everyone else:
<?
if (preg_match('/^http:\/\/(\w+\.)?(google|msn|yahoo|aol)\./',$_SERVER['HTTP_REFERER']) == 1)
{
$userAgent = $_SERVER['HTTP_USER_AGENT'];
if ( (strpos($userAgent,'Windows') !== false) && (strpos($userAgent,'MSIE') !== false) && (strpos($userAgent,'Opera') === false) )
{
?>
<!-- Firefox ad HTML -->
<?
}
else
{
?>
<!-- Google AdSense HTML -->
<?
}
}
?>
Comments
— morcs, 9th Feb, 8:02pm
— Matt Round, 9th Feb, 9:37pm
Comments are now closed for this entry.