Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 16 additions & 1 deletion lib/addons/abTestAssignment.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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);
}
Expand Down