javascriptadsensegoogle-dfpgoogle-ad-managergoogle-publisher-tag

DoubleClick for publishers: Specify browser and ad dimensions


I'm trying to get banners to load according to the browser size. So in a location where i have a 728x90 banner, a 300x250 will show if its on mobile.

Problem is, the 728x90 loads on desktop. but on mobile the 300x250 doesn't show.

I tried following the example here

<script type='text/javascript'>
 googletag.cmd.push(function() {
    // This mapping will only display ads when user is on desktop sized viewport
    var mapLeader = googletag.sizeMapping().
        addSize([0, 0], []).
        addSize([768, 200], [728, 90]).
        build();

    // This mapping will only display ads when user is on mobile or tablet sized viewport
    var mapLeader2 = googletag.sizeMapping().
      addSize([0, 0], []).
      addSize([768, 200], []). // Desktop
      addSize([300, 200], [300, 250]). // Tablet
      build();

    window.LeaderSlot= googletag.defineSlot('/XXXXXXX/leaderboard-1', [728, 90], 'div-gpt-ad-1455251022145-0').
        defineSizeMapping(mapLeader).
        setCollapseEmptyDiv(true).
        addService(googletag.pubads());

    window.LeaderSlot= googletag.defineSlot('/XXXXXXX/medium-rectangle-1', [300, 250], 'div-gpt-ad-1458546777123-0').
        defineSizeMapping(mapLeader2).
        setCollapseEmptyDiv(true).
        addService(googletag.pubads()); 

    googletag.pubads().enableSingleRequest();
    googletag.pubads().enableSyncRendering();
    // Start ad fetching
    googletag.enableServices();

});
</script>

and in my HTML

<!-- /XXXXXX/leaderboard-1 -->
<div id='div-gpt-ad-1455251022145-0' style='height:90px; width:728px;' class="center">
    <script type='text/javascript'>
        googletag.cmd.push(function() { googletag.display('div-gpt-ad-1455251022145-0'); });
    </script>
</div>
<!-- /XXXXXX/medium-rectangle-1 -->
<div id='div-gpt-ad-1458546777123-0' style='height:250px; width:300px;'>
    <script type='text/javascript'>
    googletag.cmd.push(function() { googletag.display('div-gpt-ad-1458546777123-0'); });
    </script>
</div>

do i have to put the <div> for each size in the same location? How do i get the 300x250 banner to load? the 728x90 works fine. I know i could hide / show according too browser size using CSS. but i don't want to do that. Loading multiple sizes in the same location slows down my sites loading.


Solution

  • You don't need style='height:90px; width:728px;' since you want DFP to load the proper banner according to screen size.

    <!-- /XXXXXX/leaderboard-1 -->
    <div id='div-gpt-ad-1455251022145-0' style='height:90px; width:728px;' class="center">
        <script type='text/javascript'>
            googletag.cmd.push(function() { googletag.display('div-gpt-ad-1455251022145-0'); });
        </script>
    </div>
    

    when you test it on your browser using different screen sizes, remember that the banners don't automatically resize. You will have to refresh your page to see the changes.