Clear search
Close search
Google apps
Main menu

DFP macros

In DoubleClick for Publishers, macros are strings of text that the DoubleClick ad server replaces with other strings of text according to a set of rules. They can be included in any code that's processed by the ad server during the delivery process: third-party or custom creative code, DoubleClick Rich Media code, click-through URLs, DFP tags, even within Flash files that are hosted by DFP.

For each macro, we've provided suggestions on how you might use it. However, how you use macros is up to you. Like the DoubleClick for Publishers API, macros can be put to whatever uses are most valuable for you.

Macros are case-sensitive. For example, %%CACHEBUSTER%% works, but %%Cachebuster%% doesn't.

Escaped, double-escaped, and unescaped URL macros

URL macros such as destination URL, click macro, viewed impression macro, and some video-specific macros, are available in unescaped and double-escaped variants. It's important to understand when to choose the right variant of these macros. For this reason, we recommend reading the article on escaped vs. unescaped URL macros.

Add macros to creative code

One of the main uses of macros is within the creative code of custom and third-party creatives. DFP inserts the macros automatically for third-party creatives or VAST tag URLs, if DFP recognizes the third party. For custom creatives and creative templates, you need to add the macros manually.

Supported macros

DoubleClick for Publishers supports the following macros. We've grouped them according to their most common uses, but macros are highly flexible tools that you might use differently.

Rendering macros

These macros are most commonly used in the process of rendering creatives, ensuring that they look right and work correctly.


Ad unit macro

Here's how you would add the ADUNIT macro to an ad tag:

<script type="text/javascript">
document.write('<sc'+'ript type="text/javascript" src=""></scr'+'ipt>');

The ad unit macro expands to the full ad unit path contained in the original ad request, including the network code. Here's an example of how the ad unit path would expand when the ad gets served:

<script type="text/javascript">
document.write('<sc'+'ript type="text/javascript" src=""<>/scr'+'ipt>');

This macro only works for approved suggested ad units. If a suggested ad unit is at the third level, this macro only expands to the second-level ad unit when used in the creative snippet. Once the suggested ad unit is approved, the macro expands to the third ad unit level in the creative snippet.
This feature is not available in DFP Small Business.
Destination URL macro
Macro Escaping
%%DEST_URL_ESC%% Escaped
%%DEST_URL_ESC_ESC%% Double-escaped
%%DEST_URL_UNESC%% Unescaped

The destination URL macro expands to the creative's click-through URL, which is either the line item's click-through URL or an override set for the creative within the line item.

The destination URL macro is useful in custom creatives and creative templates. If the creative code needs to include the creative's click-through URL, and the click-through URL changes as the creative or template is used across different line items, then this macro can help.

There are three click-through URL macros: escaped, double-escaped, and unescaped. Typically the amount of escaping (whether you include ESC once, twice, or not at all) depends on the third-party click-tracking company. Some third-party click-tracking companies require the URL to be escaped, while others do not. The level of escaping required by a third-party click-tracking company is often specified in the tag with the click= string in the third-party ad tag or custom creative code in DFP:

  • click=: Escape it once (%%DEST_URL_ESC%%).

  • click0=: Don't escape it (%%DEST_URL_UNESC%%).

  • click1=: Escape it once (%%DEST_URL_ESC%%).

  • click2=: Escape it twice (%%DEST_URL_ESC_ESC%%).

DFP also supports %%DEST_URL%%, which does exactly the same thing as %%DEST_URL_ESC%%. We recommend using %%DEST_URL_ESC%% for clarity, but there's no need to recode older creatives and templates.
File server macro

The file server macro %%FILE:file_display_name%% can be used within the code of a custom creative. It expands to the full URL of a creative asset that has been uploaded to the creative. It can be useful if your custom creative code needs to call the asset.

To use the macro, replace file_display_name with the display name of the creative asset, as it's shown in DFP when you view the creative details.

Height and width macros

These macros insert the creative height and width into the custom code of a creative during the ad serving process.

The height and width macros can be especially useful if you're creating a creative template that you want to reuse with creatives of different sizes. Instead of hard-coding the size for each creative, you can let the height and width macros insert the values into each creative dynamically.

