@oneuptime/react-native-replay 17.0.0 → 17.1.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.
package/dist/index.cjs CHANGED
@@ -99,7 +99,7 @@ var SESSION_REPLAY_MAX_OFFLINE_DELAY_MS = 3 * 24 * 60 * 60 * 1e3;
99
99
  var MOBILE_CAPTURE_INTERVAL_MS = 500;
100
100
  var MOBILE_RECORDER_KIND = "rn-view-tree";
101
101
  var SYNTHETIC_RRWEB_VERSION = "synthetic-1";
102
- var RECORDER_VERSION = true ? "17.0.0" : "0.0.0-dev";
102
+ var RECORDER_VERSION = true ? "17.1.0" : "0.0.0-dev";
103
103
  var SESSION_REPLAY_MOBILE_RECORDER_CAPABILITIES = [
104
104
  "mobile-view-tree",
105
105
  "mobile-touch-events",
@@ -250,10 +250,11 @@ function maskTextValue(value) {
250
250
  const maskedLength = length <= 8 ? 4 : length <= 32 ? 16 : 40;
251
251
  return "\u2022".repeat(maskedLength);
252
252
  }
253
- function maskStringMapValues(values) {
253
+ var READABLE_VALUE_PATTERN = /^[A-Za-z0-9_.:/-]{0,32}$/;
254
+ function maskStringMapValues(values, readableKeys = []) {
254
255
  const masked = /* @__PURE__ */ Object.create(null);
255
256
  for (const [key, value] of Object.entries(values)) {
256
- masked[key] = maskTextValue(value);
257
+ masked[key] = readableKeys.indexOf(key) !== -1 && READABLE_VALUE_PATTERN.test(value) ? value : maskTextValue(value);
257
258
  }
258
259
  return masked;
259
260
  }
@@ -519,22 +520,28 @@ var RollingReplayBuffer = class {
519
520
  // src/CustomSignals.ts
520
521
  var TRIGGERS = [
521
522
  "click",
523
+ "tap",
522
524
  "page",
523
525
  "event",
524
526
  "error"
525
527
  ];
528
+ var MAX_TAP_TARGET_TEST_IDS = 32;
526
529
  function asRecord(value) {
527
530
  return value && typeof value === "object" && !Array.isArray(value) ? value : {};
528
531
  }
529
532
  function isTrigger(value) {
530
533
  return typeof value === "string" && TRIGGERS.indexOf(value) !== -1;
531
534
  }
535
+ function isTapPatternUsable(pattern) {
536
+ const star = pattern.indexOf("*");
537
+ return star === -1 || star > 0 && star === pattern.length - 1;
538
+ }
532
539
  function readCustomSignal(value) {
533
540
  const record = asRecord(value);
534
541
  const name = typeof record["name"] === "string" ? record["name"].trim() : "";
535
542
  const trigger = record["trigger"];
536
543
  const match = typeof record["match"] === "string" ? record["match"].trim() : "";
537
- if (name.length === 0 || name.length > SESSION_REPLAY_MAX_CUSTOM_SIGNAL_NAME_LENGTH || !isTrigger(trigger) || match.length === 0 || match.length > (trigger === "event" ? SESSION_REPLAY_MAX_CUSTOM_SIGNAL_EVENT_NAME_LENGTH : SESSION_REPLAY_MAX_CUSTOM_SIGNAL_MATCH_LENGTH) || trigger === "page" && match.charAt(0) !== "/" && match.charAt(0) !== "*") {
544
+ if (name.length === 0 || name.length > SESSION_REPLAY_MAX_CUSTOM_SIGNAL_NAME_LENGTH || !isTrigger(trigger) || match.length === 0 || match.length > (trigger === "event" ? SESSION_REPLAY_MAX_CUSTOM_SIGNAL_EVENT_NAME_LENGTH : SESSION_REPLAY_MAX_CUSTOM_SIGNAL_MATCH_LENGTH) || trigger === "page" && match.charAt(0) !== "/" && match.charAt(0) !== "*" || trigger === "tap" && !isTapPatternUsable(match)) {
538
545
  return null;
539
546
  }
540
547
  return {
@@ -655,6 +662,29 @@ function matchesErrorText(match, text) {
655
662
  function matchesEventName(match, name) {
656
663
  return match.trim().toLowerCase() === name.trim().toLowerCase();
657
664
  }
665
+ function matchesTapTarget(match, testId) {
666
+ const pattern = match.trim().toLowerCase();
667
+ const target = testId.toLowerCase();
668
+ if (!pattern || !target || !isTapPatternUsable(pattern)) {
669
+ return false;
670
+ }
671
+ return pattern.charAt(pattern.length - 1) === "*" ? target.indexOf(pattern.slice(0, -1)) === 0 : target === pattern;
672
+ }
673
+ function readTapTargetTestIds(value) {
674
+ if (!Array.isArray(value)) {
675
+ return [];
676
+ }
677
+ const testIds = [];
678
+ for (const entry of value) {
679
+ if (typeof entry === "string" && entry.length > 0 && entry.length <= SESSION_REPLAY_MAX_CUSTOM_SIGNAL_MATCH_LENGTH) {
680
+ testIds.push(entry);
681
+ }
682
+ if (testIds.length >= MAX_TAP_TARGET_TEST_IDS) {
683
+ break;
684
+ }
685
+ }
686
+ return testIds;
687
+ }
658
688
  function routeMatchPath(route) {
659
689
  let path = typeof route === "string" ? route.trim() : "";
660
690
  const scheme = path.indexOf("://");
@@ -676,6 +706,26 @@ var CustomSignalMatcher = class {
676
706
  constructor() {
677
707
  __publicField(this, "lastFiredAt", /* @__PURE__ */ new Map());
678
708
  }
709
+ /*
710
+ * A tap on a view whose testID a tap signal names - or on anything inside
711
+ * it: testIds holds the touched view's testID and those of the views
712
+ * around it, out to the replay root, the touched one first.
713
+ */
714
+ matchTap(signals, testIds, atUnixMs) {
715
+ if (testIds.length === 0) {
716
+ return [];
717
+ }
718
+ return this.collect(
719
+ signals,
720
+ "tap",
721
+ atUnixMs,
722
+ (signal) => {
723
+ return testIds.some((testId) => {
724
+ return matchesTapTarget(signal.match, testId);
725
+ });
726
+ }
727
+ );
728
+ }
679
729
  matchPage(signals, route, atUnixMs) {
680
730
  const path = routeMatchPath(route);
681
731
  return this.collect(
@@ -936,6 +986,7 @@ function failClosed(reason) {
936
986
  isTargeted: false,
937
987
  frustrationSignals: { customSignals: [] },
938
988
  productAnalytics: { enabled: false, userPropertyKeys: [] },
989
+ readableEventPropertyKeys: [],
939
990
  disabledReason: reason
940
991
  };
941
992
  }
@@ -971,6 +1022,13 @@ function normalizeReplayConfig(value) {
971
1022
  */
972
1023
  productAnalytics: readProductAnalyticsRecorderConfig(
973
1024
  config.productAnalytics
1025
+ ),
1026
+ /*
1027
+ * Read as product analytics reads the traits it keeps, as Common's
1028
+ * normalizeSessionReplayReadablePropertyKeys does: absent, none.
1029
+ */
1030
+ readableEventPropertyKeys: normalizeProductAnalyticsUserPropertyKeys(
1031
+ config.readableEventPropertyKeys
974
1032
  )
975
1033
  };
976
1034
  if (typeof config.disabledReason === "string") {
@@ -1554,6 +1612,24 @@ var ReactNativeViewTreeAdapter = class {
1554
1612
  return true;
1555
1613
  }
1556
1614
  }
1615
+ /*
1616
+ * A native module built before tap signals existed (an over-the-air
1617
+ * update of the JavaScript alone) has no such method: no testIDs, so no
1618
+ * tap signal fires, and nothing else changes.
1619
+ */
1620
+ async touchTargetTestIds(targetTag, replayRootTag) {
1621
+ const nativeModule = import_react_native.NativeModules["OneUptimeReplayViewTree"];
1622
+ if (typeof nativeModule?.touchTargetTestIds !== "function") {
1623
+ return [];
1624
+ }
1625
+ try {
1626
+ return readTapTargetTestIds(
1627
+ await nativeModule.touchTargetTestIds(targetTag, replayRootTag)
1628
+ );
1629
+ } catch {
1630
+ return [];
1631
+ }
1632
+ }
1557
1633
  };
1558
1634
  var defaultNativeViewTreeAdapter = new ReactNativeViewTreeAdapter();
1559
1635
  function getViewport() {
@@ -3203,6 +3279,259 @@ var REPLAY_ID_PATTERN2 = /^[0-9a-f]{32}$/u;
3203
3279
  function productAnalyticsSessionKey(namespace) {
3204
3280
  return `@oneuptime/replay/${namespace}/analytics-session`;
3205
3281
  }
3282
+ function productAnalyticsQueueKey(namespace) {
3283
+ return `@oneuptime/replay/${namespace}/analytics-queue`;
3284
+ }
3285
+ var PRODUCT_ANALYTICS_STORED_QUEUE_VERSION = 1;
3286
+ var PRODUCT_ANALYTICS_MAX_STORED_BYTES = 512 * 1024;
3287
+ var PRODUCT_ANALYTICS_STORE_DELAY_MS = 1e3;
3288
+ var storeWrites = /* @__PURE__ */ new WeakMap();
3289
+ function writesOf(storage) {
3290
+ let writes = storeWrites.get(storage);
3291
+ if (!writes) {
3292
+ writes = /* @__PURE__ */ new Map();
3293
+ storeWrites.set(storage, writes);
3294
+ }
3295
+ return writes;
3296
+ }
3297
+ function enqueueStoreWrite(storage, key, write) {
3298
+ const writes = writesOf(storage);
3299
+ const run = async () => {
3300
+ try {
3301
+ await write();
3302
+ } catch {
3303
+ }
3304
+ };
3305
+ const next = (writes.get(key) ?? Promise.resolve()).then(
3306
+ run,
3307
+ run
3308
+ );
3309
+ writes.set(key, next);
3310
+ return next;
3311
+ }
3312
+ async function storeWritesSettled(storage, key) {
3313
+ try {
3314
+ await writesOf(storage).get(key);
3315
+ } catch {
3316
+ }
3317
+ }
3318
+ async function clearStoredProductAnalytics(storage, namespace) {
3319
+ await Promise.all([
3320
+ enqueueStoreWrite(
3321
+ storage,
3322
+ productAnalyticsQueueKey(namespace),
3323
+ async () => {
3324
+ await storage.removeItem(productAnalyticsQueueKey(namespace));
3325
+ }
3326
+ ),
3327
+ enqueueStoreWrite(
3328
+ storage,
3329
+ productAnalyticsSessionKey(namespace),
3330
+ async () => {
3331
+ await storage.removeItem(productAnalyticsSessionKey(namespace));
3332
+ }
3333
+ )
3334
+ ]);
3335
+ }
3336
+ var EVENT_KINDS = [
3337
+ "session" /* Session */,
3338
+ "page" /* Page */,
3339
+ "custom" /* Custom */
3340
+ ];
3341
+ function isPlainRecord(value) {
3342
+ return value !== null && typeof value === "object" && !Array.isArray(value);
3343
+ }
3344
+ function isStringMap(value) {
3345
+ return isPlainRecord(value) && Object.keys(value).every((key) => {
3346
+ return typeof value[key] === "string";
3347
+ });
3348
+ }
3349
+ function isFiniteTime(value) {
3350
+ return typeof value === "number" && Number.isFinite(value);
3351
+ }
3352
+ function readStoredEvent(value) {
3353
+ if (!isPlainRecord(value) || typeof value["kind"] !== "string" || EVENT_KINDS.indexOf(value["kind"]) === -1 || typeof value["name"] !== "string" || !isFiniteTime(value["at"]) || value["page"] !== void 0 && typeof value["page"] !== "string" || value["properties"] !== void 0 && !isStringMap(value["properties"])) {
3354
+ return null;
3355
+ }
3356
+ const event = {
3357
+ kind: value["kind"],
3358
+ name: value["name"],
3359
+ at: value["at"]
3360
+ };
3361
+ if (typeof value["page"] === "string") {
3362
+ event.page = value["page"];
3363
+ }
3364
+ if (isStringMap(value["properties"])) {
3365
+ event.properties = value["properties"];
3366
+ }
3367
+ return event;
3368
+ }
3369
+ function readStoredMetric(value) {
3370
+ if (!isPlainRecord(value) || !isProductAnalyticsMetricName(value["name"]) || !isFiniteTime(value["value"]) || !isFiniteTime(value["at"]) || value["attributes"] !== void 0 && !isStringMap(value["attributes"])) {
3371
+ return null;
3372
+ }
3373
+ const metric = {
3374
+ name: value["name"],
3375
+ value: value["value"],
3376
+ at: value["at"]
3377
+ };
3378
+ if (isStringMap(value["attributes"])) {
3379
+ metric.attributes = value["attributes"];
3380
+ }
3381
+ return metric;
3382
+ }
3383
+ function readStoredScope(value) {
3384
+ if (!isPlainRecord(value) || typeof value["sessionId"] !== "string" || !REPLAY_ID_PATTERN2.test(value["sessionId"]) || typeof value["userRef"] !== "string" || value["userProperties"] !== null && !isStringMap(value["userProperties"])) {
3385
+ return null;
3386
+ }
3387
+ return {
3388
+ sessionId: value["sessionId"],
3389
+ userRef: value["userRef"],
3390
+ userProperties: value["userProperties"]
3391
+ };
3392
+ }
3393
+ function readStoredBatch(value) {
3394
+ if (!isPlainRecord(value) || value["v"] !== PRODUCT_ANALYTICS_WIRE_VERSION || typeof value["batchId"] !== "string" || !REPLAY_ID_PATTERN2.test(value["batchId"]) || typeof value["sessionId"] !== "string" || !REPLAY_ID_PATTERN2.test(value["sessionId"]) || typeof value["visitorId"] !== "string" || typeof value["consentState"] !== "string" || !isFiniteTime(value["sentAt"]) || !isStringMap(value["context"]) || !Array.isArray(value["events"]) || !Array.isArray(value["metrics"]) || value["events"].length > PRODUCT_ANALYTICS_MAX_EVENTS_PER_BATCH || value["metrics"].length > PRODUCT_ANALYTICS_MAX_METRICS_PER_BATCH || value["userRef"] !== void 0 && typeof value["userRef"] !== "string" || value["userProperties"] !== void 0 && !isStringMap(value["userProperties"])) {
3395
+ return null;
3396
+ }
3397
+ const events = [];
3398
+ const metrics = [];
3399
+ for (const entry of value["events"]) {
3400
+ const event = readStoredEvent(entry);
3401
+ if (!event) {
3402
+ return null;
3403
+ }
3404
+ events.push(event);
3405
+ }
3406
+ for (const entry of value["metrics"]) {
3407
+ const metric = readStoredMetric(entry);
3408
+ if (!metric) {
3409
+ return null;
3410
+ }
3411
+ metrics.push(metric);
3412
+ }
3413
+ const context = value["context"];
3414
+ const batch = {
3415
+ v: PRODUCT_ANALYTICS_WIRE_VERSION,
3416
+ batchId: value["batchId"],
3417
+ sessionId: value["sessionId"],
3418
+ visitorId: value["visitorId"],
3419
+ consentState: value["consentState"],
3420
+ sentAt: value["sentAt"],
3421
+ context: {
3422
+ deviceType: context["deviceType"] ?? "",
3423
+ browserName: context["browserName"] ?? "",
3424
+ osName: context["osName"] ?? "",
3425
+ appVersion: context["appVersion"] ?? ""
3426
+ },
3427
+ events,
3428
+ metrics
3429
+ };
3430
+ if (typeof value["userRef"] === "string") {
3431
+ batch.userRef = value["userRef"];
3432
+ batch.userProperties = isStringMap(value["userProperties"]) ? value["userProperties"] : {};
3433
+ }
3434
+ return batch;
3435
+ }
3436
+ function readStoredQueue(raw) {
3437
+ if (!raw) {
3438
+ return null;
3439
+ }
3440
+ let parsed;
3441
+ try {
3442
+ parsed = JSON.parse(raw);
3443
+ } catch {
3444
+ return null;
3445
+ }
3446
+ if (!isPlainRecord(parsed) || parsed["v"] !== PRODUCT_ANALYTICS_STORED_QUEUE_VERSION || !Array.isArray(parsed["scopes"]) || !Array.isArray(parsed["items"])) {
3447
+ return null;
3448
+ }
3449
+ const scopes = parsed["scopes"].map(readStoredScope);
3450
+ const items = [];
3451
+ for (const entry of parsed["items"]) {
3452
+ if (!isPlainRecord(entry) || typeof entry["s"] !== "number") {
3453
+ continue;
3454
+ }
3455
+ const scope = scopes[entry["s"]];
3456
+ if (!scope) {
3457
+ continue;
3458
+ }
3459
+ if (entry["e"] !== void 0) {
3460
+ const event = readStoredEvent(
3461
+ entry["e"]
3462
+ );
3463
+ if (event) {
3464
+ items.push({ scope, event });
3465
+ }
3466
+ continue;
3467
+ }
3468
+ const metric = readStoredMetric(
3469
+ entry["m"]
3470
+ );
3471
+ if (metric) {
3472
+ items.push({ scope, metric });
3473
+ }
3474
+ }
3475
+ return {
3476
+ items,
3477
+ pending: parsed["pending"] === null || parsed["pending"] === void 0 ? null : readStoredBatch(parsed["pending"])
3478
+ };
3479
+ }
3480
+ function scopeKey(scope) {
3481
+ return JSON.stringify([scope.sessionId, scope.userRef, scope.userProperties]);
3482
+ }
3483
+ function buildStoredQueue(items, pending) {
3484
+ const named = items.filter((item) => {
3485
+ return item.scope.sessionId !== "";
3486
+ });
3487
+ if (named.length === 0 && !pending) {
3488
+ return null;
3489
+ }
3490
+ const build = (from2) => {
3491
+ const scopes = [];
3492
+ const scopeIndex = /* @__PURE__ */ new Map();
3493
+ const stored = [];
3494
+ for (const item of named.slice(from2)) {
3495
+ const key = scopeKey(item.scope);
3496
+ let index = scopeIndex.get(key);
3497
+ if (index === void 0) {
3498
+ index = scopes.length;
3499
+ scopes.push(item.scope);
3500
+ scopeIndex.set(key, index);
3501
+ }
3502
+ if (item.event) {
3503
+ stored.push({ s: index, e: item.event });
3504
+ } else if (item.metric) {
3505
+ stored.push({ s: index, m: item.metric });
3506
+ }
3507
+ }
3508
+ const queue = {
3509
+ v: PRODUCT_ANALYTICS_STORED_QUEUE_VERSION,
3510
+ scopes,
3511
+ items: stored,
3512
+ pending
3513
+ };
3514
+ return JSON.stringify(queue);
3515
+ };
3516
+ let from = 0;
3517
+ let value = build(from);
3518
+ if (byteLength(value) <= PRODUCT_ANALYTICS_MAX_STORED_BYTES) {
3519
+ return value;
3520
+ }
3521
+ let low = 1;
3522
+ let high = named.length;
3523
+ while (low < high) {
3524
+ const middle = Math.floor((low + high) / 2);
3525
+ if (byteLength(build(middle)) <= PRODUCT_ANALYTICS_MAX_STORED_BYTES) {
3526
+ high = middle;
3527
+ } else {
3528
+ low = middle + 1;
3529
+ }
3530
+ }
3531
+ from = low;
3532
+ value = build(from);
3533
+ return byteLength(value) <= PRODUCT_ANALYTICS_MAX_STORED_BYTES ? value : null;
3534
+ }
3206
3535
  function itemAt(item) {
3207
3536
  return item.event ? item.event.at : item.metric?.at ?? 0;
3208
3537
  }
@@ -3253,6 +3582,13 @@ var MobileProductAnalytics = class {
3253
3582
  __publicField(this, "flushTimer", null);
3254
3583
  __publicField(this, "appStateSubscription", null);
3255
3584
  __publicField(this, "unsubscribeConnectivity", null);
3585
+ /*
3586
+ * The stored queue (issue #9577). Written only once what a previous run
3587
+ * left there has been read back, so a channel that never got that far
3588
+ * can never overwrite it with nothing.
3589
+ */
3590
+ __publicField(this, "restored", false);
3591
+ __publicField(this, "storeTimer", null);
3256
3592
  this.host = host;
3257
3593
  this.config = config;
3258
3594
  }
@@ -3266,6 +3602,9 @@ var MobileProductAnalytics = class {
3266
3602
  return;
3267
3603
  }
3268
3604
  let stored = null;
3605
+ let storedQueue = null;
3606
+ const queueKey = productAnalyticsQueueKey(this.host.namespace);
3607
+ await storeWritesSettled(this.host.storage, queueKey);
3269
3608
  try {
3270
3609
  stored = await this.host.storage.getItem(
3271
3610
  productAnalyticsSessionKey(this.host.namespace)
@@ -3273,11 +3612,20 @@ var MobileProductAnalytics = class {
3273
3612
  } catch {
3274
3613
  stored = null;
3275
3614
  }
3615
+ try {
3616
+ storedQueue = await this.host.storage.getItem(queueKey);
3617
+ } catch {
3618
+ storedQueue = null;
3619
+ }
3276
3620
  if (this.started || this.stopReason) {
3277
3621
  return;
3278
3622
  }
3279
3623
  this.announcedSessionId = typeof stored === "string" && REPLAY_ID_PATTERN2.test(stored) ? stored : null;
3280
3624
  this.started = true;
3625
+ this.restored = true;
3626
+ if (!this.revoked) {
3627
+ this.restoreQueue(storedQueue, this.host.now());
3628
+ }
3281
3629
  this.enterCurrentSession(this.host.now(), false);
3282
3630
  this.flushTimer = setInterval(() => {
3283
3631
  this.flush(false);
@@ -3379,6 +3727,7 @@ var MobileProductAnalytics = class {
3379
3727
  if (this.metricCount > PRODUCT_ANALYTICS_MAX_PENDING_EVENTS) {
3380
3728
  this.dropOldest("metric");
3381
3729
  }
3730
+ this.scheduleStore();
3382
3731
  if (this.metricCount >= PRODUCT_ANALYTICS_MAX_METRICS_PER_BATCH) {
3383
3732
  this.flush(false);
3384
3733
  }
@@ -3417,6 +3766,7 @@ var MobileProductAnalytics = class {
3417
3766
  forget() {
3418
3767
  this.revoked = true;
3419
3768
  this.clear();
3769
+ this.forgetStoredQueue();
3420
3770
  this.forgetAnnouncedSession();
3421
3771
  }
3422
3772
  /*
@@ -3426,6 +3776,7 @@ var MobileProductAnalytics = class {
3426
3776
  */
3427
3777
  reset() {
3428
3778
  this.clear();
3779
+ this.forgetStoredQueue();
3429
3780
  this.forgetAnnouncedSession();
3430
3781
  }
3431
3782
  /* Granted: what was held is filed under the session the grant opened, and goes now. */
@@ -3433,6 +3784,7 @@ var MobileProductAnalytics = class {
3433
3784
  this.revoked = false;
3434
3785
  this.retryAtUnixMs = 0;
3435
3786
  this.enterCurrentSession(this.host.now(), false);
3787
+ this.scheduleStore();
3436
3788
  this.flush(false);
3437
3789
  }
3438
3790
  /*
@@ -3445,13 +3797,23 @@ var MobileProductAnalytics = class {
3445
3797
  return;
3446
3798
  }
3447
3799
  this.enterCurrentSession(this.host.now(), false);
3800
+ this.scheduleStore();
3448
3801
  this.flush(false);
3449
3802
  }
3450
3803
  stop(reason = "stopped") {
3804
+ const isFirstStop = !this.stopReason;
3451
3805
  if (!this.stopReason) {
3452
3806
  this.stopReason = reason;
3453
3807
  }
3808
+ if (isFirstStop && this.restored) {
3809
+ if (reason === "stopped") {
3810
+ this.storeNow();
3811
+ } else {
3812
+ this.forgetStoredQueue();
3813
+ }
3814
+ }
3454
3815
  this.clear();
3816
+ this.cancelStoreTimer();
3455
3817
  if (this.flushTimer) {
3456
3818
  clearInterval(this.flushTimer);
3457
3819
  this.flushTimer = null;
@@ -3585,6 +3947,7 @@ var MobileProductAnalytics = class {
3585
3947
  this.eventCount++;
3586
3948
  this.announce(sessionId);
3587
3949
  this.trimEvents();
3950
+ this.scheduleStore();
3588
3951
  }
3589
3952
  if (firstUnnamed < 0 && isNewSession && this.route) {
3590
3953
  this.shownPage = this.route;
@@ -3599,6 +3962,7 @@ var MobileProductAnalytics = class {
3599
3962
  });
3600
3963
  this.eventCount++;
3601
3964
  this.trimEvents();
3965
+ this.scheduleStore();
3602
3966
  }
3603
3967
  return sessionId;
3604
3968
  }
@@ -3606,6 +3970,7 @@ var MobileProductAnalytics = class {
3606
3970
  this.items.push({ scope, event });
3607
3971
  this.eventCount++;
3608
3972
  this.trimEvents();
3973
+ this.scheduleStore();
3609
3974
  if (this.eventCount >= PRODUCT_ANALYTICS_MAX_EVENTS_PER_BATCH) {
3610
3975
  this.flush(false);
3611
3976
  }
@@ -3698,6 +4063,7 @@ var MobileProductAnalytics = class {
3698
4063
  this.items.splice(0, count);
3699
4064
  this.eventCount -= batch.events.length;
3700
4065
  this.metricCount -= batch.metrics.length;
4066
+ this.scheduleStore();
3701
4067
  return batch;
3702
4068
  }
3703
4069
  dropTooOld(now) {
@@ -3715,8 +4081,116 @@ var MobileProductAnalytics = class {
3715
4081
  this.metricCount--;
3716
4082
  }
3717
4083
  }
4084
+ if (kept.length !== this.items.length) {
4085
+ this.scheduleStore();
4086
+ }
3718
4087
  this.items = kept;
3719
4088
  }
4089
+ /*
4090
+ * What a previous run left in the device's storage (issue #9577), taken
4091
+ * back before anything new: the items under the sessions and people they
4092
+ * happened under, the batch that was on its way under its own id. What
4093
+ * the server would no longer keep (PRODUCT_ANALYTICS_MAX_EVENT_AGE_MS) is
4094
+ * left out, and the caps apply as to anything held: past them the oldest
4095
+ * events go, counted in the diagnostics' dropped.
4096
+ */
4097
+ restoreQueue(raw, now) {
4098
+ if (!raw) {
4099
+ return;
4100
+ }
4101
+ const queue = readStoredQueue(raw);
4102
+ if (!queue) {
4103
+ this.host.onDiagnostic("analytics-store-unreadable");
4104
+ this.forgetStoredQueue();
4105
+ return;
4106
+ }
4107
+ const oldest = now - PRODUCT_ANALYTICS_MAX_EVENT_AGE_MS;
4108
+ const droppedBefore = this.droppedEvents;
4109
+ let tooOld = 0;
4110
+ const pending = queue.pending;
4111
+ if (pending) {
4112
+ const isLive = [...pending.events, ...pending.metrics].some(
4113
+ (item) => {
4114
+ return item.at >= oldest;
4115
+ }
4116
+ );
4117
+ if (isLive) {
4118
+ this.pending = pending;
4119
+ } else {
4120
+ this.droppedEvents += pending.events.length;
4121
+ tooOld += pending.events.length + pending.metrics.length;
4122
+ }
4123
+ }
4124
+ this.items = queue.items.filter((item) => {
4125
+ if (itemAt(item) >= oldest) {
4126
+ return true;
4127
+ }
4128
+ if (item.event) {
4129
+ this.droppedEvents++;
4130
+ }
4131
+ tooOld++;
4132
+ return false;
4133
+ });
4134
+ this.eventCount = this.items.filter((item) => {
4135
+ return Boolean(item.event);
4136
+ }).length;
4137
+ this.metricCount = this.items.length - this.eventCount;
4138
+ const droppedTooOld = this.droppedEvents - droppedBefore;
4139
+ this.trimEvents();
4140
+ while (this.metricCount > PRODUCT_ANALYTICS_MAX_PENDING_EVENTS) {
4141
+ this.dropOldest("metric");
4142
+ }
4143
+ this.host.onDiagnostic("analytics-restored", {
4144
+ events: this.eventCount + (this.pending?.events.length ?? 0),
4145
+ metrics: this.metricCount + (this.pending?.metrics.length ?? 0),
4146
+ droppedTooOld,
4147
+ droppedOverCap: this.droppedEvents - droppedBefore - droppedTooOld
4148
+ });
4149
+ if (tooOld > 0 || this.items.length < queue.items.length) {
4150
+ this.scheduleStore();
4151
+ }
4152
+ }
4153
+ /* A change to what is held: written once the burst it belongs to is over. */
4154
+ scheduleStore() {
4155
+ if (!this.restored || this.stopReason || this.storeTimer) {
4156
+ return;
4157
+ }
4158
+ this.storeTimer = setTimeout(() => {
4159
+ this.storeTimer = null;
4160
+ this.storeNow();
4161
+ }, PRODUCT_ANALYTICS_STORE_DELAY_MS);
4162
+ }
4163
+ /* What is held, written now, in the order the writes were asked for. */
4164
+ storeNow() {
4165
+ this.cancelStoreTimer();
4166
+ if (!this.restored || this.revoked) {
4167
+ return;
4168
+ }
4169
+ const storage = this.host.storage;
4170
+ const key = productAnalyticsQueueKey(this.host.namespace);
4171
+ const value = buildStoredQueue(this.items, this.pending);
4172
+ void enqueueStoreWrite(storage, key, async () => {
4173
+ if (value === null) {
4174
+ await storage.removeItem(key);
4175
+ } else {
4176
+ await storage.setItem(key, value);
4177
+ }
4178
+ });
4179
+ }
4180
+ forgetStoredQueue() {
4181
+ this.cancelStoreTimer();
4182
+ const storage = this.host.storage;
4183
+ const key = productAnalyticsQueueKey(this.host.namespace);
4184
+ void enqueueStoreWrite(storage, key, async () => {
4185
+ await storage.removeItem(key);
4186
+ });
4187
+ }
4188
+ cancelStoreTimer() {
4189
+ if (this.storeTimer) {
4190
+ clearTimeout(this.storeTimer);
4191
+ this.storeTimer = null;
4192
+ }
4193
+ }
3720
4194
  send(batch) {
3721
4195
  const generation = this.sendGeneration;
3722
4196
  const controller = typeof AbortController === "undefined" ? null : new AbortController();
@@ -3727,7 +4201,7 @@ var MobileProductAnalytics = class {
3727
4201
  ...this.host.headers,
3728
4202
  "Content-Type": PRODUCT_ANALYTICS_CONTENT_TYPE
3729
4203
  },
3730
- body: JSON.stringify(batch)
4204
+ body: JSON.stringify({ ...batch, sentAt: this.host.now() })
3731
4205
  };
3732
4206
  if (controller) {
3733
4207
  init.signal = controller.signal;
@@ -3771,6 +4245,7 @@ var MobileProductAnalytics = class {
3771
4245
  if (status === 202 || status === 200) {
3772
4246
  this.pending = null;
3773
4247
  this.failures = 0;
4248
+ this.scheduleStore();
3774
4249
  if (this.items.length > 0) {
3775
4250
  this.flush(false);
3776
4251
  }
@@ -3778,6 +4253,7 @@ var MobileProductAnalytics = class {
3778
4253
  }
3779
4254
  if (status === 413) {
3780
4255
  this.pending = null;
4256
+ this.scheduleStore();
3781
4257
  return;
3782
4258
  }
3783
4259
  if (status === 204 || status === 400 || status === 403) {
@@ -3827,6 +4303,7 @@ var MobileProductAnalytics = class {
3827
4303
  return;
3828
4304
  }
3829
4305
  this.flush(true);
4306
+ this.storeNow();
3830
4307
  }
3831
4308
  );
3832
4309
  } catch {
@@ -4286,6 +4763,8 @@ function isTouchPrivate(privacyMap, x2, y) {
4286
4763
  // src/MobileReplayRecorder.ts
4287
4764
  var MAX_DIAGNOSTIC_EVENTS = 100;
4288
4765
  var TOUCH_MOVE_SAMPLE_MS = 50;
4766
+ var TAP_SLOP_POINTS = 10;
4767
+ var MAX_TAP_CANDIDATES = 10;
4289
4768
  var MOBILE_POLICY_REFRESH_INTERVAL_MS = 5 * 6e4;
4290
4769
  var MAX_EVENTS_HELD_FOR_RESUME = 500;
4291
4770
  var SAFE_ERROR_NAMES = /* @__PURE__ */ new Set([
@@ -4375,7 +4854,7 @@ var MobileReplayRecorder = class {
4375
4854
  __publicField(this, "now");
4376
4855
  __publicField(this, "serializer", new ViewTreeSerializer());
4377
4856
  __publicField(this, "chunkBuffer", new ReplayChunkBuffer());
4378
- /* The application's own page, event and error signals (issue #6443). */
4857
+ /* The application's own tap, page, event and error signals (#6443, #6517). */
4379
4858
  __publicField(this, "customSignalMatcher", new CustomSignalMatcher());
4380
4859
  __publicField(this, "rollingBuffer", new RollingReplayBuffer());
4381
4860
  __publicField(this, "diagnosticsStatus", "idle");
@@ -4471,6 +4950,11 @@ var MobileReplayRecorder = class {
4471
4950
  __publicField(this, "suppressedTouchPointers", /* @__PURE__ */ new Set());
4472
4951
  __publicField(this, "unidentifiedTouchCount", 0);
4473
4952
  __publicField(this, "unidentifiedTouchPrivate", false);
4953
+ /*
4954
+ * Each touch still down that may yet be a tap, for the application's tap
4955
+ * signals (issue #6517): where it started and on which view.
4956
+ */
4957
+ __publicField(this, "tapCandidates", /* @__PURE__ */ new Map());
4474
4958
  /*
4475
4959
  * When the person last did something: a touch (any touch - one inside a
4476
4960
  * ReplayMask is still someone using the app, even though it is never
@@ -4859,6 +5343,7 @@ var MobileReplayRecorder = class {
4859
5343
  await this.outbox?.clear();
4860
5344
  await this.sessionStore?.clear();
4861
5345
  await this.forgetCachedConfig();
5346
+ await this.forgetStoredProductAnalytics();
4862
5347
  this.identity = null;
4863
5348
  this.userRef = null;
4864
5349
  this.traits = {};
@@ -4932,7 +5417,8 @@ var MobileReplayRecorder = class {
4932
5417
  this.analytics?.track(name, properties);
4933
5418
  });
4934
5419
  const sanitizedProperties = maskStringMapValues(
4935
- sanitizeEventProperties(properties)
5420
+ sanitizeEventProperties(properties),
5421
+ this.readableEventPropertyKeys()
4936
5422
  );
4937
5423
  this.runExternalActivity(this.now(), () => {
4938
5424
  this.appendCustom(
@@ -5119,8 +5605,16 @@ var MobileReplayRecorder = class {
5119
5605
  if (!this.running || !this.acceptingEvents || !this.foreground || !this.isCurrentGeneration(generation)) {
5120
5606
  return;
5121
5607
  }
5122
- if (this.updateTouchSuppression(touch, pointIsPrivate)) {
5608
+ const suppressed = this.updateTouchSuppression(
5609
+ touch,
5610
+ pointIsPrivate
5611
+ );
5612
+ const tap = this.followTap(touch);
5613
+ if (suppressed) {
5123
5614
  this.diagnostic("touch-suppressed-private-region");
5615
+ if (tap) {
5616
+ await this.recordTapSignals(tap, touch.timestamp, generation, false);
5617
+ }
5124
5618
  return;
5125
5619
  }
5126
5620
  if (touch.phase === "move" && touch.timestamp - this.lastTouchMoveAtUnixMs < TOUCH_MOVE_SAMPLE_MS) {
@@ -5146,6 +5640,90 @@ var MobileReplayRecorder = class {
5146
5640
  y: touch.y
5147
5641
  });
5148
5642
  }
5643
+ if (tap) {
5644
+ await this.recordTapSignals(tap, touch.timestamp, generation, true);
5645
+ }
5646
+ }
5647
+ /* The application's signals that fire on a tap. */
5648
+ tapSignals() {
5649
+ return this.customSignals().filter(
5650
+ (signal) => {
5651
+ return signal.trigger === "tap";
5652
+ }
5653
+ );
5654
+ }
5655
+ /*
5656
+ * Follows each touch for the application's tap signals (issue #6517): a
5657
+ * touch that ends within TAP_SLOP_POINTS of where it started is a tap on
5658
+ * the view it started on, and is returned on its end. A touch that slid
5659
+ * further was a scroll or a drag. Nothing is followed while the
5660
+ * application has no tap signal, so a touch costs what it always did.
5661
+ */
5662
+ followTap(touch) {
5663
+ const key = touch.pointerId === void 0 ? "unidentified" : `${typeof touch.pointerId}:${String(touch.pointerId)}`;
5664
+ if (touch.phase === "start") {
5665
+ if (this.tapSignals().length === 0 || this.tapCandidates.size >= MAX_TAP_CANDIDATES && !this.tapCandidates.has(key)) {
5666
+ return null;
5667
+ }
5668
+ this.tapCandidates.set(key, {
5669
+ x: touch.x,
5670
+ y: touch.y,
5671
+ targetTag: touch.targetTag
5672
+ });
5673
+ return null;
5674
+ }
5675
+ const started = this.tapCandidates.get(key);
5676
+ if (!started) {
5677
+ return null;
5678
+ }
5679
+ const slid = Math.hypot(touch.x - started.x, touch.y - started.y) > TAP_SLOP_POINTS;
5680
+ if (touch.phase === "move") {
5681
+ if (slid) {
5682
+ this.tapCandidates.delete(key);
5683
+ }
5684
+ return null;
5685
+ }
5686
+ this.tapCandidates.delete(key);
5687
+ return slid || started.targetTag === void 0 ? null : { targetTag: started.targetTag, x: touch.x, y: touch.y };
5688
+ }
5689
+ /*
5690
+ * A tap's signals: the touched view's testID, and those of the views
5691
+ * around it out to the replay root, matched here on the device - never
5692
+ * recorded or sent. A hit carries the signal's name, where the tap was
5693
+ * when the touch itself is recorded (withPosition), and like a page or
5694
+ * an event signal it starts an "On error or frustration" upload when it
5695
+ * counts as frustration.
5696
+ */
5697
+ async recordTapSignals(tap, atUnixMs, generation, withPosition) {
5698
+ const signals = this.tapSignals();
5699
+ if (signals.length === 0 || !this.rootTag || typeof this.nativeViewTree.touchTargetTestIds !== "function") {
5700
+ return;
5701
+ }
5702
+ let testIds = [];
5703
+ try {
5704
+ testIds = readTapTargetTestIds(
5705
+ await this.nativeViewTree.touchTargetTestIds(
5706
+ tap.targetTag,
5707
+ this.rootTag
5708
+ )
5709
+ );
5710
+ } catch {
5711
+ this.diagnostic("tap-target-unreadable");
5712
+ return;
5713
+ }
5714
+ if (!this.running || !this.acceptingEvents || !this.isCurrentGeneration(generation)) {
5715
+ return;
5716
+ }
5717
+ const hits = this.customSignalMatcher.matchTap(signals, testIds, atUnixMs);
5718
+ if (withPosition) {
5719
+ for (const hit of hits) {
5720
+ hit.x = tap.x;
5721
+ hit.y = tap.y;
5722
+ }
5723
+ }
5724
+ if (this.recordCustomSignals(hits)) {
5725
+ await this.trigger("frustration" /* Frustration */);
5726
+ }
5149
5727
  }
5150
5728
  setRootTag(rootTag) {
5151
5729
  this.rootTag = typeof rootTag === "number" && Number.isSafeInteger(rootTag) && rootTag > 0 ? rootTag : null;
@@ -5482,10 +6060,17 @@ var MobileReplayRecorder = class {
5482
6060
  }
5483
6061
  }
5484
6062
  }
5485
- /* The page, event and error signals the application's policy defines. */
6063
+ /* The tap, page, event and error signals the application's policy defines. */
5486
6064
  customSignals() {
5487
6065
  return this.config?.frustrationSignals.customSignals ?? [];
5488
6066
  }
6067
+ /*
6068
+ * The application's readable event properties (issue #7501): none until
6069
+ * the policy is read, so a track() before it is masked whole.
6070
+ */
6071
+ readableEventPropertyKeys() {
6072
+ return this.config?.readableEventPropertyKeys ?? [];
6073
+ }
5489
6074
  /*
5490
6075
  * Marks each hit in the stream and counts it on the chunk, as the browser
5491
6076
  * recorder does. Returns whether one of them counts as frustration, which
@@ -5512,6 +6097,10 @@ var MobileReplayRecorder = class {
5512
6097
  isFrustration: hit.isFrustration,
5513
6098
  atUnixMs: hit.atUnixMs
5514
6099
  };
6100
+ if (typeof hit.x === "number" && Number.isFinite(hit.x) && typeof hit.y === "number" && Number.isFinite(hit.y)) {
6101
+ payload.x = hit.x;
6102
+ payload.y = hit.y;
6103
+ }
5515
6104
  this.appendEvent(
5516
6105
  createCustomEvent(
5517
6106
  CUSTOM_SIGNAL_CUSTOM_EVENT_TAG,
@@ -6032,6 +6621,20 @@ var MobileReplayRecorder = class {
6032
6621
  );
6033
6622
  }
6034
6623
  }
6624
+ /*
6625
+ * The product analytics events and metric points the device kept unsent
6626
+ * (issue #9577) go with consent too, as the outbox's chunks do - a
6627
+ * channel running now drops its own (forget(), reset()), and this
6628
+ * removes them whether one runs or not, a previous launch's included.
6629
+ */
6630
+ async forgetStoredProductAnalytics() {
6631
+ if (this.options) {
6632
+ await clearStoredProductAnalytics(
6633
+ this.storage,
6634
+ getReplayStorageNamespace(this.options)
6635
+ );
6636
+ }
6637
+ }
6035
6638
  mayPersistUnder(config) {
6036
6639
  return !config.enabled || config.consentMode === "NotRequired" /* NotRequired */ || this.consentGranted;
6037
6640
  }
@@ -6148,6 +6751,7 @@ var MobileReplayRecorder = class {
6148
6751
  this.suppressedTouchPointers.clear();
6149
6752
  this.unidentifiedTouchCount = 0;
6150
6753
  this.unidentifiedTouchPrivate = false;
6754
+ this.tapCandidates.clear();
6151
6755
  }
6152
6756
  async applyIdentityChange(ref, traits, identityEpoch) {
6153
6757
  if (identityEpoch !== this.identityCancellationEpoch) {
@@ -6408,6 +7012,7 @@ var MobileReplayRecorder = class {
6408
7012
  await this.outbox?.clear();
6409
7013
  await this.sessionStore?.clear();
6410
7014
  await this.forgetCachedConfig();
7015
+ await this.forgetStoredProductAnalytics();
6411
7016
  if (!this.running || !this.isCurrentGeneration(generation)) {
6412
7017
  return false;
6413
7018
  }
@@ -6887,7 +7492,10 @@ var MobileReplayRecorder = class {
6887
7492
  return this.analyticsIdentity();
6888
7493
  },
6889
7494
  toProperties: (value) => {
6890
- return maskStringMapValues(sanitizeEventProperties(value));
7495
+ return maskStringMapValues(
7496
+ sanitizeEventProperties(value),
7497
+ this.readableEventPropertyKeys()
7498
+ );
6891
7499
  },
6892
7500
  getContext: () => {
6893
7501
  return this.analyticsContext();