शुरू करें

ज़रूरी शर्तें

Cast के लिए PAL SDK को इंटिग्रेट और टेस्ट करने के लिए, आपको इनकी ज़रूरत होगी:

PAL SDK को इंटिग्रेट करने के लिए, आपको सिर्फ़ अपने रिसीवर ऐप्लिकेशन को अपडेट करना होगा. इसलिए, अपने रिसीवर की जांच करने के लिए, वेब सेंडर के तौर पर Cast Command and Control (CAC) टूल का इस्तेमाल किया जा सकता है.

हर चरण के आखिर में सैंपल चलाया जा सकता है. इसके लिए, सबसे पहले CAC टूल में वेब रिसीवर ऐप्लिकेशन लॉन्च करें. इसके बाद, कोई लोड करने का अनुरोध करें.

Generate nonce

"नॉन्स" एक एन्क्रिप्ट (सुरक्षित) की गई स्ट्रिंग होती है. इसे PAL, NonceManager के ज़रिए जनरेट करता है. NonceManager को NonceLoader के loadNonceManager तरीके से जनरेट किया जाता है. यह NonceRequest में पास की गई सेटिंग पर आधारित होता है. PAL का इस्तेमाल करके नॉनस जनरेट करने वाला सैंपल ऐप्लिकेशन देखने के लिए, GitHub से Cast का उदाहरण डाउनलोड करें.

स्ट्रीम करने के हर नए अनुरोध के लिए, एक नया नॉनस ज़रूरी होता है. एक ही स्ट्रीम में मौजूद कई विज्ञापन अनुरोधों के लिए, एक ही नॉन्स का इस्तेमाल किया जा सकता है. PAL SDK टूल का इस्तेमाल करके, नॉनस जनरेट करने के लिए, सबसे पहले पसंद के मुताबिक बनाया गया वेब रिसीवर ऐप्लिकेशन बनाएं और यह कोड जोड़ें:

receiver.html

<!DOCTYPE html>
<html>
<head>
  <script src="//www.gstatic.com/cast/sdk/libs/caf_receiver/v3/cast_receiver_framework.js"></script>
  <script src="//imasdk.googleapis.com/pal/sdkloader/cast_pal.js"></script>
</head>
<body>
  <cast-media-player></cast-media-player>
  <footer>
    <script src="js/receiver.js" type="module"></script>
  </footer>
</body>
</html>

<cast-media-player> एलिमेंट, Cast Web Receiver API की ओर से उपलब्ध कराया गया, पहले से मौजूद प्लेयर यूज़र इंटरफ़ेस (यूआई) दिखाता है. स्ट्रीम के टाइप के हिसाब से, इस्तेमाल किया गया प्लेयर अलग-अलग हो सकता है. इन प्लेयर के सटीक वर्शन, Google Cast SDK के रिलीज़ नोट में देखे जा सकते हैं.

इसके बाद, LOAD इवेंट को इंटरसेप्ट करने के लिए, यह कोड जोड़ें. साथ ही, हर बार जब रिसीवर कोई नया MediaInformation ऑब्जेक्ट लोड करता है, तब एक नॉनस जनरेट करें:

js/receiver.js

const castContext = cast.framework.CastReceiverContext.getInstance();
const playerManager = castContext.getPlayerManager();

const consentSettings = new goog.cast.pal.ConsentSettings();
// For the correct usage of the allowStorage property, See
// developers.google.com/ad-manager/pal/cast/reference/js/ConsentSettings#allowStorage.
consentSettings.allowStorage = true;

// You need a nonce loader to request your stream's nonceManager. The
// nonceManager provides your nonce. You should reuse the same nonce loader for
// the entire lifecycle of the receiver.
const nonceLoader = new goog.cast.pal.NonceLoader(consentSettings);

// You need a reference to the NonceManager to track when an ad is shown or
// clicked.
let nonceManager;

/**
 * Sends a debug message to the CAF sender.
 *
 * @param {String} message - The message to send
 */
const log = (message) => {
  // Use CastDebugLogger to log a message to the sender. See
  // https://developers.google.com/cast/docs/debugging/cast_debug_logger.
}

/**
 * Stores the nonce manager in the outer scoped variable and retrieves a nonce,
 * so it can be used to build your ad request URL
 *
 * @param {NonceManager} loadedNonceManager - The loaded nonce manager
 */
const buildAdRequest = (loadedNonceManager) => {
  nonceManager = loadedNonceManager;

  const nonce = nonceManager.getNonce();
  log('received nonce:' + nonce);

  // TODO: Set this nonce as the value for the `givn` parameter of your ad
  // request URL. For example:
  // const adRequestURL = 'https://myadserver.com/ads?givn=' + nonce;
}

/**
 * Configures a new nonce request, then requests a nonce.
 *
 * @param {LoadRequestData} loadRequestData - the load request object,
 * which contains the MediaInformation object from the sender. See
 * developers.google.com/cast/docs/reference/web_receiver/cast.framework.messages.LoadRequestData
 * @return {(Promise<LoadRequestData>)} - A Promise to build an ad request.
 */
