How to Target DFP Ads to Specific URLs

Recently I had an advertiser running a direct campaign that wasn’t having great success. We decided if we could match the ad to specific pages, the results might be better.

Unfortunately, like many things in Google DFP – this was going to be complex to set-up. I got it working in the end, and the campaign had almost double the click through.

Why Target Specific URLs?

By default, when a line item is assigned to an ad unit, this will appear across the whole site (run of site). As most sites are based on templates or themes, the ad tags will be on every page (such as in a sidebar or header).

In some sites we may have ad units only serving on certain categories of page (such as a blog post, or a product page, etc).

But what if you want more fine-grained control, and hand-pick the pages the ad will appear on? This is called custom criteria, and it’s a bit of a pig to set-up.

Here’s how to do it:

1. Setup Targeting Criteria on Your Website

We need an extra line of Javascript in the tagging code. There are some examples here.

In this case we are setting criteria for the entire page (not an individual ad slot). We call a method called .setTargeting, assigning a key (I’ve called it “url”), and a value (the URL of the page).

googletag.defineSlot("/1234/travel/asia/food", [300, 250], "div-gpt-ad-123456789-1.addService(googletag.pubads());
// New line here
// Before this line here

It depends on your individual site as to how you come up with the URL name. I’m using WordPress, so this is what I do on a post.

In the header.php (where I setup the DFP ad slots) I’ve added some extra PHP. This is inside the Javascript. The PHP is parsed on the server and outputs the URL into the Javascript line.

$url = parse_url(get_permalink($post_id));
$targeturl = substr($url['path'],0,40);

googletag.pubads().setTargeting("url","<?php  echo  $targeturl ?>");

Unfortunately DFP restricts criteria value to 40 characters, so the PHP code here will remove the hostname, and restrict to 40 characters. So if you have a massive URL, this is what will get generated.

If your URL is

The code will look like this:


This is just a limitation of DFP, so if you have massive URLs you may not be able have as fine-grained control as you wish.

2. Targeting Your DFP Line Item

First we need to setup the new custom targeting key:


  1. Open up DFP in your browser.
  2. Navigate to the Inventory tab.
  3. Select Custom Targeting, and click New Key.
  4. Name should be url, then click Add values.
  5. Into the box you can enter a large list of URLs and Apply.
  6. Then click Save.

Then assign the keys and values to the line item targeting:

  1. Go to your Line Item settings, and scroll down to Add targeting.
  2. Click on Custom criteria, then Add key.
  3. Type in url (the criteria key name you already setup in inventory).
  4. You can then quickly go through and check each URL you want to target (you can also free-form enter additional URLs).
  5. Make sure ALL URLs you want to target are entered into the single entry. Don’t add a new key record for each URL or else DFP will try to AND them together.


That’s it!

In order to test this, I had a dummy order and line item, and targeted them to an ad slot on a local developer site. I was then able to check the ad was only appearing on the URLs chosen.

The very stupid thing I did was to not stop this dummy campaign, and because I had the same ad slot on a live site – I served dummy ads on the live site for a few days. Oops.

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.