Skip to content

Instantly share code, notes, and snippets.

@vpotap
Last active April 6, 2017 17:14
Show Gist options
  • Save vpotap/a748d7cd7aeca168690cdb862847b183 to your computer and use it in GitHub Desktop.
Save vpotap/a748d7cd7aeca168690cdb862847b183 to your computer and use it in GitHub Desktop.
<!DOCTYPE html>
<html lang="en">
<head>
<title>Test DFP Ads</title>
<!-- google cdn -->
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.3/jquery.min.js"></script>
<script>(function (w, d, s, l, i) {
w[l] = w[l] || [];
w[l].push({
'gtm.start': new Date().getTime(), event: 'gtm.js'
});
var f = d.getElementsByTagName(s)[0],
j = d.createElement(s), dl = l != 'dataLayer' ? '&l=' + l : '';
j.async = true;
j.src =
'https://www.googletagmanager.com/gtm.js?id=' + i + dl;
f.parentNode.insertBefore(j, f);
})(window, document, 'script', 'dataLayer', 'GTM-KQ3K6V');</script>
<script type="text/javascript" async="async" src="https://www.googletagservices.com/tag/js/gpt.js"></script>
<script type="text/javascript">
var googletag = googletag || {};
googletag.cmd = googletag.cmd || [];
window.dfpLoaded = [];
</script>
<script type="text/javascript">
//if you look at the definition above, this is actually an array
//and is used as a queue where commands are storred until the JS code above
//is loaded asynchronously
//see https://developers.google.com/doubleclick-gpt/reference#googletag.CommandArray
googletag.cmd.push(function () {
//define first the responsive query. It works just like Media Queries (min-width, min-height)
//https://developers.google.com/doubleclick-gpt/reference#googletag.SizeMappingBuilder_addSize
var desktop_branding = googletag.sizeMapping()
.addSize([1320, 500], [[1320, 550], [1320, 500]])
//for sizes smaller than the ones above, don't diplay any size for the AD
.addSize([0, 0], [])
.build();
//simply define the Ad Unit
//https://developers.google.com/doubleclick-gpt/reference#googletag.defineSlot
googletag.defineSlot('/133335187/Jauns.lv_fons_sievietes', [[1320, 500], [1320, 550]], 'dfp-l')
.defineSizeMapping(desktop_branding)
.addService(googletag.pubads());
//enable all ads to be received together (it is faster because we don't have multiple HTTP requests to the Ad Server and usefull for enforcing companionship -> all banners to show the content regarding the same brand)
//https://developers.google.com/doubleclick-gpt/reference#googletag.PubAdsService_enableSingleRequest
googletag.pubads().enableSingleRequest();
//adding this call will make sure this slow will be hidden in case there is no ad
//for a specific viewport size
googletag.pubads().collapseEmptyDivs();
//enables DFP for previously defined Ad Slots
//https://developers.google.com/doubleclick-gpt/reference#googletag.enableServices
googletag.enableServices();
//some debugging for slot
googletag.pubads().addEventListener('slotRenderEnded', function(event) {
if (event.slot.getSlotElementId() == "dfp-l") {
var containsAd = !event.isEmpty;
console.log("contains ad ? : " + containsAd);
}
});
});
//ON WINDOW RESIZE, we need to tell DFP to refresh the ads to their
//coresponding size, as defined above
$(window).resize(function() {
googletag.pubads().refresh();
});
</script>
</head>
<body>
<div id="dfp-l" style="background-color:#dedede;">
<script>
//again the command is put in the array queue
googletag.cmd.push(function () {
//instruct DFP to render this ad
//https://developers.google.com/doubleclick-gpt/reference#googletag.display
googletag.display("dfp-l");
});
</script>
</div>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment