【问题标题】:closing div script in the tag of google ad manager关闭谷歌广告管理器标签中的 div 脚本
【发布时间】:2020-05-19 15:30:44
【问题描述】:

我正在尝试使脚本在 Google Ad Manager 的代码中运行。该脚本应该在没有广告显示时关闭 div

我的脚本是这样的:

<script>
try{
parent.document.getElementById('div-gpt-ad-1407836088099-0').style.display = 'none';
}catch(e){
console.log(e)
}
</script>

这在谷歌浏览器的控制台中可以正常工作,但在网页中却不行。

我已经尝试在广告代码中使用它:

.setCollapseEmptyDiv()

但不幸的是,如果您尝试使用响应式标签,它就不起作用。

最后,应该工作的网页如下: https://www.perlentaucher.de/static/yl-raw.html

有什么想法吗?

【问题讨论】:

  • 目标是什么?如果没有调用任何广告,折叠广告位?
  • 完全正确。如果为空,则应折叠

标签: javascript google-ad-manager


【解决方案1】:

为了折叠空的广告 div,在 Google 发布商代码中定义了两个专用选项:

//for all adslots on the page
googletag.pubads().collapseEmptyDivs()

//for a dedicated adslot on the page
googletag.defineSlot(...).setCollapseEmptyDiv(...).addService(googletag.pubads())

有几种配置方式:

  1. 对于页面中的所有广告位/如果为空,则在广告调用后折叠

  2. 对于页面中的所有广告位/默认折叠,如果不为空则打开

  3. 对于特定的广告位/如果为空,则在广告调用后折叠

  4. 对于特定的广告位/默认折叠,如果不为空则打开

以下示例来自official documentation

<script async src="https://securepubads.g.doubleclick.net/tag/js/gpt.js"></script>
<script>
      window.googletag = window.googletag || {cmd: []};
      googletag.cmd.push(function() {
        // Configure all ad slots on the page to be expanded by default, but
        // collapse slots that are unable to be filled with an ad.
        googletag.pubads().collapseEmptyDivs();

        // The above setting assumes all ad slots are likely to be filled.
        // If ad slots are not likely to be filled, pass true as a parameter as
        // shown below. In this configuration, slots will be collapsed by
        // default and expanded only if they are able to be filled.
        // googletag.pubads().collapseEmptyDivs(true);

        // This slot will use the page-level settings configured above.
        googletag.defineSlot('/6355419/Travel', [300, 250], 'ad-slot-1')
            .setTargeting("test", "responsive")
            .addService(googletag.pubads());

        // Configure per-slot overrides.
        // This slot will be expanded by default, but collapse if it cannot be
        // filled.
        googletag.defineSlot('/6355419/Travel', [250, 200], 'ad-slot-2')
            .setTargeting("test", "responsive")
            .setCollapseEmptyDiv(true)
            .addService(googletag.pubads());

        // This slot will be expanded by default and never collapse.
        googletag.defineSlot('/6355419/Travel', [200, 150], 'ad-slot-3')
            .setTargeting("test", "responsive")
            .setCollapseEmptyDiv(false)
            .addService(googletag.pubads());

        // This slot will be collapsed by default and only expand if it can be
        // filled.
        googletag.defineSlot('/6355419/Travel', [150, 100], 'ad-slot-4')
            .setTargeting("test", "responsive")
            .setCollapseEmptyDiv(true, true)
            .addService(googletag.pubads());

        // Enable SRA and services.
        googletag.pubads().enableSingleRequest();
        googletag.enableServices();
      });
    </script>

这很好用,不需要自己手动隐藏广告位。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-08-29
    • 1970-01-01
    • 1970-01-01
    • 2021-02-01
    相关资源
    最近更新 更多