我有一个包含AdSense的Angular网站,它会在首次加载或刷新时加载广告,但如果我浏览到其他路线,则不会加载广告.这是我使用的指令:
.directive('googleAdSense', function () {
return {
restrict: 'A',
replace: true,
templateUrl: "../../templates/googleads.html",
controller: function () {
(adsbygoogle = window.adsbygoogle || []).push({});
}
};
});
Run Code Online (Sandbox Code Playgroud)
这是我将脚本标记放在索引文件头部的位置.所有视图都通过ng-view加载进/出索引文件:
<!-- Google Adsense -->
<script async src="//pagead2.googlesyndication.com/pagead/js/adsbygoogle.js"></script>
Run Code Online (Sandbox Code Playgroud)
这是用法:
<div data-google-ad-sense></div>
Run Code Online (Sandbox Code Playgroud)
我如何解决此问题,以便在我转到另一个视图后加载广告?
更新:经过进一步测试后,它只会加载前3个广告,这与Google每页预防超过3个广告一致....问题是我有多个视图未被视为"网页".我想知道HTML5模式处理历史是否与此有关...
我已经研究了这个过去8h,到目前为止最好的解决方案,或者我应该说解决方法,我能找到答案是Angular.js和Adsense的衍生物.
我正在使用ui-router并在我的app.js中添加了以下代码:
.run(function ($rootScope, $window) {
// delete all the google related variables before you change the url
$rootScope.$on('$locationChangeStart', function () {
Object.keys($window).filter(function(k) { return k.indexOf('google') >= 0 }).forEach(
function(key) {
delete($window[key]);
}
);
});
})
Run Code Online (Sandbox Code Playgroud)
这会在更改网址之前删除所有与谷歌相关的变量,这并不理想,但它确实允许在ng-views上加载广告.我根据adsense条款不知道这是否有效.
在放弃并诉诸于此之前,我已尝试操纵DOM,因此我会加载广告一次,然后在切换视图时分离/附加广告.不幸的是,似乎将广告附加到DOM会触发广告请求,并且该广告会在第3个广告请求结束后结束.我为此创建的指令代码位于https://gist.github.com/dmarcelino/3f83371d120b9600eda3.
从阅读https://productforums.google.com/forum/#!topic/adsense/cl6eonjsbF0我得到的印象Google真的不希望广告在部分视图中显示...
请参阅此处以供参考(https://productforums.google.com/forum/#!msg/adsense/Ya498_sUlBE/hTWj64zROoEJ),但是将脚本标记移动到index.html对我有用。基本上,我使用 ng-include 作为顶部菜单,为顶部广告使用脚本标记,然后是 ng-view,最后是最终广告。
| 归档时间: |
|
| 查看次数: |
4084 次 |
| 最近记录: |