修改GPT setTargeting API调用

在调用Google Publisher Tag .setTargeting方法之前,添加if语句以检查Audience ManagerCookie。

使用IF语句检查Audience ManagerCookie

.setTargeting方法从Audience Manager目标Cookie和唯一用户ID Cookie(aam_uuid)中获取数据。 但是,如果在DIL写入这些Cookie之前调用.setTargeting,或者Cookie为空,则在页面加载时可能会看到错误。 为避免出现这种情况,请将.setTargeting方法包装在检查这些Cookie的if语句中。 如果未设置这些值,此语句将阻止.setTargeting调用AamGpt函数。

IF 语句代码示例

在此示例中,Audience Manager目标Cookie名称为Sample。 在Audience Manager用户界面中创建目标Cookie时,需设置此名称。 DIL 设置 aam_uuid Cookie,且名称无法更改。

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"));
};
重要

根据您希望与Google Ad Manager集成的方式,您只需要上面代码示例中的一些行:

  • 客户端集成:仅使用第1-3行。
  • 服务器端集成:不需要任何行。
  • 摄取Google Ad Manager日志文件以在Audience Manager中报告:仅使用第4-6行。 此代码会将aam_uuid Cookie的值插入日志,以便可以摄取这些值以进行报告。

AamGpt 函数和数据类型

定义if语句中使用的键变量。

函数 类型 描述

AamGpt.getKey

字符串

返回键值区段对中的键。 例如,如果键值对包含 color=blue ,则返回 color

AamGpt.getValues

字符串数组

返回数组中的值,例如 ["value1","value2"]

AamGpt.getCookie

整数

返回Audience Manager用户ID,例如 12345

在此页面上