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:
- 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.
- Another guide on how to add Javascript (including Google AdSense) to Drupal. On Bryght.
Comments
Anonymous (not verified)
great page BUT......in the
Thu, 2010/02/25 - 10:29great page BUT......in the instructions above on "How to configure Drupal for AdSense" it states
Paste the AdSense code from your account. Make sure that you paste the entire code....
my question is.....WHERE???? i dont see any option to paste php code here on the blocks page. could someone please help me out on this one. there is no option for input format either
rmakrishna (not verified)
google adsense
Wed, 2010/07/07 - 08:23hi sir i want to add google adsense to my website could you please tell me where i have to paste the google addsense code.and how to add google addsense in the content block rightside for example
Cinemachances.com is a portal which highlights
the arts and skills of the artists who want
to enter into Telugu Film Industry (Tollywood)
over Internet so that prospective producers
and directors who are in immediate need and
search of the artists can get quick information
the remining place after the content i want to place addsense could u please help me
thanks a lot
Pages