Search
Clear search
Close search
Google apps
Main menu

DFP macros

Macros are special strings in a creative's code snippet that DFP later replaces with another value based on a creative's settings. Macros 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, or Flash files that are hosted by DFP.

For each macro, we've provided suggestions on how you might use it, but macros can be put to whatever uses are most valuable for you.

Important notes about macros
  • Macros are case-sensitive. Use %%CACHEBUSTER%%, not %%Cachebuster%%.
  • Unescaped and escaped macro versions expand differently. Learn more
  • DFP inserts 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. If you are unsure where to place a macro, contact your creative vendor.

Rendering macros

These macros are commonly used to render creatives, ensuring they look right and work correctly.

EXPAND ALL COLLAPSE ALL

Ad unit macro
Macro
%%ADUNIT%%

The ad unit macro inserts the ad unit path from your Google Publisher Tag into the creative code when it is served.

Here's how you would add the ADUNIT macro to a third-party creative code snippet:

<script type="text/javascript">
document.write('<sc'+'ript type="text/javascript" src="http://www.3rdparty.com/?dfpAdUnit=%%ADUNIT%%"></scr'+'ipt>');
</script>

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="http://www.3rdparty.com/?dfpAdUnit=/1234/site/section"<>/scr'+'ipt>');
</script>

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
Macro
%%FILE:file_display_name%%

The file server macro %%FILE:file_display_name%% expands to the full URL of a creative asset that has been uploaded for a creative. Use this macro to call uploaded assets, such as an image, with custom creative code.

To use this 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
Macro
%%HEIGHT%%
%%WIDTH%%

These macros insert the creative height and width, as defined in DFP, 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
Macro
%%PATTERN:key%%
%%PATTERN:TARGETINGMAP%%
%%PATTERN:url%%

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.

%%PATTERN:key%%

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")
    .addService(googletag.pubads())
    .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.

%%PATTERN:TARGETINGMAP%%

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:

.updateTargetingFromMap(%%PATTERN:TARGETINGMAP%%).display();

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='http://www.googletagservices.com/tag/js/gpt.js'>
googletag.pubads().definePassback('/network_B/Homepage', [468, 60])
 .setTargeting("myAdUnit", "%%ADUNIT%%")
 .updateTargetingFromMap(%%PATTERN:TARGETINGMAP%%)
 .display();
</script>

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='//www.googletagservices.com/tag/js/gpt.js'>
googletag.pubads()
 .definePassback('%%PATTERN:myAdUnit%%', [468, 60])
 .updateTargetingFromMap(%%PATTERN:TARGETINGMAP%%)
 .setTargeting("kwd", "noloop") // to avoid infinite loops
 .display();
</script>
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="http://www.3rdparty.com/generatePassback.php?dfpAdUnit=%%ADUNIT%%&targeting='+encodeURIComponent(JSON.stringify(%%PATTERN:TARGETINGMAP%%))+'"></scr'+'ipt>');
</script>

When served by DFP, this expands to:

<script type="text/javascript">
document.write('<sc'+'ript type="text/javascript" src="http://www.3rdparty.com/generatePassback.php?dfpAdUnit=/network_A/first_level_ad_unit/second_level_ad_unit&targeting=%7B%22kw%22%3A%5B%22testpercentp%22%5D%7D"></scr'+'ipt>');
</script>

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

<script src='//www.googletagservices.com/tag/js/gpt.js'>
googletag.pubads()
 .definePassback('/network_A/first_level_ad_unit/second_level_ad_unit', [728, 90])
 .setTargeting("kw", "noloop") // to avoid infinite loop (see https://support.google.com/dfp_premium/answer/2811375?hl=en)
 .updateTargetingFromMap({"kw":["testpercentp"]}).display();
</script>

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="https://www.googletagservices.com/tag/js/gpt.js"></scr'+'ipt>');
document.write('<scr'+'ipt>');
  callback = function() {
    googletag.pubads().definePassback('$dfpAdUnit', [728, 90])
    .setTargeting("kwd", "noloop")
    .updateTargetingFromMap(myTargetingObject)
    .display();
  };

  window.googletag = window.googletag || {};
  googletag.cmd = googletag.cmd || [];
  googletag.cmd.push(callback);

document.write('</sc'+'ript>');

%%PATTERN:url%%

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
Macro
%%TARGET_IN_NEW_WINDOW%%

The target in new window macro indicates whether the target window setting specified for the ad unit where a creative is served is "_blank" (new window) or otherwise.

  • 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.

