0

對於單頁的應用程序,我想知道是否有可能要求新的廣告,以取代現有的廣告在一個div容器GPT重新加載廣告

var slotName = '/blogs/peter/recentPosts' // this changes according to route 
var adContainer = 'div-common-container' // this never changes 
window.googletag.defineSlot(slotName), ['fluid'], adContainer).addService(this.googletag.pubads()) 

我發現的例子到目前爲止,確認這是可能刷新現有slots,但我的使用情況是不同的(https://support.google.com/dfp_premium/answer/2694377?hl=en&ref_topic=4390040

我的目標是有共同的模板頁面的常見的廣告容器元素,併爲每個pageTransition,其中分類爲不同 請求一個新的廣告。

我到目前爲止所做的當前測試,試着改變slotName然後調用refresh(),但它似乎不起作用!例如(比下面的例子更復雜,但只是爲了暴露點):

<!DOCTYPE html> 
<html> 
<head> 
    <script async='async' src='https://www.googletagservices.com/tag/js/gpt.js'></script> 
    <script> 
    var myAddSlot 
    var googletag = googletag || {}; 
    googletag.cmd = googletag.cmd || []; 
    </script> 

    <script> 
    googletag.cmd.push(function() { 
     myAddSlot = googletag.defineSlot('/6355419/Travel/Europe/France/Paris', ['fluid'], 'div-common-ad-container') 
        .addService(googletag.pubads()); 
     googletag.enableServices(); 
    }); 
    </script> 
</head> 
<body> 
    <div id='div-common-ad-container' /> 
    <button id="refresh">refresh</button> 
    <script> 
    googletag.cmd.push(function() { googletag.display('div-common-ad-container'); }); 
    document.querySelector('#refresh').addEventListener('click', function() { 
    googletag.destroySlots() 
    googletag.cmd.push(function() { 
     myAddSlot = googletag.defineSlot('/6355419/Travel', ['fluid'], 'div-common-ad-container') 
        .addService(googletag.pubads()); 
     googletag.enableServices(); 
    }); 
    }) 
    </script> 
</body> 
</html> 

任何提示是讚賞!

回答

0

這可能對未來的其他人有用。不知道這是否是最好的方法,但它有效。

我使用gpt提供的方法銷燬插槽,並清除容器元素和data-google-query-id屬性!

<!DOCTYPE html> 
<html> 
<head> 
    <script async='async' src='https://www.googletagservices.com/tag/js/gpt.js'></script> 
    <script> 
    var myAddSlot 
    var googletag = googletag || {}; 
    googletag.cmd = googletag.cmd || []; 
    </script> 

    <script> 
    googletag.cmd.push(function() { 
     myAddSlot = googletag.defineSlot('/6355419/Travel/Europe/France/Paris', ['fluid'], 'div-common-ad-container') 
        .addService(googletag.pubads()); 
     googletag.enableServices(); 
    }); 
    </script> 
</head> 
<body> 
    <div id='div-common-ad-container'></div> 
    <button id="refresh">refresh</button> 
    <script> 
    googletag.cmd.push(function() { googletag.display('div-common-ad-container'); }); 
    document.querySelector('#refresh').addEventListener('click', function() { 
    googletag.destroySlots() 
    document.querySelector('#div-common-ad-container').setAttribute('data-google-query-id', '') 
    document.querySelector('#div-common-ad-container').innerHTML = '' 
    googletag.cmd.push(function() { 
     myAddSlot = googletag.defineSlot('/6355419/Travel/Europe', ['fluid'], 'div-common-ad-container') 
        .addService(googletag.pubads()); 
     googletag.enableServices(); 
     googletag.display('div-common-ad-container'); 
    }); 
    }) 
    </script> 
</body> 
</html> 
相關問題