Created
March 4, 2016 19:51
-
-
Save CodeVachon/817f9263d436ebb8447a to your computer and use it in GitHub Desktop.
How to Modify a DoubleClick Advertisement
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
$(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