控制广告加载和刷新

在我们的使用入门示例和基本概念示例中,Google 发布商代码 使用 (GPT) 库的 display() 方法 注册并显示广告位但有时 最好或甚至必要来分隔这些操作, 精确地控制何时加载广告内容。例如,使用 意见征求管理平台或出于用户意愿而请求广告内容 操作。

在本指南中,我们将探讨 GPT 提供的机制 控制广告内容的加载,并按需提取新的广告内容。完整代码 基于事件的请求 示例页。

控制广告加载

默认情况下,display() 方法的行为是 在广告位中注册、请求和呈现广告内容。自动 可通过 PubAdsService.disableInitialLoad() 方法。

停用初始加载后,调用 display() 将仅注册广告位。 在进行第二项操作之前,不会加载任何广告内容。这样,您就可以 从而精确控制何时发出广告请求

为避免发出意外的广告请求,必须调用 disableInitialLoad() 和调用 display() 之前。

<!doctype html>
<html>
  <head>
    <meta charset="utf-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1" />
    <meta name="description" content="Request GPT ads based on events." />
    <title>Event-based ad requests</title>
    <script
      async
      src="https://securepubads.g.doubleclick.net/tag/js/gpt.js"
      crossorigin="anonymous"
    ></script>
    <script>
      window.googletag = window.googletag || { cmd: [] };

      googletag.cmd.push(() => {
        // Define the ad slot.
        googletag
          .defineSlot("/6355419/Travel", [728, 90], "div-for-slot")
          .setTargeting("test", "event")
          .addService(googletag.pubads());

        // Disable initial load.
        // This prevents GPT from automatically fetching ads when display is called.
        googletag.pubads().disableInitialLoad();
        googletag.enableServices();
      });
    </script>
    <style></style>
  </head>
  <body>
    <div id="div-for-slot" style="width: 300px; height: 250px"></div>
    <script>
      googletag.cmd.push(() => {
        // Register the ad slot.
        // An ad will not be fetched until refresh is called.
        googletag.display("div-for-slot");

        // Register click event handler.
        document.getElementById("showAdButton").addEventListener("click", () => {
          googletag.cmd.push(() => {
            googletag.pubads().refresh();
          });
        });
      });
    </script>
  </body>
</html>

在本例中,系统会停用初始加载,以确保不会发出任何广告请求 并且调用 display() 时不会呈现任何广告内容。广告位 接受并展示广告,但要等到广告位展示时, 刷新之后。

刷新

PubAdsService.refresh() 方法用于填充 包含新广告内容的一个或多个广告位。此方法可用于 尚未加载任何内容(由于 disableInitialLoad()),或将 已填充广告位的内容。不过,只有 可以刷新。display()

<!doctype html>
<html>
  <head>
    <meta charset="utf-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1" />
    <meta name="description" content="Request GPT ads based on events." />
    <title>Event-based ad requests</title>
    <script
      async
      src="https://securepubads.g.doubleclick.net/tag/js/gpt.js"
      crossorigin="anonymous"
    ></script>
    <script>
      window.googletag = window.googletag || { cmd: [] };

      googletag.cmd.push(() => {
        // Define the ad slot.
        googletag
          .defineSlot("/6355419/Travel", [728, 90], "div-for-slot")
          .setTargeting("test", "event")
          .addService(googletag.pubads());

        // Disable initial load.
        // This prevents GPT from automatically fetching ads when display is called.
        googletag.pubads().disableInitialLoad();
        googletag.enableServices();
      });
    </script>
    <style></style>
  </head>
  <body>
    <div id="div-for-slot" style="width: 300px; height: 250px"></div>
    <button id="showAdButton">Show/Refresh Ad</button>
    <script>
      googletag.cmd.push(() => {
        // Register the ad slot.
        // An ad will not be fetched until refresh is called.
        googletag.display("div-for-slot");

        // Register click event handler.
        document.getElementById("showAdButton").addEventListener("click", () => {
          googletag.cmd.push(() => {
            googletag.pubads().refresh();
          });
        });
      });
    </script>
  </body>
</html>

在此修改后的示例中,当用户点击“显示/刷新广告”按钮时按钮, 系统会调用 refresh() 方法。这会触发获取新广告内容的请求 并将其加载到已注册的槽中,从而覆盖任何预先存在的内容。

请注意,在上例中,调用了 refresh() 方法时未使用 参数,这相当于刷新所有已注册的广告位。 不过,您还可以通过传递 传递给 refresh() 方法。请参阅刷新广告位 示例。

最佳做法

使用 refresh() 时,应遵循一些最佳实践 。

  1. 不要刷新太快。

    如果广告位刷新太快,可能会导致您的广告请求受限。 为防止出现这种情况,请避免以高于每 30 次刷新一次的频率刷新槽 。

  2. 请勿不必要地调用 clear()

    刷新广告位时,不要调用 PubAdsService.clear()。这是没有必要的 refresh() 会替换指定广告位的内容,而不考虑 之前是否加载过任何广告内容。立即拨打 clear() 在调用 refresh() 之前,只会增加空白槽时间 对相应用户可见

  3. 仅刷新可见广告位

    使用 refresh() 替换一律不可见的广告位的内容 会显著降低 ActiveView 率通过 ImpressionViewableEvent 可用于 确定广告位何时可见,如下例所示。

    googletag.cmd.push(function() {
      var REFRESH_KEY = 'refresh';
      var REFRESH_VALUE = 'true';
    
      googletag.defineSlot('/6355419/Travel',[728, 90], 'div-for-slot')
          .setTargeting(REFRESH_KEY, REFRESH_VALUE)
          .setTargeting('test', 'event')
          .addService(googletag.pubads());
    
      // Number of seconds to wait after the slot becomes viewable.
      var SECONDS_TO_WAIT_AFTER_VIEWABILITY = 60;
    
      googletag.pubads().addEventListener('impressionViewable', function(event) {
        var slot = event.slot;
        if (slot.getTargeting(REFRESH_KEY).indexOf(REFRESH_VALUE) > -1) {
          setTimeout(function() {
            googletag.pubads().refresh([slot]);
          }, SECONDS_TO_WAIT_AFTER_VIEWABILITY * 1000);
        }
      });
    
      googletag.enableServices();
    });