AngularJS and Adsense ads not loaded on route change (up to 3 ads for the entire app)

前端 未结 4 570
予麋鹿
予麋鹿 2020-12-17 03:19

I have an Angular site with AdSense and it will load Ads on first load or refresh, but if I browse to another route, it doesn\'t load the Ads. Here is the directive I am usi

4条回答
  •  渐次进展
    2020-12-17 03:26

    I've researched this for the last 8h and so far the best solution, or should I say workaround, I could find has been a derivative of answer Angular.js & Adsense.

    I'm using ui-router and in my app.js I've added the following code:

      .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]);
            }
          );
        });
      })
    

    This deletes all google related variables before changing url, not ideal, but it does allow to load ads on ng-views. And I have no idea if this is valid per the adsense terms.

    Other failed approach - DOM

    Before giving up and resorting to this I've tried manipulating the DOM so I would load the ad once and then detach / append the ad as I switched views. Unfortunately it appears that appending the ad to the DOM triggers an ad request and the party ends after the 3rd one. The directive code I've created for this is in https://gist.github.com/dmarcelino/3f83371d120b9600eda3.

    From reading https://productforums.google.com/forum/#!topic/adsense/cl6eonjsbF0 I get the impression Google really doesn't want ads to be shown in partial views...

提交回复
热议问题