I need to trigger some JavaScript after DFP has finished rendering all the ads on the page - or at least when it has triggered collapseEmptyDivs (which hides ad units that contain no line items).
Is anyone aware of a way to get DFP to trigger a callback after either of these events?
I'm pretty sure that DFP doesn't provide for a callback after the ad has rendered. I have used the following code to do this. It calls the callback function after one of the following has happened:
-The ad has loaded and the iframe has rendered
-No ad was loaded, and the ad unit was hidden by collapseEmptyDivs()
-A certain amount of time has passed (in this case, 2 seconds) with neither one happening. Like if there was some sort of network error connecting to DFP.
adId would be the id of your ad container
assumes you are using jQuery
Load script in the part of your page:
Initialise google publisher tag with the following script, preferable as well in the section of your page:
Register slots individually (can be generated with a foreach loop) and render them. The event listener can be registered as well per slot. Here the important part: make sure that you refresh them together to avoid ending up with the same ads on both slots (if the ad is assign to both slots) => googletag.pubads().refresh([slot1, slot2]]);
After the ad has been rendered, the callback gets triggered.
For more information have a look on this file: https://github.com/davidecantoni/googletag
The GPT API now has a callback that is triggered after each slot is filled.
For example:
See https://developers.google.com/doubleclick-gpt/reference#googletag.events.SlotRenderEndedEvent
Check out the jQuery DFP extension I'm working on... it is still a bit of a work in progress but provides the callback you are after.
An example of how to use it is in this file.
You will see two callbacks are available... after each ad has loaded and after all ads have loaded. A class is also set on the ad unit container element, this can either be display-none (when no ad is found), display-block (when an ad is found) or display-original (for when no ad is found but the container div contained content to begin with, I use this for over riding certain parts of sites with ad content when required). These classes are of course useful to work with once inside the callback.
If you need to identify the render end of a specific slot ( useful if you are using same creative for multiple slots ) you can do the following
I hacked googletag's debug_log.log function and pushed it through jQuery to fire events on a lot of DFP's actions. The hack does require jQuery.
https://github.com/mcountis/dfp-events