One of the most useful features of DFP is Custom Criteria, a way of targeting certain ads to specific pages of a website. We will describe some of the most useful targeting parameters and show you how to implement them in WordPress.

A targeting parameter can be almost anything you want. There are some a few prohibitions, such as passing personal identifiable information via targeting parameters. For more information, see the notes from Google: https://support.google.com/dfp_premium/answer/177383?hl=en

Article Roadblock

A very common use of custom targeting criteria is to perform a single article roadblock where an advertiser buys all the ads on a specific page. In WordPress, this is easily done using the Post ID. We also like to include the permalink as well. The permalink is more recognizable, but risks being changed by an editor, for example, to fix a typo.

To allow targeting by Post ID and Permalink, add the following to your header.php, right in the middle of the DFP code.

<?php
// insert after the last ad slot definition
if(is_single()) {
  echo "googletag.pubads().setTargeting('page', 'single');\n";
  echo "googletag.pubads().setTargeting('post_id', '" . $post->ID ."');\n";
  echo "googletag.pubads().setTargeting('permalink', '" . get_permalink($post->ID) ."');\n";
}
?>
// the rest of the ad code is unchanged... 
googletag.pubads().enableSingleRequest();
googletag.pubads().collapseEmptyDivs();
googletag.enableServices();

Homepage takeover

Equally useful is the Homepage takeover. As the name implies, this is where an advertiser has purchased all ad units on the homepage. Note that WordPress considers all reverse chronological pages to be “home”. In this example, we only target the real homepage, by excluding those other pages.

<?php
// insert after the last ad slot definition
if(is_home() && !is_paged()) {
  echo "googletag.pubads().setTargeting('page', 'home');\n";
  echo "googletag.pubads().setTargeting('permalink', '" . get_home_url() ."');\n";
}
?>
// the rest of the ad code is unchanged... 
googletag.pubads().enableSingleRequest();
googletag.pubads().collapseEmptyDivs();
googletag.enableServices();

Tag/Category/Section takeover

Suppose you run an automotive blog. You probably use tags or categories to organize your articles by Make/Model. This provides convenient navigation for readers. The great news is that it’s also useful for ads. You can use your existing tags and categories to segment your inventory.

The code is a little more complex, but as before, you can copy-paste this into your header inside the DFP code.

<?php
// insert after the last ad slot definition
if(is_single()) {
  // build tags JS array
  $aoa_tags = array();
  $aoa_the_tags = get_the_tags($post->ID);
  if($aoa_the_tags) {
    foreach($aoa_the_tags as $tag) {
      $aoa_tags[] = "'".$tag->slug."'";
    }
  }
  $aoa_tags = '['.join(', ', $aoa_tags).']';
  echo "googletag.pubads().setTargeting('post_tags', " . $aoa_tags .");\n";

  // build categories JS array
  $aoa_categories = array();
  $aoa_the_categories = get_the_category($post->ID);
  if($aoa_the_categories) {
    foreach($aoa_the_categories as $category) {
      $aoa_categories[] = "'".$category->slug."'";
    }
  }
  $aoa_categories = '['.join(', ', $aoa_categories).']';
  echo "googletag.pubads().setTargeting('post_cats', " . $aoa_categories .");\n";
}
?>
// the rest of the ad code is unchanged... 
googletag.pubads().enableSingleRequest();
googletag.pubads().collapseEmptyDivs();
googletag.enableServices();

Test / Staging Site

It be can very useful to have a “testing” flag available, especially for a development environment. This allows you to run a test campaign with placeholder creatives without affecting the live site. Here is one way to do it:

<?php
// substitute the name of your development server
if( ($_SERVER['HTTP_HOST'] == 'dev.example.com' ) ) {
    echo "googletag.pubads().setTargeting(\"testing\", \"y\");\n";
}
?>
// the rest of the ad code is unchanged... 
googletag.pubads().enableSingleRequest();
googletag.pubads().collapseEmptyDivs();
googletag.enableServices();

Other possibilities

Custom criteria can be used to target just about anything you can imagine. Author pages, custom post types and forums are other examples and follow a similar pattern. If you have multiple sites, passing the site name as a custom targeting parameter can be very useful, depending how things are tagged.

NSFW Negative Targeting

Google and many other advertisers have specific restrictions against running their ads against NSFW and other prohibited content. Depending on the contents of your site, this may be something that never crosses your radar, or it may be an everyday battle. Occasional violations aren’t necessarily a big deal, but repeated offenses can get you banned from AdSense and other advertisers, which can be very serious. One way to avoid this problem altogether is by using Custom Criteria to flag any risky content. Provide a checkbox in the CMS, then simply target your Google line items away from that flag. We’ll cover a WordPress implementation of this technique in a future article.

Please Note! If you’re using AdSense dynamic allocation, you will still be running AdSense against those pages, despite the flag. We don’t recommend AdSense dynamic allocation for precisely this reason. You get all the benefits and more flexibility from AdExchange.

No Ads Targeting

In rare cases, you may want a flag to disable all advertisements. This might be appropriate for a news article about a major tragedy.For examples, Google for “funny inappropriate website ads”. Unlike NSFW content, which can get you banned from certain ad networks, a truly unfortunate ad placement can draw outrage on social media and create angry readers. It’s great to have a kill-switch available, just in case you ever need it.

Caveats

Be extra careful when serving ads to multiple websites through the same instance of DFP. Suppose you have two websites, Site A and Site B. On Site A, an advertiser has purchased an article roadblock against PostID = 1234. Site B comes along several months later and also has a PostID = 1234. If the no additional targeting is used, Site B will suddenly show the roadblock from Site A! This is easy solved by adding an extra “site” parameter, or prefixing the PostID with the site name. A similar problem can happen with other shared parameters such as tag names and categories.

Drop Us a Line

We’re dedicated to working with the hungriest minds in our field. If you’re involved with digital content production or digital advertising and marketing, we’d love to hear from you. Big, small, experienced or beginner, please feel free to get in contact if you think we can be of service.

Say Hello