自訂廣告使用者介面

本指南將說明如何使用 IMA SDK 導入自訂使用者介面 。如要這麼做,您必須停用預設 UI,然後設定新的 然後在新的 UI 中填入從 SDK。本指南以 簡易範例

在 DOM 中新增 UI 元素

撰寫任何 JavaScript 之前,請先修改 HTML 和樣式表,以新增 [瞭解詳情] 按鈕、[略過廣告] 按鈕和新的 UI 元素 倒數計時器:

index.html

<div id="mainContainer">
  <div id="content">
    <video id="contentElement">
      <source src="https://storage.googleapis.com/gvabox/media/samples/stock.mp4"></source>
    </video>
  </div>
  <div id="adContainer"></div>
  <div id="customUi">
    <div id="adClick" class="customUIButton">Click me!</div>
    <div id="skipButton" class="customUIButton"></div>
    <div id="adCountdown"></div>
  </div>
</div>

Style.css

...
#customUI {
  display: none;
  text-align: center;
  width: 100%;
  height: 100%;
}

.customUIButton {
  position: absolute;
  right: 0px;
  background-color: red;
}

#adClick {
  top: 0px;
  width: 100px;
  height: 50px;
  line-height: 3em;
  cursor: pointer;
}

#skipButton {
  top: 213px;
  width: 150px;
  height: 35px;
  line-height: 2em;
  display: none;
}

#adCountdown {
  position: absolute;
  left: 0px;
  bottom: 10px;
  width: 120px;
  height: 20px;
  background-color: red;
}
...

您可以視需要調整 UI 的樣式;上述 UI 只是 範例。

停用內建 UI

首先,請告知 SDK 您要停用其內建 UI。

ads.js

function onAdsManagerLoaded(adsManagerLoadedEvent) {
  const adsRenderingSettings = new google.ima.AdsRenderingSettings();
  adsRenderingSettings.disableUi = true;
  // videoContent should be set to the content video element.
  adsManager = adsManagerLoadedEvent.getAdsManager(
      videoContent, adsRenderingSettings);
  ...
}

允許顯示及隱藏自訂 UI

某些 Google 廣告 (例如 AdSense 廣告) 不允許自訂使用者介面,而且永遠 並轉譯其使用者介面在自訂 UI div 上方的樣式表中 預設為隱藏。只在目前播放的廣告隱藏其畫面時顯示 使用者介面,並在每則廣告結束後隱藏自訂使用者介面,以免接下來的廣告 允許自訂 UI:

ads.js

let mainContainerDiv, currentAd, customUiDiv;
...
function setUpIMA() {
  customUiDiv = document.getElementById('customUi');
  mainContainerDiv = document.getElementById('mainContainer');
  mainContainerDiv.addEventListener('click', () => {
    // Need this to resume ads after clickthrough.
    adsManager.resume();
  });
  ...
}

function onAdsManagerLoaded(adsManagerLoadedEvent) {
  ...
  adsManager.addEventListener(
      google.ima.AdEvent.Type.SKIPPED,
      onAdEvent);
}

function onAdEvent(adEvent) {
  switch(adEvent.type) {
    ...
    case google.ima.AdEvent.Type.STARTED:
      currentAd = adEvent.getAd();
      if (currentAd.isUiDisabled()) {
        showCustomUi();
      }
      break;
    case google.ima.AdEvent.Type.COMPLETE:
    case google.ima.AdEvent.Type.SKIPPED:
      hideCustomUi();
      break;
  }
}

function showCustomUi() {
  customUiDiv.style.display = 'block';
}

function hideCustomUi() {
  customUiDiv.style.display = 'none';
}

將邏輯新增至「Learn More」按鈕

需要連接的第一個 UI 元件是「瞭解詳情」按鈕。這個元素 通知 SDK 獲得點擊時,應用程式會發出通知:

ads.js

