Skip to main content

Insert AdSense Ads Automatically in Drupal Nodes and Teaser Lists

Here's a useful Drupal module that tweaks a node's body or teaser to insert an inline AdSense ad, when a node is rendered on a full page or teaser list.

This allows site administrators to enable automatic insertion of an AdSense ad into the page without the content editor having to insert inline [adense] filter tags, or having to edit a site's template.php (or other template files.) This offers a big advantage, as you can change the ad format, group, and channel on the fly without hand-editing each node (which is the only way I can think of doing it if you are using inline filters). Another advantage is that you can enable/disable the inserted AdSense with just a click of the mouse - no need to edit each node.

This module relies heavily on the AdSense module (so no, I'm not reinventing the wheel!)

We like this module!

We use this module here on our own site, as well as several others that we operate.
You can see this module in operation on this site when you view any article on its own - the ad you see near the top of the node body is dynamically inserted into the body of the article when it is rendered - the AdSense code is not stored in the node's database record.

Why not just massage node.tpl.php (and other template files?) Well, if you administer multiple sites (as I do), or are trying out new themes, doing so can be tedious at best. And, once you have tweaked a theme's files, you will have headaches if the theme is updated to fix bugs or browser compatibility problems.

The excellent AdSense module does most of the heavy lifting. Obviously, this requires that the AdSense module be installed, enabled, and configured correctly.

The site administrator has the following options:

  • Enable/disable insertion globally.
  • Select Ad format, channel, and group (as defined by AdSense module).
  • Edit template string for insertion - insert Adsense module tags, before or after body or teaser.
    This could be enhanced later to use templates or regular expression replacement capabilities.
  • Enable or disable insertion for individual node type(s).
  • NEW: set minimum node body "word count" threshold, below which the inline ads will not be inserted. This uses the same technique used by Drupal's node.module for minimum word count when submitting nodes (it just counts things separated by spaces, and doesn't adjust the count for HTML tags or any other markup). The module will insert HTML comments to help you see the word counts and limits (in case you think you should see an ad, but don't, or vice versa.) This limit is 'global' to all node types, but could be enhanced later to allow setting it on a per type basis.

Possible future enhancements

  • Repeated ad insertions after one or more paragraphs
  • Pattern matching, or other more sophisticated insertion strategy

Recent changes

  • Set minimum node body "word count" threshold, below which the inline ads will not be inserted. This uses the same technique used by Drupal's node.module for minimum word count when submitting nodes (it just counts things separated by spaces, and doesn't adjust the count for HTML tags or any other markup). The module will insert HTML comments to help you see the word counts and limits (in case you think you should see an ad, but don't, or vice versa.) This limit is 'global' to all node types, but could be enhanced later to allow setting it on a per type basis.
  • NEW: (1/7/2007) - Respects the Adsense module visibility settings - inserts only on pages that Adsense module will allow, based on the AdSense module's visibility settings.
  • NEW: (3/16/2007) - Insert ads in teaser lists (like the front page)
  • NEW: (3/16/2007) - Template-based insertion - define a template which can include any valid Adsense module tag, insert multiple ads, insert before and after body/teaser, etc.

Sounds great! How do use it on my own site?

Now hosted on Drupal.org CVS/Project repository. Please visit the Drupal.org project page for latest releases, bug/issue reports, etc.

Highly interested

Just to drop you a line about this module - we're highly interested in it and think that it would be a really nice addition to the module repository on drupal.org. Contact me if you don't intend to put it up on drupal.org, cause I'd like to have a copy :P

---------------
Biology News Net - Recent Biology News & Articles

I am interested too. Great idea

I am interested too. Great idea. How about preparing the module for Drupal 5.0. Thanks guys for your work.

5.0

I'm going to start work on setting up a 5.0 dev/test site. I'll keep you posted.

5.0 version committed to Drupal CVS

Visit the drupal project site and download from there.

Name that module

Ok, this module has been approved to be hosted in the drupal.org CVS tree as an contributed module under the adsense module project.

While I like the name "adsense injector", others may not agree.

Here's your chance to help suggest a better name - before we commit this to CVS.

Suggestions?

Let's get the ball rolling:

  • Adsense Stuffer
  • Adsense Node Blaster
  • Adsense Helper
  • Adsense Assistant
  • Adsense Assist
  • Adsense Insertion
  • Adsense Inserter
  • Adsense Automation
  • Adsense Automator

Adsense Injector it is

I've decided to commit it to the Drupal project repository as adsense_injector.

To me, Adsense injector is

To me, Adsense injector is better than all the other options you mentionned, especially if we can get the 'inject adsense after x paragraphs if the entry is lengthy enough` feature! I wouldnt change the name, its great!

Biology News Net - Recent Biology News & Articles

Version 133 dated December 28, 2006 has a simple method

Today's version has a simple minimum word count (rather than paragraphs) threshold capability. Counting paragraphs is beyond the scope of the project, for now.

Here's the version header from the current source:
  /*  $Id: adsense_injector.module 133 2006-12-28 07:33:50Z mcurry $ */

Inject after # of words

Excellent module, tried it on a test setup.. Would love to make a donation if you can add a feature of Injection after a location..

It can be either
1. After the teaser
2. After # of words
3. After # of chars..

Which ever works the easiest. Is there anyway to eliminate the comments in the text about word count etc ?

All things are possible....

... but they take time. I think the next thing I am going to add is the ability to specify injection before or after the teaser when the node is not being shown individually.

Of course it's possible (and trivial) to remove the word count comments, but I'm not sure why those should be removed. Care to explain why you don't want them there? If I remove them, then I'll get questions from people asking me why an ad didn't show up on a particular node... :D

I like it too

The adsense module crew asked for a more meaningful name - apparently injector doesn't do it. Anyway, I think I'll leave it alone - I like injector, too.

So, regarding your suggestion, I think we need to find a reliable way to count paragraphs in an article - html <p> ... </p>, text separated by newlines, etc... it all depends on the input filter being used!

AdSense Injector

Just wanted to let you know I really appreciate your work on this module. Just installed AdSense and Injector yesterday on a test site and it's working great.
The URL above is just a test site.
Thanks!
BK

Thanks a lot.

Hey Mike, Thanks a lot for developing such a nice module. I am using it on my site and believe me its really very cool to see ads on the starting and end of the node/blog. I am using it on http://www.share-ebooks.com . Was looking for ads between two teasers and landed up on this page so thought to thank you.

Thanks a lot

- Mittal Patel
http://www.MittalPatel.co.in

openads (now OpenX)

This is what I was looking for, but, is there any chance this could work with other-than-adsense ads, like OpenAds would be perfect? I'd like to cycle through random images too. I can do all I want with OpenAds / OpenX except inject the ads in the Drupal node body. If Adsense Injector works with only the Adsense Module, can you give me an idea how hard it would be to modify it? I'm fairly techy, but weak on PHP.

thanks for the module!

BTW, I'm glad you stuck with the name 'Injector'.

Version for 6.x?

Would it be possible to get a version update for Drupal 6.x?

No plans at present

I have no plans to do the port right now.

Volunteers welcome. Any takers?

Drupal 6

I would really love to see this module updated for Drupal 6.x

I'd even offer to do it myself, but don't know how.

I would love to see the AdSense module updated for Drupal 6

I started looking at this task, but the AdSense module (upon which AdSense Injector depends) is not available, in any form, for Drupal 6. So, in order to make this module work for Drupal 6, I'll have to make the AdSense module work first, or divorce this module from the AdSense module.

Please see: http://drupal.org/node/265666 and http://drupal.org/node/211584

So, as far as I'm concerned, given my schedule and the state of the AdSense module, I'm not likely to start working on this until the AdSense module is available for D6 (if only in beta test form.)

adsense module is updated to version 6

Hey!

I started using your module quite some time and I am now relying heavily on your heavy work. I just saw that the parent adsense module is updated to version 6, I'm sure I'm only one of the various people who would love an update to your module to version 6 aswell! :D

Thanks in advance!

D6 port -- would contributions help?

Mike,

I just saw your module and it looks like just the thing I've been looking for...but my site is a D6 site.

Notice that there are other people who commented here, asking about a D6 port, so my guess is that there are a bunch more people who'd love D6 port, too.

Wondering if collective contributions would help get a D6 port going? I can't sponsor the whole port myself at the moment, but I'd love to chip in and co-sponsor a port -- assuming that others can chip in, too.

Anybody else interested?

Jessica
aka..."jessla" (on Drupal.org and groups.drupal)

Coding contributions always welcome

Thanks for the offer of support.

Coding? Well, I'll probably pick up the code and start modding it for D6 soon. Anyone else who wants to help should feel free to submit patches to the issue queue on Drupal.org.

stuck

Man I was stuck with this on Drupal 6x for days. The only way I could get it to work was with a block. I wrote how I did it on a blog here if anyone is interested: http://computerquestionhelp.com/blogs/Misc/getting-google-adsense-to-eas...

GL to the rest of you :/

Great Work!!!

I looked for days and hours for the best way to add adsense after my content (above comments). There were lots of "hacks" and theme editing methods. Finally i came across this module that i absolutely love. I really like the %body feature as I've used that insertion method quite alot and it made perfect sense, and allowed for great customization (it reminds my of 'c' programming with %d or %s for integers and strings). Great work 5/5