開始する

ノンスを生成する

「ノンス」は、PAL が NonceLoader を使用して生成する単一の暗号化された文字列です。PAL SDK では、新しいストリーム リクエストごとに、新しく生成されたノンスを付加する必要があります。ただし、同じストリーム内の複数の広告リクエストで 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 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();

ノンスを広告リクエストに添付する

生成されたノンスを使用するには、広告リクエストを行う前に、広告タグに 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 を 1 回呼び出す必要があります。sendPlaybackEnd は、動画再生セッションが終了したときに呼び出す必要があります。sendAdClick は、視聴者が広告をクリックするたびに呼び出す必要があります。sendAdTouch は、プレーヤーとのタッチ操作ごとに呼び出す必要があります。

(省略可)第三者広告サーバー経由で Google アド マネージャーのシグナルを送信する

アド マネージャーに対するサードパーティ広告サーバーのリクエストを設定します。

アド マネージャーへのサーバーのリクエストに nonce を含めるように、第三者広告サーバーを設定します。以下に、第三者広告サーバー内で構成された広告タグの例を示します。

'https://pubads.serverside.net/gampad/ads?givn=%%custom_key_for_google_nonce%%&...'

詳しくは、Google アド マネージャーのサーバーサイド実装ガイドをご覧ください。

アド マネージャーは、givn= を探して nonce 値を特定します。第三者広告サーバーは、%%custom_key_for_google_nonce%% などの独自のマクロをサポートし、前の手順で指定した nonce クエリ パラメータに置き換える必要があります。この方法について詳しくは、サードパーティ広告サーバーのドキュメントをご覧ください。

これで、これで、PAL SDK から仲介サーバーを経由して Google アド マネージャーに nonce パラメータが伝播されるようになります。これにより、Google アド マネージャーを通じて収益化を促進できます。