使用Ajax调用加载附加到主体中的Dynamic rotator种类的Dynamic HTML样式. AD的DIV ID将在折叠模式下动态添加到rotator的主体中.
使用尺寸为180×150的DFP Iframe广告
在初始页面上加载时,网页源中没有DIV ID.
我有一种通过Ajax调用将DIV ID动态添加到网页正文中的方案.
代码流:
<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 Server.禁用初始加载,因为在初始页面加载期间定义插槽将检查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调试控制台验证了以上页面.
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行解决方案.
我遇到的唯一障碍是缺少“ enableSingleRequest”选项….如果您在页面上加载广告的次数超过一次,则需要将其设置为false.