@bidspark/analytics 0.43.1 → 0.45.2

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/dist/index.mjs CHANGED
@@ -285,6 +285,8 @@ var TraceEventType = {
285
285
  * signals (bitmask), sdk_loaded, device (mobile|desktop), browser, forced ("1" on forced show).
286
286
  */
287
287
  RECOVERY: 20,
288
+ /** PAGEVIEW - Pageview start (once per initial load or SPA navigate()). */
289
+ PAGEVIEW: 21,
288
290
  UNRECOGNIZED: -1
289
291
  };
290
292
  var BidderOutcome = {
@@ -326,7 +328,8 @@ function createBaseTraceEventBatch() {
326
328
  deviceModel: "",
327
329
  city: "",
328
330
  postalCode: "",
329
- sampleRate: void 0
331
+ sampleRate: void 0,
332
+ automated: false
330
333
  };
331
334
  }
332
335
  var TraceEventBatch = {
@@ -381,6 +384,9 @@ var TraceEventBatch = {
381
384
  if (message.sampleRate !== void 0) {
382
385
  writer.uint32(129).double(message.sampleRate);
383
386
  }
387
+ if (message.automated !== void 0 && message.automated !== false) {
388
+ writer.uint32(136).bool(message.automated);
389
+ }
384
390
  return writer;
385
391
  },
386
392
  decode(input, length) {
@@ -505,6 +511,13 @@ var TraceEventBatch = {
505
511
  message.sampleRate = reader.double();
506
512
  continue;
507
513
  }
514
+ case 17: {
515
+ if (tag !== 136) {
516
+ break;
517
+ }
518
+ message.automated = reader.bool();
519
+ continue;
520
+ }
508
521
  }
509
522
  if ((tag & 7) === 4 || tag === 0) {
510
523
  break;
@@ -1022,6 +1035,33 @@ function isVisitorSampled(visitorKey, sampleRate) {
1022
1035
  }
1023
1036
  return (hash >>> 0) / 4294967296 < rate;
1024
1037
  }
1038
+ var AUTOMATED_UA_RE = /headless|phantomjs|slimerjs|puppeteer|playwright/i;
1039
+ var DESKTOP_CHROME_UA_RE = /Chrome\/\d+/i;
1040
+ var NON_DESKTOP_CHROME_UA_RE = /Mobile|Android|iPhone|iPad|iPod|CriOS|FxiOS|Edg\/|OPR\/|SamsungBrowser/i;
1041
+ function isAutomatedClient() {
1042
+ const g = typeof globalThis !== "undefined" ? globalThis : void 0;
1043
+ const nav = g && "navigator" in g ? g.navigator : void 0;
1044
+ if (!nav) return false;
1045
+ if (nav.webdriver === true) return true;
1046
+ const ua = typeof nav.userAgent === "string" ? nav.userAgent : "";
1047
+ if (AUTOMATED_UA_RE.test(ua)) return true;
1048
+ const win = g && "window" in g ? g.window : g;
1049
+ let weakSignals = 0;
1050
+ if (win && typeof win.outerWidth === "number" && typeof win.outerHeight === "number" && win.outerWidth === 0 && win.outerHeight === 0) {
1051
+ weakSignals++;
1052
+ }
1053
+ if (Array.isArray(nav.languages) && nav.languages.length === 0) {
1054
+ weakSignals++;
1055
+ }
1056
+ if (DESKTOP_CHROME_UA_RE.test(ua) && !NON_DESKTOP_CHROME_UA_RE.test(ua)) {
1057
+ if (win && !win.chrome) weakSignals++;
1058
+ const plugins = nav.plugins;
1059
+ if (plugins !== void 0 && plugins !== null && plugins.length === 0) {
1060
+ weakSignals++;
1061
+ }
1062
+ }
1063
+ return weakSignals >= 2;
1064
+ }
1025
1065
 
1026
1066
  // ../../protogen/usage_event.ts
1027
1067
  var usage_event_exports = {};
@@ -1187,6 +1227,8 @@ function sendEvents(opts) {
1187
1227
  viewableDurationMs: void 0,
1188
1228
  bidderOutcomes: []
1189
1229
  }));
