Skip to main content

Time to rework my strategy for adding AdSense to Drupal blogs

Posted in

I recently had the epiphany, that Drupal's Blog module is not that great for blogging after all, at least not if you are using your website mainly as a single user blog, like I do.
In fact, the Blog module has lately proven itself to be cumbersome in so many ways, that I finally decided to phase it out completely. Naturally, this was no small task and involved doing lots of massive behind the scene changes and while I was at it anyway, I also decided to completely overhaul my previous strategy for adding banner ads to blog posts. So, if you are looking for instructions for integrating AdSense into your own blog, read on.

But before going into the technical details of my new setup, I first feel a need to apologize to the authors of the Drupal AdSense module for calling it unfit in my earlier post. I simply missed an important piece of documentation, cleverly hidden away in a collapsed panel, describing it's API.
Having said that and acknowledged, that the AdSense module is indeed a very useful tool, let's start with the project outline:

  • All banners should be managed by the AdSense module. This ensures that even automatically generated list pages (e.g. the teaser view on the frontpage) cannot accidentally exceed the maximum amount of allowed units. Also there is no need to mess around with copy&paste JavaScript code any longer this way.
  • Authenticated users (editors) should only see empty boxes instead of real ads. This allows for checking the layout of blog posts without accidentally producing invalid clicks or messing up statistics.
  • Blog posts should automatically and consistently be decorated with banner ads. The layout should be controlled by a central template, allowing for easy modification of all blog posts at once. This naturally rules out using tags and input filters for individual banner placement. Decoration must either be done in the theme or through the contemplate module.
  • It should be possible to publish ad free blog posts. Previously I used CCK to toggle this. However, with the Blog module gone, the better solution is to simply use different content types. This removes clutter from the edit forms and also cuts down on database queries.
  • I currently only want full pages to be decorated. Teaser lists should remain ad free. The contemplate module handles this nicely as well.
  • The most important thing of all: an appealing page layout with a high click rate.

Ok, with the requirements outlined, let's start with installing the modules. You'll need adsense and contemplate. Download and enable both. Optionally, adjust permissions to make sure, editorial staff only sees placeholder boxes instead of banners.

Next item on the list is to pick a content type for blogging, which will automatically be decorated. If you follow my example of not using the Blog module, simply create a new content type called journal (this leaves the story type for ad free posts). Otherwise, just stick with the blog type.

With the infrastructure in place, it's time to think about page layout. Coming up with a good layout, that results in a high click rate is a science in itself and what works well for one blog does not necessarily have to work for another. However, in most cases, embedding the banners directly in the articles, with the text flowing around them is a good idea.
A layout, I found to be working nicely, includes using two ad units. The first being a link block at the beginning of the first paragraph, flushed to the left and the second one being a text-only square flushed to the right of the second paragraph.

The code below will implement this layout. Just safe it as sites/all/contemplates/node-journal-body.tpl.php, then make sure it is loaded by visiting Administer | Content Management | Content Templates. Afterwards, the bodies of all nodes of the journal type will be decorated.


<?php

$decoration1 = adsense_display(array('format' => '120x90', 'slot' => '4243067992'));
$decoration2 = adsense_display(array('format' => '250x250', 'slot' => '0335368690'));

$content = explode("

", $node->body);

for ($i = 0; $i < count($content); $i++) {

if ($i == 0 ) {
?>

<?php
print $decoration1;
?>

<?php
}

if ($i == 1) {
?>

<?php
print $decoration2;
?>

<?php
}
print $content[$i] . "

";
}

?>

Please note, that the code above requires an input format to be used, which produces proper XHTML output ("Line break converter" and "HTML corrector" enabled). You also have to replace my slot IDs with yours. The correct IDs can be found under AdSense Setup | Manage Ads or in the JavaScript code of the individual units.