Doubleclick for Publishers & Drupal

What do Google's Doubleclick for Publishers and Drupal have in common? They each have a zillion different ways to set things up. Below is walkthrough of how you might configure everything to get your display ads going using the DFP module.

For the moment, lets assume we are talking about a new site that has a few main sections: "sports", "local news", "business", "national news" and "arts & liesure". Let's also assume that you have setup pathauto to include these sections in your URLs so that a typical URL might be Oh, and lets assume that your site has 4 ads on most pages: topbanner, box1, box2 and bottombanner.

Setting up your inventory

If you are selling your ads by section then setting up your inventory should be as simple as creating a single Ad Unit for each site section. However, if you are selling the "topbanner" ad for a premium then you might want to create two separate Ad Units for each section: one simply called sports and one called sports/topbanner. You can do this for each ad on the page, but unless you are selling them spearately then you are just going to make things harder for yourself.

Creating your ad tags (or slots)

Start by enabling the DFP module and then make sure you set your network id at admin/structure/dfp_ads/settings. This id is provided for you in Google DFP admin tool. Now visit admin/structure/dfp_ads/add and create the first ad tag:

Notice that the Ad Unit Pattern is defined using Drupal tokens to display your Network ID followed by the first part of your URL. In our case this Ad Unit Pattern would resolve to something like 123456/sports assuming the URL for the current request is

In our hypothetical example above we might want our second tag (the one for the topbanner) to be a bit different: 

Notice that the Ad Unit Pattern now has a third token. Now, assuming we are on, the Ad Unit Pattern will resolve to 123456/local/topbanner.  This is perfect since we named our ad unit "local/topbanner" in our inventory.

The last step is to get our ads to actually appear in the correct location. To do this, its highly recommended that you create your ad tags (aka slots) as a block:

Just for good measure, we also slugged this particular ad tag with the word Advertisment. Now you can go ahead and use your favorite method for placing these blocks on the page (insert heated discussion about Panels vs. Contexts here). Oh, and dont forget to create your box2 and bottombanner ad tags.

What if my inventory & URL patterns dont match?

So all that stuff I just said is all very convenient if your URLs match your inventory but realistically this will not always be the case. In fact, unless your site is a very simple blog chances are that at least a few areas of your site will not be properly tagged using the method described above. In this case you can take advantage of the hook_dfp_tag_alter(&$tag). Let's say that in our hypothetical example above we want to sell the homepage of our newsite as its own product. In the inventory we will call it homepage. Now, since we cannot rely upon the URL here since the URL is going to be just we can take advantage of the hook instead:

mymodule_dfp_tag_alter(&$tag) {
  if (drupal_is_front_page()) {
    $tag->adunit = '[dfp_tag:network_id]/homepage';
    if ($tag->slot == 'topbanner') {
      $tag->adunit .= '/[dfp_tag:slot]';

And voila!