2016-10-25 111 views
0

我在我的網站上運行響應式DFP廣告時遇到問題。 我閱讀了所有的谷歌規格,但仍無法弄清楚。DFP廣告管理系統自適應廣告

<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(); 
 
});

,這裏是我的映射代碼:

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

可以請您給一些明確的方式,使其完美運行在移動等? 謝謝

回答

1

你的代碼不正確,因爲它不與廣告單元鏈接大小映射。 注意,在我的代碼加粗部分: 'DIV-GPT-AD-1431019765846-0').defineSizeMapping(映射) .addService(googletag的 另外,我認爲你需要不同尺寸的映射爲每個廣告位 我我舉一個例子: 1.您的網站頂部有一個大橫幅,假設廣告單元(Ad Slot)被稱爲廣告牌。因此,如果是桌面,您想要投放一個大橫幅 - 1000x300,如果大的平板電腦或桌面小廣告 - 728x90,如果它是一個移動設備的移動排行榜 - 的320x100 2.比你有另一個廣告單元,比方說,它被稱爲矩形希望此服務的300x250或300x600的。在平板電腦和手機上的300x250。 3.然後你有第三個廣告單元,比如說它叫做Smallbanner。您希望在桌面設備和平板電腦上爲468x60提供服務,而在手機上則不提供橫幅廣告。

您需要爲每個廣告單元創建尺寸映射,然後在調用時附加它。所以在我的例子中,輸出代碼就像這樣。我添加了內聯註釋,以便您可以更好地理解它。

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

這是您試圖實現的簡化版本(考慮到您的尺寸映射很奇怪,因爲您將在具有640x480視口的設備上顯示1000x300橫幅?)。

<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() { 
    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],).build(); 
    gptAdSlots[0] = googletag.defineSlot('/4421777/Textlink', [[970, 250], [1000, 300], [320, 50], [120, 60], [468, 60], [88, 31]], 'div-gpt-ad-1431019765846-0').defineSizeMapping(mapping).addService(googletag.pubads()); 
    googletag.pubads().enableSingleRequest(); 
    googletag.companionAds().setRefreshUnfilledSlots(true); 
    googletag.pubads().enableVideoAds();  
    googletag.enableServices(); 
    }); 
</script> 

拿一張紙條,上面有一些小的改動:1。 創建廣告位的數組,從第一個是0。但是,如果你要撥打的第二槽,你應該增加類似此後的第一個(注意[1],下一個將是[2]等):

gptAdSlots[1] = googletag.defineSlot('/4421777/Textlink2', [300, 250], 'div-gpt-ad-1431019765846-1').defineSizeMapping(mapping2).addService(googletag.pubads()); 
  • 您應該列出在你的尺寸映射定義的所有尺寸在廣告單元尺寸中也是如此。在DFP廣告管理系統廣告單元設置和DefineSlot調用中(我已經完成了上述操作)。
  • 0

    這是我的代碼現在它的正確嗎?

    <script async='async' src='https://www.googletagservices.com/tag/js/gpt.js'></script> 
     
    <script> 
     
        var googletag = googletag || {}; 
     
        googletag.cmd = googletag.cmd || []; 
     
    </script> 
     
    
     
    <script> 
     
        googletag.cmd.push(function() { 
     
         var mapping = googletag.sizeMapping().addSize([1600, 1024], [1000, 300]).addSize([980, 690], [[1000, 300],[320, 50]]).addSize([640, 480], [[120, 60],[1000, 300],[320, 50], [468, 60]]).addSize([0, 0], [88, 31],).build(); 
     
    
     
        gptAdSlots[0] = googletag.defineSlot('/15076752/300x250_Fifth', [300, 250], 'div-gpt-ad-1476951614543-0').addService(googletag.pubads()); 
     
         gptAdSlots[1] = googletag.defineSlot('/15076752/300x250_First', [300, 250], 'div-gpt-ad-1476951614543-1').addService(googletag.pubads()); 
     
        gptAdSlots[2] = googletag.defineSlot('/15076752/300x250_Fourth', [300, 250], 'div-gpt-ad-1476951614543-2').addService(googletag.pubads()); 
     
        gptAdSlots[3] = googletag.defineSlot('/15076752/300x250_Second', [300, 250], 'div-gpt-ad-1476951614543-3').addService(googletag.pubads()); 
     
        gptAdSlots[4] = googletag.defineSlot('/15076752/300x250_Third', [300, 250], 'div-gpt-ad-1476951614543-4').addService(googletag.pubads()); 
     
        gptAdSlots[5] = googletag.defineSlot('/15076752/300x600', [300, 600], 'div-gpt-ad-1476951614543-5').addService(googletag.pubads()); 
     
        gptAdSlots[6] = googletag.defineSlot('/15076752/728x90_Second', [728, 90], 'div-gpt-ad-1476951614543-6').addService(googletag.pubads()); 
     
        gptAdSlots[7] = googletag.defineSlot('/15076752/BillBoard-2-1000x300', [1000, 300], 'div-gpt-ad-1476951614543-7').addService(googletag.pubads()); 
     
        gptAdSlots[8] = googletag.defineSlot('/15076752/Billboard1000x300', [1000, 300], 'div-gpt-ad-1476951614543-8').addService(googletag.pubads()); 
     
        gptAdSlots[9] = googletag.defineSlot('/15076752/Skin_bg', [1600, 1024], 'div-gpt-ad-1476951614543-9').addService(googletag.pubads()); 
     
        gptAdSlots[10] = googletag.defineSlot('/15076752/Text_link', [468, 60], 'div-gpt-ad-1476951614543-10').addService(googletag.pubads()); 
     
        gptAdSlots[11] = googletag.defineSlot('/15076752/Text_link2', [468, 60], 'div-gpt-ad-1476951614543-11').addService(googletag.pubads()); 
     
        gptAdSlots[12] = googletag.defineSlot('/15076752/Text_link3', [468, 60], 'div-gpt-ad-1476951614543-12').addService(googletag.pubads()); 
     
        gptAdSlots[13] = googletag.defineSlot('/15076752/Top_728x90', [728, 90], 'div-gpt-ad-1476951614543-13').addService(googletag.pubads()); 
     
        googletag.pubads().enableSingleRequest(); 
     
        googletag.companionAds().setRefreshUnfilledSlots(true); 
     
        googletag.pubads().enableVideoAds();  
     
        googletag.enableServices(); 
     
        }); 
     
    </script>

    0

    它看起來像你還在每個gptAdSlots失蹤[X]

    .defineSizeMapping(mapping2) 
    
    相關問題