coop182 / jquery.dfp.js

A jQuery implementation for Google DoubleClick for Publishers (DFP)
MIT License
278 stars 93 forks source link

Duplicated afterEachAdLoaded when $.dfp() has been called more than once #135

Open tatai opened 8 years ago

tatai commented 8 years ago

Hi,

First of all, thanks for creating this plugin.

I have a page with an infinite scroll and I have followed the steps given in the instructions and also explained in some issues like #112, but when I create a callback to afterEachAdLoaded, each time I invoke dfp() to define the new ad, afterEachAdLoaded receives a repeated callback.

I have followed the code and I think the issue is related to the pubadsService.addEventListener('slotRenderEnded') made on line 137. This listener is added with each call to dfp().

I cannot show public code right now, but it can be reproduced with:

var options = {
    dfpID: 'xxxxxxxxx',
    enableSingleRequest: false,
    afterEachAdLoaded: function(ad) {
        console.log('afterEachAdLoaded', ad);
    }
};

$(document).ready(function() {
    $.dfp(options);
});

// This event is triggered when a new page is loaded using ajax
$(document).on('new-page-loaded', function($page) {
    console.log('New page');
    $page.find('.adunit').dfp(options);
});

The output shown in the console will be something like:

afterEachAdLoaded banner1
New page
afterEachAdLoaded banner2
afterEachAdLoaded banner2

After some develop I have created a version that distinguishes between "init" and "new ad" calls.

I am going to make a pull request for your consideration. I have made a strong effort to make it backward-compatible with current code.

I have not added the minified version or propose changes in the readme prior to your comments.

pribeh commented 7 years ago

@tatai Any chance you'd be able to share the code here? I'm trying to get this going as well but to no avail.

tatai commented 7 years ago

@pribeh I created pul request #136 but it does not passed all tests so it cannot be evaluated for merging. Hopefully this week or next I will have some time to fix it. Feedback is welcomed

mkhazov commented 7 years ago

136 didn't help me, slotRenderEnded listener is still being added on each $.dfp call.

BowlingX commented 7 years ago

143 should fix this issue.