Modify the GPT setTargeting API Call 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 Manager 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 user interface. DIL sets the aam_uuid cookie and the name cannot be changed.

if(typeof AamGpt.getCookie("Sample") != "undefined"){
if(typeof AamGpt.getCookie("aam_uuid") != "undefined" ){
   googletag.pubads().setTargeting("aamId", AamGpt.getCookie("aam_uuid"));
Depending on how you want to integrate with Google Ad Manager, you only need some of the lines in the code sample above:
  • Client-side integration: use lines 1-3 only.
  • Server-side integration: none of the lines are needed.
  • Ingest Google Ad Manager log files for reporting in Audience Manager: use lines 4-6 only. This code inserts the value of the aam_uuid cookie into the logs so they can be ingested for reporting.

AamGpt Functions and Data Types

Defines the key variables used in the if statement.

Returns the key in the key-value segment pair. For example, if your key-value pair consisted of color=blue , this returns color .
Array of strings
Returns values in an array, e.g., ["value1","value2"] .
Returns the Audience Manager user ID, e.g., 12345 .