יצירת מספר חד-פעמי
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 ועיבוד סמלים מופיעים במאמר בנושא הסמל של 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 ברגע שהתחילה הפעלת הסרטון. צריך להפעיל את 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 אמור להתפשט מ-PAL SDK, דרך השרתים המתווכים שלכם, ואז אל Google Ad Manager. כך אפשר לשפר את המונטיזציה באמצעות Google Ad Manager.