生成 Nonce
“Nonce”是由 PAL 使用 NonceLoader 生成的单个加密字符串。
PAL SDK 要求每个新的视频流请求附带新的
生成的 Nonce。不过,Nonce 可以重复用于
同一直播如需查看使用 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 storageConsent = true;
let playbackStarted = false;
/**
 * A placeholder for the publisher's own method of obtaining user
 * consent, either by integrating with a CMP or based on other
 * methods the publisher chooses to handle storage consent.
 * @return {boolean} Whether storage consent has been given.
 */
function getConsentToStorage() {
  return storageConsent;
}
/**
 * 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);
  // The default value for `allowStorage` is false, but can be
  // changed once the appropriate consent has been gathered.
  const consentSettings = new goog.pal.ConsentSettings();
  consentSettings.allowStorage = getConsentToStorage();
  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 附加到广告请求中
要使用生成的 Nonce,请在广告代码中附加 givn 参数和
Nonce 值。
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);
      })
跟踪播放事件
最后,您需要为播放器实现各种事件处理脚本。对于 您可以将这些对象附加到按钮点击事件中, 将由相应的播放器事件触发:
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= 以标识 Nonce 值。第三方广告
服务器需要支持自己的某个宏,例如
%%custom_key_for_google_nonce%%,并将其替换为 Nonce 查询参数
您在上一步中提供的 ID。详细了解如何实现这一目标
。
大功告成!现在,您应该已经从 PAL SDK 传播了 Nonce 参数, 然后将数据传输到 Google Ad Manager这样一来, 获得更高收益