From 9708438ff7c6c1329e14c683a2b3aacd2b803813 Mon Sep 17 00:00:00 2001 From: Jean-Philippe Laroche Date: Wed, 5 Aug 2026 13:08:56 -0400 Subject: [PATCH] abTestAssignment: defer setHooks onto pbjs.que when Prebid not ready Mirror OptablePrebidAnalytics.hookIntoPrebid: setupAB's setHooks now checks whether the Prebid instance is ready (onEvent is a function) and, if not, queues hook registration onto pbjs.que instead of calling getEvents()/onEvent() directly. Lets callers pass a queue-only stub ({ que: [] }) safely; previously that threw. --- lib/addons/abTestAssignment.ts | 17 ++++++++++++++++- 1 file changed, 16 insertions(+), 1 deletion(-) diff --git a/lib/addons/abTestAssignment.ts b/lib/addons/abTestAssignment.ts index 8ec63c8..1a4080b 100644 --- a/lib/addons/abTestAssignment.ts +++ b/lib/addons/abTestAssignment.ts @@ -134,7 +134,7 @@ export function setupAB(config: SetupABConfig): ABTestSetupResult { }); } - function setHooks(pbjsInstance: any): void { + function registerHooks(pbjsInstance: any): void { pbjsInstance.getEvents().forEach((event: any) => { if (event.eventType === "auctionEnd") { applyToAuctionEvent(event.args); @@ -143,6 +143,21 @@ export function setupAB(config: SetupABConfig): ABTestSetupResult { pbjsInstance.onEvent("auctionEnd", applyToAuctionEvent); } + // Mirrors OptablePrebidAnalytics.hookIntoPrebid: when Prebid.js has not + // finished loading yet (`onEvent` is not a function), defer registration onto + // its command queue so hooks attach once the real global drains `que`. + // This lets callers pass a queue-only stub (`{ que: [] }`) safely instead of + // having to queue `setHooks` themselves. + function setHooks(pbjsInstance: any): void { + if (!pbjsInstance) return; + if (typeof pbjsInstance.onEvent !== "function") { + pbjsInstance.que = pbjsInstance.que || []; + pbjsInstance.que.push(() => registerHooks(pbjsInstance)); + } else { + registerHooks(pbjsInstance); + } + } + if (pbjs) { setHooks(pbjs); }