You can also use these macros in the custom code for creatives where you've overridden the creative size (which you can do on the "Settings" tab of a creative). When you override the size, you can enter multiple creative sizes. The creative can then be served to ad units of any of those sizes. You can use the height and width macros to add the dimensions dynamically to the creative code when the creative is served.

Pattern match macro

Use the pattern match macro to pass one or more custom variables such as a targeting value into a creative. You can use this macro to pass key-values, replace the entire targeting string, or expand the referrer URL. This can be helpful if you want to serve different creatives based on information you know about a user, such as a user's content preferences.

Escaping the pattern match macro
The pattern match macro returns the value as received. If you are using the macro in one of the URL fields, such as the "VAST tag URL" or "Third-party tracking URL" field in a video creative, you may want the expanded value to return escaped. To do this, you need to triple-escape the value in the request.


For example, you have two creatives for a given line item: one that was designed to appeal to female users and one that was designed to appeal to male users. The "key" used in the macro must be alphanumeric characters or the underscore (_).

Here is how the process works:

  1. You're passing the custom value (in this case, user's gender) into an ad tag on your page using key-values:
    googletag.defineSlot("/1234/adunit1/adunit2", [728, 90], "div-gpt-ad-123456789-0")
    .setTargeting("gender", "male");
  2. In the custom or third-party creative, dynamically pass the custom value (user’s gender) using the following macro: <some creative script here>...&gender=%%PATTERN:gender%%
  3. The entire macro of %%PATTERN:gender%% will be replaced with "male".
  4. DFP will call and serve the “male” creative file to this user.


You can use %%PATTERN:TARGETINGMAP%% to replace the entire targeting map and dynamically create passback tags.

Here's how you would add the TARGETINGMAP macro to an ad tag:


Here's an example of the complete key-value string when the ad gets served:

.updateTargetingFromMap({“key1”:[“value1”], “key2”: [“value2”,”value3”]}).display();

Sample passback tag between two DFP networks

You can use this macro to set up a passback tag between two DFP networks ("Network A" and "Network B").

Here is a sample creative booked in DFP Network A:

<script type='text/javascript' src=''>
googletag.pubads().definePassback('/network_B/Homepage', [468, 60])
 .setTargeting("myAdUnit", "%%ADUNIT%%")

In case DFP Network B does not have any sold campaign to serve, it can give the impression back to Network A using a passback creative such as the following:

<script src='//'>
 .definePassback('%%PATTERN:myAdUnit%%', [468, 60])
 .setTargeting("kwd", "noloop") // to avoid infinite loops
Sample passback tag between DFP and a third-party ad server

You can also use TARGETINGMAP in a passback tag that gets sent in a request to a third-party ad server, enabling you to have multiple values in a dynamic creative.

Here is a sample creative booked in DFP:

<script type="text/javascript">
document.write('<sc'+'ript type="text/javascript" src="'+encodeURIComponent(JSON.stringify(%%PATTERN:TARGETINGMAP%%))+'"></scr'+'ipt>');

When served by DFP, this expands to:

<script type="text/javascript">
document.write('<sc'+'ript type="text/javascript" src=""></scr'+'ipt>');

The third party now has access to the dynamically generated “dfpAdUnit” and “targeting” parameters and can build the following passback:

<script src='//'>
 .definePassback('/network_A/first_level_ad_unit/second_level_ad_unit', [728, 90])
 .setTargeting("kw", "noloop") // to avoid infinite loop (see

To do that, the third party may be given a template code that looks something like this:

