【发布时间】:2014-01-30 19:31:12
【问题描述】:
加载使用 Ajax 调用附加到正文中的动态旋转器类型的动态 HTML 样式。 ADs DIV ID 将在折叠模式下动态附加到 Rotator 的主体中。
使用尺寸为 180x150 的 DFP iframe 广告
在网页源中没有 DIV ID 的初始页面加载时。
我有一个通过 Ajax 调用将 DIV Id 动态附加到网页正文中的场景。
代码流:
在网页的 HEAD 标记中添加了 GPT 库包含。
<head>
<script type='text/javascript'>
var googletag = googletag || {};
googletag.cmd = googletag.cmd || [];
(function() {
var gads = document.createElement('script');
gads.async = true;
gads.type = 'text/javascript';
var useSSL = 'https:' == document.location.protocol;
gads.src = (useSSL ? 'https:' : 'http:') +
'//www.googletagservices.com/tag/js/gpt.js';
var node = document.getElementsByTagName('script')[0];
node.parentNode.insertBefore(gads, node);
})();
</script>
</head>
以及在 HEAD 标记中的 cmd.push() 方法中定义 SLOT,这将调用 DFP 服务器。禁用初始加载,因为定义槽将在初始页面加载期间检查 DIV ID,这会引发错误。
<head>
<script type='text/javascript'>
var slot1 = '';
googletag.cmd.push(function() {
slot1 = googletag.defineSlot('/12638013/Connect_180x150', [180, 150], 'div-gpt-ad- 1389885200465-0').addService(googletag.pubads());
googletag.pubads().enableSingleRequest();
googletag.pubads().disableInitialLoad();
googletag.enableServices();
});
</script>
</head>
一旦页面通过 HEAD 标记中的上述脚本加载。通过 Ajax 调用将 DIV ID 附加到网页正文中。
jQuery('body').append('<div id="div-gpt-ad-1389885200465-0" style="width:180px; height:150px;"><script type="text/javascript">googletag.cmd.push(function() { googletag.display("div-gpt-ad-1389885200465-0"); });</script></div>');
在 DIV ID 动态呈现到网页后。刷新 HEAD 标记中定义的广告位。
googletag.pubads().refresh([slot1]);
最终结果:
使用 Google 调试控制台验证上述页面。
页面请求没有任何错误,表明页面标记正确。
852 毫秒渲染页面 491 毫秒获取广告 0 毫秒呈现广告
Status Time Description
Information Start Created slot: /12638013/Connect_180x150_0
Information 1 ms Google service JS loaded
Information 1 ms Created service: publisher_ads
Information 1 ms Associated service "publisher_ads" with slot "/12638013/Connect_180x150".
Information 2 ms Using single request mode to fetch ads.
Information 2 ms Fetching GPT PubAds implementation
Information 2 ms Invoked queued function. Total: 1. Errors: 0.
Information 57 ms GPT implementation fetched.
Information 759 ms Calling fillslot.
Information 773 ms Invoked queued function. Total: 2. Errors: 0.
Information 852 ms Page load complete
Information 7090 ms Refreshing ads.
Information 7135 ms Fetching ad for slot: /12638013/Connect_180x150
Information 7626 ms Receiving ad for slot: /12638013/Connect_180x150
Information 7626 ms Rendering ad for slot: /12638013/Connect_180x150
Information 7626 ms Completed rendering ad for slot: /12638013/Connect_180x150
但在动态注入 DIV 的旋转器内未呈现广告。
我错过了什么?建议对我有用。
【问题讨论】:
标签: javascript jquery ajax ads google-dfp