google-dfp

DFP responsive ads


I have so problem to run responsive DFP ads in my website. I read all the google specs but still cant figure it out.

<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>

<script type='text/javascript'>
googletag.cmd.push(function() {
  googletag.defineSlot('/4421777/Textlink', [468, 60], 'div-gpt-ad-1431019765846-0').addService(googletag.pubads());
googletag.pubads().enableSingleRequest();
googletag.companionAds().setRefreshUnfilledSlots(true);
googletag.pubads().enableVideoAds();
googletag.enableServices();
});

and here is my mapping code :

var mapping = googletag.sizeMapping().
  addSize([1024, 768], [970, 250]).
  addSize([980, 690], [[1000, 300],[320, 50]]).
  addSize([640, 480], [[120, 60],[1000, 300],[320, 50], [468, 60]]).
  addSize([0, 0], [88, 31],).
  // Fits browsers of any size smaller than 640 x 480
  build();
adSlot.defineSizeMapping(mapping);

can you please give some clear way to make it run perfect on mobile etc? thanks


Solution

  • Your code is incorrect as it doesn't link the size mapping with ad unit. Note the bold part in my code: 'div-gpt-ad-1431019765846-0').defineSizeMapping(mapping).addService(googletag Also, I think you need different size mappings for each ad slot. I am giving an example: 1. You have a big banner on the top of your website, let's say the Ad Unit (Ad Slot) is called Billboard. So, if it is desktop, you want to serve a big banner - 1000x300, if large tablet or small desktop - 728x90 and if it is a mobile device a mobile leaderboard - 320x100. 2. Than you have another ad unit, let's say it is called Rectangle. You want this to serve 300x250 or 300x600 on desktop. 300x250 on tablets and phones. 3. Then you have a third ad unit, let's say it is called Smallbanner. You want this to serve 468x60 on desktop and tablets and no banner on phones.

    You will need to create a size mapping for each ad unit and then attach it when called. So in my example case, the output code will be something like this. I am adding inline comments so you can probably understand it better.

    <script async='async' src='https://www.googletagservices.com/tag/js/gpt.js'></script>
    <script>
      var googletag = googletag || {};
      googletag.cmd = googletag.cmd || [];
    </script>
    
    <script type='text/javascript'>
    var gptAdSlots = [];
        googletag.cmd.push(function() {
        // Create size mapping for Billboard position. If viewport is above 1000x768 show banner 1000x300. If viewport is above 728x300 (but bellow 1000x768) show banner 728x90, if viewport is lower than 728x300 show banner 320x100
        var billboardsizes = googletag.sizeMapping().addSize([1000, 768], [1000, 300]).addSize([728, 300], [728, 90]).addSize([0, 0], [320, 100]).build();
    
        // Create size mapping for Rectangle position. If viewport is above 1000x768 (considered as desktop, you may lower the height) show 300x250 OR 300x600. If the viewport is smaller than 1000x768 show 300x250 only.
        var rectanglesizes = googletag.sizeMapping().addSize([1000, 768], [[300, 60], [300, 250]]).addSize([0, 0], [300, 250]).build();
    
        // Create size mapping for Smallbanner position. If viewport is above 468x300 (considered as desktop + bigger tablets) show 468x60. If smaller, don't show banner.
        var smallbannersizes = googletag.sizeMapping().addSize([468, 300], [468, 60]).addSize([0, 0], []).build();  
    
        // Now create the first slot. Please note that we add all the sizes described in the size mapping. This should be set in the DFP Ad Unit configuration as well. Please also note the part of the code: .defineSizeMapping(billboardsizes) - it tells DFP what banner to serve on what device size.
        gptAdSlots[0] = googletag.defineSlot('/4421777/billboard', [[1000, 300], [320, 100], [728, 90]], 'div-gpt-ad-1431019765846-0').defineSizeMapping(billboardsizes).addService(googletag.pubads());
    
        // Now create the second slot. Please note that we add all the sizes described in the size mapping. This should be set in the DFP Ad Unit configuration as well. Please also note the part of the code: .defineSizeMapping(rectanglesizes) - it tells DFP what banner to serve on what device size. We also incremented gptAdSlots[1] by one and the last number of the div id by 1.
        gptAdSlots[1] = googletag.defineSlot('/4421777/rectangle', [[300, 600], [300, 250]], 'div-gpt-ad-1431019765846-1').defineSizeMapping(rectanglesizes).addService(googletag.pubads());
    
        // Now create the third slot. Please note that we add all the sizes described in the size mapping. This should be set in the DFP Ad Unit configuration as well. Please also note the part of the code: .defineSizeMapping(smallbannersizes) - it tells DFP what banner to serve on what device size. We also incremented gptAdSlots[1] by one and the last number of the div id by 1.   
        gptAdSlots[2] = googletag.defineSlot('/4421777/smallbanner', [468, 60], 'div-gpt-ad-1431019765846-2').defineSizeMapping(smallbannersizes).addService(googletag.pubads());
    
        googletag.pubads().enableSingleRequest();
        googletag.companionAds().setRefreshUnfilledSlots(true); 
        googletag.pubads().enableVideoAds();    
        googletag.enableServices();
      });   
    </script>