const handleLoadRequest = (loadRequestData) => {
  // Clear any old nonceManager before loading new media.
  nonceManager = null;

  // See developers.google.com/ad-manager/pal/cast/reference/js/NonceRequest
  // for details about each property. The NonceRequest parameters set here are
  // example parameters. You should set your parameters based on your own app
  // characteristics.
  const nonceRequest = new goog.cast.pal.NonceRequest();
  nonceRequest.adWillAutoPlay = true;
  // A URL describing the video stream.
  nonceRequest.descriptionUrl = 'https://example.com';
  nonceRequest.iconsSupported = true;
  nonceRequest.ppid = 'Sample PPID';
  nonceRequest.sessionId = 'Sample SID';
  nonceRequest.url = loadRequestData.media.contentUrl;
  // The height of the player in physical pixels.
  // For a fullscreen player on a 1080p screen, the video height would be 1080.
  nonceRequest.videoHeight = window.devicePixelRatio * window.screen.height;
  // The width of the player in physical pixels.
  // For a fullscreen player on a 1080p screen, the video width would be 1920.
  nonceRequest.videoWidth = window.devicePixelRatio * window.screen.width;

  return nonceLoader.loadNonceManager(nonceRequest)
    .then(buildAdRequest)
    .catch((e) => {
      log("Error: " + e.message);
    });
};

// Set up the event handler for the LOAD event type.
playerManager.setMessageInterceptor(cast.framework.messages.MessageType.LOAD, handleLoadRequest);

castContext.start();

डाइरेक्ट वीएएसटी कॉल (डीवीसी) करते समय, इस नॉनस को givn पैरामीटर की वैल्यू के तौर पर सेट करें. नॉन्स, यूआरएल के लिए सुरक्षित होता है. इसलिए, आपको इसके डेटा को कोड में बदलने की ज़रूरत नहीं है.

अगर Google से विज्ञापनों का अनुरोध किया जा रहा है और उन्हें दिखाया जा रहा है, तो आपको AdChoices आइकॉन और ओवरले दिखाना होगा. वीएएसटी रिस्पॉन्स को पार्स करने और आइकॉन रेंडर करने के बारे में ज़्यादा जानने के लिए, AdChoices आइकॉन और ओवरले देखें.

वीडियो इंटरैक्शन ट्रैक करना

नॉनस जनरेट करने के साथ-साथ, PAL SDK को वीडियो के कुछ इंटरैक्शन के बारे में सूचना देनी होती है. कास्ट रिसीवर के साथ इंटरैक्शन को ट्रैक करने के लिए, अपने कस्टम रिसीवर में यह कोड जोड़ें:

js/receiver.js

const castContext = cast.framework.CastReceiverContext.getInstance();
const playerManager = castContext.getPlayerManager();

const consentSettings = new goog.cast.pal.ConsentSettings();
// For the correct usage of the allowStorage property, See
// developers.google.com/ad-manager/pal/cast/reference/js/ConsentSettings#allowStorage.
consentSettings.allowStorage = true;

// You need a nonce loader to request your stream's nonceManager. The
// nonceManager provides your nonce. You should reuse the same nonce loader for
// the entire lifecycle of the receiver.
const nonceLoader = new goog.cast.pal.NonceLoader(consentSettings);

// You need a reference to the NonceManager for sending ad events.
let nonceManager;

// Track playback status.
let playbackDidStart = false;

...

// Register the start of playback.
playerManager.addEventListener(cast.framework.events.EventType.PLAYING, () => {
  if (playbackDidStart) return;

  playbackDidStart = true;
  if (nonceManager) {
    log('Registered playback start');
    nonceManager.sendPlaybackStart();
  } else {
    log("Error: There is no nonce manager for this media.");
  }
});

// Register any interactions with the player.
const interactionEvents = [
  cast.framework.events.EventType.REQUEST_SEEK,
  cast.framework.events.EventType.REQUEST_STOP,
  cast.framework.events.EventType.REQUEST_PAUSE,
  cast.framework.events.EventType.REQUEST_PLAY,
  cast.framework.events.EventType.REQUEST_SKIP_AD,
  cast.framework.events.EventType.REQUEST_PLAY_AGAIN,
  cast.framework.events.EventType.REQUEST_PLAYBACK_RATE_CHANGE,
  cast.framework.events.EventType.REQUEST_VOLUME_CHANGE,
  cast.framework.events.EventType.REQUEST_USER_ACTION,
  cast.framework.events.EventType.REQUEST_FOCUS_STATE,
];
playerManager.addEventListener(interactionEvents, (interactionEvent) => {
  if (nonceManager) {
    log('Registered interaction: ' + interactionEvent);
    nonceManager.sendAdTouch(interactionEvent);
  } else {
    log("Error: There is no nonce manager for this media.");
  }
});

// Register the end of playback.
playerManager.addEventListener(cast.framework.events.EventType.MEDIA_FINISHED, () => {
  playbackDidStart = false;
  if (nonceManager) {
    log('Registered playback end');
    nonceManager.sendPlaybackEnd();
  } else {
    log("Error: There is no nonce manager for this media.");
  }
});

castContext.start();

(ज़रूरी नहीं) तीसरे पक्ष के विज्ञापन सर्वर के ज़रिए, Google Ad Manager के सिग्नल भेजना

Ad Manager के लिए, तीसरे पक्ष के विज्ञापन सर्वर के अनुरोध को कॉन्फ़िगर करें. यहां दिया गया तरीका पूरा करने के बाद, नॉनस पैरामीटर PAL SDK से आपके इंटरमीडियरी सर्वर और फिर Google Ad Manager तक पहुंचता है. इससे Google 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%%. साथ ही, इसे पिछले चरण में दिए गए नॉन्स क्वेरी पैरामीटर से बदलना होगा. इसे पूरा करने के तरीके के बारे में ज़्यादा जानकारी, तीसरे पक्ष के विज्ञापन सर्वर के दस्तावेज़ में उपलब्ध होनी चाहिए.