Modify the GPT setTargeting API Call

Add an if statement to check for Audience Manager cookies before calling the Google Publisher Tag .setTargeting method.

Check for Audience Manager Cookies With an IF Statement

The .setTargeting method gets data from the Audience Management destination cookie and the unique user ID cookie (aam_uuid). However, if .setTargeting gets invoked before DIL writes these cookies, or the cookies are empty, you may see errors when the page loads. To help avoid this, wrap the .setTargeting method in an if statement that checks for these cookies. If they're not set, this statement prevents .setTargeting from calling the AamGpt function.

IF Statement Code Sample

In this example, the Audience Manager destination cookie name is Sample. You set this name when you create the destination cookie in the Audience Manager UI. DIL sets the aam_uuid cookie and the name cannot be changed.
if(typeof AamGpt.getCookie("Sample") != "undefined"){
  googletag.pubads().setTargeting(AamGpt.getKey("Sample"),AamGpt.getValues("Sample"));
};
if(typeof AamGpt.getCookie("aam_uuid") != "undefined" ){
   googletag.pubads().setTargeting("aamId", AamGpt.getCookie("aam_uuid"));
};

AamGpt Functions and Data Types

Defines the key variables used in the if statement.
Function Type Description

AamGpt.getKey

String

Returns the key in the key-value segment pair. For example, if your key-value pair consisted of color=blue, this returns color.

AamGpt.getValues

Array of strings

Returns values in an array, e.g., ["value1","value2"].

AamGpt.getCookie

Int

Returns the Audience Manager user ID, e.g., 12345.