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