jquery.dfp.js icon indicating copy to clipboard operation
jquery.dfp.js copied to clipboard

Refreshing on AJAX pages

Open derekcoleman opened this issue 9 years ago • 9 comments

I can't seem to get ads refreshing every 30 seconds and working for ajax pages. I have the ads refreshing but it currently doesn't work once I change pages two or more times. This is what my code looks like right now.

$('.adunit:not(".display-block")').dfp({ dfpID: '52137663', enableSingleRequest: false, afterAllAdsLoaded: function() { setInterval(function() { jQuery.dfp(); }, '30000'); } });

derekcoleman avatar Feb 19 '16 21:02 derekcoleman

I have also tried with this code:

`

function ads () {
    $('.adunit:not(".display-block")').dfp({
        dfpID: '52137663/top-ad-gh-music-web',
        enableSingleRequest: false,
        afterAllAdsLoaded: function() {
        setInterval(function() {
        jQuery.dfp();
    }, '30000');
}
    });
    }

$(document)
        .on('page:load', function () {
            ads();
        })
        .ready(function () {
            ads();
        });`

derekcoleman avatar Feb 20 '16 04:02 derekcoleman

I am running a test on this page http://derekcoleman.me/pzzb6/

derekcoleman avatar Feb 20 '16 04:02 derekcoleman

I have added this code to refresh the ad whenever the URL changes and it helps it work for the first 'page' change but then it doesn't refresh the ad once it's on the new URL.

function hrefHandler(){
    this.oldHref = window.location.href;
    this.Check;
    var that = this;
    var detect = function(){
        if(that.oldHref!=window.location.href){
            ads();
        }
    };
    this.Check = setInterval(function(){ detect() }, 100);
}
var hrefDetection = new hrefHandler();

derekcoleman avatar Feb 20 '16 18:02 derekcoleman

Your example page seems to be stuck in some kind of loop... its calling it repeatedly over and over 10 times per second at least...

coop182 avatar Feb 22 '16 08:02 coop182

This jquery mobile test I put together a while back seems to have the same kind of idea... and the same issue with ads not showing up after a while... from what I can see though everything is loading correctly... it just appears to be google limiting the amount of times a creative will appear on a single page within a certain amount of time... if you have more creatives it will be fine I think.

http://coop182.github.io/jquery.dfp.js/dfptests/demo1.html

coop182 avatar Feb 22 '16 08:02 coop182

I removed the last bit of code that was checking if the URL has changed so now I have the same exact code as what is on the demo page but it's not working correctly. It is still having the same problem of not working once I navigated to a new 'page'.  Derek Riccardo Coleman 

On Monday, February 22, 2016 3:59 AM, Matt Cooper <[email protected]> wrote:

This jquery mobile test I put together a while back seems to have the same kind of idea... and the same issue with ads not showing up after a while... from what I can see though everything is loading correctly... it just appears to be google limiting the amount of times a creative will appear on a single page within a certain amount of time... if you have more creatives it will be fine I think.http://coop182.github.io/jquery.dfp.js/dfptests/demo1.html— Reply to this email directly or view it on GitHub.

derekcoleman avatar Feb 22 '16 15:02 derekcoleman

@derekcoleman i had the similar problem and solved it by implementing it like this first example: https://support.google.com/dfp_premium/answer/4578089

take notice of the googletag.pubads().refresh([slot]);

samburgers avatar Apr 13 '16 05:04 samburgers

@samburgers could you please explain for someone who's not that code savvy how you did it? :)

boooooooorbs avatar Jul 16 '16 16:07 boooooooorbs

@boooooooorbs You will need to use some of Google's native methods in jquery.dfp.js beforeEachAdLoaded callback like such:

beforeEachAdLoaded: function (adUnit) {
	var slots = googletag.pubads().getSlots(); // returns array of Slot objects
	var slot = slots[slots.length - 1];
	var slotName = adUnit.attr('id');

	googletag.display(slotName);
	googletag.pubads().refresh([slot]);
}

tinyearth avatar Jan 24 '17 19:01 tinyearth