@amplitude/session-replay-browser 1.52.0-codexsemantic-review-integration.0 → 1.52.0

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.
Files changed (111) hide show
  1. package/lib/cjs/config/joined-config.d.ts +1 -0
  2. package/lib/cjs/config/joined-config.d.ts.map +1 -1
  3. package/lib/cjs/config/joined-config.js +10 -1
  4. package/lib/cjs/config/joined-config.js.map +1 -1
  5. package/lib/cjs/config/local-config.d.ts +3 -3
  6. package/lib/cjs/config/local-config.d.ts.map +1 -1
  7. package/lib/cjs/config/local-config.js +5 -3
  8. package/lib/cjs/config/local-config.js.map +1 -1
  9. package/lib/cjs/config/types.d.ts +17 -9
  10. package/lib/cjs/config/types.d.ts.map +1 -1
  11. package/lib/cjs/config/types.js.map +1 -1
  12. package/lib/cjs/helpers.d.ts.map +1 -1
  13. package/lib/cjs/helpers.js +7 -4
  14. package/lib/cjs/helpers.js.map +1 -1
  15. package/lib/cjs/hooks/click.d.ts +2 -8
  16. package/lib/cjs/hooks/click.d.ts.map +1 -1
  17. package/lib/cjs/hooks/click.js +3 -42
  18. package/lib/cjs/hooks/click.js.map +1 -1
  19. package/lib/cjs/index.d.ts +1 -0
  20. package/lib/cjs/index.d.ts.map +1 -1
  21. package/lib/cjs/index.js.map +1 -1
  22. package/lib/cjs/plugins/url-tracking-plugin.d.ts +0 -3
  23. package/lib/cjs/plugins/url-tracking-plugin.d.ts.map +1 -1
  24. package/lib/cjs/plugins/url-tracking-plugin.js +9 -16
  25. package/lib/cjs/plugins/url-tracking-plugin.js.map +1 -1
  26. package/lib/cjs/semantic-identity.d.ts +1 -1
  27. package/lib/cjs/semantic-identity.d.ts.map +1 -1
  28. package/lib/cjs/semantic-identity.js +4 -3
  29. package/lib/cjs/semantic-identity.js.map +1 -1
  30. package/lib/cjs/semantic-types.d.ts +45 -0
  31. package/lib/cjs/semantic-types.d.ts.map +1 -0
  32. package/lib/cjs/semantic-types.js +3 -0
  33. package/lib/cjs/semantic-types.js.map +1 -0
  34. package/lib/cjs/semantic.d.ts +9 -12
  35. package/lib/cjs/semantic.d.ts.map +1 -1
  36. package/lib/cjs/semantic.js +17 -90
  37. package/lib/cjs/semantic.js.map +1 -1
  38. package/lib/cjs/session-replay.d.ts +39 -2
  39. package/lib/cjs/session-replay.d.ts.map +1 -1
  40. package/lib/cjs/session-replay.js +267 -132
  41. package/lib/cjs/session-replay.js.map +1 -1
  42. package/lib/cjs/utils/rrweb.d.ts +0 -3
  43. package/lib/cjs/utils/rrweb.d.ts.map +1 -1
  44. package/lib/cjs/utils/rrweb.js.map +1 -1
  45. package/lib/cjs/version.d.ts +1 -1
  46. package/lib/cjs/version.d.ts.map +1 -1
  47. package/lib/cjs/version.js +1 -1
  48. package/lib/cjs/version.js.map +1 -1
  49. package/lib/cjs/window-focus.d.ts +40 -0
  50. package/lib/cjs/window-focus.d.ts.map +1 -0
  51. package/lib/cjs/window-focus.js +182 -0
  52. package/lib/cjs/window-focus.js.map +1 -0
  53. package/lib/esm/config/joined-config.d.ts +1 -0
  54. package/lib/esm/config/joined-config.d.ts.map +1 -1
  55. package/lib/esm/config/joined-config.js +10 -1
  56. package/lib/esm/config/joined-config.js.map +1 -1
  57. package/lib/esm/config/local-config.d.ts +3 -3
  58. package/lib/esm/config/local-config.d.ts.map +1 -1
  59. package/lib/esm/config/local-config.js +5 -3
  60. package/lib/esm/config/local-config.js.map +1 -1
  61. package/lib/esm/config/types.d.ts +17 -9
  62. package/lib/esm/config/types.d.ts.map +1 -1
  63. package/lib/esm/config/types.js.map +1 -1
  64. package/lib/esm/helpers.d.ts.map +1 -1
  65. package/lib/esm/helpers.js +8 -5
  66. package/lib/esm/helpers.js.map +1 -1
  67. package/lib/esm/hooks/click.d.ts +2 -8
  68. package/lib/esm/hooks/click.d.ts.map +1 -1
  69. package/lib/esm/hooks/click.js +3 -42
  70. package/lib/esm/hooks/click.js.map +1 -1
  71. package/lib/esm/index.d.ts +1 -0
  72. package/lib/esm/index.d.ts.map +1 -1
  73. package/lib/esm/index.js.map +1 -1
  74. package/lib/esm/plugins/url-tracking-plugin.d.ts +0 -3
  75. package/lib/esm/plugins/url-tracking-plugin.d.ts.map +1 -1
  76. package/lib/esm/plugins/url-tracking-plugin.js +9 -16
  77. package/lib/esm/plugins/url-tracking-plugin.js.map +1 -1
  78. package/lib/esm/semantic-identity.d.ts +1 -1
  79. package/lib/esm/semantic-identity.d.ts.map +1 -1
  80. package/lib/esm/semantic-identity.js +4 -3
  81. package/lib/esm/semantic-identity.js.map +1 -1
  82. package/lib/esm/semantic-types.d.ts +45 -0
  83. package/lib/esm/semantic-types.d.ts.map +1 -0
  84. package/lib/esm/semantic-types.js +2 -0
  85. package/lib/esm/semantic-types.js.map +1 -0
  86. package/lib/esm/semantic.d.ts +9 -12
  87. package/lib/esm/semantic.d.ts.map +1 -1
  88. package/lib/esm/semantic.js +17 -90
  89. package/lib/esm/semantic.js.map +1 -1
  90. package/lib/esm/session-replay.d.ts +39 -2
  91. package/lib/esm/session-replay.d.ts.map +1 -1
  92. package/lib/esm/session-replay.js +267 -132
  93. package/lib/esm/session-replay.js.map +1 -1
  94. package/lib/esm/utils/rrweb.d.ts +0 -3
  95. package/lib/esm/utils/rrweb.d.ts.map +1 -1
  96. package/lib/esm/utils/rrweb.js.map +1 -1
  97. package/lib/esm/version.d.ts +1 -1
  98. package/lib/esm/version.d.ts.map +1 -1
  99. package/lib/esm/version.js +1 -1
  100. package/lib/esm/version.js.map +1 -1
  101. package/lib/esm/window-focus.d.ts +40 -0
  102. package/lib/esm/window-focus.d.ts.map +1 -0
  103. package/lib/esm/window-focus.js +179 -0
  104. package/lib/esm/window-focus.js.map +1 -0
  105. package/lib/scripts/index-min.js +1 -1
  106. package/lib/scripts/index-min.js.gz +0 -0
  107. package/lib/scripts/index-min.js.map +1 -1
  108. package/lib/scripts/session-replay-browser-min.js +1 -1
  109. package/lib/scripts/session-replay-browser-min.js.gz +0 -0
  110. package/lib/scripts/session-replay-browser-min.js.map +1 -1
  111. package/package.json +4 -4