Use the %%TARGET_WINDOW%% macro if you want to insert the actual target window setting value.

Target window macro
Macro
%%TARGET_WINDOW%%

The target window macro expands to the target window specified for the ad unit where a creative is 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 DFP_News.com ad unit has the target window set to _top and the DFP_Fashion.com 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 DFP_News.com and _new for users of of DFP_Fashion.com.

Here's an example implementation:

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

Use the %%TARGET_IN_NEW_WINDOW%% macro if you want to determine whether the target window setting value is "_blank" or another value.

Tag for child-directed content macro
Macro
%%TFCD%%

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 encoding macro for creative template variables
Macro
[%URI_ENCODE:variable%]

The URI encoding macro correctly translates spaces and other special characters in creative template variable values for use in URIs, also known as URLs.

This macro only works with creative templates, not with custom or third-party creatives, and doesn't double-encode characters that have already been encoded.
Let's say your creative template has a "list" variable called "Color" with two values: "dark blue" and "light green". By default, DFP adds [%Color%] to your creative template snippet:

<a href="https://paint.com/wall.html?color=[%Color%]">Paint!</a>

When the creative is created, DFP replaces [%Color%] with the literal text string of the selected list item. In this example, the literal text strings contain space characters and will break the URL since space characters need to be encoded. Use [%URI_ENCODE:Color%] to avoid this problem.

<a href="https://paint.com/wall.html?color=[%URI_ENCODE:Color%]">Paint!</a>
 

[%Color%] dark blue
[%URI_ENCODE:Color%] dark%20blue

This macro is especially helpful when using a creative template variable to define a click-through URL or redirect URL. Without correct encoding, the full URL in your creative might not function.

Let's say your creative template has a "URL" variable called "LandingPage". By default, DFP adds [%LandingPage%] to your creative template snippet:

<a href="%%CLICK_URL_UNESC%%[%LandingPage%]">Click here!</a>

If the [%LandingPage%] URL contains query parameters, such as ?page=home&sport=baseball, 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. Use [%URI_ENCODE:LandingPage%] to avoid this problem.

<a href="%%CLICK_URL_UNESC%%[%URI_ENCODE:LandingPage%]">Click here!</a>
 

[%LandingPage%] https://www.mysportswebsite.com?page=home&sport=baseball
[%URI_ENCODE:LandingPage%] https%3A%2F%2Fwww.mysportswebsite.com%3F
page%3Dhome%26sport%3Dbaseball

Tracking macros

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

EXPAND ALL COLLAPSE ALL

Cachebuster (random number) macro
Macro
%%CACHEBUSTER%%

The cachebuster macro ensures that a fresh call is made to the ad server every time the code is executed, so you can count impressions accurately. If you don't add the cache-busting macro to 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:

http://abc.3rd-party-serving.com/Targeting/;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:

http://abc.3rd-party-serving.com/Targeting/adServer.php?ab=cd&e=12fg=click&ord=%%CACHEBUSTER%%

Select the standard ad tag and insert CACHEBUSTER macro to reduce the discrepancies when you use DCM impression tracker for DFP.

http://ad.doubleclick.net/ad/Nxxxx.site/Byyyy;sz=1x1;ord=%%CACHEBUSTER%%?

The cachebuster macros in 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_UNESC%% Unescaped
%%CLICK_URL_ESC%% Escaped
%%CLICK_URL_ESC_ESC%% Double-escaped

The click macro expands to a special URL so you can track clicks in DFP for third-party creatives. This macro is generally paired with the click-through URL of a creative in the anchor tag (<a>). 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. 

Use %%CLICK_URL_UNESC%% at the beginning of a link. Use %%CLICK_URL_ESC%% when the click-tracker is the parameter of another URL. Learn more about using escaped and unescaped macros

<a href="%%CLICK_URL_UNESC%%http://www.google.com">link</a>
<a href="http://www.google.com?rd_url=%%CLICK_URL_ESC%%">link</a>

Verify your click macro implementation

An incorrectly-placed click macro won't track clicks. Follow these steps to confirm that DFP will record clicks as you intend.

  1. Preview the creative.
  2. Click on the preview. DFP displays a confirmation that clicks will be recorded. If you do not see this confirmation, your click macro is not implemented correctly.
    If your third-party creative redirects to a Flash file, no confirmation is displayed, even if your creative will track clicks correctly.
