How to setup DFP Targeting parameters

Pushing custom page/section/whatever targeting parameters into adverts is a common requirement. DFP has some very straight forward functions that allow you to push in custom parameters across all ad slots on a page or individual ad slots on a page.

Please see my previous articles on DFP, How to setup Google DFP ads on your site and How to refresh Google DFP ads, for more information on some of the concepts put forward in this article. I've also written an article on How to debug DFP ads.

Setting targeting parameters

DFP already offers section and subsection targeting via it's ad paths, however you might encounter a situation where you need to target specific pieces of content that cannot be broken down in terms of site structure.

You might have a set of articles that are all about a similar subject, such as a programming language such PHP, these articles might span a few different sections of your site such as Development, Architecture and Performance.

Let's imagine a situation where you want to be able to target PHP related ads at these pages but not the rest of your site. The best way to achieve this is by pushing in a targeting parameter (eg. 'language', 'php') into DFP for any article that has been tagged as being about PHP.

So how do we go about this? DFP provides a function within it's pubads class called SetTargeting, it can be used like so:

googletag.cmd.push(function() {
    googletag.pubads().setTargeting('[PARAM_NAME]', '[STRING/ARRAY]');  

This function will add a targeting parameter with the specified value into all of the ad slots on the page, you can also call this function on a specific ad slot like so:

var slots = {};

googletag.cmd.push(function() { 
    slots['ad'] = googletag.defineSlot('/[ACCOUNT_ID]/[PATH]', [[120, 600], [160, 600]], 'div-gpt-ad-sky').addService(googletag.pubads());
    slots['ad'].setTargeting('[PARAM_NAME]', '[STRING/ARRAY]');

The setTargeting function can accept strings or arrays as values. Ad's can be targeted to these custom parameters from the DFP admin console. It's important to note that the PARAM_NAME field can only be 10 characters long, anything longer than this will be truncated.

In order for the targeting parameters to appear you must set them before you make the call to enableServices(), this is because this is the point at which the call to the DFP server happens and the actual content for ad slots is retrieved from the DFP ads server.

Here is a full example of DFP targeting parameters, minus account details and whatnot of course:

      <script type='text/javascript'>
         var googletag = googletag || {};
         googletag.cmd = googletag.cmd || [];
         (function() {
         var gads = document.createElement('script');
         gads.async = true;
         gads.type = 'text/javascript';
         var useSSL = 'https:' == document.location.protocol;
         gads.src = (useSSL ? 'https:' : 'http:') +
         var node = document.getElementsByTagName('script')[0];
         node.parentNode.insertBefore(gads, node);
      <script type='text/javascript'>
         var slots = {};
         googletag.cmd.push(function() {
         googletag.pubads().setTargeting('tags', ['php','symfony2','javascript']);
         slots['sky'] = googletag.defineSlot('/[ACCOUNT_ID]/[PATH]', [[120, 600], [160, 600]], 'div-gpt-ad-sky').addService(googletag.pubads());
         slots['mpu'] = googletag.defineSlot('/[ACCOUNT_ID]/[PATH]', [[300, 250], [300, 600], [300, 900], [300, 1050]], 'div-gpt-ad-mpu').addService(googletag.pubads());
         slots['leader'] = googletag.defineSlot('/[ACCOUNT_ID]/[PATH]', [[728, 90], [970, 250]], 'div-gpt-ad-leader').addService(googletag.pubads());
         slots['leader'].setTargeting('homepage', true);
      <div id='div-gpt-ad-sky'>
         <script type='text/javascript'>
            googletag.cmd.push(function() { googletag.display('div-gpt-ad-sky'); });
      <div id='div-gpt-ad-mpu'>
         <script type='text/javascript'>
            googletag.cmd.push(function() { googletag.display('div-gpt-ad-mpu'); });
      <div id='div-gpt-ad-leader'>
         <script type='text/javascript'>
            googletag.cmd.push(function() { googletag.display('div-gpt-ad-leader'); });

Debugging your targeting parameters using the console

The Google debug console will show that the parameters have been passed into the slots.

If you open the console (add ?google_console to the URL and press ctrl+f10) and check the page request tab you should see some messages that begin with 'Setting targeting attribute...'. Global parameters should appear before the ad slots are created, slot specific parameters should appear just after the messages about the targeted slot being created.

Debugging the call to the DFP ads server

If you want to further debug the targeting parameters then you can see them being passed to the DFP server via the URL used to retrieve the ads.

You can capture this URL by using Firebug or any other browser debug console.

Simply open the console, switch it to the network tab and then open the page in question. Look for a call to, this is the actual call to the ads server.

If you look at the query params attached to this URL you should see a field called prev_scp and one called cust_params, prev_scp contains slot specific parameters and cust_params contains global targeting parameters.

The DFP server has a character limit

One thing to be wary of is that the DFP server has a maximum character limit for URLs, I believe it is 2000 characters. If a URL used to retrieve the ads is over this limit then the ads will not be returned properly and may not display at all.

This limit can be reached if you have lots of ad slots and targeting parameters, slot level targeting parameters can cause this limit to be reached quickly.