Xử lý nhiều yêu cầu quảng cáo
Sử dụng bộ sưu tập để sắp xếp ngăn nắp các trang
Lưu và phân loại nội dung dựa trên lựa chọn ưu tiên của bạn.
Hầu hết các trường hợp sử dụng SDK IMA chỉ yêu cầu quản lý một yêu cầu quảng cáo tại một thời điểm. Tuy nhiên, một số trường hợp triển khai đặc biệt (chẳng hạn như tải trước dữ liệu quảng cáo trước khi người dùng chọn một video) có thể yêu cầu thực hiện nhiều yêu cầu đồng thời. Vì các yêu cầu quảng cáo được thực hiện không đồng bộ, nên việc đảm bảo liên kết đúng trình quản lý quảng cáo với đúng ngữ cảnh có vẻ như là một nhiệm vụ khó khăn.
Để đơn giản hoá quá trình phân biệt nhiều trình quản lý quảng cáo, SDK IMA cho HTML5 cho phép nhà xuất bản truyền bất kỳ giá trị hoặc đối tượng nào vào trường UserContext của bất kỳ yêu cầu quảng cáo nào. Sau đó, bạn có thể truy xuất giá trị hoặc đối tượng này trong trình xử lý AdsManagerLoadedEvent bằng cách sử dụng phương thức getUserRequestContext().
Ví dụ:
...
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);
}
...
Trừ phi có lưu ý khác, nội dung của trang này được cấp phép theo Giấy phép ghi nhận tác giả 4.0 của Creative Commons và các mẫu mã lập trình được cấp phép theo Giấy phép Apache 2.0. Để biết thông tin chi tiết, vui lòng tham khảo Chính sách trang web của Google Developers. Java là nhãn hiệu đã đăng ký của Oracle và/hoặc các đơn vị liên kết với Oracle.
Cập nhật lần gần đây nhất: 2025-08-31 UTC.
[null,null,["Cập nhật lần gần đây nhất: 2025-08-31 UTC."],[[["\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 ..."]]