產生 Nonce
「隨機碼」是 PAL 使用 NonceLoader 產生的單一加密字串。PAL SDK 要求每個新的串流請求都必須附上新產生的隨機值。不過,在同一個串流中,隨機碼可能會重複用於多個廣告請求。如要查看使用 PAL 生成 Nonce 的範例應用程式,請從 GitHub 下載 HTML5 範例。
如要使用 PAL SDK 生成隨機數,請建立 HTML 檔案並新增下列內容:
pal.html
<html>
<head></head>
<body>
<div id="placeholder-video"></div>
<button id="generate-nonce">Generate Nonce</button>
<script src="//imasdk.googleapis.com/pal/sdkloader/pal.js"></script>
<script src="pal.js"></script>
</body>
</html>
接著,建立 JavaScript 檔案並加入下列內容:
pal.js
let videoElement;
let nonceLoader;
let managerPromise;
let nonceManager;
let playbackStarted = false;
/**
* Initializes the PAL loader.
*/
function init() {
const videoElement = document.getElementById('placeholder-video');
videoElement.addEventListener('mousedown', (e) => void onVideoTouch(e));
videoElement.addEventListener('touchstart', (e) => void onVideoTouch(e));
videoElement.addEventListener('play', () => {
if (!playbackStarted) {
sendPlaybackStart();
playbackStarted = true;
}
});
videoElement.addEventListener('ended', () => void sendPlaybackEnd());
videoElement.addEventListener('error', () => {
console.log("Video error: " + videoElement.error.message);
sendPlaybackEnd();
});
document.getElementById('generate-nonce')
.addEventListener('click', generateNonce);
// PAL automatically determines whether limited ads apply based on the TCF data
// found on the page. You can optionally use the `forceLimitedAds` parameter
// to manually enable limited ads regardless of the TCF data.
const consentSettings = new goog.pal.ConsentSettings();
nonceLoader = new goog.pal.NonceLoader(consentSettings);
}
/**
* Generates a nonce with sample arguments and logs it to the console.
*
* The NonceRequest parameters set here are example parameters.
* You should set your parameters based on your own app characteristics.
*/
function generateNonce() {
const request = new goog.pal.NonceRequest();
request.adWillAutoPlay = true;
request.adWillPlayMuted = false;
request.continuousPlayback = false;
request.descriptionUrl = 'https://example.com';
request.iconsSupported = true;
request.playerType = 'Sample Player Type';
request.playerVersion = '1.0';
request.ppid = 'Sample PPID';
request.sessionId = 'Sample SID';
// Player support for VPAID 2.0, OMID 1.0, and SIMID 1.1
request.supportedApiFrameworks = '2,7,9';
request.url = 'https://developers.google.com/ad-manager/pal/html5';
request.videoHeight = 480;
request.videoWidth = 640;
managerPromise = nonceLoader.loadNonceManager(request);
managerPromise
.then((manager) => {
nonceManager = manager;
console.log('Nonce generated: ' + manager.getNonce());
})
.catch((error) => {
console.log('Error generating nonce: ' + error);
});
}
init();
將 Nonce 附加至廣告請求
如要使用產生的隨機值,請在發送廣告請求前,將 givn 參數和隨機值附加至廣告代碼。
pal.js
/**
* The ad tag for your ad request, for example:
* https://pubads.g.doubleclick.net/gampad/ads?sz=640x480&iu=/124319096/external/single_ad_samples&ciu_szs=300x250&impl=s&gdfp_req=1&env=vp&output=vast&unviewed_position_start=1&cust_params=deployment%3Ddevsite%26sample_ct%3Dlinear&correlator=
*
* For more sample ad tags, see https://developers.google.com/interactive-media-ads/docs/sdks/html5/client-side/tags
*/
const DEFAULT_AD_TAG = "Your ad tag";
...
managerPromise = nonceLoader.loadNonceManager(request);
managerPromise
.then((manager) => {
nonceManager = manager;
console.log('Nonce generated: ' + manager.getNonce());
// Append the nonce to the ad tag URL.
makeAdRequest(DEFAULT_AD_TAG + "&givn=" + nonceString);
})
如果您向 Google 請求並放送廣告,就必須放送 AdChoices 圖示和疊加層。如要進一步瞭解如何剖析 VAST 回應及顯示圖示,請參閱「AdChoices 圖示和疊加層」。
追蹤播放事件
最後,您需要為播放器導入各種事件處理常式。為了測試,您可以將這些事件附加至按鈕點擊事件,但在實際導入時,這些事件會由適當的播放器事件觸發:
pal.js
/**
* Informs PAL that an ad click has occurred. How this function is
* called will vary depending on your ad implementation.
*/
function sendAdClick() {
nonceManager?.sendAdClick();
}
/**
* Handles the user touching on the video element, passing it to PAL.
* @param {!TouchEvent|!MouseEvent} touchEvent
*/
function onVideoTouch(touchEvent) {
nonceManager?.sendAdTouch(touchEvent);
}
/** Informs PAL that playback has started. */
function sendPlaybackStart() {
nonceManager?.sendPlaybackStart();
}
/** Informs PAL that playback has ended. */
function sendPlaybackEnd() {
nonceManager?.sendPlaybackEnd();
}
在實作中,影片播放工作階段開始後,應呼叫 sendPlaybackStart 一次。影片播放工作階段結束後,請呼叫 sendPlaybackEnd。每當觀眾點按廣告時,都應呼叫 sendAdClick。每次與播放器進行觸控互動時,都應呼叫 sendAdTouch。
(選用) 透過第三方廣告伺服器傳送 Google Ad Manager 信號
設定第三方廣告伺服器對 Ad Manager 的請求。
設定第三方廣告伺服器,在伺服器向 Ad Manager 發出的請求中加入隨機碼。以下是在第三方廣告伺服器中設定的廣告代碼範例:
'https://pubads.serverside.net/gampad/ads?givn=%%custom_key_for_google_nonce%%&...'
詳情請參閱 Google Ad Manager 伺服器端導入指南。
Ad Manager 會尋找 givn=,以找出隨機碼值。第三方廣告伺服器必須支援自己的巨集 (例如 %%custom_key_for_google_nonce%%),並將其替換為您在上一個步驟中提供的隨機值查詢參數。如要進一步瞭解如何完成這項操作,請參閱第三方廣告伺服器的說明文件。
大功告成!您現在應該已透過中繼伺服器,將 Nonce 參數從 PAL SDK 傳播至 Google Ad Manager。這有助於透過 Google Ad Manager 提高營利。