Stay organized with collections
Save and categorize content based on your preferences.
Most 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.
To 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 field of any ad request. This value or object can then be retrieved in the AdsManagerLoadedEvent handler, by using the method getUserRequestContext().
Example
...adsLoader.addEventListener(google.ima.AdsManagerLoadedEvent.Type.ADS_MANAGER_LOADED,onAdsManagerLoaded,false);adsLoader.addEventListener(google.ima.AdErrorEvent.Type.AD_ERROR,onAdsManagerError,false);constcontextA={id:"Request A",element:videoElementA};constcontextB={id:"Request B",element:videoElementB}adsLoader.requestAds(adsRequestA,contextA);adsLoader.requestAds(adsRequestB,contextB);...functiononAdsManagerLoaded(adsManagerLoadedEvent){constcontext=adsManagerLoadedEvent.getUserRequestContext();adsManager=adsManagerLoadedEvent.getAdsManager(context.element);console.log("Successfully loaded ID: "+context.id);}functiononAdsManagerError(adsManagerErrorEvent){constcontext=adsManagerErrorEvent.getUserRequestContext();console.log("Error with AdRequest ID: "+context.id);}...
[[["Easy to understand","easyToUnderstand","thumb-up"],["Solved my problem","solvedMyProblem","thumb-up"],["Other","otherUp","thumb-up"]],[["Missing the information I need","missingTheInformationINeed","thumb-down"],["Too complicated / too many steps","tooComplicatedTooManySteps","thumb-down"],["Out of date","outOfDate","thumb-down"],["Samples / code issue","samplesCodeIssue","thumb-down"],["Other","otherDown","thumb-down"]],["Last updated 2025-08-28 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 ..."]]