@bidspark/analytics 0.45.2 → 0.46.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +29 -0
- package/dist/analytics.global.js +1 -1
- package/dist/index.d.mts +20 -5
- package/dist/index.d.ts +20 -5
- package/dist/index.js +136 -159
- package/dist/index.mjs +135 -159
- package/package.json +1 -1
package/dist/index.mjs
CHANGED
|
@@ -27,18 +27,6 @@ var COLORS = {
|
|
|
27
27
|
eventError: "color: #ef4444; font-weight: bold",
|
|
28
28
|
reset: "color: inherit"
|
|
29
29
|
};
|
|
30
|
-
var logCounter = 0;
|
|
31
|
-
var localGlobalListeners = /* @__PURE__ */ new Set();
|
|
32
|
-
function getGlobalListeners() {
|
|
33
|
-
if (typeof window !== "undefined") {
|
|
34
|
-
const win = window;
|
|
35
|
-
if (!win.__bidsparkLogListeners) {
|
|
36
|
-
win.__bidsparkLogListeners = /* @__PURE__ */ new Set();
|
|
37
|
-
}
|
|
38
|
-
return win.__bidsparkLogListeners;
|
|
39
|
-
}
|
|
40
|
-
return localGlobalListeners;
|
|
41
|
-
}
|
|
42
30
|
function formatTime(ms) {
|
|
43
31
|
if (ms < 0 || !Number.isFinite(ms)) ms = 0;
|
|
44
32
|
if (ms < 1e3) return `${ms.toFixed(2)}ms`;
|
|
@@ -64,15 +52,11 @@ var Logger = class {
|
|
|
64
52
|
_loadedAt;
|
|
65
53
|
_now;
|
|
66
54
|
_console;
|
|
67
|
-
_listeners = /* @__PURE__ */ new Set();
|
|
68
55
|
constructor(options = {}) {
|
|
69
56
|
this._prefix = options.prefix ?? "[ads]";
|
|
70
57
|
this._level = normalizeLogLevel(options.logLevel ?? LogLevel.INFO);
|
|
71
58
|
this._now = options.now;
|
|
72
59
|
this._console = options.console;
|
|
73
|
-
if (options.onLog) {
|
|
74
|
-
this._listeners.add(options.onLog);
|
|
75
|
-
}
|
|
76
60
|
this._loadedAt = options.loadedAt ?? (this._now ? this._now() : typeof performance !== "undefined" && typeof performance.now === "function" ? performance.now() : Date.now());
|
|
77
61
|
}
|
|
78
62
|
get prefix() {
|
|
@@ -105,15 +89,6 @@ var Logger = class {
|
|
|
105
89
|
getLoadedAt() {
|
|
106
90
|
return this._loadedAt;
|
|
107
91
|
}
|
|
108
|
-
addListener(listener) {
|
|
109
|
-
this._listeners.add(listener);
|
|
110
|
-
return () => {
|
|
111
|
-
this._listeners.delete(listener);
|
|
112
|
-
};
|
|
113
|
-
}
|
|
114
|
-
removeListener(listener) {
|
|
115
|
-
this._listeners.delete(listener);
|
|
116
|
-
}
|
|
117
92
|
isEnabled(level) {
|
|
118
93
|
const target = normalizeLogLevel(level);
|
|
119
94
|
return (LOG_LEVEL_MAP[target] ?? 20) >= (LOG_LEVEL_MAP[this._level] ?? 20);
|
|
@@ -127,23 +102,6 @@ var Logger = class {
|
|
|
127
102
|
const current = this._now ? this._now() : typeof performance !== "undefined" && typeof performance.now === "function" ? performance.now() : Date.now();
|
|
128
103
|
return Math.max(0, current - this._loadedAt);
|
|
129
104
|
}
|
|
130
|
-
_dispatchRecord(record) {
|
|
131
|
-
for (const l of this._listeners) {
|
|
132
|
-
try {
|
|
133
|
-
l(record);
|
|
134
|
-
} catch {
|
|
135
|
-
}
|
|
136
|
-
}
|
|
137
|
-
const globalListeners = getGlobalListeners();
|
|
138
|
-
for (const gl of globalListeners) {
|
|
139
|
-
if (!this._listeners.has(gl)) {
|
|
140
|
-
try {
|
|
141
|
-
gl(record);
|
|
142
|
-
} catch {
|
|
143
|
-
}
|
|
144
|
-
}
|
|
145
|
-
}
|
|
146
|
-
}
|
|
147
105
|
log(level, nameOrMsg, ...args) {
|
|
148
106
|
const normLevel = normalizeLogLevel(level);
|
|
149
107
|
if (!this.isEnabled(normLevel)) {
|
|
@@ -151,19 +109,6 @@ var Logger = class {
|
|
|
151
109
|
}
|
|
152
110
|
const elapsed = this._getElapsed();
|
|
153
111
|
const formattedTimeStr = formatTime(elapsed);
|
|
154
|
-
const nowEpoch = Date.now();
|
|
155
|
-
const recordId = `bk_log_${nowEpoch}_${++logCounter}`;
|
|
156
|
-
const record = {
|
|
157
|
-
id: recordId,
|
|
158
|
-
timestamp: nowEpoch,
|
|
159
|
-
elapsedMs: elapsed,
|
|
160
|
-
level: normLevel,
|
|
161
|
-
prefix: this._prefix,
|
|
162
|
-
message: nameOrMsg,
|
|
163
|
-
args,
|
|
164
|
-
data: args.length === 1 ? args[0] : args.length > 1 ? args : void 0
|
|
165
|
-
};
|
|
166
|
-
this._dispatchRecord(record);
|
|
167
112
|
const c = this._getConsole();
|
|
168
113
|
if (!c) return;
|
|
169
114
|
let badgeStyle = COLORS.badge;
|
|
@@ -1466,9 +1411,11 @@ var CLICK_DEDUP_MS = 1e3;
|
|
|
1466
1411
|
var SESSION_KEY = "ads_session";
|
|
1467
1412
|
var SESSION_TS_KEY = "ads_session_ts";
|
|
1468
1413
|
var THIRTY_MINUTES_MS = 30 * 60 * 1e3;
|
|
1414
|
+
var PAGE_EVENT_NAME_RE = /^[a-zA-Z0-9:_.-]{1,64}$/;
|
|
1469
1415
|
var inMemorySessionId = null;
|
|
1470
1416
|
var inMemorySessionTs = 0;
|
|
1471
1417
|
var sessionStartedHere = false;
|
|
1418
|
+
var announcedSessionId = "";
|
|
1472
1419
|
var storageAllowed = true;
|
|
1473
1420
|
function generateUUID2() {
|
|
1474
1421
|
if (typeof crypto !== "undefined" && typeof crypto.randomUUID === "function") {
|
|
@@ -1923,6 +1870,7 @@ var BidsparkPrebidAnalytics = class _BidsparkPrebidAnalytics {
|
|
|
1923
1870
|
recordedConsentKeys = /* @__PURE__ */ new Set();
|
|
1924
1871
|
consentCount = 0;
|
|
1925
1872
|
cmpDetach = null;
|
|
1873
|
+
sessionResumeUnsub = null;
|
|
1926
1874
|
pageviewEmitted = false;
|
|
1927
1875
|
logger;
|
|
1928
1876
|
// True when managing session lifecycle internally; false if caller supplied fixed sessionId.
|
|
@@ -1984,7 +1932,8 @@ var BidsparkPrebidAnalytics = class _BidsparkPrebidAnalytics {
|
|
|
1984
1932
|
pbjsGlobalName: config.pbjsGlobalName || "pbjs",
|
|
1985
1933
|
attachPbjsListeners: config.attachPbjsListeners ?? true,
|
|
1986
1934
|
revenueFlushDelayMs: Number.isFinite(config.revenueFlushDelayMs) && config.revenueFlushDelayMs >= 0 ? config.revenueFlushDelayMs : REVENUE_FLUSH_DELAY_MS,
|
|
1987
|
-
alias: config.alias || DEFAULT_ALIAS
|
|
1935
|
+
alias: config.alias || DEFAULT_ALIAS,
|
|
1936
|
+
events: config.events || {}
|
|
1988
1937
|
};
|
|
1989
1938
|
_BidsparkPrebidAnalytics.allInstances.add(this);
|
|
1990
1939
|
this.logger = createLogger({
|
|
@@ -2078,8 +2027,11 @@ var BidsparkPrebidAnalytics = class _BidsparkPrebidAnalytics {
|
|
|
2078
2027
|
document.addEventListener("visibilitychange", this.boundVisibilityChange);
|
|
2079
2028
|
}
|
|
2080
2029
|
bindActivityListeners();
|
|
2030
|
+
this.sessionResumeUnsub = onSessionResume(() => this.syncSessionAndRotateVisitor());
|
|
2031
|
+
this.syncSessionId();
|
|
2081
2032
|
if (!this.pageviewEmitted) {
|
|
2082
2033
|
this.pageviewEmitted = true;
|
|
2034
|
+
this.emitPageEvent("pageview");
|
|
2083
2035
|
this.enqueue(TraceEventType.PAGEVIEW, "pageview", {});
|
|
2084
2036
|
}
|
|
2085
2037
|
this.flushTimer = setInterval(() => this.flush(), FLUSH_INTERVAL_MS);
|
|
@@ -2221,6 +2173,8 @@ var BidsparkPrebidAnalytics = class _BidsparkPrebidAnalytics {
|
|
|
2221
2173
|
}
|
|
2222
2174
|
this.cmpDetach = null;
|
|
2223
2175
|
}
|
|
2176
|
+
this.sessionResumeUnsub?.();
|
|
2177
|
+
this.sessionResumeUnsub = null;
|
|
2224
2178
|
this.flush();
|
|
2225
2179
|
}
|
|
2226
2180
|
handleVisibilityChange() {
|
|
@@ -3026,6 +2980,9 @@ var BidsparkPrebidAnalytics = class _BidsparkPrebidAnalytics {
|
|
|
3026
2980
|
setSampleRate(sampleRate) {
|
|
3027
2981
|
this.config.sampleRate = normalizeSampleRate(sampleRate);
|
|
3028
2982
|
}
|
|
2983
|
+
setEvents(events) {
|
|
2984
|
+
this.config.events = events || {};
|
|
2985
|
+
}
|
|
3029
2986
|
setSessionId(sessionId) {
|
|
3030
2987
|
this.config.sessionId = sessionId;
|
|
3031
2988
|
this.managesSessionId = !sessionId;
|
|
@@ -3068,12 +3025,8 @@ var BidsparkPrebidAnalytics = class _BidsparkPrebidAnalytics {
|
|
|
3068
3025
|
this.config.pageviewId = pageviewId || generateUUID2();
|
|
3069
3026
|
this.pageUrl = nextPageUrl;
|
|
3070
3027
|
this.logger.resetTiming();
|
|
3071
|
-
const prevSessionId = this.config.sessionId;
|
|
3072
3028
|
recordActivity();
|
|
3073
|
-
this.
|
|
3074
|
-
if (!this.config.visitorId || this.config.sessionId !== prevSessionId && this.config.visitorId === prevSessionId) {
|
|
3075
|
-
this.config.visitorId = this.config.userId || this.config.sessionId;
|
|
3076
|
-
}
|
|
3029
|
+
this.syncSessionAndRotateVisitor();
|
|
3077
3030
|
this.enable();
|
|
3078
3031
|
}
|
|
3079
3032
|
recordAuctionStart(auctionId, adUnitCode = "", bidderCount, mediaType) {
|
|
@@ -3516,6 +3469,55 @@ var BidsparkPrebidAnalytics = class _BidsparkPrebidAnalytics {
|
|
|
3516
3469
|
syncSessionId() {
|
|
3517
3470
|
if (!this.managesSessionId) return;
|
|
3518
3471
|
this.config.sessionId = sessionNeedsSync() ? getOrCreateSessionId() : inMemorySessionId;
|
|
3472
|
+
this.announceSession();
|
|
3473
|
+
}
|
|
3474
|
+
syncSessionAndRotateVisitor() {
|
|
3475
|
+
const prevSessionId = this.config.sessionId;
|
|
3476
|
+
this.syncSessionId();
|
|
3477
|
+
if (!this.config.visitorId || this.config.sessionId !== prevSessionId && this.config.visitorId === prevSessionId) {
|
|
3478
|
+
this.config.visitorId = this.config.userId || this.config.sessionId;
|
|
3479
|
+
}
|
|
3480
|
+
}
|
|
3481
|
+
announceSession() {
|
|
3482
|
+
if (!this.isEnabled || !this.managesSessionId || !sessionStartedHere || this.config.sessionId === announcedSessionId) {
|
|
3483
|
+
return;
|
|
3484
|
+
}
|
|
3485
|
+
announcedSessionId = this.config.sessionId;
|
|
3486
|
+
this.emitPageEvent("session");
|
|
3487
|
+
}
|
|
3488
|
+
emitPageEvent(key) {
|
|
3489
|
+
const detail = {
|
|
3490
|
+
pageviewId: this.config.pageviewId,
|
|
3491
|
+
sessionId: this.config.sessionId
|
|
3492
|
+
};
|
|
3493
|
+
const configured = this.config.events?.[key];
|
|
3494
|
+
if (typeof configured === "function") {
|
|
3495
|
+
try {
|
|
3496
|
+
configured(detail);
|
|
3497
|
+
} catch (e) {
|
|
3498
|
+
this.log("ERROR", `Error in ${key} event handler`, e);
|
|
3499
|
+
}
|
|
3500
|
+
return;
|
|
3501
|
+
}
|
|
3502
|
+
const defaultName = `${this.config.alias}:${key}`;
|
|
3503
|
+
let eventName = defaultName;
|
|
3504
|
+
const trimmed = typeof configured === "string" ? configured.trim() : "";
|
|
3505
|
+
if (trimmed) {
|
|
3506
|
+
if (PAGE_EVENT_NAME_RE.test(trimmed) && (!trimmed.startsWith("ads:") || trimmed === defaultName)) {
|
|
3507
|
+
eventName = trimmed;
|
|
3508
|
+
} else {
|
|
3509
|
+
this.log(
|
|
3510
|
+
"WARN",
|
|
3511
|
+
`Invalid ${key} event name "${configured}"; falling back to "${defaultName}".`
|
|
3512
|
+
);
|
|
3513
|
+
}
|
|
3514
|
+
}
|
|
3515
|
+
if (typeof window === "undefined" || typeof window.dispatchEvent !== "function") return;
|
|
3516
|
+
try {
|
|
3517
|
+
window.dispatchEvent(new CustomEvent(eventName, { detail }));
|
|
3518
|
+
} catch (e) {
|
|
3519
|
+
this.log("ERROR", `Failed to dispatch ${eventName} event`, e);
|
|
3520
|
+
}
|
|
3519
3521
|
}
|
|
3520
3522
|
isSampledVisitor() {
|
|
3521
3523
|
const key = this.config.visitorId || this.config.sessionId;
|
|
@@ -3924,6 +3926,8 @@ function registerPrebidAnalytics(pbjsGlobalName = "pbjs", defaults) {
|
|
|
3924
3926
|
var internals = (analytics) => analytics;
|
|
3925
3927
|
var adsLoaderListenerMap = /* @__PURE__ */ new WeakMap();
|
|
3926
3928
|
var adsLoadersWithErrorListener = /* @__PURE__ */ new WeakSet();
|
|
3929
|
+
var hookedImaFns = /* @__PURE__ */ new WeakSet();
|
|
3930
|
+
var attachedImaManagers = /* @__PURE__ */ new WeakMap();
|
|
3927
3931
|
function describeImaError(event) {
|
|
3928
3932
|
const err = typeof event?.getError === "function" ? event.getError() : event?.error || event;
|
|
3929
3933
|
const call = (name) => err && typeof err[name] === "function" ? err[name]() : void 0;
|
|
@@ -3983,7 +3987,7 @@ function hookAdsManagerLoadedEvent(loadedEventCtor) {
|
|
|
3983
3987
|
if (!loadedEventCtor) return;
|
|
3984
3988
|
try {
|
|
3985
3989
|
const loadedProto = loadedEventCtor.prototype;
|
|
3986
|
-
if (loadedProto && typeof loadedProto.getAdsManager === "function" && !loadedProto.getAdsManager
|
|
3990
|
+
if (loadedProto && typeof loadedProto.getAdsManager === "function" && !hookedImaFns.has(loadedProto.getAdsManager)) {
|
|
3987
3991
|
const originalGetAdsManager = loadedProto.getAdsManager;
|
|
3988
3992
|
const hookedGetAdsManager = function(...args) {
|
|
3989
3993
|
const adsManager = originalGetAdsManager.apply(this, args);
|
|
@@ -3997,7 +4001,7 @@ function hookAdsManagerLoadedEvent(loadedEventCtor) {
|
|
|
3997
4001
|
}
|
|
3998
4002
|
return adsManager;
|
|
3999
4003
|
};
|
|
4000
|
-
hookedGetAdsManager
|
|
4004
|
+
hookedImaFns.add(hookedGetAdsManager);
|
|
4001
4005
|
loadedProto.getAdsManager = hookedGetAdsManager;
|
|
4002
4006
|
}
|
|
4003
4007
|
} catch {
|
|
@@ -4007,7 +4011,7 @@ function hookAdsManagerPrototype(adsManagerCtorOrProto) {
|
|
|
4007
4011
|
if (!adsManagerCtorOrProto) return;
|
|
4008
4012
|
try {
|
|
4009
4013
|
const proto = adsManagerCtorOrProto.prototype || adsManagerCtorOrProto;
|
|
4010
|
-
if (proto && typeof proto.init === "function" && !proto.init
|
|
4014
|
+
if (proto && typeof proto.init === "function" && !hookedImaFns.has(proto.init)) {
|
|
4011
4015
|
const origInit = proto.init;
|
|
4012
4016
|
const hookedInit = function(...args) {
|
|
4013
4017
|
for (const instance of BidsparkPrebidAnalytics.getActiveInstances()) {
|
|
@@ -4018,10 +4022,10 @@ function hookAdsManagerPrototype(adsManagerCtorOrProto) {
|
|
|
4018
4022
|
}
|
|
4019
4023
|
return origInit.apply(this, args);
|
|
4020
4024
|
};
|
|
4021
|
-
hookedInit
|
|
4025
|
+
hookedImaFns.add(hookedInit);
|
|
4022
4026
|
proto.init = hookedInit;
|
|
4023
4027
|
}
|
|
4024
|
-
if (proto && typeof proto.start === "function" && !proto.start
|
|
4028
|
+
if (proto && typeof proto.start === "function" && !hookedImaFns.has(proto.start)) {
|
|
4025
4029
|
const origStart = proto.start;
|
|
4026
4030
|
const hookedStart = function(...args) {
|
|
4027
4031
|
for (const instance of BidsparkPrebidAnalytics.getActiveInstances()) {
|
|
@@ -4032,7 +4036,7 @@ function hookAdsManagerPrototype(adsManagerCtorOrProto) {
|
|
|
4032
4036
|
}
|
|
4033
4037
|
return origStart.apply(this, args);
|
|
4034
4038
|
};
|
|
4035
|
-
hookedStart
|
|
4039
|
+
hookedImaFns.add(hookedStart);
|
|
4036
4040
|
proto.start = hookedStart;
|
|
4037
4041
|
}
|
|
4038
4042
|
} catch {
|
|
@@ -4043,7 +4047,7 @@ function hookAdsLoader(adsLoaderCtor) {
|
|
|
4043
4047
|
try {
|
|
4044
4048
|
const proto = adsLoaderCtor.prototype;
|
|
4045
4049
|
if (!proto) return;
|
|
4046
|
-
if (typeof proto.addEventListener === "function" && !proto.addEventListener
|
|
4050
|
+
if (typeof proto.addEventListener === "function" && !hookedImaFns.has(proto.addEventListener)) {
|
|
4047
4051
|
const origAddEventListener = proto.addEventListener;
|
|
4048
4052
|
const hookedAddEventListener = function(type, listener, ...rest) {
|
|
4049
4053
|
ensureAdsLoaderErrorListener(this, origAddEventListener);
|
|
@@ -4051,9 +4055,9 @@ function hookAdsLoader(adsLoaderCtor) {
|
|
|
4051
4055
|
let wrappedListener = adsLoaderListenerMap.get(listener);
|
|
4052
4056
|
if (!wrappedListener) {
|
|
4053
4057
|
wrappedListener = function(event) {
|
|
4054
|
-
if (event && typeof event.getAdsManager === "function" && !event.getAdsManager
|
|
4058
|
+
if (event && typeof event.getAdsManager === "function" && !hookedImaFns.has(event.getAdsManager)) {
|
|
4055
4059
|
const origGetAdsManager = event.getAdsManager;
|
|
4056
|
-
|
|
4060
|
+
const hookedGetAdsManager = function(...args) {
|
|
4057
4061
|
const adsManager = origGetAdsManager.apply(this, args);
|
|
4058
4062
|
if (adsManager) {
|
|
4059
4063
|
for (const sdk of BidsparkPrebidAnalytics.getActiveInstances()) {
|
|
@@ -4065,7 +4069,8 @@ function hookAdsLoader(adsLoaderCtor) {
|
|
|
4065
4069
|
}
|
|
4066
4070
|
return adsManager;
|
|
4067
4071
|
};
|
|
4068
|
-
|
|
4072
|
+
hookedImaFns.add(hookedGetAdsManager);
|
|
4073
|
+
event.getAdsManager = hookedGetAdsManager;
|
|
4069
4074
|
}
|
|
4070
4075
|
return listener.apply(this, arguments);
|
|
4071
4076
|
};
|
|
@@ -4075,16 +4080,16 @@ function hookAdsLoader(adsLoaderCtor) {
|
|
|
4075
4080
|
}
|
|
4076
4081
|
return origAddEventListener.call(this, type, listener, ...rest);
|
|
4077
4082
|
};
|
|
4078
|
-
hookedAddEventListener
|
|
4083
|
+
hookedImaFns.add(hookedAddEventListener);
|
|
4079
4084
|
proto.addEventListener = hookedAddEventListener;
|
|
4080
4085
|
}
|
|
4081
|
-
if (typeof proto.removeEventListener === "function" && !proto.removeEventListener
|
|
4086
|
+
if (typeof proto.removeEventListener === "function" && !hookedImaFns.has(proto.removeEventListener)) {
|
|
4082
4087
|
const origRemoveEventListener = proto.removeEventListener;
|
|
4083
4088
|
const hookedRemoveEventListener = function(type, listener, ...rest) {
|
|
4084
4089
|
const wrapped = typeof listener === "function" ? adsLoaderListenerMap.get(listener) : void 0;
|
|
4085
4090
|
return origRemoveEventListener.call(this, type, wrapped || listener, ...rest);
|
|
4086
4091
|
};
|
|
4087
|
-
hookedRemoveEventListener
|
|
4092
|
+
hookedImaFns.add(hookedRemoveEventListener);
|
|
4088
4093
|
proto.removeEventListener = hookedRemoveEventListener;
|
|
4089
4094
|
}
|
|
4090
4095
|
} catch {
|
|
@@ -4226,9 +4231,6 @@ function initImaInterception() {
|
|
|
4226
4231
|
}
|
|
4227
4232
|
}
|
|
4228
4233
|
}
|
|
4229
|
-
if (typeof window !== "undefined") {
|
|
4230
|
-
initImaInterception();
|
|
4231
|
-
}
|
|
4232
4234
|
function findCachedWinningBid(analytics, filter) {
|
|
4233
4235
|
const self = internals(analytics);
|
|
4234
4236
|
const now = Date.now();
|
|
@@ -4299,18 +4301,11 @@ function attachImaAdsManager(analytics, adsManager, options) {
|
|
|
4299
4301
|
return () => {
|
|
4300
4302
|
};
|
|
4301
4303
|
}
|
|
4302
|
-
|
|
4303
|
-
|
|
4304
|
-
|
|
4305
|
-
|
|
4306
|
-
configurable: true,
|
|
4307
|
-
writable: true
|
|
4308
|
-
});
|
|
4309
|
-
} catch {
|
|
4310
|
-
adsManager.__bidsparkAttachedInstances = /* @__PURE__ */ new Map();
|
|
4311
|
-
}
|
|
4304
|
+
let attached = attachedImaManagers.get(adsManager);
|
|
4305
|
+
if (!attached) {
|
|
4306
|
+
attached = /* @__PURE__ */ new Map();
|
|
4307
|
+
attachedImaManagers.set(adsManager, attached);
|
|
4312
4308
|
}
|
|
4313
|
-
const attached = adsManager.__bidsparkAttachedInstances;
|
|
4314
4309
|
const previous = attached.get(analytics);
|
|
4315
4310
|
if (previous) {
|
|
4316
4311
|
if (!options) return () => {
|
|
@@ -4522,7 +4517,9 @@ function attachImaAdsManager(analytics, adsManager, options) {
|
|
|
4522
4517
|
}
|
|
4523
4518
|
const cleanup = () => {
|
|
4524
4519
|
activeAdSession = null;
|
|
4525
|
-
attached.
|
|
4520
|
+
if (attached.get(analytics) === cleanup) {
|
|
4521
|
+
attached.delete(analytics);
|
|
4522
|
+
}
|
|
4526
4523
|
for (const { type, handler } of listeners) {
|
|
4527
4524
|
try {
|
|
4528
4525
|
adsManager.removeEventListener(type, handler);
|
|
@@ -4567,14 +4564,22 @@ function attachVideoPlayer(analytics, target, options) {
|
|
|
4567
4564
|
const getWinningBid = () => {
|
|
4568
4565
|
return (auctionId ? self.getCachedBid(`${auctionId}:${adUnitCode}`) : void 0) || (auctionId && slotId !== adUnitCode ? self.getCachedBid(`${auctionId}:${slotId}`) : void 0) || self.getCachedBid(adUnitCode) || (slotId !== adUnitCode ? self.getCachedBid(slotId) : void 0);
|
|
4569
4566
|
};
|
|
4570
|
-
|
|
4571
|
-
|
|
4572
|
-
|
|
4573
|
-
|
|
4574
|
-
|
|
4575
|
-
videoEl = el.querySelector("video");
|
|
4567
|
+
const isVideoEl = Boolean(el?.tagName && el.tagName.toLowerCase() === "video");
|
|
4568
|
+
const resolveTargetVideo = (e) => {
|
|
4569
|
+
const target2 = e?.target;
|
|
4570
|
+
if (target2 && (typeof HTMLVideoElement !== "undefined" && target2 instanceof HTMLVideoElement || target2.tagName?.toLowerCase() === "video")) {
|
|
4571
|
+
return target2;
|
|
4576
4572
|
}
|
|
4577
|
-
|
|
4573
|
+
if (target2 && typeof target2.closest === "function") {
|
|
4574
|
+
const parentVideo = target2.closest("video");
|
|
4575
|
+
if (parentVideo) return parentVideo;
|
|
4576
|
+
}
|
|
4577
|
+
if (target2 && target2 !== el) {
|
|
4578
|
+
return null;
|
|
4579
|
+
}
|
|
4580
|
+
return isVideoEl ? el : el?.querySelector?.("video") ?? null;
|
|
4581
|
+
};
|
|
4582
|
+
const isForeignChildEvent = (e, targetVideo) => Boolean(e?.target && e.target !== el && !targetVideo);
|
|
4578
4583
|
const quartilesFired = {
|
|
4579
4584
|
q1: false,
|
|
4580
4585
|
q2: false,
|
|
@@ -4582,7 +4587,7 @@ function attachVideoPlayer(analytics, target, options) {
|
|
|
4582
4587
|
q4: false
|
|
4583
4588
|
};
|
|
4584
4589
|
const triggerPlaybackImpression = (activeVideo) => {
|
|
4585
|
-
const vEl = activeVideo ||
|
|
4590
|
+
const vEl = activeVideo || resolveTargetVideo();
|
|
4586
4591
|
const cached = getWinningBid();
|
|
4587
4592
|
const mergedBid = {
|
|
4588
4593
|
...cached?.rawBid || options?.bid,
|
|
@@ -4609,12 +4614,13 @@ function attachVideoPlayer(analytics, target, options) {
|
|
|
4609
4614
|
}
|
|
4610
4615
|
};
|
|
4611
4616
|
const handlePlaying = (e) => {
|
|
4617
|
+
const targetVideo = resolveTargetVideo(e);
|
|
4618
|
+
if (isForeignChildEvent(e, targetVideo)) return;
|
|
4612
4619
|
self.log("DEBUG", "HTML5 video playing event received");
|
|
4613
|
-
const targetVideo = e?.target instanceof HTMLVideoElement ? e.target : videoEl;
|
|
4614
4620
|
triggerPlaybackImpression(targetVideo);
|
|
4615
4621
|
};
|
|
4616
4622
|
const handleTimeUpdate = (e) => {
|
|
4617
|
-
const targetVideo = e
|
|
4623
|
+
const targetVideo = resolveTargetVideo(e);
|
|
4618
4624
|
if (targetVideo && targetVideo.currentTime > 0) {
|
|
4619
4625
|
triggerPlaybackImpression(targetVideo);
|
|
4620
4626
|
if (Number.isFinite(targetVideo.duration) && targetVideo.duration > 0) {
|
|
@@ -4634,15 +4640,17 @@ function attachVideoPlayer(analytics, target, options) {
|
|
|
4634
4640
|
}
|
|
4635
4641
|
}
|
|
4636
4642
|
};
|
|
4637
|
-
const handleEnded = () => {
|
|
4643
|
+
const handleEnded = (e) => {
|
|
4644
|
+
if (isForeignChildEvent(e, resolveTargetVideo(e))) return;
|
|
4638
4645
|
if (!quartilesFired.q4) {
|
|
4639
4646
|
quartilesFired.q4 = true;
|
|
4640
4647
|
options?.onMilestone?.("complete", slotId);
|
|
4641
4648
|
}
|
|
4642
4649
|
};
|
|
4643
4650
|
const handleError = (e) => {
|
|
4644
|
-
const targetVideo = e
|
|
4645
|
-
|
|
4651
|
+
const targetVideo = resolveTargetVideo(e);
|
|
4652
|
+
if (isForeignChildEvent(e, targetVideo)) return;
|
|
4653
|
+
const err = targetVideo?.error;
|
|
4646
4654
|
const cached = getWinningBid();
|
|
4647
4655
|
const msg = err ? `HTML5 video error code ${err.code}: ${err.message}` : "Video playback error";
|
|
4648
4656
|
self.log("WARN", msg);
|
|
@@ -4667,41 +4675,13 @@ function attachVideoPlayer(analytics, target, options) {
|
|
|
4667
4675
|
}
|
|
4668
4676
|
}
|
|
4669
4677
|
};
|
|
4670
|
-
const
|
|
4671
|
-
|
|
4672
|
-
|
|
4673
|
-
|
|
4674
|
-
|
|
4675
|
-
|
|
4676
|
-
|
|
4677
|
-
const removeListenersFromVideo = (targetVideo) => {
|
|
4678
|
-
targetVideo.removeEventListener("playing", handlePlaying);
|
|
4679
|
-
targetVideo.removeEventListener("play", handlePlaying);
|
|
4680
|
-
targetVideo.removeEventListener("timeupdate", handleTimeUpdate);
|
|
4681
|
-
targetVideo.removeEventListener("ended", handleEnded);
|
|
4682
|
-
targetVideo.removeEventListener("error", handleError);
|
|
4683
|
-
};
|
|
4684
|
-
let mutationObserver = null;
|
|
4685
|
-
if (el && el !== videoEl) {
|
|
4686
|
-
el.addEventListener("playing", handlePlaying, true);
|
|
4687
|
-
el.addEventListener("play", handlePlaying, true);
|
|
4688
|
-
el.addEventListener("timeupdate", handleTimeUpdate, true);
|
|
4689
|
-
el.addEventListener("ended", handleEnded, true);
|
|
4690
|
-
el.addEventListener("error", handleError, true);
|
|
4691
|
-
}
|
|
4692
|
-
if (videoEl) {
|
|
4693
|
-
attachListenersToVideo(videoEl);
|
|
4694
|
-
}
|
|
4695
|
-
if (el && el.tagName?.toLowerCase() !== "video" && typeof MutationObserver !== "undefined") {
|
|
4696
|
-
mutationObserver = new MutationObserver(() => {
|
|
4697
|
-
const found = el.querySelector("video");
|
|
4698
|
-
if (found && found !== videoEl) {
|
|
4699
|
-
if (videoEl) removeListenersFromVideo(videoEl);
|
|
4700
|
-
videoEl = found;
|
|
4701
|
-
if (videoEl) attachListenersToVideo(videoEl);
|
|
4702
|
-
}
|
|
4703
|
-
});
|
|
4704
|
-
mutationObserver.observe(el, { childList: true, subtree: true });
|
|
4678
|
+
const useCapture = !isVideoEl;
|
|
4679
|
+
if (el) {
|
|
4680
|
+
el.addEventListener("playing", handlePlaying, useCapture);
|
|
4681
|
+
el.addEventListener("play", handlePlaying, useCapture);
|
|
4682
|
+
el.addEventListener("timeupdate", handleTimeUpdate, useCapture);
|
|
4683
|
+
el.addEventListener("ended", handleEnded, useCapture);
|
|
4684
|
+
el.addEventListener("error", handleError, useCapture);
|
|
4705
4685
|
}
|
|
4706
4686
|
const cachedInitial = getWinningBid();
|
|
4707
4687
|
if (options?.trackViewability !== false && el) {
|
|
@@ -4715,19 +4695,12 @@ function attachVideoPlayer(analytics, target, options) {
|
|
|
4715
4695
|
});
|
|
4716
4696
|
}
|
|
4717
4697
|
const cleanup = () => {
|
|
4718
|
-
if (
|
|
4719
|
-
|
|
4720
|
-
|
|
4721
|
-
|
|
4722
|
-
|
|
4723
|
-
el.removeEventListener("
|
|
4724
|
-
el.removeEventListener("play", handlePlaying, true);
|
|
4725
|
-
el.removeEventListener("timeupdate", handleTimeUpdate, true);
|
|
4726
|
-
el.removeEventListener("ended", handleEnded, true);
|
|
4727
|
-
el.removeEventListener("error", handleError, true);
|
|
4728
|
-
}
|
|
4729
|
-
if (videoEl) {
|
|
4730
|
-
removeListenersFromVideo(videoEl);
|
|
4698
|
+
if (el) {
|
|
4699
|
+
el.removeEventListener("playing", handlePlaying, useCapture);
|
|
4700
|
+
el.removeEventListener("play", handlePlaying, useCapture);
|
|
4701
|
+
el.removeEventListener("timeupdate", handleTimeUpdate, useCapture);
|
|
4702
|
+
el.removeEventListener("ended", handleEnded, useCapture);
|
|
4703
|
+
el.removeEventListener("error", handleError, useCapture);
|
|
4731
4704
|
}
|
|
4732
4705
|
self.videoDetachCleanups.delete(cleanup);
|
|
4733
4706
|
};
|
|
@@ -4740,16 +4713,17 @@ BidsparkPrebidAnalytics.prototype.attachImaAdsManager = function(adsManager, opt
|
|
|
4740
4713
|
BidsparkPrebidAnalytics.prototype.attachVideoPlayer = function(target, options) {
|
|
4741
4714
|
return attachVideoPlayer(this, target, options);
|
|
4742
4715
|
};
|
|
4743
|
-
var
|
|
4744
|
-
|
|
4745
|
-
|
|
4746
|
-
|
|
4716
|
+
var imaAutoInterceptionInstalled = false;
|
|
4717
|
+
function installImaAutoInterception() {
|
|
4718
|
+
if (imaAutoInterceptionInstalled) return;
|
|
4719
|
+
imaAutoInterceptionInstalled = true;
|
|
4720
|
+
initImaInterception();
|
|
4721
|
+
const coreEnable = BidsparkPrebidAnalytics.prototype.enable;
|
|
4722
|
+
BidsparkPrebidAnalytics.prototype.enable = function() {
|
|
4723
|
+
coreEnable.call(this);
|
|
4747
4724
|
initImaInterception();
|
|
4748
|
-
|
|
4749
|
-
|
|
4750
|
-
}
|
|
4751
|
-
}
|
|
4752
|
-
};
|
|
4725
|
+
};
|
|
4726
|
+
}
|
|
4753
4727
|
function normalizeImaEventName(name) {
|
|
4754
4728
|
if (!name) return "";
|
|
4755
4729
|
const lower = name.toLowerCase().replace(/[_-]/g, "");
|
|
@@ -4868,6 +4842,7 @@ var ImaAdsManagerStub = class {
|
|
|
4868
4842
|
};
|
|
4869
4843
|
|
|
4870
4844
|
// src/index.ts
|
|
4845
|
+
installImaAutoInterception();
|
|
4871
4846
|
function getAdsGlobal(alias = DEFAULT_ALIAS) {
|
|
4872
4847
|
if (typeof window === "undefined") {
|
|
4873
4848
|
return { q: [] };
|
|
@@ -4938,6 +4913,7 @@ export {
|
|
|
4938
4913
|
getPrebidEventKey,
|
|
4939
4914
|
idToBytes,
|
|
4940
4915
|
idToString,
|
|
4916
|
+
installImaAutoInterception,
|
|
4941
4917
|
isAutomatedClient,
|
|
4942
4918
|
isSessionActive,
|
|
4943
4919
|
isTrustedEndpoint,
|
package/package.json
CHANGED