Expand macros
Macro What it expands to
%eaid! Line item ID
%eadv! Advertiser ID for the line item being served
%ebuy! Order ID for the line item being served
%ecid! Creative ID
%eenv! Environment (tag type) indicator: i for iframe, j for JavaScript
%epid! ID of the ad unit where the line item is being served
%esid! ID of the highest-level ad unit above the ad unit where the line item is being served

Use the expand macros within custom creative code or in a click-through URL to display ID values from DFP when serving a creatives to a page. This is often used to match IDs from DFP with IDs in a publisher's own database for troubleshooting or other reporting.

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, http://www.site.com/?%eaid!;%ecid! expands to http://www.site.com/?1234567;4265598.

Device ID macros (iOS, Android, Roku, and tvOS)
Macro
%%ADVERTISING_IDENTIFIER_PLAIN%% (the device identifier)
%%ADVERTISING_IDENTIFIER_TYPE%% (the device type)
%%ADVERTISING_IDENTIFIER_IS_LAT%% (the Limit Ad Tracking setting)
 

The %%ADVERTISING_IDENTIFIER_PLAIN%% 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 advertising partners for serving interest-based ads. This makes it easier for you to give mobile advertising ID access to your advertisers and agencies.

In-app publishers can also use the %%ADVERTISING_IDENTIFIER_TYPE%% macro to send the type of device, and the %%ADVERTISING_IDENTIFIER_IS_LAT%% macro to send the Limit Ad Tracking (LAT) setting, which enables users to opt-out of targeting based on user behavior.

Device identifiers are provided by the device operating system and can be reset by the user. Learn more

Prerequisites

  • Your mobile app must be using the latest version of the Google Mobile Ads SDK.
  • 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.

How it works

  1. In DFP, the publisher includes the macros in their creative and/or tracking URLs.
  2. The macros expand to have the IDFA/AdID/RIDA/tvOS of the user, device type and/or LAT setting, which are then included in the creative as they make calls to the third party.
    The identifier macro won't expand if the ad request is from a child-directed app (TFCD) or if Limit Ad Tracking (LAT) is enabled. In these cases, this macro returns an empty string ("").

Format

For iOS, Android, Roku, and tvOS, this macro sends a 32 uppercase hexadecimal digits value, 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).

Example: 201F2EB1-8041-9E39-CD07-FCBBA5A29B41

See the possible values for supported device types and Limit Ad Tracking (LAT).

Preview mode macro
Macro
%%PREVIEW_MODE%%

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
Macro
%%SCHEME%%

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%%//www.website.com/img/logo.gif>

Site macro
Macro
%%SITE%%

Expands to the domain of the URL parameter in an ad tag (for example, google.com). The macro can 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 displayed, rather than when it's served. This macro is used primarily for interstitial or similar creatives.

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 macros

These macros are commonly used in video creatives to include details about the context or content.

EXPAND ALL COLLAPSE ALL

Video description URL macro
Macro Escaping
%%DESCRIPTION_URL_UNESC%% Unescaped
%%DESCRIPTION_URL_ESC%% Escaped
%%DESCRIPTION_URL_ESC_ESC%% Double-escaped

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

In redirect ad tags, these macros expand to the description URL parameter from the original Google Publisher Tag's description_url query parameter. The description URL should typically be the same as the referrer URL. You should specify the description_url in the Google Publisher 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 referrer URL macro
Macro Escaping
%%REFERRER_URL_UNESC%% Unescaped
%%REFERRER_URL_ESC%% Single-escaped
%%REFERRER_URL_ESC_ESC%% Double-escaped

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 content macros

These macros are used for ads that target video content sourcesThis feature is not available in DFP Small Business.

CMS ID macro
Macro
%%CMS_ID%%

Expands to the DFP Content Source ID ("CMS ID") currently playing. This value does not appear in your MRSS feed, but is visible in the DFP UI.

This macro only works in an ad tag configured in DFP within a Dynamic Ad Insertion video on demand content source.

Video duration macro
Macro
%%VIDEO_DURATION%%

Expands to the duration of the video in milliseconds.

Video ID macro
Macro
%%VIDEO_ID%%

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. 

Video metadata macro
Macro
%%VIDEO_METADATA:key%%

 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.

Video title macro
Macro Escaping
%%VIDEO_TITLE%% Single-escaped

The video title macro expands to display the title of a video as defined in DFP.

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

Nathan is a DFP expert and author of this page. Help him improve the content by leaving feedback below.

Was this article helpful?
How can we improve it?