letvideoElement;letnonceLoader;letmanagerPromise;letnonceManager;letstorageConsent=true;letplaybackStarted=false;/** * A placeholder for the publisher's own method of obtaining user * consent, either by integrating with a CMP or based on other * methods the publisher chooses to handle storage consent. * @return {boolean} Whether storage consent has been given. */functiongetConsentToStorage(){returnstorageConsent;}/** * Initializes the PAL loader. */functioninit(){constvideoElement=document.getElementById('placeholder-video');videoElement.addEventListener('mousedown',(e)=>voidonVideoTouch(e));videoElement.addEventListener('touchstart',(e)=>voidonVideoTouch(e));videoElement.addEventListener('play',()=>{if(!playbackStarted){sendPlaybackStart();playbackStarted=true;}});videoElement.addEventListener('ended',()=>voidsendPlaybackEnd());videoElement.addEventListener('error',()=>{console.log("Video error: "+videoElement.error.message);sendPlaybackEnd();});document.getElementById('generate-nonce').addEventListener('click',generateNonce);// The default value for `allowStorage` is false, but can be// changed once the appropriate consent has been gathered.constconsentSettings=newgoog.pal.ConsentSettings();consentSettings.allowStorage=getConsentToStorage();nonceLoader=newgoog.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. */functiongenerateNonce(){constrequest=newgoog.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.1request.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 */constDEFAULT_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);})
/** * Informs PAL that an ad click has occurred. How this function is * called will vary depending on your ad implementation. */functionsendAdClick(){nonceManager?.sendAdClick();}/** * Handles the user touching on the video element, passing it to PAL. * @param {!TouchEvent|!MouseEvent} touchEvent */functiononVideoTouch(touchEvent){nonceManager?.sendAdTouch(touchEvent);}/** Informs PAL that playback has started. */functionsendPlaybackStart(){nonceManager?.sendPlaybackStart();}/** Informs PAL that playback has ended. */functionsendPlaybackEnd(){nonceManager?.sendPlaybackEnd();}
[null,null,["最后更新时间 (UTC):2025-08-21。"],[[["\u003cp\u003eA nonce is an encrypted string generated by the PAL SDK's \u003ccode\u003eNonceLoader\u003c/code\u003e and is required for each new stream request, although it can be reused for multiple ad requests within the same stream.\u003c/p\u003e\n"],["\u003cp\u003eGenerating a nonce involves creating a \u003ccode\u003eNonceRequest\u003c/code\u003e object with specific parameters like ad playback details, player information, and supported API frameworks, then using the \u003ccode\u003eNonceLoader\u003c/code\u003e to create a \u003ccode\u003eNonceManager\u003c/code\u003e, which holds the generated nonce.\u003c/p\u003e\n"],["\u003cp\u003eTo use the generated nonce, it must be appended to the ad tag URL as the \u003ccode\u003egivn\u003c/code\u003e parameter before making ad requests, and it is recommended to migrate from the deprecated \u003ccode\u003epaln\u003c/code\u003e parameter to \u003ccode\u003egivn\u003c/code\u003e.\u003c/p\u003e\n"],["\u003cp\u003eImplementing event handlers such as \u003ccode\u003esendAdClick\u003c/code\u003e, \u003ccode\u003eonVideoTouch\u003c/code\u003e, \u003ccode\u003esendPlaybackStart\u003c/code\u003e, and \u003ccode\u003esendPlaybackEnd\u003c/code\u003e is essential for tracking user interactions and playback status, which PAL requires to be properly informed.\u003c/p\u003e\n"],["\u003cp\u003eThird-party ad servers can forward the nonce to Google Ad Manager by including it in their ad tag requests using the \u003ccode\u003egivn\u003c/code\u003e parameter, ensuring proper propagation of the nonce and enabling enhanced monetization through Google Ad Manager.\u003c/p\u003e\n"]]],[],null,["# Get Started\n\nGenerate a nonce\n----------------\n\nA \"nonce\" is a single encrypted string generated by PAL using the `NonceLoader`.\nThe PAL SDK requires each new stream request to be accompanied by a newly\ngenerated nonce. However, nonces may be reused for multiple ad requests within\nthe same stream. To see a sample app that uses PAL to generate a nonce, download\nthe HTML5 example from [GitHub](//github.com/googleads/googleads-pal).\n\nTo generate a nonce using the PAL SDK, create an HTML file and add the\nfollowing:\n\n#### pal.html\n\n \u003chtml\u003e\n \u003chead\u003e\u003c/head\u003e\n \u003cbody\u003e\n \u003cdiv id=\"placeholder-video\"\u003e\u003c/div\u003e\n \u003cbutton id=\"generate-nonce\"\u003eGenerate Nonce\u003c/button\u003e\n \u003cscript src=\"//imasdk.googleapis.com/pal/sdkloader/pal.js\"\u003e\u003c/script\u003e\n \u003cscript src=\"pal.js\"\u003e\u003c/script\u003e\n \u003c/body\u003e\n \u003c/html\u003e\n\nThen, create a JavaScript file and add the following:\n\n#### pal.js\n\n let videoElement;\n let nonceLoader;\n let managerPromise;\n let nonceManager;\n let storageConsent = true;\n let playbackStarted = false;\n\n /**\n * A placeholder for the publisher's own method of obtaining user\n * consent, either by integrating with a CMP or based on other\n * methods the publisher chooses to handle storage consent.\n * @return {boolean} Whether storage consent has been given.\n */\n function getConsentToStorage() {\n return storageConsent;\n }\n\n /**\n * Initializes the PAL loader.\n */\n function init() {\n const videoElement = document.getElementById('placeholder-video');\n videoElement.addEventListener('mousedown', (e) =\u003e void onVideoTouch(e));\n videoElement.addEventListener('touchstart', (e) =\u003e void onVideoTouch(e));\n videoElement.addEventListener('play', () =\u003e {\n if (!playbackStarted) {\n sendPlaybackStart();\n playbackStarted = true;\n }\n });\n videoElement.addEventListener('ended', () =\u003e void sendPlaybackEnd());\n videoElement.addEventListener('error', () =\u003e {\n console.log(\"Video error: \" + videoElement.error.message);\n sendPlaybackEnd();\n });\n\n document.getElementById('generate-nonce')\n .addEventListener('click', generateNonce);\n\n // The default value for `allowStorage` is false, but can be\n // changed once the appropriate consent has been gathered.\n const consentSettings = new goog.pal.ConsentSettings();\n consentSettings.allowStorage = getConsentToStorage();\n\n nonceLoader = new goog.pal.NonceLoader(consentSettings);\n }\n\n /**\n * Generates a nonce with sample arguments and logs it to the console.\n *\n * The NonceRequest parameters set here are example parameters.\n * You should set your parameters based on your own app characteristics.\n */\n function generateNonce() {\n const request = new goog.pal.NonceRequest();\n request.adWillAutoPlay = true;\n request.adWillPlayMuted = false;\n request.continuousPlayback = false;\n request.descriptionUrl = 'https://example.com';\n request.iconsSupported = true;\n request.playerType = 'Sample Player Type';\n request.playerVersion = '1.0';\n request.ppid = 'Sample PPID';\n request.sessionId = 'Sample SID';\n // Player support for VPAID 2.0, OMID 1.0, and SIMID 1.1\n request.supportedApiFrameworks = '2,7,9';\n request.url = 'https://developers.google.com/ad-manager/pal/html5';\n request.videoHeight = 480;\n request.videoWidth = 640;\n\n managerPromise = nonceLoader.loadNonceManager(request);\n managerPromise\n .then((manager) =\u003e {\n nonceManager = manager;\n console.log('Nonce generated: ' + manager.getNonce());\n })\n .catch((error) =\u003e {\n console.log('Error generating nonce: ' + error);\n });\n }\n\n init();\n\nAttach your nonce to the ad request\n-----------------------------------\n\nTo use the generated nonce, append your ad tag with a `givn` parameter and the\nnonce value, before making your ad requests.\n| **Caution:** If you previously provided a nonce using the `paln` parameter, it is strongly recommended to migrate to `givn` and stop sending `paln`. If both parameters are included, it is undefined behavior regarding which nonce one is used.\n\n#### pal.js\n\n /\\*\\*\n \\* The ad tag for your ad request, for example:\n \\* 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=\n \\*\n \\* For more sample ad tags, see https://developers.google.com/interactive-media-ads/docs/sdks/html5/client-side/tags\n \\*/\n const DEFAULT_AD_TAG = \"Your ad tag\";\n\n ...\n\n managerPromise = nonceLoader.loadNonceManager(request);\n managerPromise\n .then((manager) =\u003e {\n nonceManager = manager;\n console.log('Nonce generated: ' + manager.getNonce());\n // Append the nonce to the ad tag URL.\n makeAdRequest(DEFAULT_AD_TAG + \"\\&givn=\" + nonceString);\n })\n\nTrack playback events\n---------------------\n\nLastly, you need to implement various event handlers for your player. For\ntesting purposes, you can attach these to button click events, but in a real\nimplementation, these would be triggered by the appropriate player events:\n\n#### pal.js\n\n /**\n * Informs PAL that an ad click has occurred. How this function is\n * called will vary depending on your ad implementation.\n */\n function sendAdClick() {\n nonceManager?.sendAdClick();\n }\n\n /**\n * Handles the user touching on the video element, passing it to PAL.\n * @param {!TouchEvent|!MouseEvent} touchEvent\n */\n function onVideoTouch(touchEvent) {\n nonceManager?.sendAdTouch(touchEvent);\n }\n\n /** Informs PAL that playback has started. */\n function sendPlaybackStart() {\n nonceManager?.sendPlaybackStart();\n }\n\n /** Informs PAL that playback has ended. */\n function sendPlaybackEnd() {\n nonceManager?.sendPlaybackEnd();\n }\n\nIn your implementation, `sendPlaybackStart` should be called once your video\nplayback session begins. `sendPlaybackEnd` should be called once your video\nplayback session comes to an end. `sendAdClick` should be called each time the\nviewer clicks an ad. `sendAdTouch` should be called on every touch interaction\nwith the player.\n\n(Optional) Send Google Ad Manager signals through third-party ad servers\n------------------------------------------------------------------------\n\nConfigure the third-party ad server's request for Ad Manager.\n| **Note:** If you use multiple third-party solutions---for example, a third-party SSAI server calling another third-party ad server, you need to make sure the PAL SDK's encrypted nonce is forwarded to each third party.\n\nConfigure your third-party ad server to include the nonce in the server's\nrequest to Ad Manager. Here's an example of an ad tag configured inside of the\nthird-party ad server: \n\n 'https://pubads.serverside.net/gampad/ads?givn=%%custom_key_for_google_nonce%%&...'\n\nFor more details, see the [Google Ad Manager Server-side implementation\nguide](//support.google.com/admanager/answer/10668760).\n\nAd Manager looks for `givn=` to identify the nonce value. The third-party ad\nserver needs to support some macro of its own, such as\n`%%custom_key_for_google_nonce%%`, and replace it with the nonce query parameter\nyou provided in the previous step. More information on how to accomplish this\nshould be available in the third-party ad server's documentation.\n\nThat's it! You should now have the nonce parameter propagated from the PAL SDK,\nthrough your intermediary servers, and then to Google Ad Manager. This enables\nbetter monetization through Google Ad Manager."]]