Google DFP异步加载Adsense广告
进入DFP后先进入广告资源,新建广告单元,展示位置什么的可以不用设置,那个主要是为了方便管理的,然后新建一个订单,再在订单里新建一个订单项,订单项包含你建好的广告单元,类型建议选为赞助,这里功能非常多,我也没仔细研究
建好订单项后,添加广告,把你的adsense代码添加进去,然后把订单启用,回到广告资源,选择生成代码,把你的广告单元都包含了,选择异步模式
代码添加方式如下,在主题的head里加入广告单元定义
<script type='text/javascript'> var googletag = googletag || {}; googletag.cmd = googletag.cmd || []; </script> <script type='text/javascript'> googletag.cmd.push(function() { googletag.defineSlot('/xxxxxxx/侧边栏广告位', [200, 200], 'div-gpt-ad-xxxxxxxxxxxxxxx-0').addService(googletag.pubads()); googletag.pubads().enableSingleRequest(); googletag.enableServices(); }); </script>
然后在你需要显示广告的地方插入广告单元标签
<!-- 侧边栏广告 --> <div id='div-gpt-ad-xxxxxxxxxx-0' style='width:200px; height:200px;'> <script type='text/javascript'> googletag.cmd.push(function() { googletag.display('div-gpt-ad-xxxxxxxxxxxx-0'); }); </script> </div>
最后再在footer里加入gtp.js的载入
<script type='text/javascript'> (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>
参考自:http://xfeng.me/dfp-asyncronous-load-adsense/