myTargetingObject = JSON.parse('$targeting'); document.write('<sc'+'ript src=""></scr'+'ipt>');
  callback = function() {
    googletag.pubads().definePassback('$dfpAdUnit', [728, 90])
    .setTargeting("kwd", "noloop")

  window.googletag = window.googletag || {};
  googletag.cmd = googletag.cmd || [];



You can also use the reserved key url to expand to the URL of the referring page.

Note that url is a reserved key; any key-values in your DoubleClick tags that have url as the key name will be overwritten with the referrer URL.

Target in new window macro

The target in new window macro expands to a value that corresponds to the target window specified for the publisher and ad slot where an ad is being served:

  • If the target window is _blank, the macro expands to 1.

  • If the target window is any other value, or if no target window is specified, the macro expands to 0.

Target window macro

The target window macro expands to the target window specified for the ad unit where a creative is being served. You can use the macro within the code of a creative to specify whether a click on the creative will take the user to a new window or load the landing page in the same window, depending on the settings for each site where users will see the ad.

For example, the ad unit has the target window set to _top and the ad unit has it set to _new. If the %%TARGET_WINDOW%% macro is included in the creative's code, it will expand to _top for users at and _new for users of of

Here's an example implementation:

<a href="%%CLICK_URL_UNESC%%%%DEST_URL%%" target="%%TARGET_WINDOW%%"><img src="my ad"></a>

Tag for child-directed content macro

The tag for child-directed content (TFCD) enables you to let third parties know that a given ad request comes from a page with child-directed content. The macro is intended to assist in compliance with the Children’s Online Privacy Protection Act (COPPA).

You can include the macro in any redirects or requests for third-party creatives, in the format coppa=%%TFCD%% or something similar. It can also be used in JavaScript to set GPT passback tag requests for child-directed treatment. The macro expands to 1 for child-directed content, or 0 for other content, based on information about the nature of the page, site or mobile app that sent the request to DFP.

If you are using Google's advertising services and would like to implement child-directed treatment at the site or app level, please see Tag a site or app for child-directed treatment.

URI escaping macro

The URI escaping macro can ensure that values that are inserted into URIs are formatted correctly, with all of the necessary escaping of characters such as spaces. For example, if you're dynamically adding query parameters to a URL, and some of those query parameters contain spaces, you can use the URI escaping macro to ensure that the query values are formatted into valid strings for a URL.

Note that the macro doesn't double-encode characters that have already been encoded. For example, a period (.) is encoded to "%3A". But the macro would leave "%3A" unchanged because it's already encoded.

This macro only works with creative templates, not with custom or third-party creatives.

Use creative template variables as query parameters

The URI encode macro allows creative template variables to be used as query parameters.

For example, a creative template has a list variable "Flavor," which is for an ice cream flavor, with the possible values "chocolate fudge brownie," "rum raisin," etc. The creative template formatter includes the following code:

<a href="[%Flavor%]">Click me!</a>

The "flavor" variable must be properly URI-encoded or the click-through URL will end up containing spaces, which doesn't work: fudge brownie

To prevent this from happening, enter [%URI_ENCODE:variable%] in the creative template formatter. Then specify the values of variable. In our example, you would enter chocolate fudge brownie, rum raisin, etc. The result is that the spaces in the variables get escaped, replaced with %20, resulting in a URL that works:

Use creative template variables as redirect URLs

To ensure proper URL formatting, use the URI escaping macro when passing a template variable as a redirect URL.

For example, you have the following code in your template:

<a href="%%CLICK_URL_UNESC%%[%click-throughURL1%]">Click me!</a>

If the value of click-throughURL1 contains query parameters (?k1=v1&k2=v2), the URL becomes malformed and the redirect won't work as it should. In this example, the content after the ampersand (&) will be attributed to the full URL rather than the redirect URL. To ensure that the URL is correctly formed, in the creative template formatter, specify the click-throughURL1 variable with the URI encode macro: <a href="%%CLICK_URL_UNESC%%[%URI_ENCODE:click-throughUrl1%]">Click me!</a>

Tracking macros

These macros are primarily or most commonly used for tracking and reporting purposes.


Cachebuster (random number) macro

The cachebuster macro ensures that a fresh call is made to the ad server every time the code is executed, so you’re accurately counting impressions. If you don't add the cache-busting macro to the creative code, you’re more likely to see impression counting discrepancies between DoubleClick for Publishers and a third-party ad server. Note that not all third parties require the cachebuster macro.

Most creative vendors will offer a placeholder in their tag to insert cache-buster macros. For example, if your third-party creative code contains something like this:;adServer.php?ab=cd&e=12fg=click&ord=[RANDOM_NUMBER]

You will want to select [RANDOM_NUMBER] and click Insert cachebuster macro so that it replaces the placeholder:

When you use DCM impression tracker for DFP, select the standard ad tag and insert CACHEBUSTER macro to reduce the discrepancies.

Example tag:;sz=1x1;ord=%%CACHEBUSTER%%?

If you are unsure of how to place cachebuster macros properly in your third-party creatives, please contact the vendor that created your creatives.

About cachebuster and VAST responses

The %%CACHEBUSTER%% macro expands to the same random number for all events in a given creative. This is particularly useful for video ads, since the macro expands to the same random number for every video tracking event in a VAST response.

Click macro
Macro Escaping
%%CLICK_URL_ESC_ESC%% Double-escaped
%%CLICK_URL_UNESC%% Unescaped

A click macro allows you to track clicks on third-party creatives.

When to use double-escaped versus unescaped versions of the macro

  • Use %%CLICK_URL_ESC_ESC%% when the click-tracker is the parameter of another URL.
    For example, <a href="">link</a>
  • Use %%CLICK_URL_UNESC%% at the beginning of a link.
    For example, <a href="%%CLICK_URL_UNESC%%">link</a>

As a general rule, use an unescaped click macro when the creative hosted by another server is a standard image file (GIF/JPG). Use the double-escaped click macro %%CLICK_URL_ESC_ESC%% for Flash (SWF) creatives and for certain third parties. You can preview the ad and right-click it to determine its file type. If you see a “Save Image As...” or “Save Picture As...” option appear in the right-click menu, the creative is a standard image. If you see an “About Adobe Flash Player...” option, the creative is a Flash creative. Learn more about escaped vs. unescaped macros

For certified third parties, DFP auto-inserts the right click macro. If you’re unsure which click macro to use or where to place it within the creative code, ask the third party for confirmation. Incorrectly placed click-tracking macros probably won't track clicks.

Do not enter the click macro as the source of an image or iframe. Doing so results in a click being recorded every time the image or iframe renders, which is not legitimate and is filtered out as an invalid click.

To check whether your creative is tracking clicks properly:

  1. Preview the creative.

  2. Click on the preview. If clicks are being recorded, DFP displays a confirmation letting you know. (If the third-party creative redirects to a Flash file, the confirmation won't be displayed, even if your creative is tracking clicks correctly.)
DFP also supports %%CLICK_URL%% and %%CLICK_URL_ESC%%, which do exactly the same thing as %%CLICK_URL_ESC_ESC%%. We recommend using%%CLICK_URL_ESC_ESC%% for clarity, but there's no need to recode older creatives and templates.


Expand macros
Macro What it expands to


Line item ID


Advertiser ID for the line item being served


Order ID for the line item being served


Creative ID


Environment (tag type) indicator: i for iframe, j for JavaScript


ID of the ad unit where the line item is being served


ID of the highest-level ad unit above the ad unit where the line item is being served

The expand macros can be used within custom creative code or in a click-through URL. They're most commonly used to track line items with your backend reporting system. You can match the IDs from DFP with IDs in your own database. The information you store in this way can be useful for troubleshooting.

You can insert the expand macro at the end of the line item's click-through URL (or the click-through URL for the creative, if you're overriding the line item's URL). For example,! inserts the creative ID for the line item.

Expand macros can be separated from one another in the click-through URL with any character that is safe for use in DFP click-through URLs. For example,!;%ecid! might expand to;4265598.

Pass Device ID macro (iOS, Android, Roku, and tvOS)

This macro allows in-app publishers to send a resettable mobile advertising ID (IDFA for iOS, AdID for Android, RIDA for Roku, and tvOS for Apple TV) to your advertising partners for serving interest-based ads. This makes it easier for you to give mobile advertising ID access to your advertisers and agencies. Here’s what happens:

  1. In DFP, the publisher includes the macro in their creative/tracking URLs.
  2. The macro expands to have the IDFA/AdID/RIDA/tvOS of the user, which will then be included in the creative as they make calls to the third party.
    Note that the macro won't expand if the ad request is from a child-directed app (TFCD) or if Limit Ad Tracking (LAT) is turned on. The macro will instead return an empty string ("").
You must do the following before you can use this macro:
  1. Make sure you have the latest version of the Google Mobile Ads SDK.
  2. Google is making a concerted effort to have HTTPS as the new default for all of ad serving, and almost all DFP app ads are now served through HTTPS (there may be minor discrepancies because of other factors that currently prevent HTTPS in a few situations). In addition to this ad request-level security that’s applied automatically, you need to follow some steps to set HTTPS encryption for creatives. This is a requirement for both the creative as well as any third-party pixels that you use for conversion tracking.  If creatives aren’t encrypted for HTTPS, or if you override automatic SSL compatibility, the macro will not expand the IDFA/AdID/RIDA/tvOS value, but the creatives will still serve.

Format (same for iOS, Android, Roku, and tvOS)

32 uppercase hexadecimal digits, displayed in five groups and separated by hyphens, in the form 8-4-4-4-12 for a total of 36 characters (32 alphanumeric characters and four hyphen). For example:


Preview mode macro

You can use the preview mode macro in custom creative code to prevent counting of preview impressions by your backend systems or third-party systems. The macro expands to true if the creative is being viewed as a DFP preview, false if it's a regular impression.

Scheme macro

The scheme macro expands to http: or https:, whichever matches the security scheme of the webpage where a creative is being served. You can put it at the beginning of a URL:

<img src=%%SCHEME%%//>

Site macro

Expands to the domain of the URL parameter in an ad tag (for example, The macro be used to modify your creative based on where the request came from. It doesn't work on AdExchange ads that are marked as anonymous.

Viewed impression macro
Macro Escaping
%%VIEW_URL_ESC%% Escaped
%%VIEW_URL_UNESC%% Unescaped

The viewed impression macro enables DFP to count an impression when a creative is viewed rather than when it's served. It's used primarily for interstitial or similar creatives. It's included automatically in the code for some built-in pop-up and pop-under creative templates, and you can add it to the code of custom templates as well. It expands to a URL that contains the information DFP needs to record the impression.

This macro works in conjunction with signals in the ad tag that let DFP know not to count the impression immediately. For GPT, this applies to tags that include d_imp=1 for delayed impressions.

If the creative is served to tags that don't include one of these signals, DFP expands the macro to "".


Video-specific macros

Many publishers require access to description URL and referrer URL, both of which DoubleClick for Publishers Small Business support.

Your DFP network must be connected to a content source for some of these video-specific macros to expand properly, since DFP obtains the values from ingested content metadata.


Video description URL macro
Macro Escaping





Use %%DESCRIPTION_URL_ESC_ESC%% (double-escaped) when the macro is inside a URL, and use %%DESCRIPTION_URL_UNESC%% (unescaped) in other contexts.

In redirect ad tags, these macros expand to the description URL parameter from the original GPT ad tag's description_url query parameter. The description URL should typically be the same as the referrer URL. You should specify the description_url on the GPT ad tag if there are embeds where the IMA SDK might not accurately detect the page URL, or if you are using video in Ad Exchange or AdSense for Video.

DFP also supports %%DESCRIPTION_URL%%, which does the same thing as %%DESCRIPTION_URL_UNESC%%, so there's no need to recode older creatives and templates.
Video duration macro
Macro Description and requirements


Expands to the duration of the video in milliseconds.

Content ingestion is required for this macro to work. This feature is not available in DFP Small Business.

Video ID macro
Macro Description and requirements


Expands to the ID of the video content currently playing, which is the unique ID in the CMS or the dfpvideo:ContentID value from mRSS.

Content ingestion is required for this macro to work. This feature is not available in DFP Small Business.

Video metadata macro
Macro Description and requirements


Expands to metadata about the video where the creative is being served, based on key-values associated with the video content. The key-values are set up in DFP in the "Video" tab.

Content ingestion is required for this macro to work. This feature is not available in DFP Small Business.

Video referrer URL macro
Macro Escaping







Use %%REFERRER_URL_ESC%% (single-escaped) when the macro is inside a URL, and use %%REFERRER_URL_UNESC%% (unescaped) in other contexts. Learn more about escaped versus unescaped URL macros

This macro expands to the referrer URL (the URL of the page where the video player is located) in redirect ad tags. It does so in the following manner: DFP parses the ad tag's URL query parameter, isolates the referrer URL, and replaces the macro text with that URL. It works only with the Google IMA SDK, with the following qualifications:

  • Script access must be enabled on the page for this macro to work.

  • The macro doesn't work inside an iframe.

DFP also supports %%REFERRER_URL%%, which does the same thing as %%REFERRER_URL_UNESC%%, so there's no need to recode older creatives and templates.
Video title macro
Macro Description and requirements


  • Expands to the title of the video.
  • Single-escaped.
  • Content ingestion is required for this macro to work. This feature is not available in DFP Small Business.
Was this article helpful?
How can we improve it?