let clickDiv;
...
function setUpIma() {
  ...
  clickDiv = document.getElementById('adClick');
  clickDiv.addEventListener('click', (e) => {
    // Prevent this from propagating to the customUi div. That would cause the ad to
    // resume immediately after the user clicks it, and we want it to pause.
    e.stopPropagation();
    adsManager.clicked();
  });
  ...
}

在倒數計時器中加入邏輯

接下來,請建立用於輪詢剩餘時間的倒數計時器 廣告。

ads.js

let countdownDiv, uiUpdateInterval;
...
function setUpIma() {
  ...
  countdownDiv = document.getElementById('adCountdown');
  ...
}

function showCustomUi() {
  uiUpdateInterval = setInterval(updateUi, 100);
  customUiDiv.style.display = 'block';
}

function hideCustomUi() {
  if (uiUpdateInterval) {
    clearInterval(uiUpdateInterval);
  }
  customUiDiv.style.display = 'none';
}

function updateUi() {
  updateCountdown();
}

function updateCountdown() {
  let countdownText = 'Ad ';
  const adPodInfo = currentAd.getAdPodInfo();
  const totalAds = adPodInfo.getTotalAds();
  if (totalAds > 1) {
    const position = adPodInfo.getAdPosition();
    countdownText += position + ' of ' + totalAds;
  }
  const remainingTime = Math.ceil(adsManager.getRemainingTime());
  countdownText +=  ' (' + remainingTime + 's)';
  countdownDiv.innerText = countdownText;
}

將邏輯新增至「略過廣告」按鈕

最後,連接「略過廣告」按鈕。這個按鈕只會在 可略過的廣告及其倒數計時器必須達到 0,才會啟動 可讓使用者略過廣告

ads.js

let skipDiv;
// Set this infinitely high to fail early <=0 checks.
let timeTillSkip = Number.POSITIVE_INFINITY;
...
function setUpIma() {
  ...
  skipButton = document.getElementById('skipButton');
  skipButton.addEventListener('click', (e) => {
    // Prevent this from propagating to the customUi div. That would cause the ad to
    // resume immediately after the user clicks it, and we want it to pause.
    e.stopPropagation();
    if (timeTillSkip <= 0) {
      adsManager.skip();
    }
  });
  ...
}

function showCustomUi() {
  if (currentAd.isSkippable()) {
    skipButton.innerText = '';
    skipButton.style.display = 'block';
  } else {
    skipButton.style.display = 'none';
  }
  ...
}

function updateUi() {
  updateCountdown();
  if (currentAd.isSkippable()) {
    updateSkip();
  }
}

function updateSkip() {
  const currentTime = currentAd.getDuration() - adsManager.getRemainingTime();
  timeTillSkip = Math.ceil(currentAd.getSkipTimeOffset() - currentTime);
  if (timeTillSkip > 0) {
    skipButton.innerText = "Skip this ad in " + timeTillSkip + '...';
    skipButton.style.cursor = 'default';
   } else {
    skipButton.innerText = "Skip ad";
    skipButton.style.cursor = 'pointer';
  }
}

疑難排解

是否有可用於停用廣告 UI 的範例代碼?
您可以複製這個網址 代碼範例 並貼到 IMA 導入中
我無法停用預設使用者介面。
確認你已將「adsRenderingSettings.disableUi」設為「true」 並傳遞至 getAdsManager確認ad.isUiDisabled() 會傳回 true。此外,您必須在 Ad Manager 中啟用聯播網,才能停用 廣告使用者介面。如果您已啟用,VAST 會包含 Extension, 例如:
<Extension type="uiSettings">
<UiHideable>1</UiHideable>
</Extension>
敬上 如果問題仍未解決,請與客戶經理聯絡 部分廣告類型需要使用特定使用者介面;這些廣告會傳回 <UiHideable> 值為 0。出現這種情況時,廣告投放團隊必須 加以修改,確保這些廣告類型無法放送