对于单页应用程序,我想知道是否可以请求新广告以替换 div 容器中的现有广告
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>