生成 Nonce
“nonce”是 PAL 使用 NonceLoader 生成的单个加密字符串。PAL SDK 要求每个新的流请求都附带新生成的随机数。不过,在同一视频流中,随机数可能会重复用于多个广告请求。如需查看使用 PAL 生成 Nonce 的示例应用,请从 GitHub 下载 HTML5 示例。
如需使用 PAL SDK 生成 Nonce,请创建一个 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 响应和呈现图标,请参阅“广告选项”图标和叠加层。
跟踪播放事件
最后,您需要为播放器实现各种事件处理程序。出于测试目的,您可以将这些附加到按钮点击事件,但在实际实现中,这些将由相应的播放器事件触发:
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%%),并将其替换为您在上一步中提供的随机数查询参数。如需详细了解如何实现此目的,请参阅第三方广告服务器的文档。
大功告成!现在,您应该已将随机数参数从 PAL SDK 通过中介服务器传播到 Google Ad Manager。这样一来,您就可以通过 Google Ad Manager 更好地创收。