このガイドでは、IMA SDK を使用して独自のカスタム管理画面を実装する方法を説明します 作成しますそのためには、デフォルトの UI を無効にし、新しい カスタム UI を作成し、そこから取得した広告情報を新しい UI に表示します。 説明します。このガイドは、 シンプルなサンプル 作成します
UI 要素を DOM に追加する
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 を無効にする
まず、組み込み UI を無効にするよう SDK に指示します。
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 を表示または非表示にする
AdSense 広告などの一部の Google 広告では UI をカスタマイズできないため、
代わりに独自の UI をレンダリングできます上のスタイルシートで、カスタム UI の div は次のようになります。
デフォルトで非表示になっています。現在再生中の広告が非表示になっている場合にのみ表示します
カスタム UI を各広告の後に非表示にして、後続の広告が
カスタム 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'; }
[詳細] ボタンにロジックを追加する
最初に接続する必要がある UI コンポーネントは、[Learn More] ボタンです。この要素 クリックすると 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 を無効にするために有効になっているサンプルタグはありますか?
- このサイトの URL をコピーできます サンプルタグ それを IMA 実装に貼り付けます
- デフォルト UI を無効にできません。
adsRenderingSettings.disableUiがtrueに設定されていることを確認してくださいgetAdsManagerに渡します。ad.isUiDisabled()を確認してください。trueを返します。また、アド マネージャーでこの機能を無効にするには、 広告 UI を表示します。有効になっている場合、VAST には次のようなExtensionが含まれます。 例: それでも問題が解決しない場合は、アカウント マネージャーに 有効にします。一部の広告タイプでは特定の UI が必要になります。これらの広告は<Extension type="uiSettings"> <UiHideable>1</UiHideable> </Extension>
<UiHideable>値は 0。これが発生した場合、入稿チームは そうした広告タイプが配信されないように変更する