我正在为一个通过Google广告管理器运行多个广告空间的网站设置A/B测试(也就是双击发布程序)。测试的一部分涉及移动和调整广告空间的iframes,因此我们需要在广告中提供不同的图像。我们试图避免通过广告管理器仪表板来做这件事,但是Google的广告是在一个iframe中提供的,这使得我们测试站点上的前端Javascript不能将img src更改为新的广告创意。
有没有办法让Google Ad iframe在页面加载时接受Javascript的更改?
发布于 2021-06-29 18:24:24
首先,这取决于您的safeframe设置(有关safeframe API的更多详细信息,请参阅here )。如果您的目标是从父站点触发"banner“ad iframe中的函数:
案例1:未启用GPT safeframe API
创意脚本(自定义模板或自定义脚本添加到您的‘基本’创意)
function changeBackground() {
var creative = document.querySelector('#creativeId');
creative.style.backgroundImage = 'url("yournewimage.jpeg")';
}父站点DOM脚本:
document.querySelector('#bannerId iframe').contentWindow.changeBackground();情况2:启用GPT safeframe API您需要通过窗口case消息=>创建专用的通信协议有关详细信息,请参阅here
另外,请记住以下几点:
然后,出现了几个问题:
这就是为什么我建议直接从你的Google Publisher标签设置中处理“响应式”渲染:
如果需要“实时”响应效果,
此解决方案的优势在于覆盖了站点adserver集成中的所有创意类型:
<script async src="https://securepubads.g.doubleclick.net/tag/js/gpt.js"></script>
<script>
window.googletag = window.googletag || {cmd: []};
googletag.cmd.push(function() {
// Define a responsive ad slot.
// [START responsive_ad]
responsiveAdSlot =
googletag.defineSlot('/6355419/Travel/Europe',
[[300, 250], [728, 90], [750, 200]], 'responsive-ad')
.addService(googletag.pubads());
var mapping =
googletag.sizeMapping()
.addSize([1024, 768], [[750, 200], [728, 90]])
.addSize([640, 480], [300, 250])
.addSize([0, 0], [])
.build();
responsiveAdSlot.defineSizeMapping(mapping);
// [END responsive_ad]
// Enable SRA and services.
googletag.pubads().enableSingleRequest();
googletag.enableServices();
});
function refreshSlot() {
googletag.cmd.push(function() {
googletag.pubads().refresh([responsiveAdSlot]);
})
}
//widow being resized (end of event to prevent multiple adcalls)
var timeOutFunctionId;
window.addEventListener('resize', function() {
clearTimeout(timeOutFunctionId);
timeOutFunctionId = setTimeout(refreshSlot, 500)
});
</script>
<div class="ad-container">
<div id="responsive-ad" class="ad-slot"></div>
</div>
<script>
googletag.cmd.push(function() {
googletag.display('responsive-ad');
});
</script>“等待调整大小结束事件”的Credit。
https://stackoverflow.com/questions/68165603
复制相似问题