Page 1 of 1

DoubleClick for Publishers (DFP) Responsive Ads Uses

Posted: Tue Sep 05, 2017 7:59 am
by arnlweb
Here is the sample of DoubleClick for Publishers (DFP) responsive Ads. DFP does not provide responsive ads features like Google Adsense, so you can use this coding.

Put this code in your header file.

Code: Select all

<script type="text/javascript">
  var googletag = googletag || {};
  googletag.cmd = googletag.cmd || [];
  (function() {
    var gads = document.createElement("script");
    gads.async = true;
    gads.type = "text/javascript";
    var useSSL = "https:" == document.location.protocol;
    gads.src = (useSSL ? "https:" : "http:") +
      "//www.googletagservices.com/tag/js/gpt.js";
    var node = document.getElementsByTagName("script")[0];
    node.parentNode.insertBefore(gads, node);
  })();
</script>
Replace "XXXXXXXXX" value with your DFP publisher ID and put this code in your header file.

Code: Select all

<script type="text/javascript">
	googletag.cmd.push(function() {
	
  var mappingLeaderboard = googletag.sizeMapping().
		addSize([1024, 768 ], [728, 90]).
		addSize([500, 500 ], [468, 60]).
		addSize([1, 1 ], [320, 50]).
		build();
		
  var mappingLeaderboard2 = googletag.sizeMapping().
  		addSize([1024, 768 ], [970, 90]).
		addSize([800, 600 ], [728, 90]).
		addSize([500, 500 ], [468, 60]).
		addSize([1, 1 ], [320, 50]).
		build();
		
  var mappingLeaderboard3 = googletag.sizeMapping().
  		addSize([1024, 768 ], [468, 60]).
		addSize([800, 600 ], [468, 60]).
		addSize([500, 500 ], [468, 60]).
		addSize([1, 1 ], [300, 250]).
		build();

  var mappingBox = googletag.sizeMapping().
		addSize([1024, 768 ], [336, 280]).
		addSize([800, 600 ], [336, 280]).
		addSize([500, 500 ], [300, 250]).
		addSize([1, 1 ], [250, 250]).
		build();

	googletag.defineSlot("/XXXXXXXXX/Responsive_ads", [[320, 50], [468, 60], [970, 90], [728, 90]], "div-gpt-ad-1499850832266-0").defineSizeMapping(mappingLeaderboard2).addService(googletag.pubads());
	
	googletag.defineSlot("/XXXXXXXXX/mappingLeaderboard", [[320, 50], [468, 60], [728, 90]], "div-gpt-ad-1499851829170-0").defineSizeMapping(mappingLeaderboard).addService(googletag.pubads());

	googletag.defineSlot("/XXXXXXXXX/mappingBox", [[336, 280], [250, 250], [300, 250]], "div-gpt-ad-1499851906927-0").defineSizeMapping(mappingBox).addService(googletag.pubads());
	
	googletag.defineSlot("/XXXXXXXXX/mappingLeaderboard2", [[468, 60], [300, 250]], "div-gpt-ad-1499863451422-0").defineSizeMapping(mappingLeaderboard3).addService(googletag.pubads());

	googletag.pubads().enableSingleRequest();

	googletag.enableServices();

});

</script>
And put this code where you want show the responsive ads.

Code: Select all

<div id="div-gpt-ad-1499851906927-0"><script type="text/javascript">googletag.cmd.push(function() { googletag.display("div-gpt-ad-1499851906927-0"); });</script></div>