@@ -1,7 +1,6 @@
1
1
  import { __assign, __awaiter, __generator, __read, __spreadArray, __values } from "tslib";
2
2
  import { ReplaySemanticAdapter } from './semantic';
3
3
  import { getSemanticIdentity } from './semantic-identity';
4
- import { finder } from './libs/finder';
5
4
  import { createPrivacyRecorderOptions, getBlockSelectors, getMaskTextSelectors, } from '@amplitude/session-replay-dom-privacy';
6
5
  import { getAnalyticsConnector, getGlobalScope, Logger, returnWrapper, SpecialEventType, generateHashCode, getOrCreateWindowMessenger, enableBackgroundCapture, AMPLITUDE_ORIGINS_MAP, } from '@amplitude/analytics-core';
7
6
  // Import only specific types to avoid pulling in the entire rrweb-types package
@@ -22,6 +21,7 @@ import { isSessionInSample } from './sampling';
22
21
  import { VERSION } from './version';
23
22
  import { createUrlTrackingPlugin, subscribeToUrlChanges } from './plugins/url-tracking-plugin';
24
23
  import { isInIframe, CrossOriginIframeCoordinator, listenForParentSignals } from './cross-origin-iframes';
24
+ import { WindowFocusTracker } from './window-focus';
25
25
  var SessionReplay = /** @class */ (function () {
26
26
  function SessionReplay() {
27
27
  var _this = this;
@@ -64,6 +64,7 @@ var SessionReplay = /** @class */ (function () {
64
64
  */
65
65
  this.startChildRecordingOnSetup = false;
66
66
  this.pendingEmitEvents = [];
67
+ this.windowFocus = new WindowFocusTracker();
67
68
  /** Current page URL, kept in sync with SPA navigations for URL-based masking */
68
69
  this.currentPageUrl = '';
69
70
  this.recordEventsPendingShouldLogMetadata = null;
@@ -92,20 +93,70 @@ var SessionReplay = /** @class */ (function () {
92
93
  globalScope.removeEventListener('beforeunload', _this.pageLeaveListener);
93
94
  !teardown && globalScope.addEventListener('beforeunload', _this.pageLeaveListener);
94
95
  }
96
+ // Tab switches fire visibilitychange on the document. Focus does not: a background
97
+ // tab can stay blurred without ever hiding, and hidden tabs often never regain focus
98
+ // until this event. Guarded because unit-test scopes sometimes stub document.
99
+ var documentRef = globalScope.document;
100
+ if ((documentRef === null || documentRef === void 0 ? void 0 : documentRef.addEventListener) && documentRef.removeEventListener) {
101
+ documentRef.removeEventListener('visibilitychange', _this.visibilityListener);
102
+ !teardown && documentRef.addEventListener('visibilitychange', _this.visibilityListener);
103
+ }
95
104
  }
96
105
  };
97
106
  this.blurListener = function () {
98
- _this.sendEvents();
107
+ // Blur covers window switches that do not hide the document (a second window
108
+ // beside this one). Drain the idle queue first: sendEvents only ships what is
109
+ // already in the store, and the mutations for the screen the user just left
110
+ // are often still waiting on requestIdleCallback.
111
+ _this.scheduleHiddenSend();
112
+ _this.windowFocus.noteBlur();
113
+ };
114
+ /**
115
+ * Tab show/hide. `focus` is the wrong signal for this: it also fires for iframes
116
+ * and editors, which is why `captureFullSnapshotOnFocus` defaults off. A document
117
+ * visibility change happens once per real tab switch.
118
+ *
119
+ * Hidden: flush queued mutations so playback is not left on the last snapshot that
120
+ * happened to be in the store.
121
+ * Visible: take one checkout snapshot of this document and send it. Incremental
122
+ * events from another tab use that tab's node ids and are dropped, so without a
123
+ * fresh checkout the player stays on whichever snapshot it last applied.
124
+ */
125
+ this.visibilityListener = function () {
126
+ var _a, _b;
127
+ var visibilityState = (_b = (_a = getGlobalScope()) === null || _a === void 0 ? void 0 : _a.document) === null || _b === void 0 ? void 0 : _b.visibilityState;
128
+ if (visibilityState === 'hidden') {
129
+ _this.windowFocus.onDocumentHidden();
130
+ _this.scheduleHiddenSend();
131
+ return;
132
+ }
133
+ if (visibilityState === 'visible') {
134
+ // The deferred hide upload has not run. This send includes those events
135
+ // plus the checkout snapshot. Drop the external-focus flag so the focus
136
+ // event that follows a tab return does not take a second checkout.
137
+ _this.windowFocus.onDocumentVisible();
138
+ _this.cancelHiddenSend();
139
+ _this.captureVisibleTab();
140
+ }
99
141
  };
100
142
  this.focusListener = function () {
101
143
  var _a;
102
144
  if (!_this.recordingEnabled) {
103
145
  return;
104
146
  }
147
+ var focusFromOutside = _this.windowFocus.consumeArrivedFromOutside();
148
+ // Another window of the same session stayed visible, so visibilitychange never
149
+ // fired. One checkout replaces the replayed document; otherwise that window's
150
+ // incremental adds share node ids and append onto this one.
151
+ if (focusFromOutside) {
152
+ _this.captureVisibleTab();
153
+ return;
154
+ }
105
155
  if (_this.recordCancelCallback && _this.recordFunction) {
106
156
  // Recording is already active. The on-focus full snapshot is tunable: when
107
157
  // `captureFullSnapshotOnFocus` is false we skip it entirely so high focus-churn pages
108
158
  // don't generate a full snapshot (and, with eager send, a network request) per focus.
159
+ // Returning to a backgrounded tab is handled by visibilityListener instead.
109
160
  if (((_a = _this.config) === null || _a === void 0 ? void 0 : _a.captureFullSnapshotOnFocus) === false) {
110
161
  return;
111
162
  }
@@ -126,11 +177,12 @@ var SessionReplay = /** @class */ (function () {
126
177
  * prevent duplicate listener actions from firing.
127
178
  */
128
179
  this.pageLeaveListener = function (e) {
129
- var _a;
180
+ // Drop the deferred hide upload. It must not resolve before the beacon below
181
+ // copies the buffer. Unload order is visibilitychange(hidden), then pagehide.
182
+ _this.cancelHiddenSend();
130
183
  // Synchronously drain any events still queued in the requestIdleCallback
131
184
  // pipeline so they are available to send before the page unloads.
132
- (_a = _this.eventCompressor) === null || _a === void 0 ? void 0 : _a.flushQueue();
133
- _this.sendEvents();
185
+ _this.flushPendingReplayEvents();
134
186
  _this.pageLeaveFns.forEach(function (fn) {
135
187
  fn(e);
136
188
  });
@@ -264,21 +316,19 @@ var SessionReplay = /** @class */ (function () {
264
316
  });
265
317
  });
266
318
  };
267
- this.stopRecordingEvents = function (reason, state) {
268
- var _a, _b, _c, _d;
269
- if (reason === void 0) { reason = 'targeting'; }
270
- if (state === void 0) { state = 'stop'; }
319
+ this.stopRecordingEvents = function () {
320
+ var _a, _b, _c;
271
321
  try {
272
- (_a = _this.semanticAdapter) === null || _a === void 0 ? void 0 : _a.stop(reason, state);
322
+ _this.useSemanticAdapter(function (adapter) { return adapter.stop(); });
273
323
  _this.loggerProvider.log('Session Replay capture stopping.');
274
324
  _this.recordCancelCallback && _this.recordCancelCallback();
275
325
  _this.recordCancelCallback = null;
276
- (_b = _this.networkObservers) === null || _b === void 0 ? void 0 : _b.stop();
277
- (_c = _this.crossOriginIframeCoordinator) === null || _c === void 0 ? void 0 : _c.stop();
326
+ (_a = _this.networkObservers) === null || _a === void 0 ? void 0 : _a.stop();
327
+ (_b = _this.crossOriginIframeCoordinator) === null || _b === void 0 ? void 0 : _b.stop();
278
328
  _this.crossOriginIframeCoordinator = null;
279
329
  // Remove the child-mode parent signal listener so a later mode change
280
330
  // (e.g. crossOriginIframes disabled) does not leave a stale listener.
281
- (_d = _this.crossOriginParentSignalCleanup) === null || _d === void 0 ? void 0 : _d.call(_this);
331
+ (_c = _this.crossOriginParentSignalCleanup) === null || _c === void 0 ? void 0 : _c.call(_this);
282
332
  _this.crossOriginParentSignalCleanup = null;
283
333
  }
284
334
  catch (error) {
@@ -349,18 +399,18 @@ var SessionReplay = /** @class */ (function () {
349
399
  return currentUrl != null ? { url: currentUrl } : undefined;
350
400
  };
351
401
  SessionReplay.prototype._init = function (apiKey, options) {
352
- var _a, _b, _c, _d, _e, _f, _g, _h, _j, _k, _l, _m, _o, _p, _q;
402
+ var _a, _b, _c, _d, _e, _f, _g, _h, _j, _k, _l, _m, _o, _p;
353
403
  return __awaiter(this, void 0, void 0, function () {
354
- var now, _r, _s, joinedConfig, localConfig, remoteConfig, scrollWatcher, managers, storeType, compressionWorkerScript, trackDestinationWorkerScript, globalScope, _t, compressionScript, trackDestinationScript, rrwebEventManager, error_1, typedError, payloadBatcher, interactionEventManager, error_2, typedError, onFullSnapshotProcessed, pending_2, pending_1, pending_1_1, _u, event_1, sessionId, messenger, needsUrlTracking;
355
- var e_2, _v;
404
+ var now, _q, _r, joinedConfig, localConfig, remoteConfig, scrollWatcher, managers, storeType, compressionWorkerScript, trackDestinationWorkerScript, globalScope, _s, compressionScript, trackDestinationScript, rrwebEventManager, error_1, typedError, payloadBatcher, interactionEventManager, error_2, typedError, onFullSnapshotProcessed, pending_2, pending_1, pending_1_1, _t, event_1, sessionId, messenger, needsUrlTracking;
405
+ var e_2, _u;
356
406
  var _this = this;
357
- return __generator(this, function (_w) {
358
- switch (_w.label) {
407
+ return __generator(this, function (_v) {
408
+ switch (_v.label) {
359
409
  case 0:
360
410
  // Re-init should always tear down any previous URL-change subscription, even when the
361
411
  // next config has no targeting config and we don't subscribe again.
362
412
  (_a = this.urlChangeCleanup) === null || _a === void 0 ? void 0 : _a.call(this);
363
- (_b = this.semanticAdapter) === null || _b === void 0 ? void 0 : _b.stop('customer_stop');
413
+ this.useSemanticAdapter(function (adapter) { return adapter.stop(); });
364
414
  this.semanticAdapter = undefined;
365
415
  // A new init always allows capture again. `stop()` only pauses the current instance.
366
416
  this.recordingEnabled = true;
@@ -374,19 +424,19 @@ var SessionReplay = /** @class */ (function () {
374
424
  pruneStaleReplayStartTimes(apiKey, now, this.loggerProvider);
375
425
  this.sessionStartTime =
376
426
  options.sessionId !== undefined
377
- ? (_c = getOrInitReplayStartTime(apiKey, options.sessionId, now, this.loggerProvider)) !== null && _c !== void 0 ? _c : now
427
+ ? (_b = getOrInitReplayStartTime(apiKey, options.sessionId, now, this.loggerProvider)) !== null && _b !== void 0 ? _b : now
378
428
  : now;
379
- _r = this;
429
+ _q = this;
380
430
  return [4 /*yield*/, createSessionReplayJoinedConfigGenerator(apiKey, options)];
381
431
  case 1:
382
- _r.joinedConfigGenerator = _w.sent();
432
+ _q.joinedConfigGenerator = _v.sent();
383
433
  return [4 /*yield*/, this.joinedConfigGenerator.generateJoinedConfig()];
384
434
  case 2:
385
- _s = _w.sent(), joinedConfig = _s.joinedConfig, localConfig = _s.localConfig, remoteConfig = _s.remoteConfig;
435
+ _r = _v.sent(), joinedConfig = _r.joinedConfig, localConfig = _r.localConfig, remoteConfig = _r.remoteConfig;
386
436
  this.config = joinedConfig;
387
- this.setMetadata(options.sessionId, joinedConfig, localConfig, remoteConfig, (_d = options.version) === null || _d === void 0 ? void 0 : _d.version, VERSION, (_e = options.version) === null || _e === void 0 ? void 0 : _e.type);
437
+ this.setMetadata(options.sessionId, joinedConfig, localConfig, remoteConfig, (_c = options.version) === null || _c === void 0 ? void 0 : _c.version, VERSION, (_d = options.version) === null || _d === void 0 ? void 0 : _d.type);
388
438
  this.pageLeaveFns = [];
389
- if (options.sessionId && ((_f = this.config.interactionConfig) === null || _f === void 0 ? void 0 : _f.enabled)) {
439
+ if (options.sessionId && ((_e = this.config.interactionConfig) === null || _e === void 0 ? void 0 : _e.enabled)) {
390
440
  scrollWatcher = ScrollWatcher.default({
391
441
  sessionId: options.sessionId,
392
442
  type: 'interaction',
@@ -397,7 +447,7 @@ var SessionReplay = /** @class */ (function () {
397
447
  }
398
448
  managers = [];
399
449
  storeType = this.config.storeType;
400
- if (storeType === 'idb' && !((_g = getGlobalScope()) === null || _g === void 0 ? void 0 : _g.indexedDB)) {
450
+ if (storeType === 'idb' && !((_f = getGlobalScope()) === null || _f === void 0 ? void 0 : _f.indexedDB)) {
401
451
  storeType = 'memory';
402
452
  this.loggerProvider.warn('Could not use preferred indexedDB storage, reverting to in memory option.');
403
453
  }
@@ -406,42 +456,42 @@ var SessionReplay = /** @class */ (function () {
406
456
  if (!(this.config.useWebWorker && globalScope && globalScope.Worker)) return [3 /*break*/, 4];
407
457
  return [4 /*yield*/, import('./worker')];
408
458
  case 3:
409
- _t = _w.sent(), compressionScript = _t.compressionScript, trackDestinationScript = _t.trackDestinationScript;
459
+ _s = _v.sent(), compressionScript = _s.compressionScript, trackDestinationScript = _s.trackDestinationScript;
410
460
  compressionWorkerScript = compressionScript;
411
461
  trackDestinationWorkerScript = trackDestinationScript;
412
- _w.label = 4;
462
+ _v.label = 4;
413
463
  case 4:
414
- _w.trys.push([4, 6, , 7]);
464
+ _v.trys.push([4, 6, , 7]);
415
465
  return [4 /*yield*/, createEventsManager({
416
466
  config: this.config,
417
467
  type: 'replay',
418
- minInterval: (_h = this.config.flushIntervalConfig) === null || _h === void 0 ? void 0 : _h.minIntervalMs,
419
- maxInterval: (_j = this.config.flushIntervalConfig) === null || _j === void 0 ? void 0 : _j.maxIntervalMs,
468
+ minInterval: (_g = this.config.flushIntervalConfig) === null || _g === void 0 ? void 0 : _g.minIntervalMs,
469
+ maxInterval: (_h = this.config.flushIntervalConfig) === null || _h === void 0 ? void 0 : _h.maxIntervalMs,
420
470
  maxPersistedEventsSize: this.config.maxPersistedEventsSizeBytes,
421
471
  storeType: storeType,
422
472
  trackDestinationWorkerScript: trackDestinationWorkerScript,
423
473
  shouldSend: function () { return !_this.isBelowMinSessionDuration(); },
424
474
  })];
425
475
  case 5:
426
- rrwebEventManager = _w.sent();
476
+ rrwebEventManager = _v.sent();
427
477
  this.rrwebEventManager = rrwebEventManager;
428
478
  managers.push({ name: 'replay', manager: rrwebEventManager });
429
479
  return [3 /*break*/, 7];
430
480
  case 6:
431
- error_1 = _w.sent();
481
+ error_1 = _v.sent();
432
482
  typedError = error_1;
433
483
  this.loggerProvider.warn("Error occurred while creating replay events manager: ".concat(typedError.toString()));
434
484
  return [3 /*break*/, 7];
435
485
  case 7:
436
- if (!((_k = this.config.interactionConfig) === null || _k === void 0 ? void 0 : _k.enabled)) return [3 /*break*/, 11];
486
+ if (!((_j = this.config.interactionConfig) === null || _j === void 0 ? void 0 : _j.enabled)) return [3 /*break*/, 11];
437
487
  payloadBatcher = this.config.interactionConfig.batch ? clickBatcher : clickNonBatcher;
438
- _w.label = 8;
488
+ _v.label = 8;
439
489
  case 8:
440
- _w.trys.push([8, 10, , 11]);
490
+ _v.trys.push([8, 10, , 11]);
441
491
  return [4 /*yield*/, createEventsManager({
442
492
  config: this.config,
443
493
  type: 'interaction',
444
- minInterval: (_l = this.config.interactionConfig.trackEveryNms) !== null && _l !== void 0 ? _l : INTERACTION_MIN_INTERVAL,
494
+ minInterval: (_k = this.config.interactionConfig.trackEveryNms) !== null && _k !== void 0 ? _k : INTERACTION_MIN_INTERVAL,
445
495
  maxInterval: INTERACTION_MAX_INTERVAL,
446
496
  maxPersistedEventsSize: this.config.maxPersistedEventsSizeBytes,
447
497
  payloadBatcher: payloadBatcher,
@@ -449,11 +499,11 @@ var SessionReplay = /** @class */ (function () {
449
499
  trackDestinationWorkerScript: trackDestinationWorkerScript,
450
500
  })];
451
501
  case 9:
452
- interactionEventManager = _w.sent();
502
+ interactionEventManager = _v.sent();
453
503
  managers.push({ name: 'interaction', manager: interactionEventManager });
454
504
  return [3 /*break*/, 11];
455
505
  case 10:
456
- error_2 = _w.sent();
506
+ error_2 = _v.sent();
457
507
  typedError = error_2;
458
508
  this.loggerProvider.warn("Error occurred while creating interaction events manager: ".concat(typedError.toString()));
459
509
  return [3 /*break*/, 11];
@@ -471,14 +521,14 @@ var SessionReplay = /** @class */ (function () {
471
521
  pending_2 = this.pendingEmitEvents.splice(0);
472
522
  try {
473
523
  for (pending_1 = __values(pending_2), pending_1_1 = pending_1.next(); !pending_1_1.done; pending_1_1 = pending_1.next()) {
474
- _u = pending_1_1.value, event_1 = _u.event, sessionId = _u.sessionId;
524
+ _t = pending_1_1.value, event_1 = _t.event, sessionId = _t.sessionId;
475
525
  this.eventCompressor.enqueueEvent(event_1, sessionId);
476
526
  }
477
527
  }
478
528
  catch (e_2_1) { e_2 = { error: e_2_1 }; }
479
529
  finally {
480
530
  try {
481
- if (pending_1_1 && !pending_1_1.done && (_v = pending_1.return)) _v.call(pending_1);
531
+ if (pending_1_1 && !pending_1_1.done && (_u = pending_1.return)) _u.call(pending_1);
482
532
  }
483
533
  finally { if (e_2) throw e_2.error; }
484
534
  }
@@ -515,11 +565,11 @@ var SessionReplay = /** @class */ (function () {
515
565
  ], false);
516
566
  return [4 /*yield*/, this.initializeNetworkObservers()];
517
567
  case 12:
518
- _w.sent();
568
+ _v.sent();
519
569
  // Enable background capture when this page is opened by the Amplitude app
520
570
  // (window.opener exists). Uses the shared messenger singleton so that if
521
571
  // autocapture is also loaded, both share a single messenger and script load.
522
- if ((_m = getGlobalScope()) === null || _m === void 0 ? void 0 : _m.opener) {
572
+ if ((_l = getGlobalScope()) === null || _l === void 0 ? void 0 : _l.opener) {
523
573
  messenger = getOrCreateWindowMessenger();
524
574
  enableBackgroundCapture(messenger);
525
575
  messenger.setup(__assign({ logger: this.loggerProvider }, (this.config.serverZone && { endpoint: AMPLITUDE_ORIGINS_MAP[this.config.serverZone] })));
@@ -528,8 +578,8 @@ var SessionReplay = /** @class */ (function () {
528
578
  this.teardownEventListeners(false);
529
579
  return [4 /*yield*/, this.evaluateTargetingAndCapture({ userProperties: options.userProperties, page: this.getCurrentPageForTargeting() }, true)];
530
580
  case 13:
531
- _w.sent();
532
- needsUrlTracking = this.config.targetingConfig || ((_q = (_p = (_o = this.config.privacyConfig) === null || _o === void 0 ? void 0 : _o.urlMaskLevels) === null || _p === void 0 ? void 0 : _p.length) !== null && _q !== void 0 ? _q : 0) > 0;
581
+ _v.sent();
582
+ needsUrlTracking = this.config.targetingConfig || ((_p = (_o = (_m = this.config.privacyConfig) === null || _m === void 0 ? void 0 : _m.urlMaskLevels) === null || _o === void 0 ? void 0 : _o.length) !== null && _p !== void 0 ? _p : 0) > 0;
533
583
  if (needsUrlTracking) {
534
584
  this.setupUrlChangeListener();
535
585
  }
@@ -570,7 +620,9 @@ var SessionReplay = /** @class */ (function () {
570
620
  }
571
621
  isSessionChange = previousSessionId !== sessionId;
572
622
  if (isSessionChange && ((_c = (_b = this.config) === null || _b === void 0 ? void 0 : _b.semanticCapture) === null || _c === void 0 ? void 0 : _c.enabled) && this.semanticAdapter) {
573
- this.stopRecordingEvents('new_session');
623
+ // Keep rrweb recording through the async config/targeting pass, as it does
624
+ // without semantic capture. Only detach the old session's semantic runtime.
625
+ this.useSemanticAdapter(function (adapter) { return adapter.stop(); });
574
626
  }
575
627
  // Drop any beacon-buffered events from the previous session BEFORE installing the
576
628
  // new identifiers / start time. Otherwise the page-leave beacon path could attribute
@@ -651,6 +703,78 @@ var SessionReplay = /** @class */ (function () {
651
703
  this.eventCount++;
652
704
  return eventProperties;
653
705
  };
706
+ /** Move idle-queued rrweb events into the store, then ship the current sequence. */
707
+ SessionReplay.prototype.flushPendingReplayEvents = function () {
708
+ var _a;
709
+ (_a = this.eventCompressor) === null || _a === void 0 ? void 0 : _a.flushQueue();
710
+ this.sendEvents();
711
+ };
712
+ /**
713
+ * Flush now, upload on the next macrotask. The flush is synchronous so a
714
+ * following `pagehide` still sees the events in the beacon buffer. The upload
715
+ * waits so a memory-store `sendEvents` cannot clear that buffer before the
716
+ * beacon copies it. A tab switch never reaches `pagehide`, so the timer uploads.
717
+ */
718
+ SessionReplay.prototype.scheduleHiddenSend = function () {
719
+ var _this = this;
720
+ var _a;
721
+ (_a = this.eventCompressor) === null || _a === void 0 ? void 0 : _a.flushQueue();
722
+ if (this.hiddenFlushTimer !== undefined) {
723
+ return;
724
+ }
725
+ var schedule = this.hostSetTimeout();
726
+ this.hiddenFlushTimer = schedule(function () {
727
+ _this.hiddenFlushTimer = undefined;
728
+ _this.sendEvents();
729
+ }, 0);
730
+ };
731
+ SessionReplay.prototype.cancelHiddenSend = function () {
732
+ if (this.hiddenFlushTimer === undefined) {
733
+ return;
734
+ }
735
+ this.hostClearTimeout()(this.hiddenFlushTimer);
736
+ this.hiddenFlushTimer = undefined;
737
+ };
738
+ SessionReplay.prototype.hostSetTimeout = function () {
739
+ var globalScope = getGlobalScope();
740
+ if (typeof (globalScope === null || globalScope === void 0 ? void 0 : globalScope.setTimeout) === 'function') {
741
+ return globalScope.setTimeout.bind(globalScope);
742
+ }
743
+ return setTimeout;
744
+ };
745
+ SessionReplay.prototype.hostClearTimeout = function () {
746
+ var globalScope = getGlobalScope();
747
+ if (typeof (globalScope === null || globalScope === void 0 ? void 0 : globalScope.clearTimeout) === 'function') {
748
+ return globalScope.clearTimeout.bind(globalScope);
749
+ }
750
+ return clearTimeout;
751
+ };
752
+ /**
753
+ * Checkout the DOM the user can actually see, then send it. Queued mutations are
754
+ * flushed first so they stay ordered ahead of the new snapshot.
755
+ */
756
+ SessionReplay.prototype.captureVisibleTab = function () {
757
+ var _a;
758
+ if (!this.recordingEnabled) {
759
+ return;
760
+ }
761
+ (_a = this.eventCompressor) === null || _a === void 0 ? void 0 : _a.flushQueue();
762
+ if (this.recordCancelCallback && this.recordFunction) {
763
+ try {
764
+ // `true` is an rrweb checkout: the player drops the previous mirror and
765
+ // paints this snapshot instead of applying it onto an older document.
766
+ this.recordFunction.takeFullSnapshot(true);
767
+ }
768
+ catch (error) {
769
+ this.loggerProvider.warn('Failed to take full snapshot when the tab became visible:', error);
770
+ }
771
+ this.sendEvents();
772
+ return;
773
+ }
774
+ if (!this.recordEventsInFlight) {
775
+ void this.recordEvents(false);
776
+ }
777
+ };
654
778
  SessionReplay.prototype.sendEvents = function (sessionId) {
655
779
  var _a, _b;
656
780
  var sessionIdToSend = sessionId || ((_a = this.identifiers) === null || _a === void 0 ? void 0 : _a.sessionId);
@@ -899,71 +1023,67 @@ var SessionReplay = /** @class */ (function () {
899
1023
  return !this.recordingEnabled || generation !== this.recordingGeneration;
900
1024
  };
901
1025
  SessionReplay.prototype._recordEvents = function (shouldLogMetadata) {
902
- var _a, _b, _c, _d, _e, _f, _g, _h, _j, _k, _l, _m, _o;
1026
+ var _a, _b, _c, _d, _e, _f, _g, _h;
903
1027
  if (shouldLogMetadata === void 0) { shouldLogMetadata = true; }
904
1028
  return __awaiter(this, void 0, void 0, function () {
905
- var config, shouldRecord, sessionId, generation, recordFunction, networkLoggingConfig, trackUrl, ignoredUrls, interactionConfig, loggingConfig, heatmapHook, sharedSemanticClicks, mouseInteraction, clickHandler, hooks, ugcFilterRules, crossOriginIframesEnabled, coordinateChildren, childMode, plugins, error_5;
1029
+ var config, shouldRecord, sessionId, generation, recordFunction, networkLoggingConfig, trackUrl, ignoredUrls, interactionConfig, loggingConfig, hooks, ugcFilterRules, crossOriginIframesEnabled, coordinateChildren, childMode, plugins, error_5;
906
1030
  var _this = this;
907
- return __generator(this, function (_p) {
908
- switch (_p.label) {
1031
+ return __generator(this, function (_j) {
1032
+ switch (_j.label) {
909
1033
  case 0:
910
1034
  config = this.config;
911
1035
  shouldRecord = this.getShouldRecord();
912
1036
  sessionId = (_a = this.identifiers) === null || _a === void 0 ? void 0 : _a.sessionId;
913
1037
  if (!shouldRecord || !sessionId || !config) {
914
- if (((_b = config === null || config === void 0 ? void 0 : config.semanticCapture) === null || _b === void 0 ? void 0 : _b.enabled) && this.semanticAdapter && this.recordCancelCallback) {
915
- this.stopRecordingEvents(this.shouldOptOut() ? 'opt_out' : config.targetingConfig ? 'targeting' : 'sampled_out');
916
- }
1038
+ if (this.semanticAdapter && this.recordCancelCallback)
1039
+ this.stopRecordingEvents();
917
1040
  return [2 /*return*/];
918
1041
  }
919
- this.stopRecordingEvents('focus', 'pause');
1042
+ this.stopRecordingEvents();
920
1043
  generation = this.recordingGeneration;
921
1044
  return [4 /*yield*/, this.getRecordFunction()];
922
1045
  case 1:
923
- recordFunction = _p.sent();
1046
+ recordFunction = _j.sent();
924
1047
  // May be undefined if cannot import rrweb-record
925
1048
  if (!recordFunction || this.shouldAbandonRecordStart(generation)) {
926
1049
  return [2 /*return*/];
927
1050
  }
928
1051
  return [4 /*yield*/, this.initializeNetworkObservers()];
929
1052
  case 2:
930
- _p.sent();
1053
+ _j.sent();
931
1054
  if (this.shouldAbandonRecordStart(generation)) {
932
1055
  return [2 /*return*/];
933
1056
  }
934
- networkLoggingConfig = (_c = config.loggingConfig) === null || _c === void 0 ? void 0 : _c.network;
1057
+ networkLoggingConfig = (_b = config.loggingConfig) === null || _b === void 0 ? void 0 : _b.network;
935
1058
  trackUrl = getServerUrl(config.serverZone, config.trackServerUrl);
936
1059
  ignoredUrls = [SESSION_REPLAY_SERVER_URL, SESSION_REPLAY_EU_URL, SESSION_REPLAY_STAGING_URL, trackUrl];
937
- (_d = this.networkObservers) === null || _d === void 0 ? void 0 : _d.start(function (event) {
1060
+ (_c = this.networkObservers) === null || _c === void 0 ? void 0 : _c.start(function (event) {
938
1061
  if (ignoredUrls.some(function (url) { return event.url.startsWith(url); }))
939
1062
  return;
940
1063
  void _this.addCustomRRWebEvent(CustomRRwebEvent.FETCH_REQUEST, event);
941
1064
  }, networkLoggingConfig);
942
1065
  interactionConfig = config.interactionConfig, loggingConfig = config.loggingConfig;
943
- heatmapHook = (interactionConfig === null || interactionConfig === void 0 ? void 0 : interactionConfig.enabled) && this.eventsManager
944
- ? (_e = this.clickHandler) === null || _e === void 0 ? void 0 : _e.createHook({
945
- eventsManager: this.eventsManager,
946
- sessionId: sessionId,
947
- deviceIdFn: this.getDeviceId.bind(this),
948
- mirror: recordFunction.mirror,
949
- ugcFilterRules: (_f = interactionConfig.ugcFilterRules) !== null && _f !== void 0 ? _f : [],
950
- performanceOptions: (_g = config.performanceConfig) === null || _g === void 0 ? void 0 : _g.interaction,
951
- })
952
- : undefined;
953
- sharedSemanticClicks = ((_h = config.semanticCapture) === null || _h === void 0 ? void 0 : _h.enabled) && recordFunction.supportsNativeMouseInteraction;
954
- mouseInteraction = heatmapHook;
955
- if (sharedSemanticClicks) {
956
- clickHandler = (_j = this.clickHandler) !== null && _j !== void 0 ? _j : (this.clickHandler = new ClickHandler(this.loggerProvider));
957
- mouseInteraction = clickHandler.createSharedHook(heatmapHook, function (event) { var _a; return (_a = _this.semanticAdapter) === null || _a === void 0 ? void 0 : _a.click(event); });
958
- }
959
- hooks = __assign(__assign({}, (mouseInteraction ? { mouseInteraction: mouseInteraction } : {})), ((interactionConfig === null || interactionConfig === void 0 ? void 0 : interactionConfig.enabled) ? { scroll: this.scrollHook } : {}));
1066
+ hooks = (interactionConfig === null || interactionConfig === void 0 ? void 0 : interactionConfig.enabled)
1067
+ ? {
1068
+ mouseInteraction: this.eventsManager &&
1069
+ ((_d = this.clickHandler) === null || _d === void 0 ? void 0 : _d.createHook({
1070
+ eventsManager: this.eventsManager,
1071
+ sessionId: sessionId,
1072
+ deviceIdFn: this.getDeviceId.bind(this),
1073
+ mirror: recordFunction.mirror,
1074
+ ugcFilterRules: (_e = interactionConfig.ugcFilterRules) !== null && _e !== void 0 ? _e : [],
1075
+ performanceOptions: (_f = config.performanceConfig) === null || _f === void 0 ? void 0 : _f.interaction,
1076
+ })),
1077
+ scroll: this.scrollHook,
1078
+ }
1079
+ : {};
960
1080
  ugcFilterRules = (interactionConfig === null || interactionConfig === void 0 ? void 0 : interactionConfig.enabled) && interactionConfig.ugcFilterRules ? interactionConfig.ugcFilterRules : [];
961
1081
  this.loggerProvider.log("Session Replay capture beginning for ".concat(sessionId, "."));
962
- _p.label = 3;
1082
+ _j.label = 3;
963
1083
  case 3:
964
- _p.trys.push([3, 5, , 6]);
965
- crossOriginIframesEnabled = !!((_k = config.crossOriginIframes) === null || _k === void 0 ? void 0 : _k.enabled);
966
- coordinateChildren = ((_l = config.crossOriginIframes) === null || _l === void 0 ? void 0 : _l.coordinateChildren) !== false;
1084
+ _j.trys.push([3, 5, , 6]);
1085
+ crossOriginIframesEnabled = !!((_g = config.crossOriginIframes) === null || _g === void 0 ? void 0 : _g.enabled);
1086
+ coordinateChildren = ((_h = config.crossOriginIframes) === null || _h === void 0 ? void 0 : _h.coordinateChildren) !== false;
967
1087
  childMode = crossOriginIframesEnabled && isInIframe();
968
1088
  if (childMode && coordinateChildren) {
969
1089
  // Child mode: don't self-start; wait for a start signal from the parent.
@@ -1000,24 +1120,17 @@ var SessionReplay = /** @class */ (function () {
1000
1120
  this.startChildRecordingOnSetup = false;
1001
1121
  return [4 /*yield*/, this.getRecordingPlugins(loggingConfig)];
1002
1122
  case 4:
1003
- plugins = _p.sent();
1123
+ plugins = _j.sent();
1004
1124
  if (this.shouldAbandonRecordStart(generation)) {
1005
1125
  return [2 /*return*/];
1006
1126
  }
1007
1127
  this.prepareSemanticCapture(config, sessionId);
1008
1128
  this.recordCancelCallback = recordFunction(__assign(__assign({}, this.buildRRWebRecordOptions(config, hooks, function (event) {
1009
- var _a, _b, _c, _d;
1010
- // The terminal state contains no page or user data and must land before rrweb stops.
1011
- var semanticStop = ((_b = (_a = _this.config) === null || _a === void 0 ? void 0 : _a.semanticCapture) === null || _b === void 0 ? void 0 : _b.enabled) &&
1012
- _this.semanticAdapter &&
1013
- event.type === RRWebEventType.Custom &&
1014
- event.data.tag === 'amp-semantic/recording' &&
1015
- ((_c = event.data.payload) === null || _c === void 0 ? void 0 : _c.state) === 'stop';
1016
- if ((_this.shouldOptOut() || !_this.recordingEnabled) && !semanticStop) {
1129
+ if (_this.shouldOptOut() || !_this.recordingEnabled) {
1017
1130
  _this.loggerProvider.log(_this.shouldOptOut()
1018
1131
  ? "Opting session ".concat(sessionId, " out of recording due to optOut config.")
1019
1132
  : "Session Replay capture stopping for ".concat(sessionId, "."));
1020
- _this.stopRecordingEvents(_this.shouldOptOut() ? 'opt_out' : 'customer_stop');
1133
+ _this.stopRecordingEvents();
1021
1134
  _this.sendEvents();
1022
1135
  return;
1023
1136
  }
@@ -1033,10 +1146,10 @@ var SessionReplay = /** @class */ (function () {
1033
1146
  // Buffer the event so it can be flushed once the compressor is initialized.
1034
1147
  _this.pendingEmitEvents.push({ event: event, sessionId: sessionId });
1035
1148
  }
1036
- (_d = _this.semanticAdapter) === null || _d === void 0 ? void 0 : _d.event(event);
1149
+ _this.useSemanticAdapter(function (adapter) { return adapter.event(event); });
1037
1150
  }, 'Error while capturing replay: ')), { plugins: plugins, recordCrossOriginIframes: crossOriginIframesEnabled }));
1038
1151
  if (this.recordCancelCallback)
1039
- (_m = this.semanticAdapter) === null || _m === void 0 ? void 0 : _m.recordingReady();
1152
+ this.useSemanticAdapter(function (adapter) { return adapter.recordingReady(); });
1040
1153
  if (crossOriginIframesEnabled && !childMode && coordinateChildren) {
1041
1154
  if (!this.crossOriginIframeCoordinator) {
1042
1155
  this.crossOriginIframeCoordinator = new CrossOriginIframeCoordinator();
@@ -1049,8 +1162,8 @@ var SessionReplay = /** @class */ (function () {
1049
1162
  }
1050
1163
  return [3 /*break*/, 6];
1051
1164
  case 5:
1052
- error_5 = _p.sent();
1053
- (_o = this.semanticAdapter) === null || _o === void 0 ? void 0 : _o.stop('error');
1165
+ error_5 = _j.sent();
1166
+ this.useSemanticAdapter(function (adapter) { return adapter.stop(); });
1054
1167
  this.loggerProvider.warn('Failed to initialize session replay:', error_5);
1055
1168
  return [3 /*break*/, 6];
1056
1169
  case 6: return [2 /*return*/];
@@ -1058,51 +1171,69 @@ var SessionReplay = /** @class */ (function () {
1058
1171
  });
1059
1172
  });
1060
1173
  };
1174
+ SessionReplay.prototype.useSemanticAdapter = function (operation) {
1175
+ var adapter = this.semanticAdapter;
1176
+ if (!adapter)
1177
+ return;
1178
+ try {
1179
+ operation(adapter);
1180
+ }
1181
+ catch (error) {
1182
+ // A supplied controller must not interrupt rrweb recording or its teardown.
1183
+ this.semanticAdapter = undefined;
1184
+ this.loggerProvider.debug('Semantic capture failed:', error);
1185
+ try {
1186
+ adapter.stop();
1187
+ }
1188
+ catch (stopError) {
1189
+ this.loggerProvider.debug('Semantic capture stop failed:', stopError);
1190
+ }
1191
+ }
1192
+ };
1061
1193
  SessionReplay.prototype.prepareSemanticCapture = function (config, sessionId) {
1062
1194
  var _this = this;
1063
- var _a, _b, _c, _d;
1195
+ var _a;
1064
1196
  var scope = getGlobalScope();
1065
1197
  if (!(scope === null || scope === void 0 ? void 0 : scope.document))
1066
1198
  return;
1067
- if (!((_a = config.semanticCapture) === null || _a === void 0 ? void 0 : _a.enabled)) {
1068
- (_b = this.semanticAdapter) === null || _b === void 0 ? void 0 : _b.stop('targeting');
1069
- (_c = this.semanticAdapter) === null || _c === void 0 ? void 0 : _c.prepare({ enabled: false }, sessionId);
1199
+ var semanticCapture = config.semanticCapture;
1200
+ var createCapture = semanticCapture === null || semanticCapture === void 0 ? void 0 : semanticCapture.createCapture;
1201
+ if (!(semanticCapture === null || semanticCapture === void 0 ? void 0 : semanticCapture.enabled) || typeof createCapture !== 'function') {
1202
+ this.useSemanticAdapter(function (adapter) { return adapter.stop(); });
1203
+ this.useSemanticAdapter(function (adapter) { return adapter.prepare({ enabled: false }, sessionId); });
1070
1204
  return;
1071
1205
  }
1072
- if (!this.semanticAdapter) {
1073
- this.semanticAdapter = new ReplaySemanticAdapter({
1074
- document: scope.document,
1075
- window: scope,
1076
- sdk: "@amplitude/session-replay-browser@".concat(VERSION),
1077
- externalClicks: ((_d = this.recordFunction) === null || _d === void 0 ? void 0 : _d.supportsNativeMouseInteraction) === true,
1078
- // rrweb does not report auxiliary clicks through its mouseInteraction hook.
1079
- externalAuxClicks: false,
1080
- getNodeId: function (node) { var _a, _b, _c, _d; return (_d = (_c = (_a = _this.recordFunction) === null || _a === void 0 ? void 0 : (_b = _a.mirror).getId) === null || _c === void 0 ? void 0 : _c.call(_b, node)) !== null && _d !== void 0 ? _d : -1; },
1081
- getNode: function (id) { var _a, _b; return (_b = (_a = _this.recordFunction) === null || _a === void 0 ? void 0 : _a.mirror.getNode(id)) !== null && _b !== void 0 ? _b : null; },
1082
- getPrivacy: function () { var _a, _b; return (_b = (_a = _this.config) === null || _a === void 0 ? void 0 : _a.privacyConfig) !== null && _b !== void 0 ? _b : {}; },
1083
- getUrl: function () { return scope.location.href; },
1084
- getIdentity: function () { return getSemanticIdentity(scope); },
1085
- getAnalyticsContext: function () { var _a, _b; return (_b = (_a = _this.config) === null || _a === void 0 ? void 0 : _a.semanticAnalyticsContext) !== null && _b !== void 0 ? _b : { present: false, autocapture: [] }; },
1086
- inlineStylesheet: config.shouldInlineStylesheet,
1087
- getSelector: function (element) {
1088
- var _a, _b;
1089
- try {
1090
- var options = __assign(__assign({}, (_b = (_a = _this.config) === null || _a === void 0 ? void 0 : _a.performanceConfig) === null || _b === void 0 ? void 0 : _b.interaction), { timeoutMs: 1 });
1091
- return _this.clickHandler ? _this.clickHandler.getSelector(element, options) : finder(element, options);
1092
- }
1093
- catch (_c) {
1094
- return null;
1095
- }
1096
- },
1097
- emit: function (event) {
1098
- if (_this.recordCancelCallback && _this.recordFunction) {
1099
- _this.recordFunction.addCustomEvent("amp-semantic/".concat(event.name), event.payload);
1100
- }
1101
- },
1102
- onError: function (error) { return _this.loggerProvider.debug('Semantic capture error:', error); },
1103
- });
1206
+ if (((_a = this.semanticAdapter) === null || _a === void 0 ? void 0 : _a.createCapture) !== createCapture) {
1207
+ this.useSemanticAdapter(function (adapter) { return adapter.stop(); });
1208
+ this.semanticAdapter = undefined;
1209
+ try {
1210
+ this.semanticAdapter = new ReplaySemanticAdapter(createCapture, {
1211
+ document: scope.document,
1212
+ window: scope,
1213
+ sdk: "@amplitude/session-replay-browser@".concat(VERSION),
1214
+ getPrivacy: function () { var _a, _b; return (_b = (_a = _this.config) === null || _a === void 0 ? void 0 : _a.privacyConfig) !== null && _b !== void 0 ? _b : {}; },
1215
+ getIdentity: function () { return getSemanticIdentity(scope); },
1216
+ getAnalyticsContext: function () { var _a, _b; return (_b = (_a = _this.config) === null || _a === void 0 ? void 0 : _a.semanticAnalyticsContext) !== null && _b !== void 0 ? _b : { present: false, autocapture: [] }; },
1217
+ inlineStylesheet: config.shouldInlineStylesheet,
1218
+ emit: function (event) {
1219
+ if (_this.recordCancelCallback && _this.recordFunction) {
1220
+ try {
1221
+ _this.recordFunction.addCustomEvent("amp-semantic/".concat(event.name), event.payload);
1222
+ }
1223
+ catch (error) {
1224
+ _this.loggerProvider.debug('Error while adding semantic replay capture event:', error);
1225
+ }
1226
+ }
1227
+ },
1228
+ onError: function (error) { return _this.loggerProvider.debug('Semantic capture error:', error); },
1229
+ });
1230
+ }
1231
+ catch (error) {
1232
+ this.loggerProvider.debug('Semantic capture initialization failed:', error);
1233
+ return;
1234
+ }
1104
1235
  }
1105
- this.semanticAdapter.prepare(config.semanticCapture, sessionId);
1236
+ this.useSemanticAdapter(function (adapter) { return adapter.prepare(semanticCapture, sessionId); });
1106
1237
  };
1107
1238
  SessionReplay.prototype.buildRRWebRecordOptions = function (config, hooks, emit, errorLogPrefix) {
1108
1239
  var _this = this;
@@ -1188,7 +1319,9 @@ var SessionReplay = /** @class */ (function () {
1188
1319
  this.recordingEnabled = false;
1189
1320
  this.startChildRecordingOnSetup = false;
1190
1321
  this.recordingGeneration++;
1191
- this.stopRecordingEvents('customer_stop');
1322
+ this.cancelHiddenSend();
1323
+ this.windowFocus.stop();
1324
+ this.stopRecordingEvents();
1192
1325
  this.sendEvents();
1193
1326
  };
1194
1327
  SessionReplay.prototype.flush = function (useRetry) {
@@ -1210,11 +1343,13 @@ var SessionReplay = /** @class */ (function () {
1210
1343
  this.recordingEnabled = false;
1211
1344
  this.startChildRecordingOnSetup = false;
1212
1345
  this.recordingGeneration++;
1346
+ this.cancelHiddenSend();
1347
+ this.windowFocus.stop();
1213
1348
  (_a = this.urlChangeCleanup) === null || _a === void 0 ? void 0 : _a.call(this);
1214
1349
  (_b = this.crossOriginParentSignalCleanup) === null || _b === void 0 ? void 0 : _b.call(this);
1215
1350
  this.crossOriginParentSignalCleanup = null;
1216
1351
  this.teardownEventListeners(true);
1217
- this.stopRecordingEvents(this.semanticAdapter && this.shouldOptOut() ? 'opt_out' : 'customer_stop');
1352
+ this.stopRecordingEvents();
1218
1353
  this.sendEvents();
1219
1354
  };
1220
1355
  SessionReplay.prototype.mapSDKType = function (sdkType) {