@traqio/web 0.1.1 → 0.1.3

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/build/index.js CHANGED
@@ -299,17 +299,51 @@ var require_deeplink = __commonJS({
299
299
  "use strict";
300
300
  Object.defineProperty(exports, "__esModule", { value: true });
301
301
  exports.isTrackingUrl = isTrackingUrl;
302
+ exports.isOpenUrl = isOpenUrl;
302
303
  exports.toDeepLink = toDeepLink;
304
+ exports.upgradeStoredDeepLink = upgradeStoredDeepLink;
303
305
  var TRACKING_PATH = /^\/c\/[A-Za-z0-9_-]{1,50}\/?$/;
304
- function isTrackingUrl(url) {
306
+ var OPEN_PATH = /^\/o\/[0-9a-fA-F-]{36}\/?$/;
307
+ function pathOf(url) {
305
308
  var _a;
306
309
  const m = /^https?:\/\/[^/?#]+(\/[^?#]*)?/i.exec(url);
307
- return Boolean(m && TRACKING_PATH.test((_a = m[1]) != null ? _a : ""));
310
+ return m ? (_a = m[1]) != null ? _a : "" : null;
311
+ }
312
+ function isTrackingUrl(url) {
313
+ const path = pathOf(url);
314
+ return path !== null && (TRACKING_PATH.test(path) || OPEN_PATH.test(path));
315
+ }
316
+ function isOpenUrl(url) {
317
+ const path = pathOf(url);
318
+ return path !== null && OPEN_PATH.test(path);
308
319
  }
309
320
  function toDeepLink(answer, deferred, url) {
321
+ var _a, _b, _c;
310
322
  if (!(answer == null ? void 0 : answer.matched) || !answer.via)
311
323
  return null;
312
- return { deepLink: answer.deepLink, code: answer.code, deferred, via: answer.via, ...url ? { url } : {} };
324
+ const matchType = (_a = answer.matchType) != null ? _a : answer.via === "ip_match" ? "probabilistic" : answer.via;
325
+ const payload = (_b = answer.payload) != null ? _b : { deepLink: answer.deepLink, value: null, params: {} };
326
+ return {
327
+ payload,
328
+ matchType,
329
+ confidence: matchType === "probabilistic" ? (_c = answer.confidence) != null ? _c : "low" : null,
330
+ deferred,
331
+ code: answer.code,
332
+ deepLink: payload.deepLink,
333
+ via: answer.via,
334
+ ...url ? { url } : {}
335
+ };
336
+ }
337
+ function upgradeStoredDeepLink(stored) {
338
+ var _a, _b, _c;
339
+ if (!stored || typeof stored !== "object")
340
+ return null;
341
+ const link = stored;
342
+ if (link.payload && link.matchType)
343
+ return link;
344
+ if (!link.via)
345
+ return null;
346
+ return toDeepLink({ matched: true, via: link.via, code: (_a = link.code) != null ? _a : null, deepLink: (_b = link.deepLink) != null ? _b : null }, (_c = link.deferred) != null ? _c : true, link.url);
313
347
  }
314
348
  }
315
349
  });
@@ -327,13 +361,13 @@ var require_build = __commonJS({
327
361
  "../sdk-core/build/index.js"(exports) {
328
362
  "use strict";
329
363
  Object.defineProperty(exports, "__esModule", { value: true });
330
- exports.Traqio = exports.Client = exports.isTrackingUrl = exports.reportAdServicesToken = exports.Skan = exports.SDK_VERSION = void 0;
364
+ exports.Traqio = exports.Client = exports.isTrackingUrl = exports.isOpenUrl = exports.reportAdServicesToken = exports.Skan = exports.SDK_VERSION = void 0;
331
365
  exports.normalizeAdvertisingId = normalizeAdvertisingId;
332
366
  exports.fromMaxAd = fromMaxAd;
333
367
  var skan_1 = require_skan();
334
368
  var adservices_1 = require_adservices();
335
369
  var deeplink_1 = require_deeplink();
336
- exports.SDK_VERSION = "0.1.1";
370
+ exports.SDK_VERSION = "0.2.0";
337
371
  var skan_2 = require_skan();
338
372
  Object.defineProperty(exports, "Skan", { enumerable: true, get: function() {
339
373
  return skan_2.Skan;
@@ -343,6 +377,9 @@ var require_build = __commonJS({
343
377
  return adservices_2.reportAdServicesToken;
344
378
  } });
345
379
  var deeplink_2 = require_deeplink();
380
+ Object.defineProperty(exports, "isOpenUrl", { enumerable: true, get: function() {
381
+ return deeplink_2.isOpenUrl;
382
+ } });
346
383
  Object.defineProperty(exports, "isTrackingUrl", { enumerable: true, get: function() {
347
384
  return deeplink_2.isTrackingUrl;
348
385
  } });
@@ -355,8 +392,11 @@ var require_build = __commonJS({
355
392
  var LAST_SESSION_KEY = "traq_last_session";
356
393
  var ERASED_KEY = "traq_erased";
357
394
  var DEFERRED_KEY = "traq_deferred_link";
395
+ var DEFERRED_RETRY_KEY = "traq_deferred_consent_retry_until";
396
+ var DEFERRED_RETRY_WINDOW_MS = 60 * 6e4;
358
397
  var CONSENT_KEY = "traq_consent";
359
398
  var MAX_BATCH = 20;
399
+ var NON_RETRYABLE_STATUS = /* @__PURE__ */ new Set([400, 413, 422]);
360
400
  var MAX_QUEUE = 500;
361
401
  var MAX_AD_BATCH = 100;
362
402
  var MAX_AD_QUEUE = 1e3;
@@ -472,7 +512,7 @@ var require_build = __commonJS({
472
512
  const freshInstall = Boolean(this.opts.storage) && !await this.opts.storage.getItem(INSTALL_SENT_KEY).catch(() => "1");
473
513
  await this.sendInstallOnce();
474
514
  if (this.opts.deepLinking !== false) {
475
- this.deferred = this.resolveDeferredOnce(freshInstall);
515
+ this.deferred = this.resolveDeferredOnce(freshInstall).then((link) => link != null ? link : this.retryDeferredWithConsent());
476
516
  this.wireDeepLinks();
477
517
  }
478
518
  }
@@ -590,20 +630,47 @@ var require_build = __commonJS({
590
630
  if (!this.started || this.erased || this.opts.deepLinking === false || !(0, deeplink_1.isTrackingUrl)(url))
591
631
  return null;
592
632
  const answer = await this.postJson({ url }, "/deeplink/open");
593
- const link = (0, deeplink_1.toDeepLink)(answer, false, url);
633
+ const deferred = (0, deeplink_1.isOpenUrl)(url);
634
+ const link = (0, deeplink_1.toDeepLink)(answer, deferred, url);
635
+ if (link && deferred)
636
+ await this.keepDeferred(link);
594
637
  if (link)
595
638
  this.emitDeepLink(link);
596
639
  return link;
597
640
  }
598
641
  /**
599
- * The deferred deep link of this install, if it came from one — resolved once,
600
- * on the first launch, and remembered. Null on every install that did not
601
- * come from a link (and on iOS unless the IP match is on for this app).
642
+ * The deferred deep link of this install, if it came from one — resolved on the
643
+ * first launch and remembered. Null on every install that did not come from a
644
+ * link. `matchType` says how it was found; read `payload.value` for the token the
645
+ * shared URL carried (`deep_link_value`).
646
+ *
647
+ * iOS has no install referrer: the deterministic carriers there are the landing
648
+ * page's « Open in app » link (delivered through `handleUrl` / `onDeepLink`) and
649
+ * its code (`redeemDeepLinkCode`). The probabilistic fallback runs only when the
650
+ * user consented — `setConsent(true)` or `consent: true` at init — and the app
651
+ * turned the IP match on in the console.
602
652
  */
603
653
  getDeferredDeepLink() {
604
654
  var _a;
605
655
  return (_a = this.deferred) != null ? _a : Promise.resolve(null);
606
656
  }
657
+ /**
658
+ * The code the iOS landing page showed (« ABCD-EFGH »), typed by the user in the
659
+ * app — the manual fallback when neither the store nor a link carried the click.
660
+ * Case, spaces and the dash are forgiven. Resolves to the link (`matchType:
661
+ * "code"`), delivered to `onDeepLink` too, or null for an unknown / expired code.
662
+ */
663
+ async redeemDeepLinkCode(code) {
664
+ if (!this.started || this.erased || this.opts.deepLinking === false || !code.trim())
665
+ return null;
666
+ const answer = await this.postJson({ code }, "/deeplink/code");
667
+ const link = (0, deeplink_1.toDeepLink)(answer, true);
668
+ if (!link)
669
+ return null;
670
+ await this.keepDeferred(link);
671
+ this.emitDeepLink(link);
672
+ return link;
673
+ }
607
674
  /**
608
675
  * GDPR Art. 17, from inside the app: delete everything the server holds about this
609
676
  * device and this user, and stop sending.
@@ -647,6 +714,10 @@ var require_build = __commonJS({
647
714
  var _a, _b;
648
715
  this.consent = granted ? "granted" : "denied";
649
716
  await ((_b = (_a = this.opts) == null ? void 0 : _a.storage) == null ? void 0 : _b.setItem(CONSENT_KEY, this.consent).catch(() => void 0));
717
+ if (granted && this.deferred && !this.erased) {
718
+ this.deferred = this.deferred.then((link) => link != null ? link : this.retryDeferredWithConsent());
719
+ await this.deferred;
720
+ }
650
721
  }
651
722
  /** True once `forgetMe()` has been honoured — this install sends nothing any more. */
652
723
  get forgotten() {
@@ -733,7 +804,7 @@ var require_build = __commonJS({
733
804
  try {
734
805
  const cached = await (store == null ? void 0 : store.getItem(DEFERRED_KEY));
735
806
  if (cached)
736
- return JSON.parse(cached);
807
+ return (0, deeplink_1.upgradeStoredDeepLink)(JSON.parse(cached));
737
808
  } catch {
738
809
  }
739
810
  if (!freshInstall && !await (store == null ? void 0 : store.getItem(`${DEFERRED_KEY}_pending`).catch(() => null)))
@@ -741,18 +812,78 @@ var require_build = __commonJS({
741
812
  const platform = (_b = (_a = this.opts.native) == null ? void 0 : _a.platform) != null ? _b : "android";
742
813
  const referrer = platform === "android" ? await this.readReferrer() : null;
743
814
  await (store == null ? void 0 : store.setItem(`${DEFERRED_KEY}_pending`, "1").catch(() => void 0));
744
- const answer = await this.postJson({ platform, installReferrer: referrer == null ? void 0 : referrer.referrer }, "/deeplink/deferred");
815
+ const answer = await this.askDeferred(platform, referrer == null ? void 0 : referrer.referrer);
745
816
  if (!answer)
746
817
  return null;
747
818
  const link = (0, deeplink_1.toDeepLink)(answer, true);
819
+ const kept = await this.storedDeferred();
820
+ if (kept)
821
+ return kept;
748
822
  try {
749
823
  await (store == null ? void 0 : store.setItem(DEFERRED_KEY, JSON.stringify(link)));
824
+ if (!link && this.consent !== "granted") {
825
+ await (store == null ? void 0 : store.setItem(DEFERRED_RETRY_KEY, String(Date.now() + DEFERRED_RETRY_WINDOW_MS)));
826
+ }
750
827
  } catch {
751
828
  }
752
829
  if (link)
753
830
  this.emitDeepLink(link);
754
831
  return link;
755
832
  }
833
+ /** The deferred ask: the referrer (Android), the consent, and what the tap is compared with. */
834
+ askDeferred(platform, installReferrer) {
835
+ return this.postJson({
836
+ platform,
837
+ installReferrer,
838
+ ...this.consent ? { consent: this.consent } : {},
839
+ ...this.context.osVersion ? { osVersion: this.context.osVersion } : {},
840
+ ...this.context.deviceModel ? { deviceModel: this.context.deviceModel } : {}
841
+ }, "/deeplink/deferred");
842
+ }
843
+ /**
844
+ * The user consented AFTER the first launch asked without consent and found
845
+ * nothing: ask once more, inside the hour, so the probabilistic layer can run.
846
+ * Never when a link is already known, never twice.
847
+ */
848
+ async retryDeferredWithConsent() {
849
+ var _a, _b, _c, _d;
850
+ const store = (_a = this.opts) == null ? void 0 : _a.storage;
851
+ if (!store || this.consent !== "granted" || this.opts.deepLinking === false || this.isWeb)
852
+ return null;
853
+ const until = Number(await store.getItem(DEFERRED_RETRY_KEY).catch(() => null));
854
+ if (!until)
855
+ return null;
856
+ await store.setItem(DEFERRED_RETRY_KEY, "").catch(() => void 0);
857
+ if (Date.now() > until)
858
+ return null;
859
+ const known = await this.storedDeferred();
860
+ if (known)
861
+ return known;
862
+ const link = (0, deeplink_1.toDeepLink)(await this.askDeferred((_c = (_b = this.opts.native) == null ? void 0 : _b.platform) != null ? _c : "ios", void 0), true);
863
+ if (!link)
864
+ return null;
865
+ await ((_d = this.opts.storage) == null ? void 0 : _d.setItem(DEFERRED_KEY, JSON.stringify(link)).catch(() => void 0));
866
+ this.emitDeepLink(link);
867
+ return link;
868
+ }
869
+ /** The deferred link already stored for this install, if any. */
870
+ async storedDeferred() {
871
+ var _a;
872
+ try {
873
+ const raw = await ((_a = this.opts.storage) == null ? void 0 : _a.getItem(DEFERRED_KEY));
874
+ return raw ? (0, deeplink_1.upgradeStoredDeepLink)(JSON.parse(raw)) : null;
875
+ } catch {
876
+ return null;
877
+ }
878
+ }
879
+ /** Remember `link` as this install's deferred link, unless one is already known. */
880
+ async keepDeferred(link) {
881
+ var _a;
882
+ if (await this.storedDeferred())
883
+ return;
884
+ await ((_a = this.opts.storage) == null ? void 0 : _a.setItem(DEFERRED_KEY, JSON.stringify(link)).catch(() => void 0));
885
+ this.deferred = Promise.resolve(link);
886
+ }
756
887
  emitDeepLink(link) {
757
888
  if (!this.deepLinkListeners.size) {
758
889
  this.undelivered.push(link);
@@ -1031,13 +1162,16 @@ var require_build = __commonJS({
1031
1162
  try {
1032
1163
  while (this.queue.length) {
1033
1164
  const batch = this.queue.slice(0, MAX_BATCH);
1034
- const ok = await this.post({
1165
+ const outcome = await this.send({
1035
1166
  events: batch,
1036
1167
  appUserId: this.opts.userId,
1037
1168
  deviceId: this.context.deviceId
1038
1169
  });
1039
- if (!ok)
1170
+ if (outcome === "retry")
1040
1171
  return this.backOff();
1172
+ if (outcome === "refused") {
1173
+ console.warn(`[traqio] the server refused ${batch.length} event(s) as malformed \u2014 dropped, not retried`);
1174
+ }
1041
1175
  this.queue.splice(0, batch.length);
1042
1176
  await this.persistQueue();
1043
1177
  this.retryMs = BASE_RETRY_MS;
@@ -1114,6 +1248,15 @@ var require_build = __commonJS({
1114
1248
  return this.isWeb ? { keepalive: true } : {};
1115
1249
  }
1116
1250
  async post(body, path = "/ingest") {
1251
+ return await this.send(body, path) === "ok";
1252
+ }
1253
+ /**
1254
+ * `post`, with the failure split in two: `refused` is a status the same body will
1255
+ * get again on every retry (400 malformed, 413 too large, 422 unprocessable);
1256
+ * `retry` is everything else — network down, 5xx, 429, and 401/403 too, since a
1257
+ * key fixed in the console makes the same queued batch succeed.
1258
+ */
1259
+ async send(body, path = "/ingest") {
1117
1260
  const consent = this.consent && (path === "/ingest" || path === "/ingest/session") ? { consent: this.consent } : {};
1118
1261
  try {
1119
1262
  const res = await fetch(`${this.opts.endpoint}${path}`, {
@@ -1122,9 +1265,11 @@ var require_build = __commonJS({
1122
1265
  body: JSON.stringify({ sdkKey: this.opts.sdkKey, ...body, ...consent }),
1123
1266
  ...this.keepalive()
1124
1267
  });
1125
- return res.ok;
1268
+ if (res.ok)
1269
+ return "ok";
1270
+ return NON_RETRYABLE_STATUS.has(res.status) ? "refused" : "retry";
1126
1271
  } catch {
1127
- return false;
1272
+ return "retry";
1128
1273
  }
1129
1274
  }
1130
1275
  };