Skip to content

Instantly share code, notes, and snippets.

@CodeVachon

CodeVachon/index.js

Created Mar 4, 2016
Embed
What would you like to do?
How to Modify a DoubleClick Advertisement
$(document).ready(function() {
/*
* ...
* Load in DFP Loading Code, the important thing here is that we
* are adding in tht `slotRenderEnded` Event Listener. When triggered,
* it will fire the slotRenderedEventMethod function listed below.
*/
googletag
.pubads()
.addEventListener('slotRenderEnded', slotRenderedEventMethod)
; // close googletag
}); // close $(document).ready
var slotRenderedEventMethod = function(event) {
if (event.isEmpty === false) {
var _thisAdvertisement = $('#' + event.slot.getSlotElementId());
/*
* DFP will load in multiple iframes into the block, dont access
* the __hidden__ one. Its possible for DFP to return multiple
* content iFrames, this accounts for that.
*/
_thisAdvertisement.find('iframe:not([id$="__hidden__"])').each(function() {
var _thisIFrame = $(this);
// Set a New Width for the iFrame
_thisIFrame.prop('width', '100%');
/*
* Its possible for DFP to return multiple Anchor Tags in the
* iFrame, this accounts for that possiblity.
*/
_thisIFrame.contents().find('a').each(function() {
var _thisAnchor = $(this);
var _thisURL = _thisAnchor.attr('href');
console.log("Click URL: " + _thisURL);
/*
* Now we have the URL, we can perform whatever actions
* We wanted to do.
*/
}); // close _thisIFrame..find("a")
}); // close _thisAdvertisement.find('iframe').each()
} else {
// Send a notice into console that no advertisement was found
console.warn('No advertisements for ' + event.slot.getSlotElementId());
} // close if (event.isEmpty)
}; // close slotRenderedEventMethod
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment