เริ่มต้นใช้งาน

สร้าง Nonce

"Nonce" คือสตริงที่เข้ารหัสรายการเดียวซึ่ง PAL สร้างขึ้นโดยใช้ NonceLoader PAL SDK กำหนดให้คำขอสตรีมใหม่แต่ละรายการต้องมาพร้อมกับ Nonce ที่สร้างขึ้นใหม่ อย่างไรก็ตาม สามารถนำ Nonce ไปใช้ซ้ำกับคำขอโฆษณาหลายรายการภายในสตรีมเดียวกันได้ หากต้องการดูแอปตัวอย่างที่ใช้ PAL เพื่อสร้าง Nonce ให้ดาวน์โหลด ตัวอย่าง HTML5 จาก GitHub

หากต้องการสร้าง Nonce โดยใช้ 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 กับคำขอโฆษณา

หากต้องการใช้ 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);
      })

หากขอและแสดงโฆษณาจาก 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

กำหนดค่าเซิร์ฟเวอร์โฆษณาบุคคลที่สามให้รวม Nonce ไว้ในคำขอของเซิร์ฟเวอร์ที่ส่งไปยัง 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 ที่คุณระบุในขั้นตอนก่อนหน้า ดูข้อมูลเพิ่มเติมเกี่ยวกับวิธีดำเนินการนี้ได้ในเอกสารประกอบของเซิร์ฟเวอร์โฆษณาของบุคคลที่สาม

เท่านี้ก็เรียบร้อย ตอนนี้คุณควรมีพารามิเตอร์ Nonce ที่ส่งต่อจาก PAL SDK ผ่านเซิร์ฟเวอร์ตัวกลาง แล้วไปยัง Google Ad Manager ซึ่งจะช่วยให้สร้างรายได้ได้ดีขึ้นผ่าน Google Ad Manager