Skip to main content

Integrating Google Adsense into Drupal

The first rule of integrating AdSense into any Drupal website is to never ever paste the JavaScript code directly into any node or block for a number of reasons:

  • JavaScript uses the <script> tag, which requires the PHP code input format to display properly. Allowing PHP code to be executed in page content is a security hazard.
  • When putting the AdSense code in nodes and blocks, it is rather easy to accidentally produce a combination, where the number of units on a single page exceeds the allowed maximum amount. This is especially true for list views like the frontpage.
  • When text is suppose to flow around banners, there is no way of previewing the layout with life banners without generating impressions and risking accidental clicks.

The proper way to integrate Google AdSense into Drupal is to always use the Adsense module. The module itself can be a bit confusing at first, because instead of requiring the user to copy and paste the original JavaScript code from Google, it only wants the ad slot ID of the banner(s), it is suppose to manage.

To configure the module, follow these steps:

  1. Determine the logical banner layout of the blog. That is, write down, where a banner should be displayed and of what type it should be. It helps to come up with a unified scheme for specifying the location, such as SiteID_place_type. In this example, "SiteID" would be an optional prefix to identify the blog (useful when running ads on multiple websites), "place" would either be "block" (when showing on a block) or the name of a content type (when the banner is to be injected into the post) and "type" would be the type of the banner.
  2. After having decided on where to put banners, log into the AdSense administration interface and create one unit for each item on the list. Don't try to recycle units. Even if the same unit could be used in multiple places, always create a separate unit for each of them. This allows for experimenting with the layout later without accidentally trashing the whole website.
  3. Optionally (but highly recommended) give each ad unit a channel of it's own and name accordingly. This is where it pays to specify the banner location using a unified scheme and having a separate unit for each location, as it makes tracking the performance of individual units a lot easier.
  4. Write down the slot id of each of the created units (can be found as the value of the parameter "google_ad_slot" in the JavaScript code).
  5. Configure the Adsense module with the publisher ID.
  6. Optionally assign the "AdSense tag" filter to an input format or create a new custom input format, including it. A custom format should be the preferred option, as it allows to track posts with an individual layout (see bleow).

At this point, there are two possibilities to display ads. The first is to simply use one of the side bar blocks, the AdSense module provides, the second is to enable the AdSense input filter and use tags to embed banners directly into nodes. The second option should be used with care. Even though it is utmost flexible, it is prone to error and guaranteed to result in a very inhomogeneous page layout over time. Only use this for special cases.

Most bloggers will probably want to automatically inject banners into posts, using a central template. The easiest way to do this is to install the Contemplate module and let it do the styling. See below for code examples.


<?php

// Put a 120x90 link block in the first paragraph (flushed to the left) and a
// 250x250 square in the second (flushed to the right).
// Replace 4243067992 and 0335368690 with the ad slot ids, obtained in step 4.
$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] . "

";
}

?>

One thing to pay attention to, when using AdSense is to be careful about accidentally including undesired keywords. The AdSense module is able to automatically apply section targeting to the main content area. However, this is no absolute safe guard against keywords being extracted from navigation blocks or other page elements like the site's slogan.