Monday, May 22, 2006

Improving AdSense relevance (especially for PhpBB forums)

All you need to do for selecting the type of advertisements you want is to put an additional GET parameter into your URL!

If your site is about rats for example and is located at somewhere.com/something/blah.php, but Google insists on giving you advertisements about owls because that term just happens to appear somewhere on the page, it seems quite effective to simply make the URL somewhere.com/something/blah.php?owls.

On my server I have two PhpBB forums, both of which are displaying some advertisements. While they get ~20000 page views per week, advertisement clicks are zero. It was obvious why this was. While the target audience was teenager dog lovers, the advertisement Google's AdSense is displaying are about IT infrastructure and car parts, I couldn't imagine them being any more unrelated.

First of all I wanted to see in what kind of cases the Google server would come and reread the page content to target the ads. If I don't know that, then it would be impossible to make any improvements and know if they had any effect. So I made a test page and tried changing the ad channel name, create a new ad section and change the page content, but none of these caused the google bot to come. However I noticed that changing the name of the page does cause it to come, even if you just add GET-parameters after the URLs (I'm sure it filters out sids and such), the bot comes to reindex.

Okay, now I know how to make the bot come. How do I get it to understand what the page is about? I tried the putting the google_ad_section_start tag around forum posts, like adviced somewhere, but it didn't help at all. But here's the magic worth million dollars: Google AdSense really cares about the NAME of the page. If instead of view_topic.php you have dogs.php for example, it assumes it's about dogs! Well, I didn't want to rename all of my pages, so I tested if it would be enough to just add the a GET parameter about dogs, and sure enough it was. I was finally getting dog-related advertisements.

Now, how to make that happen in PhpBB? Here's how. Go to sessions.php and find a function definition for append_sid. Append_sid is the function which makes sure that even without cookies, sessions won't be lost. Also it's a very convenient place to add new GET parameters, as every URL which is displayed by PhpBB goes through this function. Here's my new function:



function append_sid($url, $non_html_amp = false)
{
global $SID;
if ( !empty($SID) && !preg_match('#sid=#', $url) )
{
$url .= ( ( strpos($url, '?') != false ) ? ( ( $non_html_amp ) ? '&' : '&' ) : '?' ) . $SID;
}

// Try to make adsense understand that this page is about dogs!

return str_replace("?", "?dog&", $url);
}



After this modification, all of the forum pages now show ads which are relevant. I'm happy, the users are happier, advertisers are happy and Google is happy.