API

Last modified: May 19, 2009 - 15:11

API

The adsense module provides developers with an API that can be used to control the Adsense Client ID used for the particular page view.

You can decide to select a different Adsense Client ID based on the content type, the user's role, the level of user points, the taxonomy of content page, a monthly contest, rewarding users, connected to Google's API, or anything else imaginable.

To do so, your module must implement a hook called 'adsense', as follows:

Assuming your module is called: your_module.module, you will have the following function in it. The function has an $op argument that you should check:

<?php
function your_module_adsense($op) {
  static
$client_id = NULL;

  switch (
$op) {
    case
'settings':
        return array(
         
'name' => 'Module name',
         
'desc' => 'Anything about your module',
        );
      break;
    case
'client_id':
      if (!
$client_id) {
       
// We cache the client ID on this page load, to make sure all of the
        // client IDs on one page are the same
        // Here you can use whatever logic you want to select a Google
        // Adsense client ID.
        // If the args parameter is not NULL, a format specific slot ID +
        // Publisher ID needs to be returned in an array with 'slot' and
        // 'client' fields.
        // If the args parameter is NULL, return only the Publisher ID as a
        // string.
       
$client_id = your_logic($args);
      }

      return
$client_id;
  }
}
?>

Your module's settings form must be of type MENU_LOCAL_TASK and located in 'admin/settings/adsense/publisher/your_module'.

After you install the module, it should appear on the adsense module settings page, along with other modules. You should be able to select it, and configure it.

 
 

Drupal is a registered trademark of Dries Buytaert.