- 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.
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:
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
To achieve the latter, here is what you need to do.
Say you have a phptemplate that has the following:
<?php print $content ?>
You need to change this as follows. Add the section in red, then add your Google AdSense code after it.
<div style="float: right; padding: 0px; margin: 0px;
border-width: 0px 0px 10px 10px">
<!--ADD GOOGLE ADSENSE HERE-->
<?php print $content ?>
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:
- Google Adsense
- Use of Google Adsense et. al.
- How to put a leader board Ad in Drupal
- Issue with HTMLArea and AdSense. Note that the above regular expression for hiding the Ads in edit and add pages should take care of this.
- Second block not displaying AdSense. This is a common question that novices ask. This is an AdSense feature, not really a Drupal problem.
- Google AdSense (again).
- Another Google AdSense forum discussion.