处理多个广告请求
使用集合让一切井井有条
根据您的偏好保存内容并对其进行分类。
在大多数情况下,使用 IMA SDK 时只需同时管理一个广告请求。不过,某些极端情况的实现(例如在用户选择视频之前预加载广告数据)可能需要发出多个并发请求。由于广告请求是异步发出的,因此确保将适当的广告管理平台与正确的上下文相关联可能看起来是一项艰巨的任务。
为了简化区分多个广告管理平台的流程,适用于 HTML5 的 IMA SDK 允许发布商向任何广告请求的 UserContext 字段传递任何值或对象。然后,您可以在 AdsManagerLoadedEvent 处理程序中使用 getUserRequestContext() 方法检索此值或对象。
示例
...
adsLoader.addEventListener(
google.ima.AdsManagerLoadedEvent.Type.ADS_MANAGER_LOADED,
onAdsManagerLoaded,
false);
adsLoader.addEventListener(
google.ima.AdErrorEvent.Type.AD_ERROR,
onAdsManagerError,
false);
const contextA = {id: "Request A", element: videoElementA};
const contextB = {id: "Request B", element: videoElementB}
adsLoader.requestAds(adsRequestA, contextA);
adsLoader.requestAds(adsRequestB, contextB);
...
function onAdsManagerLoaded(adsManagerLoadedEvent) {
const context = adsManagerLoadedEvent.getUserRequestContext();
adsManager = adsManagerLoadedEvent.getAdsManager(context.element);
console.log("Successfully loaded ID: " + context.id);
}
function onAdsManagerError(adsManagerErrorEvent) {
const context = adsManagerErrorEvent.getUserRequestContext();
console.log("Error with AdRequest ID: " + context.id);
}
...
如未另行说明,那么本页面中的内容已根据知识共享署名 4.0 许可获得了许可,并且代码示例已根据 Apache 2.0 许可获得了许可。有关详情,请参阅 Google 开发者网站政策。Java 是 Oracle 和/或其关联公司的注册商标。
最后更新时间 (UTC):2025-08-31。
[null,null,["最后更新时间 (UTC):2025-08-31。"],[[["\u003cp\u003eThe IMA SDK allows for multiple concurrent ad requests, useful for preloading ads or managing multiple video players.\u003c/p\u003e\n"],["\u003cp\u003eDevelopers can use the \u003ccode\u003eUserContext\u003c/code\u003e field to associate a custom object with each ad request for easy identification.\u003c/p\u003e\n"],["\u003cp\u003eThis object can be retrieved later using \u003ccode\u003egetUserRequestContext()\u003c/code\u003e in the \u003ccode\u003eAdsManagerLoadedEvent\u003c/code\u003e and \u003ccode\u003eAdErrorEvent\u003c/code\u003e handlers.\u003c/p\u003e\n"],["\u003cp\u003eThis approach simplifies associating the correct AdsManager with its corresponding video element and context.\u003c/p\u003e\n"]]],[],null,["# Handle multiple ad requests\n\nMost uses of the IMA SDK only require managing a single ad request at a time. However some edge case implementations, such as preloading ad data before the user selects a video, may require making multiple concurrent requests. Since ad requests are made asynchronously, ensuring the proper ad manager is associated with the correct context can seem to be a daunting task.\n\nTo simplify the process of differentiating multiple ad managers, the IMA SDK for HTML5 allows publishers to pass in any value or object to the [UserContext](https://developers.google.com/interactive-media-ads/docs/sdks/html5/client-side/reference/js/google.ima.AdsLoader#requestAds) field of any ad request. This value or object can then be retrieved in the [AdsManagerLoadedEvent](https://developers.google.com/interactive-media-ads/docs/sdks/html5/client-side/reference/js/google.ima.AdsManagerLoadedEvent) handler, by using the method [getUserRequestContext()](https://developers.google.com/interactive-media-ads/docs/sdks/html5/client-side/reference/js/google.ima.AdsManagerLoadedEvent#getUserRequestContext).\n\nExample\n-------\n\n ...\n adsLoader.addEventListener(\n google.ima.AdsManagerLoadedEvent.Type.ADS_MANAGER_LOADED,\n onAdsManagerLoaded,\n false);\n adsLoader.addEventListener(\n google.ima.AdErrorEvent.Type.AD_ERROR,\n onAdsManagerError,\n false);\n const contextA = {id: \"Request A\", element: videoElementA};\n const contextB = {id: \"Request B\", element: videoElementB}\n adsLoader.requestAds(adsRequestA, contextA);\n adsLoader.requestAds(adsRequestB, contextB);\n ...\n\n function onAdsManagerLoaded(adsManagerLoadedEvent) {\n const context = adsManagerLoadedEvent.getUserRequestContext();\n adsManager = adsManagerLoadedEvent.getAdsManager(context.element);\n console.log(\"Successfully loaded ID: \" + context.id);\n }\n\n function onAdsManagerError(adsManagerErrorEvent) {\n const context = adsManagerErrorEvent.getUserRequestContext();\n console.log(\"Error with AdRequest ID: \" + context.id);\n }\n ..."]]