控制廣告載入和重新整理

為入門和基本概念範例 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() 方法的 Pod 數量。請參閱重新整理廣告版位 舉例來說

最佳做法

使用 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();
    });