Skip to content

Instantly share code, notes, and snippets.

@CodeVachon
Created March 4, 2016 19:51
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save CodeVachon/817f9263d436ebb8447a to your computer and use it in GitHub Desktop.
Save CodeVachon/817f9263d436ebb8447a to your computer and use it in GitHub Desktop.
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