טעינת מודעות לפני סרטון (pre-roll) לשידור חי

הפלטפורמה: HTML5 Android iOS tvOS

אפשר להשתמש ב-IMA SDK כדי לייצר הכנסות משידורים חיים ומסרטונים על פי דרישה (VOD). בשידורים חיים, צריך לשלוח בקשה חדשה להצגת מודעה בכל הפסקה מסחרית. כדי למנוע מצב שבו כל הצופים שולחים בקשות להצגת מודעות באותו הזמן וגורמים לעומס על שרת או שרתים של מודעות, כדאי לשלוח את הבקשות האלה בזמנים שונים.

כדי לעזור בכך, ב-IMA SDK יש את המאפיין AdsRequest.liveStreamPrefetchSeconds. המאפיין הזה מציין את מספר השניות המקסימלי שערכת ה-SDK צריכה להמתין לפני שהיא פונה לשרת המודעות אחרי שקוראים ל-AdsLoader.requestAds(). זמן הבקשה בפועל יהיה אקראי. לדוגמה, אם מגדירים את AdsRequest.liveStreamPrefetchSeconds ל-30, ה-SDK ממתין בין 0 ל-30 שניות אחרי שקוראים ל-AdsLoader.requestAds() כדי לשלוח את הבקשה לשרת.

טעינה מראש של שידור חי בפועל

מומלץ לבצע אחזור מראש של הפסקת הפרסומות הבאה מיד אחרי סיום הפסקת הפרסומות הנוכחית. כך אפשר להבטיח שחלון האחזור מראש יהיה ארוך ככל האפשר. נניח שהגדרתם 5 דקות בין הפסקות הפרסום. כשמסיימים הפסקה למודעה, אפשר לשלוח בקשה להפסקה הבאה למודעה עם חלון אחזור מראש של 290 שניות (5 דקות פחות 10 שניות, כדי לוודא שהבקשות שנשלחות בסוף חלון האחזור מראש יטופלו בזמן):

// 5 minutes == 300 seconds. Include a 10 second buffer
var AD_INTERVAL = 290;

function onAdEvent(adEvent) {
  var ad = adEvent.getAd();
  switch(adEvent.type) {
    case google.ima.AdEvent.Type.ALL_ADS_COMPLETED:
      // Pre-fetch our next ad break.
      requestAds();
      // Play those ads in 5 minutes. In a real-world implementation,
      // this is likely done as the result of a message from your
      // streaming server, not a timeout.
      setTimeout(playAds, AD_INTERVAL * 1000);// Convert to ms.
  }
}

function requestAds() {
  // Destroy the current AdsManager, in case the tag you requested previously
  // contains post-rolls (don't play those now).
  if (adsManager) {
    adsManager.destroy();
  }
  // Your AdsLoader will be set up on page-load. You should re-use the same
  // AdsLoader for every request. For more info on setting up the AdsLoader,
  // see the IMA HTML5 client-side "Set up the IMA SDK" guide:
  // https://developers.google.com/interactive-media-ads/docs/sdks/html5/client-side/get-started
  if (adsLoader) {
    // Reset the IMA SDK.
    adsLoader.contentComplete();
  }
  var adsRequest = new google.ima.AdsRequest();
  adsRequest.adTagUrl = '...';
  adsRequest.linearAdSlotWidth = <linear_width>;
  adsRequest.linearAdSlotHeight = <linear_height>;
  adsRequest.nonLinearAdSlotWidth = <nonlinear_width>;
  adsRequest.nonLinearAdSlotHeight = <nonlinear_height>;
  adsRequest.liveStreamPrefechSeconds = AD_INTERVAL;
  adsLoader.requestAds(adsRequest);
}

function playAds() {
  adsManager.init(<linear_width>,  <linear_height>);
  adsManager.start();
}