Using Google AdSense with Drupal

A recurring question on the Drupal.org forums is by people new to Drupal wanting to setup their site for Google AdSense. This quick guide should help with this topic.

Updates:

  • A few weeks after I wrote this article, I wrote a new Drupal Google AdSense module. You can use that for a pre-packaged solution that may be easier to configure in some cases.
  • Some of the details here (e.g. making blocks hidden for certain URL paths) are specific to Drupal 4.5. Things are easier in Drupal 4.6 and more.

Introduction

Drupal is an Open Source Content Management System that is gaining popularity fast. It is used to run lots of sites on the internet, and more and more people are using it. For more information visit the Drupal section on this web site for a quick overview.

Google AdSense is a way for web site publishers to earn some revenue by allowing Google to advertise on their web site, using targeted keywords. Google indexes your web site pages, and as visitors access them, Google tries to display ads that best matches the keywords on the specific page.

This guide assumes that you have already applied for a Google AdSense account, and that you are using Drupal 4.5 or higher.

Selecting the Ad Format

AdSense allows so many formats to be displayed. You need to select a format that fits in your site without being a distraction.

For example, if you are using blocks, then select Skyscraper format.

Select colors for the ads that will blend in your site's color scheme. You may also want to save that color scheme by a name you know (e.g. "Drupal Pushbutton") so you can use it later.

How to configure Drupal for AdSense

There are many ways of displaying Google AdSense ads in a Drupal web site. The simplest way is to display the ads in blocks. To do so, follow these steps:

  • Log in as the site administrator
  • Go to Administer -> Blocks
  • Add a block
  • Give the block a descriptive title, e.g. "Google Ads"
  • Paste the AdSense code from your account. Make sure that you paste the entire code as it is and not miss any lines.
  • Make sure that the type is PHP. If you do not see a type PHP, then add it in the Input Format.
  • Give the block a description, e.g. "Google Ads"
  • Click on "Save"

Now you have created the block, you need to enable it and position it.

  • Click on the Enabled checkbox
  • Select a weight for the block
  • Select if it is to be displayed in the right or left column.

Do not click "Save" yet. Go to the next step.

Hiding AdSense in Admin pages

Since the Administration pages are not visible to your visitors, it does not make sense to display ads on them. Moreover, you may not want to display ads on the node/add page, the edit and the outline pages.

So, in the "Path" box, enter the following regular expression, including the < and > characters:

<^(?!admin|node/add|node/\d+/edit|node/\d+/outline)>

Theme Template To Display AdSense

The above is only the simplest way you can get AdSense going, but there are many other ways to do it differently.

For example, if you want the ads to appear in the page header, or in the side of the node, or before the node title, or between the teaser and the body, you can do so.

You can even write a filter that would replace the <!--break--> tag by Google AdSense code.

However, all this involves modifying your theme to do so. If you are using a phptemplate theme, then check the node.tpl.php

Here is an example on how to change the interlaced theme to provide this. And also a discussion on how to embed AdSense in nodes.

To achieve the latter, here is what you need to do.

Say you have a phptemplate that has the following:

  <div class="content">
<?php print $content ?>
</div>

You need to change this as follows. Add the section in red, then add your Google AdSense code after it.

<div class="content">
<div style="float: right; padding: 0px; margin: 0px;
border-width: 0px 0px 10px 10px">
<!--ADD GOOGLE ADSENSE HERE-->
</div>
<?php print $content ?>
</div>

You may need to adjust the content style in your CSS file to display the rest of the node correctly.

Resources and Links

The following discussions on Drupal.org forums can also help if your answer cannot be found above:

Contents: 

Comments

Criação de Sites

Interest! Thanks for tips.

Helpful module

I found this module great and helpful. It was piece of cake to implement adsense ads onto my site.
It's so easy to experiment with changing position, colors or size of ads. You don’t need to change page code, just do it from admin panel with a few clicks.
thanks...

Where is the path box, i

Where is the path box, i could not find it in my block page.

Issue with channels?

Today I gave the adsense module a try.

It works very well so far. There's just one thing I'm not sure whether it is a problem with google or not.

I want to use channels and revenue sharing.
But now the code shows my channels also for other authors.
From google point of view it must look like other people use my channels.

What happens if other people do use my channels?
Will it be a problem?
Will they get the revenue?
Will my channes be visible in their account now, too?

Hopefully someone could give an answer - otherwise I will have to remove the channels to stay on the safe side.

Thanks a lot

Werner

manage ads

Since Google Adsense use manage Ads, Adsense module can't applayed to sites.

Please update this module tha support Manage Ads..

Limiting Adsense to certain drupal forum pages

Is there a way to keep adsense off of pages which discuss sexuality and other issues that google does not allow? I have a forum for women, and in women's health they discuss this but I want adsense on the other pages...Thanks!
thinkhappy@thewomensnest.com

Module for 6.1?

Are there any plans for this module to support Drupal 6.1? I just installed Drupal for the first time and would rather not have to downgrade just to have Google ads. If not, maybe someone else has tips for another way of getting Google Ads working in 6.1.

Port to 6.x is planned ...

The port to 6.x is planned, but only after the new Google code is done in 5.x.

See 211584 and the links in the comments from there.

If people test the patch and confirm it is working, things will move faster.
--
Khalid Baheyeldin

can you update this article

can you update this article for the newest version? this article has lost almost all relevance to the version on drupal.org and I am having a sh!tload of problems trying to implement adsense on one of my sites. when attempting to configure "adsense: unconfigured 1" block, there is no place to copy/paste the adsense code. Yes, I have read the help, yes I have searched google, and unfortunately, your site was the first to show up.

the adsense

can you update this article for the newest version? this article has lost almost all relevance to the version on drupal.org and I am having a sh!tload of problems trying to implement adsense on one of my sites. when attempting to configure "adsense: unconfigured 1" block, there is no place to copy/paste the adsense code. Yes, I have read the help, yes I have searched google, and unfortunately, your site was the first to show up.

Pages