使用AJAX调用加载动态旋转器的动态HTML样式。ADS DIV ID将在折叠模式下动态附加到旋转器的主体中。
使用DFP iframe广告,尺寸为180x150
在初始页面上加载没有网页源中的DIV ID。
我有一个通过Ajax调用动态地将DIV ID的情景添加到网页正文的情况。
代码流:
在网页的头部标签中添加了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()方法中的插槽,该插槽将呼叫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 Tag中使用上述脚本进行加载。通过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动态渲染到网页之后。刷新头号中定义的广告插槽。
googletag.pubads().refresh([slot1]);
最终结果:
使用Google调试控制台验证了上面的页面。
页面请求没有任何错误,指示该页面正确标记。
852 ms到渲染页面491毫秒来获取广告 0 ms渲染广告
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动态注入。
我错过了什么?建议对我有用。
我自己有同样的问题。...然后我找到了:https://plugins.jquery.com/dfp/
您基本上可以删除所有Google JS,并使用提供的2-3行解决方案。我遇到的唯一障碍是缺少" en ensingLereQuest"选项。...如果您在页面上多次加载广告,则需要将其设置为False。