如何在SPA中使用Google DFA(Google Publisher Tag)?



我在Angular中使用Google DFA (gpt.js(。

广告第一次显示正常,但第二次显示空错误。

我有指令,它有以下方法,在OnInit方法中调用:

addGoogleAd() {
const googletag: any = window['googletag'] || {};
if (window['googletag'] && googletag.apiReady) {
googletag.cmd = googletag.cmd || [];
googletag.cmd.push(() => {
googletag.pubads().setTargeting('site', 'weatherinnovations');
googletag.pubads().enableSingleRequest();
googletag.defineSlot('/xx/weather_innovations', [[160, 600]], 'div-gpt-ad-xx-1')
.setTargeting('position', ['s1']).addService(googletag.pubads());
googletag.defineSlot('/xx/weather_innovations', [[300, 250]], 'div-gpt-ad-xx-2')
.setTargeting('position', ['s2']).addService(googletag.pubads());
googletag.defineSlot('/xx/weather_innovations', [[300, 250]], 'div-gpt-ad-xx-3')
.setTargeting('position', ['s3']).addService(googletag.pubads());
googletag.defineSlot('/76643716/weather_innovations', [[728, 90]], 'div-gpt-ad-xx-4')
.setTargeting('position', ['s4']).addService(googletag.pubads());
googletag.defineSlot('/xx/weather_innovations', [[728, 90]], 'div-gpt-ad-xx-5')
.setTargeting('position', ['s5']).addService(googletag.pubads());
googletag.pubads().collapseEmptyDivs(true, true);
googletag.enableServices();
});
googletag.display(this.id);
}
}

仪表板加载并显示广告,但是当我从另一个组件返回时,它给出了以下错误:

排队的 GPT 命令类型错误中的 pubads_impl_265.js:1 异常:无法 读取空的属性"设置目标">

我安慰了googletag,不是空的,内容相同。

该错误不是描述性的,无法快速调试它。现在,我正在销毁以前创建的插槽,然后再创建更多插槽,这解决了问题。使用googletag.cmd.push前面的行

googletag.destroySlots();

最新更新