ngDfp icon indicating copy to clipboard operation
ngDfp copied to clipboard

Ads do not refresh on route change

Open Soviut opened this issue 11 years ago • 15 comments

I have a big box ad on each sub page of my site. The ad displays properly on initial load but on route changes the ad won't reload and the area remains blank. My workaround is to call refreshAds() on $routeChangeSuccess events.

    $rootScope.$on '$routeChangeSuccess', (e, newRoute, oldRoute) ->
        DoubleClick.refreshAds 'div-gpt-ad-1234567890'

Soviut avatar Sep 30 '14 22:09 Soviut

Are you triggering the reload on each page change?— Ian Murray S.

On Tue, Sep 30, 2014 at 7:38 PM, Soviut [email protected] wrote:

I have a big box ad on each sub page of my site. The ad displays properly on initial load but on route changes the ad won't reload and the area remains blank.

Reply to this email directly or view it on GitHub: https://github.com/ianmurrays/ngDfp/issues/3

ianmurrays avatar Sep 30 '14 23:09 ianmurrays

Yes, see my code snippet above. On every route change I'm calling the .refreshAds() after every $routeChangeSuccess event. It doesn't work very well though if the previous and next pages have ads. This is because I've got page transitions between ng-views and this means that for a brief time, there are two separate pages visible at once during the transition, each with an ad with the same id.

Soviut avatar Oct 01 '14 00:10 Soviut

Oh, yeah the transition is definitely to blame. The only way to fix it I think, would be to find a way to catch the animation end and then refresh, making sure there's only one ad of the same id.

ianmurrays avatar Oct 01 '14 12:10 ianmurrays

Yeah, I'm having to add a 2000 millisecond timeout, far longer than the transition time, and even then it's temperamental.

Soviut avatar Oct 01 '14 16:10 Soviut

So my currently functional solution is to clear the slot on route change start:

    $rootScope.$on '$routeChangeStart', ->
        slotPromise = DoubleClick.getSlot 'div-gpt-ad-1234567890'
        slotPromise.then (slot) ->
            googletag.pubads().clear [slot]

Then refresh it on route change it on success with a timeout to ensure transitions have had time to complete:

    $rootScope.$on '$routeChangeSuccess', (e, newRoute, oldRoute) ->
        $timeout ->
            DoubleClick.refreshAds 'div-gpt-ad-1234567890'
        , 2000

Soviut avatar Oct 01 '14 17:10 Soviut

I guess that's the best solution there is for this problem

ianmurrays avatar Oct 01 '14 18:10 ianmurrays

@Soviut I guess that's coffee script Could you maybe explain how would you do it in pure js using ui-router instead?

guyyosan avatar Nov 05 '15 11:11 guyyosan

@guyyosan that would translate to

$rootScope.$on('$stateChangeStart', function () {
  DoubleClick
    .getSlot('div-gpt-ad-1234567890')
    .then(function (slot) {
      googletag.pubads().clear([slot]);
    });
});

and

$rootScope.$on('$stateChangeSuccess', function () {
  $timeout(function () {
    DoubleClick.refreshAds('div-gpt-ad-1234567890');
  }, 2000);
});

ianmurrays avatar Nov 05 '15 13:11 ianmurrays

@ianmurrays does it have to be in the $rootScope? and if it must, where do I put it... controller or run? what if i have multiple slots?

guyyosan avatar Nov 05 '15 13:11 guyyosan

Yes I believe it has to observe the $rootScope, and the .run() call makes more sense to me at least.

ianmurrays avatar Nov 05 '15 13:11 ianmurrays

@ianmurrays It's a dumb question, guess im dumb... :( what's the slot? .defineSlot('/123456/blogbox11233', [[300, 250]], 'homePage1')

i guess it's homePage1

guyyosan avatar Nov 05 '15 13:11 guyyosan

in your example homePage1 would be the slot

ianmurrays avatar Nov 05 '15 13:11 ianmurrays

@ianmurrays First thanks a lot for this module it saved my life.

second I erased this from the module:

    var googletag     = googletag || {};
    googletag.cmd = googletag.cmd || [];

and inserted it in my app.js like this:

(function() {
    'use strict';

var googletag     = googletag || {};
googletag.cmd = googletag.cmd || [];
var app = angular ...

btw, If I want multiple ads would this be alright:

rootScope.$on('$stateChangeStart', function(){
                $rootScope.state_loading = true;
                DoubleClick
                    .getSlot('homePage1')
                    .then(function (slot) {
                        googletag.pubads().clear([slot]);
                    })
                    .getSlot('homePage2')
                    .then(function (slot) {
                        googletag.pubads().clear([slot]);
                    })
                    .getSlot('homePage3')
                    .then(function (slot) {
                        googletag.pubads().clear([slot]);
                    });
            });

            $rootScope.$on('$stateChangeSuccess', function(){
                $rootScope.state_loading = false;

                $timeout(function() {
                    DoubleClick.refreshAds('homePage1', 'homePage2', 'homePage3');
                }, 2000);
            });

guyyosan avatar Nov 05 '15 13:11 guyyosan

@ianmurrays this is what i get:

n.getSlot(...).then(...).getSlot is not a function @http://localhost/dist/bundle-ctrl.js:1:4187 lf/this.$get</n.prototype.$broadcast@https://ajax.googleapis.com/ajax/libs/angularjs/1.4.7/angular.min.js:136:43 b/E.transitionTo@http://localhost/dist/bundle-mdl.js:1:31202 b/E.go@http://localhost/dist/bundle-mdl.js:1:29817 P/<.link/</i<@http://localhost/dist/bundle-mdl.js:2:6255 f/m<@https://ajax.googleapis.com/ajax/libs/angularjs/1.4.7/angular.min.js:146:220 e@https://ajax.googleapis.com/ajax/libs/angularjs/1.4.7/angular.min.js:43:93 Kf/l.defer/c<@https://ajax.googleapis.com/ajax/libs/angularjs/1.4.7/angular.min.js:45:491 angular.min.js:107:207

Error: e.pubads is not a function @http://localhost/dist/bundle-ctrl.js:1:4227 f/<@https://ajax.googleapis.com/ajax/libs/angularjs/1.4.7/angular.min.js:119:113 lf/this.$get</n.prototype.$eval@https://ajax.googleapis.com/ajax/libs/angularjs/1.4.7/angular.min.js:133:217 lf/this.$get</n.prototype.$digest@https://ajax.googleapis.com/ajax/libs/angularjs/1.4.7/angular.min.js:130:225 lf/this.$get</n.prototype.$apply@https://ajax.googleapis.com/ajax/libs/angularjs/1.4.7/angular.min.js:133:510 f/m<@https://ajax.googleapis.com/ajax/libs/angularjs/1.4.7/angular.min.js:146:298 e@https://ajax.googleapis.com/ajax/libs/angularjs/1.4.7/angular.min.js:43:93 Kf/l.defer/c<@https://ajax.googleapis.com/ajax/libs/angularjs/1.4.7/angular.min.js:45:491

guyyosan avatar Nov 07 '15 10:11 guyyosan

getSlot() isn't chainable. You might have luck dividing those calls into multiple.

ianmurrays avatar Nov 07 '15 14:11 ianmurrays