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