1230
+ const nav = g && "navigator" in g ? g.navigator : void 0;
1231
+ const automated = isAutomatedClient();
1190
1232
  const batch = {
1191
1233
  propertyId: idToBytes(opts.propertyId),
1192
1234
  pageviewId: idToBytes(pageviewId),
@@ -1198,10 +1240,10 @@ function sendEvents(opts) {
1198
1240
  deviceType: void 0,
1199
1241
  domain,
1200
1242
  events: traceEvents,
1201
- ...sampleRate < 1 ? { sampleRate } : {}
1243
+ ...sampleRate < 1 ? { sampleRate } : {},
1244
+ ...automated ? { automated: true } : {}
1202
1245
  };
1203
1246
  const encoded = TraceEventBatch.encode(batch).finish();
1204
- const nav = g && "navigator" in g ? g.navigator : void 0;
1205
1247
  if (nav && typeof nav.sendBeacon === "function") {
1206
1248
  try {
1207
1249
  const blob = new Blob([encoded], { type: SAFE_CONTENT_TYPE });
@@ -1490,6 +1532,15 @@ function sameEndpointOrigin(a, b) {
1490
1532
  return false;
1491
1533
  }
1492
1534
  }
1535
+ function normalizePageUrl(raw) {
1536
+ if (!raw) return "";
1537
+ try {
1538
+ const u = new URL(raw);
1539
+ return u.origin + u.pathname;
1540
+ } catch {
1541
+ return raw;
1542
+ }
1543
+ }
1493
1544
  var SESSION_PERSIST_INTERVAL_MS = 60 * 1e3;
1494
1545
  var persistedSessionTs = 0;
1495
1546
  var ACTIVITY_EVENTS = ["pointerdown", "pointermove", "keydown", "scroll", "wheel", "touchstart"];
@@ -1599,7 +1650,7 @@ function getOrCreateUserId() {
1599
1650
  if (storageAllowed && typeof localStorage !== "undefined") {
1600
1651
  const stored = localStorage.getItem(USER_ID_KEY);
1601
1652
  if (stored) return stored;
1602
- const newId = generateUUID2();
1653
+ const newId = inMemorySessionId || generateUUID2();
1603
1654
  localStorage.setItem(USER_ID_KEY, newId);
1604
1655
  return newId;
1605
1656
  }
@@ -1633,6 +1684,16 @@ function tcDataToConsentRecord(tcData) {
1633
1684
  purposesConsented: purposes
1634
1685
  };
1635
1686
  }
1687
+ function resolveTcfStorageAllowed(tcData, success) {
1688
+ if (!success || !tcData) return void 0;
1689
+ if (tcData.eventStatus === "cmpuishown" && !tcData.tcString) return false;
1690
+ const allowed = storageAllowedBy(tcDataToConsentRecord(tcData));
1691
+ if (!allowed) {
1692
+ clearStoredSession();
1693
+ clearStoredUserId();
1694
+ }
1695
+ return allowed;
1696
+ }
1636
1697
  function probeTcfStorageAllowed() {
1637
1698
  if (typeof window === "undefined") return void 0;
1638
1699
  const win = window;
@@ -1640,27 +1701,22 @@ function probeTcfStorageAllowed() {
1640
1701
  let allowed = void 0;
1641
1702
  try {
1642
1703
  win.__tcfapi("getTCData", 2, (tcData, success) => {
1643
- if (success && tcData) {
1644
- allowed = storageAllowedBy(tcDataToConsentRecord(tcData));
1645
- }
1704
+ allowed = resolveTcfStorageAllowed(tcData, success);
1646
1705
  });
1647
1706
  } catch {
1648
1707
  }
1649
1708
  return allowed !== void 0 ? allowed : false;
1650
1709
  }
1651
- var tcfListenerAttached = false;
1652
- function resetTcfListener() {
1653
- tcfListenerAttached = false;
1654
- }
1710
+ var attachedTcfApi = null;
1655
1711
  function attachTcfListener() {
1656
- if (tcfListenerAttached || typeof window === "undefined") return;
1712
+ if (typeof window === "undefined") return;
1657
1713
  const win = window;
1658
- if (typeof win.__tcfapi !== "function") return;
1659
- tcfListenerAttached = true;
1714
+ if (typeof win.__tcfapi !== "function" || attachedTcfApi === win.__tcfapi) return;
1715
+ attachedTcfApi = win.__tcfapi;
1660
1716
  try {
1661
1717
  win.__tcfapi("addEventListener", 2, (tcData, success) => {
1662
- if (!success || !tcData) return;
1663
- BidsparkPrebidAnalytics.setStorageAllowed(storageAllowedBy(tcDataToConsentRecord(tcData)));
1718
+ const allowed = resolveTcfStorageAllowed(tcData, success);
1719
+ if (allowed !== void 0) BidsparkPrebidAnalytics.setStorageAllowed(allowed);
1664
1720
  });
1665
1721
  } catch {
1666
1722
  }
@@ -1751,18 +1807,48 @@ var PrebidEventDeduper = class {
1751
1807
  }
1752
1808
  };
1753
1809
  function parseBidDimensions(bid) {
1754
- if (Number.isFinite(bid?.width) && Number.isFinite(bid?.height)) {
1810
+ if (Number.isFinite(bid?.width) && bid.width > 0 && Number.isFinite(bid?.height) && bid.height > 0) {
1755
1811
  return { width: bid.width, height: bid.height };
1756
1812
  }
1813
+ if (Number.isFinite(bid?.w) && bid.w > 0 && Number.isFinite(bid?.h) && bid.h > 0) {
1814
+ return { width: bid.w, height: bid.h };
1815
+ }
1816
+ if (Number.isFinite(bid?.playerWidth) && bid.playerWidth > 0 && Number.isFinite(bid?.playerHeight) && bid.playerHeight > 0) {
1817
+ return { width: bid.playerWidth, height: bid.playerHeight };
1818
+ }
1819
+ const ps = bid?.mediaTypes?.video?.playerSize;
1820
+ if (Array.isArray(ps) && ps.length > 0) {
1821
+ const tuple = Array.isArray(ps[0]) ? ps[0] : ps;
1822
+ if (Number.isFinite(tuple[0]) && tuple[0] > 0 && Number.isFinite(tuple[1]) && tuple[1] > 0) {
1823
+ return { width: tuple[0], height: tuple[1] };
1824
+ }
1825
+ }
1757
1826
  if (typeof bid?.size === "string") {
1758
1827
  const match = bid.size.match(/^(\d+)x(\d+)$/);
1759
1828
  if (match) {
1760
1829
  return { width: Number(match[1]), height: Number(match[2]) };
1761
1830
  }
1762
1831
  }
1832
+ const w = Number.isFinite(bid?.width) ? bid.width : Number.isFinite(bid?.w) ? bid.w : Number.isFinite(bid?.playerWidth) ? bid.playerWidth : 0;
1833
+ const h = Number.isFinite(bid?.height) ? bid.height : Number.isFinite(bid?.h) ? bid.h : Number.isFinite(bid?.playerHeight) ? bid.playerHeight : 0;
1834
+ return { width: w, height: h };
1835
+ }
1836
+ function resolveBidPricing(bid, fallback) {
1837
+ if (Number.isFinite(bid?.cpm)) {
1838
+ return {
1839
+ cpm: bid.cpm,
1840
+ currency: bid?.currency || bid?.originalCurrency || fallback?.currency || "USD"
1841
+ };
1842
+ }
1843
+ if (Number.isFinite(bid?.originalCpm)) {
1844
+ return {
1845
+ cpm: bid.originalCpm,
1846
+ currency: bid?.originalCurrency || bid?.currency || fallback?.currency || "USD"
1847
+ };
1848
+ }
1763
1849
  return {
1764
- width: Number.isFinite(bid?.width) ? bid.width : 0,
1765
- height: Number.isFinite(bid?.height) ? bid.height : 0
1850
+ cpm: fallback?.cpm ?? 0,
1851
+ currency: fallback?.currency || bid?.currency || bid?.originalCurrency || "USD"
1766
1852
  };
1767
1853
  }
1768
1854
  var defaultLogger = createLogger({
@@ -1790,14 +1876,7 @@ var BidsparkPrebidAnalytics = class _BidsparkPrebidAnalytics {
1790
1876
  storageAllowed = allowed;
1791
1877
  persistedSessionTs = 0;
1792
1878
  for (const inst of _BidsparkPrebidAnalytics.allInstances) {
1793
- inst.config.storageAllowed = allowed;
1794
- if (!allowed) {
1795
- inst.config.userId = "";
1796
- inst.config.visitorId = inst.config.visitorId || inst.config.pageviewId;
1797
- } else if (!inst.config.userId) {
1798
- inst.config.userId = getOrCreateUserId();
1799
- inst.config.visitorId = inst.config.userId || inst.config.pageviewId;
1800
- }
1879
+ inst.applyStorageAllowed(allowed);
1801
1880
  }
1802
1881
  }
1803
1882
  static isStorageAllowed() {
@@ -1844,9 +1923,15 @@ var BidsparkPrebidAnalytics = class _BidsparkPrebidAnalytics {
1844
1923
  recordedConsentKeys = /* @__PURE__ */ new Set();
1845
1924
  consentCount = 0;
1846
1925
  cmpDetach = null;
1926
+ pageviewEmitted = false;
1847
1927
  logger;
1848
1928
  // True when managing session lifecycle internally; false if caller supplied fixed sessionId.
1849
1929
  managesSessionId;
1930
+ configuredUserId;
1931
+ // True when visitor sampling key is derived internally from storage/session rather than caller config.
1932
+ managesVisitorId;
1933
+ // True once visitorId is settled (storage read, caller-supplied, or first batch drained).
1934
+ visitorKeyLocked;
1850
1935
  constructor(config) {
1851
1936
  const tcfStorage = probeTcfStorageAllowed();
1852
1937
  if (typeof window !== "undefined" && typeof window.__tcfapi === "function") {
@@ -1860,32 +1945,33 @@ var BidsparkPrebidAnalytics = class _BidsparkPrebidAnalytics {
1860
1945
  const requestedEndpoint = config.endpoint || "";
1861
1946
  const endpoint = !requestedEndpoint || isTrustedEndpoint(requestedEndpoint) ? requestedEndpoint : "";
1862
1947
  const pageviewId = config.pageviewId || generateUUID2();
1948
+ recordActivity();
1949
+ this.managesSessionId = !config.sessionId;
1950
+ const sessionId = config.sessionId || getOrCreateSessionId();
1951
+ this.configuredUserId = config.userId || "";
1863
1952
  let initialUserId = "";
1864
- let initialVisitorId = "";
1865
1953
  if (isStorageOn) {
1866
- initialUserId = config.userId || getOrCreateUserId();
1867
- if (config.userId) {
1954
+ initialUserId = this.configuredUserId || getOrCreateUserId();
1955
+ if (this.configuredUserId) {
1868
1956
  try {
1869
1957
  if (typeof localStorage !== "undefined") {
1870
- localStorage.setItem(USER_ID_KEY, config.userId);
1958
+ localStorage.setItem(USER_ID_KEY, this.configuredUserId);
1871
1959
  }
1872
1960
  } catch {
1873
1961
  }
1874
1962
  }
1875
- initialVisitorId = config.visitorId || initialUserId || pageviewId;
1876
- } else {
1877
- initialUserId = "";
1878
- initialVisitorId = config.visitorId || pageviewId;
1879
1963
  }
1964
+ this.managesVisitorId = !config.visitorId && !config.userId && !config.sessionId;
1965
+ this.visitorKeyLocked = isStorageOn || !this.managesVisitorId;
1880
1966
  const usageEndpoint = config.usageEndpoint || "";
1881
1967
  this.config = {
1882
1968
  endpoint,
1883
1969
  usageEndpoint,
1884
1970
  propertyId: config.propertyId || "",
1885
1971
  pageviewId,
1886
- sessionId: config.sessionId || "",
1972
+ sessionId,
1887
1973
  userId: initialUserId,
1888
- visitorId: initialVisitorId,
1974
+ visitorId: config.visitorId || this.configuredUserId || initialUserId || sessionId,
1889
1975
  deviceType: config.deviceType || "desktop",
1890
1976
  country: config.country || "",
1891
1977
  region: config.region || "",
@@ -1905,9 +1991,6 @@ var BidsparkPrebidAnalytics = class _BidsparkPrebidAnalytics {
1905
1991
  prefix: "[analytics]",
1906
1992
  logLevel: this.config.logLevel
1907
1993
  });
1908
- recordActivity();
1909
- this.managesSessionId = !config.sessionId;
1910
- if (this.managesSessionId) this.config.sessionId = getOrCreateSessionId();
1911
1994
  if (requestedEndpoint && !endpoint) {
1912
1995
  this.log(
1913
1996
  "ERROR",
@@ -1920,6 +2003,31 @@ var BidsparkPrebidAnalytics = class _BidsparkPrebidAnalytics {
1920
2003
  };
1921
2004
  this.boundVisibilityChange = () => this.handleVisibilityChange();
1922
2005
  }
2006
+ applyStorageAllowed(allowed) {
2007
+ this.config.storageAllowed = allowed;
2008
+ if (!allowed) {
2009
+ this.config.userId = "";
2010
+ return;
2011
+ }
2012
+ this.syncSessionId();
2013
+ if (!this.config.userId) {
2014
+ this.config.userId = this.configuredUserId || getOrCreateUserId();
2015
+ }
2016
+ if (this.managesVisitorId && !this.visitorKeyLocked) {
2017
+ this.config.visitorId = this.config.userId || this.config.sessionId;
2018
+ this.visitorKeyLocked = true;
2019
+ if (!this.isSampledVisitor()) {
2020
+ this.queue = [];
2021
+ if (this.revenueFlushTimer) {
2022
+ clearTimeout(this.revenueFlushTimer);
2023
+ this.revenueFlushTimer = null;
2024
+ }
2025
+ }
2026
+ }
2027
+ if (this.isEnabled) {
2028
+ this.resendPersistedBatches();
2029
+ }
2030
+ }
1923
2031
  setLogLevel(level) {
1924
2032
  const norm = normalizeLogLevel(level);
1925
2033
  this.config.logLevel = norm;
@@ -1933,6 +2041,7 @@ var BidsparkPrebidAnalytics = class _BidsparkPrebidAnalytics {
1933
2041
  if (this.isEnabled) return;
1934
2042
  this.isEnabled = true;
1935
2043
  _BidsparkPrebidAnalytics.activeInstances.add(this);
2044
+ _BidsparkPrebidAnalytics.allInstances.add(this);
1936
2045
  this.logger.setLevel(this.config.logLevel);
1937
2046
  if (this.config.attachPbjsListeners && this.config.usageEndpoint && !this.usageRecorder) {
1938
2047
  this.usageRecorder = new UsageRecorder({
@@ -1962,18 +2071,17 @@ var BidsparkPrebidAnalytics = class _BidsparkPrebidAnalytics {
1962
2071
  this.cmpDetach = this.attachCmp();
1963
2072
  if (typeof window !== "undefined") {
1964
2073
  if (!this.pageUrl) {
1965
- try {
1966
- const u = new URL(window.location.href);
1967
- this.pageUrl = u.origin + u.pathname;
1968
- } catch {
1969
- this.pageUrl = window.location.href;
1970
- }
2074
+ this.pageUrl = normalizePageUrl(window.location.href);
1971
2075
  }
1972
2076
  window.addEventListener("pagehide", this.boundFlushBeacon);
1973
2077
  if (typeof document !== "undefined") {
1974
2078
  document.addEventListener("visibilitychange", this.boundVisibilityChange);
1975
2079
  }
1976
2080
  bindActivityListeners();
2081
+ if (!this.pageviewEmitted) {
2082
+ this.pageviewEmitted = true;
2083
+ this.enqueue(TraceEventType.PAGEVIEW, "pageview", {});
2084
+ }
1977
2085
  this.flushTimer = setInterval(() => this.flush(), FLUSH_INTERVAL_MS);
1978
2086
  this.resendPersistedBatches();
1979
2087
  }
@@ -2143,6 +2251,7 @@ var BidsparkPrebidAnalytics = class _BidsparkPrebidAnalytics {
2143
2251
  }
2144
2252
  });
2145
2253
  }
2254
+ record.accumulatedTimeInViewMs = 0;
2146
2255
  }
2147
2256
  this.flushBeacon();
2148
2257
  } else if (document.visibilityState === "visible") {
@@ -2580,7 +2689,15 @@ var BidsparkPrebidAnalytics = class _BidsparkPrebidAnalytics {
2580
2689
  hasImpressionEmitted(slotId, refreshIndex, auctionId, creativeId, adId) {
2581
2690
  const rIndex = refreshIndex ?? this.slotRefreshIndices.get(slotId) ?? 0;
2582
2691
  const baseSlotKey = `${escapeKeyPart(slotId)}:${rIndex}`;
2692
+ if (auctionId ? this.slotEmittedImpressionKeys.has(`${baseSlotKey}:auc:${escapeKeyPart(auctionId)}`) : this.slotEmittedImpressionKeys.has(baseSlotKey)) {
2693
+ return true;
2694
+ }
2583
2695
  if (adId) {
2696
+ if (auctionId && this.slotEmittedImpressionKeys.has(
2697
+ `${baseSlotKey}:auc:${escapeKeyPart(auctionId)}:render`
2698
+ ) || this.slotEmittedImpressionKeys.has(`${baseSlotKey}:render`)) {
2699
+ return true;
2700
+ }
2584
2701
  if (auctionId) {
2585
2702
  if (this.slotEmittedImpressionKeys.has(
2586
2703
  `${baseSlotKey}:auc:${escapeKeyPart(auctionId)}:ad:${escapeKeyPart(adId)}`
@@ -2603,6 +2720,11 @@ var BidsparkPrebidAnalytics = class _BidsparkPrebidAnalytics {
2603
2720
  }
2604
2721
  return false;
2605
2722
  }
2723
+ if (auctionId && this.slotEmittedImpressionKeys.has(
2724
+ `${baseSlotKey}:auc:${escapeKeyPart(auctionId)}:ad-emitted`
2725
+ ) || this.slotEmittedImpressionKeys.has(`${baseSlotKey}:ad-emitted`)) {
2726
+ return true;
2727
+ }
2606
2728
  if (creativeId) {
2607
2729
  if (auctionId) {
2608
2730
  return this.slotEmittedImpressionKeys.has(
@@ -2611,10 +2733,10 @@ var BidsparkPrebidAnalytics = class _BidsparkPrebidAnalytics {
2611
2733
  }
2612
2734
  return this.slotEmittedImpressionKeys.has(`${baseSlotKey}:${escapeKeyPart(creativeId)}`);
2613
2735
  }
2614
- if (auctionId) {
2615
- return this.slotEmittedImpressionKeys.has(`${baseSlotKey}:auc:${escapeKeyPart(auctionId)}`);
2736
+ if (auctionId && this.slotEmittedImpressionKeys.has(`${baseSlotKey}:auc:${escapeKeyPart(auctionId)}:any`)) {
2737
+ return true;
2616
2738
  }
2617
- return this.slotEmittedImpressionKeys.has(baseSlotKey);
2739
+ return false;
2618
2740
  }
2619
2741
  markImpressionEmitted(slotId, refreshIndex, auctionId, creativeId, adId) {
2620
2742
  const baseSlotKey = `${escapeKeyPart(slotId)}:${refreshIndex}`;
@@ -2622,10 +2744,13 @@ var BidsparkPrebidAnalytics = class _BidsparkPrebidAnalytics {
2622
2744
  if (hasIdentity) {
2623
2745
  if (adId) {
2624
2746
  this.addImpressionKey(`${baseSlotKey}:ad:${escapeKeyPart(adId)}`);
2747
+ this.addImpressionKey(`${baseSlotKey}:ad-emitted`);
2625
2748
  if (auctionId) {
2626
2749
  this.addImpressionKey(
2627
2750
  `${baseSlotKey}:auc:${escapeKeyPart(auctionId)}:ad:${escapeKeyPart(adId)}`
2628
2751
  );
2752
+ this.addImpressionKey(`${baseSlotKey}:auc:${escapeKeyPart(auctionId)}:any`);
2753
+ this.addImpressionKey(`${baseSlotKey}:auc:${escapeKeyPart(auctionId)}:ad-emitted`);
2629
2754
  }
2630
2755
  }
2631
2756
  if (creativeId && creativeId !== adId) {
@@ -2634,15 +2759,21 @@ var BidsparkPrebidAnalytics = class _BidsparkPrebidAnalytics {
2634
2759
  this.addImpressionKey(
2635
2760
  `${baseSlotKey}:auc:${escapeKeyPart(auctionId)}:${escapeKeyPart(creativeId)}`
2636
2761
  );
2762
+ this.addImpressionKey(`${baseSlotKey}:auc:${escapeKeyPart(auctionId)}:any`);
2637
2763
  }
2638
2764
  }
2639
- if (auctionId) {
2640
- this.addImpressionKey(`${baseSlotKey}:auc:${escapeKeyPart(auctionId)}`);
2765
+ if (!adId) {
2766
+ this.addImpressionKey(`${baseSlotKey}:render`);
2767
+ if (auctionId) {
2768
+ this.addImpressionKey(`${baseSlotKey}:auc:${escapeKeyPart(auctionId)}:render`);
2769
+ }
2641
2770
  }
2642
2771
  } else {
2643
2772
  this.addImpressionKey(baseSlotKey);
2773
+ this.addImpressionKey(`${baseSlotKey}:render`);
2644
2774
  if (auctionId) {
2645
2775
  this.addImpressionKey(`${baseSlotKey}:auc:${escapeKeyPart(auctionId)}`);
2776
+ this.addImpressionKey(`${baseSlotKey}:auc:${escapeKeyPart(auctionId)}:render`);
2646
2777
  }
2647
2778
  }
2648
2779
  }
@@ -2665,13 +2796,12 @@ var BidsparkPrebidAnalytics = class _BidsparkPrebidAnalytics {
2665
2796
  );
2666
2797
  const bidTrace = {
2667
2798
  bidder: rawBid.bidderCode || rawBid.bidder || cached?.bidTrace?.bidder || "",
2668
- cpm: Number.isFinite(rawBid.originalCpm) ? rawBid.originalCpm : Number.isFinite(rawBid.cpm) ? rawBid.cpm : cached?.bidTrace?.cpm ?? 0,
2669
- currency: rawBid.originalCurrency ?? rawBid.currency ?? cached?.bidTrace?.currency ?? "USD",
2799
+ ...resolveBidPricing(rawBid, cached?.bidTrace),
2670
2800
  ...parseBidDimensions(rawBid).width ? parseBidDimensions(rawBid) : cached?.bidTrace ? { width: cached.bidTrace.width, height: cached.bidTrace.height } : parseBidDimensions(rawBid),
2671
2801
  dealId: rawBid.dealId || cached?.bidTrace?.dealId || "",
2672
2802
  mediaType,
2673
- latencyMs: Number.isFinite(rawBid.timeToRespond) ? rawBid.timeToRespond : cached?.bidTrace?.latencyMs ?? 0,
2674
- advertiserDomain: rawBid.meta?.advertiserDomains?.[0] || cached?.bidTrace?.advertiserDomain || "",
2803
+ latencyMs: Number.isFinite(rawBid.timeToRespond) ? rawBid.timeToRespond : Number.isFinite(rawBid.latencyMs) ? rawBid.latencyMs : cached?.bidTrace?.latencyMs ?? 0,
2804
+ advertiserDomain: rawBid.advertiserDomain || rawBid.meta?.advertiserDomains?.[0] || (Array.isArray(rawBid.adomain) ? rawBid.adomain[0] : "") || cached?.bidTrace?.advertiserDomain || "",
2675
2805
  creativeId: rawBid.creativeId || cached?.bidTrace?.creativeId || ""
2676
2806
  };
2677
2807
  if (isDifferentAuction && targetSlot) {
@@ -2842,16 +2972,16 @@ var BidsparkPrebidAnalytics = class _BidsparkPrebidAnalytics {
2842
2972
  const handler = (evt) => {
2843
2973
  const detail = evt.detail;
2844
2974
  if (detail) {
2845
- this.recordConsent(detail);
2846
2975
  this.followConsent(detail);
2976
+ this.recordConsent(detail);
2847
2977
  }
2848
2978
  };
2849
2979
  document.addEventListener("ads:consent", handler);
2850
2980
  const win = typeof window !== "undefined" ? window : {};
2851
2981
  const existingRecord = win[this.config.alias]?.consent?.getRecord?.() || null;
2852
2982
  if (existingRecord) {
2853
- this.recordConsent(existingRecord);
2854
2983
  this.followConsent(existingRecord);
2984
+ this.recordConsent(existingRecord);
2855
2985
  }
2856
2986
  let detached = false;
2857
2987
  return () => {
@@ -2860,15 +2990,23 @@ var BidsparkPrebidAnalytics = class _BidsparkPrebidAnalytics {
2860
2990
  document.removeEventListener("ads:consent", handler);
2861
2991
  };
2862
2992
  }
2863
- /** Device storage follows the decision; a refusal also drops the stored session. */
2993
+ /** Device storage follows the decision; a refusal also drops stored session and user IDs. */
2864
2994
  followConsent(record) {
2865
2995
  const allowed = storageAllowedBy(record);
2866
- if (!allowed) clearStoredSession();
2996
+ if (!allowed) {
2997
+ clearStoredSession();
2998
+ clearStoredUserId();
2999
+ }
2867
3000
  _BidsparkPrebidAnalytics.setStorageAllowed(allowed);
2868
3001
  }
2869
3002
  setUserId(userId) {
3003
+ this.configuredUserId = userId;
2870
3004
  this.config.userId = userId;
2871
- if (userId) this.config.visitorId = userId;
3005
+ if (userId) {
3006
+ this.config.visitorId = userId;
3007
+ this.managesVisitorId = false;
3008
+ this.visitorKeyLocked = true;
3009
+ }
2872
3010
  if (this.config.storageAllowed && typeof localStorage !== "undefined") {
2873
3011
  try {
2874
3012
  if (userId) {
@@ -2882,6 +3020,8 @@ var BidsparkPrebidAnalytics = class _BidsparkPrebidAnalytics {
2882
3020
  }
2883
3021
  setVisitorId(visitorId) {
2884
3022
  this.config.visitorId = visitorId;
3023
+ this.managesVisitorId = !visitorId;
3024
+ if (visitorId) this.visitorKeyLocked = true;
2885
3025
  }
2886
3026
  setSampleRate(sampleRate) {
2887
3027
  this.config.sampleRate = normalizeSampleRate(sampleRate);
@@ -2891,7 +3031,32 @@ var BidsparkPrebidAnalytics = class _BidsparkPrebidAnalytics {
2891
3031
  this.managesSessionId = !sessionId;
2892
3032
  if (this.managesSessionId) this.config.sessionId = getOrCreateSessionId();
2893
3033
  }
2894
- navigate(pageviewId, pageUrl) {
3034
+ getSessionId() {
3035
+ this.syncSessionId();
3036
+ return this.config.sessionId;
3037
+ }
3038
+ getPageviewId() {
3039
+ return this.config.pageviewId;
3040
+ }
3041
+ getContext() {
3042
+ return { pageviewId: this.getPageviewId(), sessionId: this.getSessionId() };
3043
+ }
3044
+ /**
3045
+ * Continues the pageview of the instance this one replaces: re-registering on
3046
+ * the same page must not count a second pageview or replay Prebid's history.
3047
+ */
3048
+ continuePageview(previous) {
3049
+ this.config.pageviewId = previous.config.pageviewId;
3050
+ this.pageviewEmitted = previous.pageviewEmitted;
3051
+ this.deduper = previous.deduper;
3052
+ this.replayedEventCount = previous.replayedEventCount;
3053
+ }
3054
+ navigate(pageviewId, pageUrl = "") {
3055
+ const nextPageUrl = normalizePageUrl(pageUrl);
3056
+ if (this.isEnabled && this.pageviewEmitted && pageviewId !== "" && pageviewId === this.config.pageviewId && (!nextPageUrl || nextPageUrl === this.pageUrl)) {
3057
+ recordActivity();
3058
+ return;
3059
+ }
2895
3060
  this.disable();
2896
3061
  this.slotRefreshIndices.clear();
2897
3062
  this.slotNames.clear();
@@ -2899,26 +3064,24 @@ var BidsparkPrebidAnalytics = class _BidsparkPrebidAnalytics {
2899
3064
  this.floorDecisions.clear();
2900
3065
  this.recordedConsentKeys.clear();
2901
3066
  this.consentCount = 0;
3067
+ this.pageviewEmitted = false;
2902
3068
  this.config.pageviewId = pageviewId || generateUUID2();
2903
- if (pageUrl) {
2904
- try {
2905
- const u = new URL(pageUrl);
2906
- this.pageUrl = u.origin + u.pathname;
2907
- } catch {
2908
- this.pageUrl = pageUrl;
2909
- }
2910
- } else {
2911
- this.pageUrl = "";
2912
- }
3069
+ this.pageUrl = nextPageUrl;
2913
3070
  this.logger.resetTiming();
3071
+ const prevSessionId = this.config.sessionId;
2914
3072
  recordActivity();
3073
+ this.syncSessionId();
3074
+ if (!this.config.visitorId || this.config.sessionId !== prevSessionId && this.config.visitorId === prevSessionId) {
3075
+ this.config.visitorId = this.config.userId || this.config.sessionId;
3076
+ }
2915
3077
  this.enable();
2916
3078
  }
2917
- recordAuctionStart(auctionId, adUnitCode = "", bidderCount) {
3079
+ recordAuctionStart(auctionId, adUnitCode = "", bidderCount, mediaType) {
2918
3080
  if (!this.isEnabled || !this.config.auctionEnabled) return;
2919
3081
  this.enqueue(TraceEventType.AUCTION_START, "auctionStart", {
2920
3082
  auctionId,
2921
3083
  adUnitCode,
3084
+ ...mediaType ? { bid: { mediaType } } : {},
2922
3085
  ...typeof bidderCount === "number" ? { metadata: { bidderCount: String(bidderCount) } } : {}
2923
3086
  });
2924
3087
  }
@@ -2978,7 +3141,7 @@ var BidsparkPrebidAnalytics = class _BidsparkPrebidAnalytics {
2978
3141
  data.bids.forEach((bid) => {
2979
3142
  const adUnitCode = bid.adUnitCode || "";
2980
3143
  const mediaTypes = Object.keys(bid.mediaTypes || {});
2981
- const mediaType = mediaTypes.length > 0 ? mediaTypes[0] : bid.mediaType || "banner";
3144
+ const mediaType = bid.mediaType || (bid.mediaTypes?.video ? "video" : mediaTypes.length > 0 ? mediaTypes[0] : "banner");
2982
3145
  const key = `${bidder}:${adUnitCode}`;
2983
3146
  if (!auctionMap.has(key)) {
2984
3147
  auctionMap.set(key, {
@@ -2996,15 +3159,15 @@ var BidsparkPrebidAnalytics = class _BidsparkPrebidAnalytics {
2996
3159
  const auctionId = data.auctionId || "";
2997
3160
  const bidder = data.bidderCode || data.bidder || "";
2998
3161
  const adUnitCode = data.adUnitCode || "";
2999
- const cpm = Number.isFinite(data.originalCpm) ? data.originalCpm : Number.isFinite(data.cpm) ? data.cpm : 0;
3162
+ const pricing = resolveBidPricing(data);
3163
+ const cpm = pricing.cpm;
3000
3164
  const latencyMs = Number.isFinite(data.timeToRespond) ? data.timeToRespond : 0;
3001
3165
  const resMediaTypes = Object.keys(data.mediaTypes || {});
3002
3166
  const mediaType = data.mediaType || (data.mediaTypes?.video ? "video" : resMediaTypes.length > 0 ? resMediaTypes[0] : "banner");
3003
3167
  if (!auctionId || !bidder) return;
3004
3168
  const bid = cpm > 0 ? {
3005
3169
  bidder,
3006
- cpm,
3007
- currency: data.originalCurrency ?? data.currency ?? "USD",
3170
+ ...pricing,
3008
3171
  ...parseBidDimensions(data),
3009
3172
  dealId: data.dealId || "",
3010
3173
  mediaType,
@@ -3054,8 +3217,7 @@ var BidsparkPrebidAnalytics = class _BidsparkPrebidAnalytics {
3054
3217
  const resolvedMediaType = data.mediaType || (data.mediaTypes?.video ? "video" : wonMediaTypes.length > 0 ? wonMediaTypes[0] : "banner");
3055
3218
  const bidTrace = {
3056
3219
  bidder: data.bidderCode || data.bidder || "",
3057
- cpm: Number.isFinite(data.originalCpm) ? data.originalCpm : Number.isFinite(data.cpm) ? data.cpm : 0,
3058
- currency: data.originalCurrency ?? data.currency ?? "USD",
3220
+ ...resolveBidPricing(data),
3059
3221
  ...parseBidDimensions(data),
3060
3222
  dealId: data.dealId || "",
3061
3223
  mediaType: resolvedMediaType,
@@ -3149,8 +3311,7 @@ var BidsparkPrebidAnalytics = class _BidsparkPrebidAnalytics {
3149
3311
  const resolvedMediaType = isVideo ? "video" : mediaType || cached?.bidTrace?.mediaType || "banner";
3150
3312
  const bidTrace = {
3151
3313
  bidder: bid.bidderCode || bid.bidder || cached?.bidTrace?.bidder || "",
3152
- cpm: Number.isFinite(bid.originalCpm) ? bid.originalCpm : Number.isFinite(bid.cpm) ? bid.cpm : cached?.bidTrace?.cpm ?? 0,
3153
- currency: bid.originalCurrency ?? bid.currency ?? cached?.bidTrace?.currency ?? "USD",
3314
+ ...resolveBidPricing(bid, cached?.bidTrace),
3154
3315
  ...parseBidDimensions(bid).width ? parseBidDimensions(bid) : cached?.bidTrace ? { width: cached.bidTrace.width, height: cached.bidTrace.height } : parseBidDimensions(bid),
3155
3316
  dealId: bid.dealId || cached?.bidTrace?.dealId || "",
3156
3317
  mediaType: resolvedMediaType,
@@ -3182,35 +3343,28 @@ var BidsparkPrebidAnalytics = class _BidsparkPrebidAnalytics {
3182
3343
  }
3183
3344
  }
3184
3345
  const currentRefreshIndex = adUnitCode ? this.slotRefreshIndices.get(adUnitCode) ?? (record ? record.refreshIndex : 0) : 0;
3185
- if (!isVideo) {
3186
- const alreadyEmitted = adUnitCode ? this.hasImpressionEmitted(
3187
- adUnitCode,
3188
- currentRefreshIndex,
3189
- resolvedAuctionId,
3190
- bidTrace.creativeId
3191
- ) : false;
3192
- if (!alreadyEmitted) {
3193
- if (adUnitCode) {
3194
- this.markImpressionEmitted(
3195
- adUnitCode,
3196
- currentRefreshIndex,
3197
- resolvedAuctionId,
3198
- bidTrace.creativeId
3199
- );
3200
- }
3201
- this.enqueue(TraceEventType.IMPRESSION, "impression", {
3202
- auctionId: resolvedAuctionId,
3203
- transactionId: resolvedTransactionId,
3346
+ const alreadyEmitted = adUnitCode ? this.hasImpressionEmitted(
3347
+ adUnitCode,
3348
+ currentRefreshIndex,
3349
+ resolvedAuctionId,
3350
+ bidTrace.creativeId
3351
+ ) : false;
3352
+ if (!alreadyEmitted) {
3353
+ if (adUnitCode) {
3354
+ this.markImpressionEmitted(
3204
3355
  adUnitCode,
3205
- bid: bidTrace,
3206
- metadata: { refresh_index: String(currentRefreshIndex) }
3207
- });
3356
+ currentRefreshIndex,
3357
+ resolvedAuctionId,
3358
+ bidTrace.creativeId
3359
+ );
3208
3360
  }
3209
- } else {
3210
- this.log(
3211
- "DEBUG",
3212
- `adRenderSucceeded received for video ad unit ${adUnitCode || "unknown"}: suppressing IMPRESSION until verified video lifecycle event`
3213
- );
3361
+ this.enqueue(TraceEventType.IMPRESSION, "impression", {
3362
+ auctionId: resolvedAuctionId,
3363
+ transactionId: resolvedTransactionId,
3364
+ adUnitCode,
3365
+ bid: bidTrace,
3366
+ metadata: { refresh_index: String(currentRefreshIndex) }
3367
+ });
3214
3368
  }
3215
3369
  if (this.config.viewabilityEnabled && typeof document !== "undefined") {
3216
3370
  let el = adUnitCode && this.slotElements.get(adUnitCode) || null;
@@ -3264,9 +3418,7 @@ var BidsparkPrebidAnalytics = class _BidsparkPrebidAnalytics {
3264
3418
  enqueue(type, eventName, data) {
3265
3419
  if (!this.isEnabled) return;
3266
3420
  if (!this.shouldSample(type)) return;
3267
- if (this.managesSessionId && sessionNeedsSync()) {
3268
- this.config.sessionId = getOrCreateSessionId();
3269
- }
3421
+ this.syncSessionId();
3270
3422
  const protoEvent = {
3271
3423
  eventId: idToBytes(generateUUID2()),
3272
3424
  timestampMs: Date.now(),
@@ -3280,19 +3432,20 @@ var BidsparkPrebidAnalytics = class _BidsparkPrebidAnalytics {
3280
3432
  namespace: data.ns || data.namespace || ""
3281
3433
  };
3282
3434
  if (data.bid) {
3435
+ const dims = parseBidDimensions(data.bid);
3283
3436
  protoEvent.bid = {
3284
- bidder: data.bid.bidder || "",
3437
+ bidder: data.bid.bidder || data.bid.bidderCode || "",
3285
3438
  cpm: Number.isFinite(data.bid.cpm) ? data.bid.cpm : 0,
3286
3439
  // No defaults here: handlers with a priced bid (bidWon, impression)
3287
3440
  // set currency/mediaType themselves. Defaulting for the rest would
3288
3441
  // stamp fake "USD"/"banner" on rows that carried no bid.
3289
3442
  currency: data.bid.currency || "",
3290
- width: Number.isFinite(data.bid.width) ? data.bid.width : 0,
3291
- height: Number.isFinite(data.bid.height) ? data.bid.height : 0,
3443
+ width: dims.width,
3444
+ height: dims.height,
3292
3445
  dealId: data.bid.dealId || "",
3293
3446
  mediaType: data.bid.mediaType || "",
3294
- latencyMs: Number.isFinite(data.bid.latencyMs) ? data.bid.latencyMs : 0,
3295
- advertiserDomain: data.bid.advertiserDomain || "",
3447
+ latencyMs: Number.isFinite(data.bid.latencyMs) ? data.bid.latencyMs : Number.isFinite(data.bid.timeToRespond) ? data.bid.timeToRespond : 0,
3448
+ advertiserDomain: data.bid.advertiserDomain || data.bid.meta?.advertiserDomains?.[0] || (Array.isArray(data.bid.adomain) ? data.bid.adomain[0] : "") || "",
3296
3449
  creativeId: data.bid.creativeId || ""
3297
3450
  };
3298
3451
  }
@@ -3357,12 +3510,19 @@ var BidsparkPrebidAnalytics = class _BidsparkPrebidAnalytics {
3357
3510
  }, this.config.revenueFlushDelayMs);
3358
3511
  }
3359
3512
  }
3513
+ // Follows the page's session, which consent or a lapse can move after this
3514
+ // instance took its ID. A settled session is read as is: only a pageview or
3515
+ // visitor input may start a new one, never a flush.
3516
+ syncSessionId() {
3517
+ if (!this.managesSessionId) return;
3518
+ this.config.sessionId = sessionNeedsSync() ? getOrCreateSessionId() : inMemorySessionId;
3519
+ }
3360
3520
  isSampledVisitor() {
3361
- const key = this.config.userId || this.config.visitorId || this.config.pageviewId;
3521
+ const key = this.config.visitorId || this.config.sessionId;
3362
3522
  return isVisitorSampled(key, this.config.sampleRate);
3363
3523
  }
3364
3524
  shouldSample(type) {
3365
- if (!this.isSampledVisitor()) {
3525
+ if (this.visitorKeyLocked && !this.isSampledVisitor()) {
3366
3526
  return false;
3367
3527
  }
3368
3528
  if (type === TraceEventType.ERROR) {
@@ -3389,8 +3549,15 @@ var BidsparkPrebidAnalytics = class _BidsparkPrebidAnalytics {
3389
3549
  // Shared by flush() and flushBeacon().
3390
3550
  drainBatch() {
3391
3551
  if (this.queue.length === 0 || !this.config.endpoint) return null;
3552
+ this.syncSessionId();
3553
+ this.visitorKeyLocked = true;
3554
+ if (!this.isSampledVisitor()) {
3555
+ this.queue = [];
3556
+ return null;
3557
+ }
3392
3558
  const domain = typeof window !== "undefined" ? window.location.hostname : "";
3393
3559
  const sampleRate = normalizeSampleRate(this.config.sampleRate);
3560
+ const automated = isAutomatedClient();
3394
3561
  const buildBatch = (evts) => ({
3395
3562
  propertyId: idToBytes(this.config.propertyId),
3396
3563
  pageviewId: idToBytes(this.config.pageviewId),
@@ -3402,7 +3569,8 @@ var BidsparkPrebidAnalytics = class _BidsparkPrebidAnalytics {
3402
3569
  userId: this.config.userId || "",
3403
3570
  domain,
3404
3571
  events: evts,
3405
- ...sampleRate < 1 ? { sampleRate } : {}
3572
+ ...sampleRate < 1 ? { sampleRate } : {},
3573
+ ...automated ? { automated: true } : {}
3406
3574
  });
3407
3575
  const headerBytes = TraceEventBatch.encode(buildBatch([])).finish().byteLength;
3408
3576
  for (; ; ) {
@@ -3503,8 +3671,10 @@ var BidsparkPrebidAnalytics = class _BidsparkPrebidAnalytics {
3503
3671
  flush() {
3504
3672
  if (Date.now() < this.nextSendAllowedAt) return;
3505
3673
  let payload = this.drainBatch();
3674
+ let useKeepalive = true;
3506
3675
  while (payload) {
3507
- this.sendFetch(payload, false);
3676
+ this.sendFetch(payload, useKeepalive);
3677
+ useKeepalive = false;
3508
3678
  if (Date.now() < this.nextSendAllowedAt) break;
3509
3679
  payload = this.drainBatch();
3510
3680
  }
@@ -3590,7 +3760,7 @@ var BidsparkPrebidAnalytics = class _BidsparkPrebidAnalytics {
3590
3760
  // event timestamps, so late rows attribute correctly.
3591
3761
  resendPersistedBatches() {
3592
3762
  try {
3593
- if (typeof localStorage === "undefined" || !this.config.endpoint) return;
3763
+ if (!storageAllowed || typeof localStorage === "undefined" || !this.config.endpoint) return;
3594
3764
  if (!this.config.propertyId) return;
3595
3765
  const ownPrefix = this.pendingKeyPrefix();
3596
3766
  const keys = [];
@@ -3708,6 +3878,7 @@ function registerPrebidAnalytics(pbjsGlobalName = "pbjs", defaults) {
3708
3878
  }
3709
3879
  }
3710
3880
  const analytics = new BidsparkPrebidAnalytics(merged);
3881
+ if (previous && !merged.pageviewId) analytics.continuePageview(previous);
3711
3882
  analytics.enable();
3712
3883
  win._bidsparkPrebidAnalytics = analytics;
3713
3884
  return analytics;
@@ -4148,8 +4319,8 @@ function attachImaAdsManager(analytics, adsManager, options) {
4148
4319
  }
4149
4320
  const slotId = options?.slotId || options?.adUnitCode || "video";
4150
4321
  const adUnitCode = options?.adUnitCode || slotId;
4151
- const auctionId = options?.auctionId || "";
4152
- const transactionId = options?.transactionId || "";
4322
+ const auctionId = options?.auctionId || options?.bid?.auctionId || "";
4323
+ const transactionId = options?.transactionId || options?.bid?.transactionId || "";
4153
4324
  const getWinningBid = (adData) => {
4154
4325
  let cached = (auctionId ? self.getCachedBid(`${auctionId}:${adUnitCode}`) : void 0) || (auctionId && slotId !== adUnitCode ? self.getCachedBid(`${auctionId}:${slotId}`) : void 0) || (adUnitCode !== "video" ? self.getCachedBid(adUnitCode) : void 0) || (slotId !== adUnitCode && slotId !== "video" ? self.getCachedBid(slotId) : void 0);
4155
4326
  if (!cached) {
@@ -4212,11 +4383,7 @@ function attachImaAdsManager(analytics, adsManager, options) {
4212
4383
  const resolvedTransactionId = transactionId || cached?.transactionId || "";
4213
4384
  const creativeId = session.creativeId || adData.creativeId || cached?.bidTrace?.creativeId || options?.bid?.creativeId || "";
4214
4385
  const stableAdIdentity = session.adId || adData.adId || creativeId || session.sessionId;
4215
- const mergedBid = {
4216
- ...cached?.rawBid || options?.bid,
4217
- mediaType: "video",
4218
- creativeId
4219
- };
4386
+ const mergedBid = resolveMergedBid(cached, creativeId);
4220
4387
  const emitted = self.recordImpression(resolvedSlotId, {
4221
4388
  adUnitCode: resolvedAdUnitCode,
4222
4389
  auctionId: resolvedAuctionId,
@@ -4249,6 +4416,19 @@ function attachImaAdsManager(analytics, adsManager, options) {
4249
4416
  }
4250
4417
  }
4251
4418
  };
4419
+ const resolveMergedBid = (cached, extraCreativeId) => {
4420
+ const rawDims = parseBidDimensions(cached?.rawBid);
4421
+ const optDims = parseBidDimensions(options?.bid);
4422
+ const creativeId = extraCreativeId || options?.bid?.creativeId || cached?.bidTrace?.creativeId || "";
4423
+ return {
4424
+ ...cached?.rawBid,
4425
+ ...options?.bid,
4426
+ ...options?.bid?.bidder || options?.bid?.bidderCode ? { bidderCode: options.bid.bidderCode || options.bid.bidder } : {},
4427
+ ...optDims.width > 0 && optDims.height > 0 ? optDims : rawDims,
4428
+ mediaType: "video",
4429
+ ...creativeId ? { creativeId } : {}
4430
+ };
4431
+ };
4252
4432
  const onMilestone = (milestone) => {
4253
4433
  self.log("DEBUG", `IMA AdEvent milestone: ${milestone}`);
4254
4434
  if (options?.onMilestone) {
@@ -4267,7 +4447,7 @@ function attachImaAdsManager(analytics, adsManager, options) {
4267
4447
  auctionId: auctionId || cached?.auctionId,
4268
4448
  transactionId: transactionId || cached?.transactionId,
4269
4449
  adUnitCode: resolvedAdUnitCode,
4270
- bid: cached?.bidTrace || options?.bid,
4450
+ bid: cached || options?.bid ? resolveMergedBid(cached) : void 0,
4271
4451
  metadata: {
4272
4452
  ...options?.metadata,
4273
4453
  media_type: "video"
@@ -4283,7 +4463,7 @@ function attachImaAdsManager(analytics, adsManager, options) {
4283
4463
  adUnitCode: resolvedAdUnitCode,
4284
4464
  auctionId: auctionId || cached?.auctionId,
4285
4465
  transactionId: transactionId || cached?.transactionId,
4286
- bid: cached?.bidTrace || options?.bid,
4466
+ bid: cached || options?.bid ? resolveMergedBid(cached) : void 0,
4287
4467
  metadata: options?.metadata
4288
4468
  });
4289
4469
  if (options?.onError) {
@@ -4695,6 +4875,24 @@ function getAdsGlobal(alias = DEFAULT_ALIAS) {
4695
4875
  const win = window;
4696
4876
  win[alias] = win[alias] || { q: [] };
4697
4877
  const sdk = win[alias];
4878
+ if (!sdk.getContext) {
4879
+ sdk.getContext = () => {
4880
+ const inst = win._bidsparkPrebidAnalytics;
4881
+ return inst?.getContext ? inst.getContext() : { pageviewId: inst?.getPageviewId?.() || "", sessionId: getOrCreateSessionId() };
4882
+ };
4883
+ }
4884
+ if (!sdk.getSessionId) {
4885
+ sdk.getSessionId = () => {
4886
+ const inst = win._bidsparkPrebidAnalytics;
4887
+ return inst?.getSessionId ? inst.getSessionId() : getOrCreateSessionId();
4888
+ };
4889
+ }
4890
+ if (!sdk.getPageviewId) {
4891
+ sdk.getPageviewId = () => {
4892
+ const inst = win._bidsparkPrebidAnalytics;
4893
+ return inst?.getPageviewId ? inst.getPageviewId() : "";
4894
+ };
4895
+ }
4698
4896
  if (!sdk.attachVideoPlayer) {
4699
4897
  sdk.attachVideoPlayer = (target, options) => {
4700
4898
  const inst = win._bidsparkPrebidAnalytics;
@@ -4740,14 +4938,15 @@ export {
4740
4938
  getPrebidEventKey,
4741
4939
  idToBytes,
4742
4940
  idToString,
4941
+ isAutomatedClient,
4743
4942
  isSessionActive,
4744
4943
  isTrustedEndpoint,
4745
4944
  isVisitorSampled,
4746
4945
  normalizeLogLevel,
4747
4946
  normalizeSampleRate,
4748
4947
  onSessionResume,
4948
+ parseBidDimensions,
4749
4949
  registerPrebidAnalytics,
4750
- resetTcfListener,
4751
4950
  sendEvents,
4752
4951
  sessionStarted
4753
4952
  };