How to Use Google Adsense With Drupal

Tags:

Although there is a great Adsense module for Drupal, I prefered to insert the Adsense by hand this time. I wanted to only show ads to visitors who were not logged in. I also wanted to make sure that Adsense wasn't displayed on the Drupal search results pages. The following code snippet will place an alternate ad on the site's search results pages.

<?php
// Puts some Google ads, but not on the search page or admin pages

// Get the request_uri to check whether you are on a search page
$uri = $_SERVER['REQUEST_URI'];

// check to see if the visitor is logged in
global $user;

if (!$user->uid) {  // only show ads to people who aren't logged in
  if (!strstr($uri, '/search/')) {  // make sure we are not on a Drupal search results page
   echo '
     // insert your adsense JavaScript code here
   ';
  } else {
   // if we are on a search results page, then insert alternate content
  }
}
?>

There are some other resources for using Adsense on Drupal on the Adsense Module web site.

No votes yet

Comments

Adsense Injector can help

Good post.

If you use the Adsense module, you can control the placement of ads (limit ads to certain pages, etc.) You might also want to try the adsense injector module - it allows admins to insert adsense ads automatically at the top of each node when viewed as a full-page node view.

http://drupal.org/project/adsense_injector

or

http://exodusdev.com/drupal/4.7/modules/insert-adsense-ads-automatically

Cheers!

Webmaster Tips's picture

Adsense Injector Module for Drupal

Thanks for posting that. It looks like a great module. I took the AdSense off this site, but may try it on another site.

Syndicate content