@traqio/web 0.3.0 → 0.3.2

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
@@ -641,7 +641,7 @@ var require_build = __commonJS({
641
641
  var adservices_1 = require_adservices();
642
642
  var deeplink_1 = require_deeplink();
643
643
  var adRevenue_1 = require_adRevenue();
644
- exports.SDK_VERSION = "0.5.0";
644
+ exports.SDK_VERSION = "0.6.0";
645
645
  var skan_2 = require_skan();
646
646
  Object.defineProperty(exports, "Skan", { enumerable: true, get: function() {
647
647
  return skan_2.Skan;
@@ -697,6 +697,9 @@ var require_build = __commonJS({
697
697
  var INSTALL_SENT_KEY = "traq_install_sent";
698
698
  var INSTALL_SENT_DEBUG_KEY = "traq_install_sent_debug";
699
699
  var INSTALL_ATTEMPTS_KEY = "traq_install_attempts";
700
+ var AD_ID_SENT_KEY = "traq_ad_id_sent";
701
+ var AD_ID_SENT_DEBUG_KEY = "traq_ad_id_sent_debug";
702
+ var AD_ID_PATH = "/ingest/advertising-id";
700
703
  var REFERRER_KEY = "traq_referrer";
701
704
  var QUEUE_KEY = "traq_queue";
702
705
  var AD_QUEUE_KEY = "traq_ad_queue";
@@ -717,6 +720,7 @@ var require_build = __commonJS({
717
720
  var FLUSH_DELAY_MS = 3e3;
718
721
  var MAX_INSTALL_ATTEMPTS = 3;
719
722
  var SESSION_TIMEOUT_MS = 30 * 60 * 1e3;
723
+ var TRACKING_AUTHORIZATIONS = ["not_determined", "restricted", "denied", "authorized"];
720
724
  var MAX_PRECISION = {
721
725
  exact: "precise",
722
726
  publisher_defined: "publisher_provided",
@@ -787,6 +791,15 @@ var require_build = __commonJS({
787
791
  }
788
792
  return void 0;
789
793
  }
794
+ function reactNativeOs() {
795
+ var _a, _b;
796
+ try {
797
+ const os = (_b = (_a = (init_empty(), __toCommonJS(empty_exports))) == null ? void 0 : _a.Platform) == null ? void 0 : _b.OS;
798
+ return os === "ios" || os === "android" ? os : void 0;
799
+ } catch {
800
+ return void 0;
801
+ }
802
+ }
790
803
  var Client2 = class {
791
804
  constructor() {
792
805
  this.queue = [];
@@ -805,6 +818,8 @@ var require_build = __commonJS({
805
818
  this.undelivered = [];
806
819
  this.deferred = null;
807
820
  this.consent = null;
821
+ this.att = null;
822
+ this.adIdSync = null;
808
823
  this.debug = false;
809
824
  this.maxRevenuePaid = (adInfo) => {
810
825
  try {
@@ -839,6 +854,7 @@ var require_build = __commonJS({
839
854
  if (!this.isWeb) {
840
855
  const freshInstall = Boolean(this.opts.storage) && !await this.opts.storage.getItem(this.installSentKey).catch(() => "1");
841
856
  await this.sendInstallOnce();
857
+ void this.syncAdvertisingId();
842
858
  if (this.opts.deepLinking !== false) {
843
859
  this.deferred = this.resolveDeferredOnce(freshInstall).then((link) => link != null ? link : this.retryDeferredWithConsent());
844
860
  this.wireDeepLinks();
@@ -891,9 +907,18 @@ var require_build = __commonJS({
891
907
  if (props == null ? void 0 : props.value)
892
908
  this.skan.addRevenue(props.value);
893
909
  }
910
+ /**
911
+ * The store this app runs on: the native bridge's word, else React Native's
912
+ * `Platform.OS`, else UNKNOWN. Never a guess — until 2026-11 a missing bridge meant
913
+ * "android", and every bridgeless iPhone was filed as an Android install whose
914
+ * purchases then went to Meta with the Android package (todo cmwl6okn).
915
+ */
916
+ get hostPlatform() {
917
+ var _a, _b;
918
+ return (_b = (_a = this.opts.native) == null ? void 0 : _a.platform) != null ? _b : reactNativeOs();
919
+ }
894
920
  defaultStore() {
895
- var _a;
896
- const platform = (_a = this.opts.native) == null ? void 0 : _a.platform;
921
+ const platform = this.hostPlatform;
897
922
  return platform === "ios" ? "app_store" : platform === "android" ? "play_store" : void 0;
898
923
  }
899
924
  /**
@@ -1132,6 +1157,7 @@ var require_build = __commonJS({
1132
1157
  if (!this.started || this.erased)
1133
1158
  return;
1134
1159
  void this.maybeStartSession();
1160
+ void this.syncAdvertisingId();
1135
1161
  }
1136
1162
  /** The conversion-value mapping in force on this device, once fetched. */
1137
1163
  get skanMapping() {
@@ -1183,7 +1209,6 @@ var require_build = __commonJS({
1183
1209
  * (still counted as fresh, since the flag is only written on an answer).
1184
1210
  */
1185
1211
  async resolveDeferredOnce(freshInstall) {
1186
- var _a, _b;
1187
1212
  const store = this.opts.storage;
1188
1213
  try {
1189
1214
  const cached = await (store == null ? void 0 : store.getItem(DEFERRED_KEY));
@@ -1193,7 +1218,7 @@ var require_build = __commonJS({
1193
1218
  }
1194
1219
  if (!freshInstall && !await (store == null ? void 0 : store.getItem(`${DEFERRED_KEY}_pending`).catch(() => null)))
1195
1220
  return null;
1196
- const platform = (_b = (_a = this.opts.native) == null ? void 0 : _a.platform) != null ? _b : "android";
1221
+ const platform = this.hostPlatform;
1197
1222
  const referrer = platform === "android" ? await this.readReferrer() : null;
1198
1223
  await (store == null ? void 0 : store.setItem(`${DEFERRED_KEY}_pending`, "1").catch(() => void 0));
1199
1224
  const answer = await this.askDeferred(platform, referrer == null ? void 0 : referrer.referrer);
@@ -1230,7 +1255,7 @@ var require_build = __commonJS({
1230
1255
  * Never when a link is already known, never twice.
1231
1256
  */
1232
1257
  async retryDeferredWithConsent() {
1233
- var _a, _b, _c, _d;
1258
+ var _a, _b;
1234
1259
  const store = (_a = this.opts) == null ? void 0 : _a.storage;
1235
1260
  if (!store || this.consent !== "granted" || this.opts.deepLinking === false || this.isWeb)
1236
1261
  return null;
@@ -1243,10 +1268,10 @@ var require_build = __commonJS({
1243
1268
  const known = await this.storedDeferred();
1244
1269
  if (known)
1245
1270
  return known;
1246
- 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);
1271
+ const link = (0, deeplink_1.toDeepLink)(await this.askDeferred(this.hostPlatform, void 0), true);
1247
1272
  if (!link)
1248
1273
  return null;
1249
- await ((_d = this.opts.storage) == null ? void 0 : _d.setItem(DEFERRED_KEY, JSON.stringify(link)).catch(() => void 0));
1274
+ await ((_b = this.opts.storage) == null ? void 0 : _b.setItem(DEFERRED_KEY, JSON.stringify(link)).catch(() => void 0));
1250
1275
  this.emitDeepLink(link);
1251
1276
  return link;
1252
1277
  }
@@ -1297,6 +1322,9 @@ var require_build = __commonJS({
1297
1322
  get installSentKey() {
1298
1323
  return this.debug ? INSTALL_SENT_DEBUG_KEY : INSTALL_SENT_KEY;
1299
1324
  }
1325
+ get adIdSentKey() {
1326
+ return this.debug ? AD_ID_SENT_DEBUG_KEY : AD_ID_SENT_KEY;
1327
+ }
1300
1328
  /** Spread into every body: present only in debug mode, so a release build's payload is unchanged. */
1301
1329
  debugField() {
1302
1330
  return this.debug ? { debug: true } : {};
@@ -1320,6 +1348,7 @@ var require_build = __commonJS({
1320
1348
  return;
1321
1349
  }
1322
1350
  void this.maybeStartSession();
1351
+ void this.syncAdvertisingId();
1323
1352
  });
1324
1353
  } catch {
1325
1354
  }
@@ -1510,11 +1539,17 @@ var require_build = __commonJS({
1510
1539
  }
1511
1540
  }
1512
1541
  async sendInstallOnce() {
1513
- var _a, _b;
1514
1542
  const store = this.opts.storage;
1515
1543
  if (store && await store.getItem(this.installSentKey))
1516
1544
  return;
1517
- const platform = (_b = (_a = this.opts.native) == null ? void 0 : _a.platform) != null ? _b : "android";
1545
+ const platform = this.hostPlatform;
1546
+ if (!platform) {
1547
+ console.warn("[traqio] unknown platform \u2014 no native bridge and not React Native: the install is not reported. Pass `native: getNativeBridge()` (React Native / Expo) or your SDK's bridge to init().");
1548
+ return;
1549
+ }
1550
+ if (!this.opts.native) {
1551
+ console.warn(`[traqio] the Traqio native module is missing on ${platform} (Expo Go, or not linked in this build): install reported without its attribution signals. Use a development / EAS build.`);
1552
+ }
1518
1553
  const referrer = await this.readReferrer();
1519
1554
  if (referrer.retryable && store) {
1520
1555
  const attempts = Number(await store.getItem(INSTALL_ATTEMPTS_KEY)) || 0;
@@ -1523,17 +1558,56 @@ var require_build = __commonJS({
1523
1558
  return;
1524
1559
  }
1525
1560
  }
1561
+ const advertisingId = await this.readAdvertisingId();
1526
1562
  const ok = await this.post({
1527
1563
  install: {
1528
1564
  platform,
1529
1565
  installReferrer: referrer.referrer,
1530
1566
  appUserId: this.opts.userId,
1531
- advertisingId: await this.readAdvertisingId(),
1567
+ advertisingId,
1532
1568
  context: this.context
1533
1569
  }
1534
1570
  });
1535
- if (ok)
1571
+ if (ok) {
1536
1572
  await (store == null ? void 0 : store.setItem(this.installSentKey, "1"));
1573
+ if (advertisingId)
1574
+ await (store == null ? void 0 : store.setItem(this.adIdSentKey, advertisingId).catch(() => void 0));
1575
+ }
1576
+ }
1577
+ /**
1578
+ * The opted-in advertising id the install could not carry (todo cmwlauev). The
1579
+ * install leaves at first launch, before an app shows its own ATT prompt, so on iOS
1580
+ * it almost never has the IDFA. Re-read at every start and foreground — reading
1581
+ * never prompts (decision cmvjohk6) — and posted ONCE when it differs from the one
1582
+ * the server last acknowledged; a failure is retried at the next foreground.
1583
+ *
1584
+ * Only after the install is reported: the install itself carries an id it can read.
1585
+ */
1586
+ syncAdvertisingId() {
1587
+ if (this.adIdSync)
1588
+ return this.adIdSync;
1589
+ this.adIdSync = this.postAdvertisingIdIfChanged().finally(() => {
1590
+ this.adIdSync = null;
1591
+ });
1592
+ return this.adIdSync;
1593
+ }
1594
+ async postAdvertisingIdIfChanged() {
1595
+ var _a;
1596
+ const store = this.opts.storage;
1597
+ if (this.isWeb || this.erased || !store || !this.opts.collectAdvertisingId)
1598
+ return;
1599
+ try {
1600
+ if (!await store.getItem(this.installSentKey))
1601
+ return;
1602
+ const advertisingId = await this.readAdvertisingId();
1603
+ if (!advertisingId || await store.getItem(this.adIdSentKey) === advertisingId)
1604
+ return;
1605
+ const deviceId = (_a = this.context.deviceId) != null ? _a : await this.getOrCreateDeviceId();
1606
+ if (await this.post({ deviceId, appUserId: this.opts.userId, advertisingId }, AD_ID_PATH)) {
1607
+ await store.setItem(this.adIdSentKey, advertisingId);
1608
+ }
1609
+ } catch {
1610
+ }
1537
1611
  }
1538
1612
  scheduleFlush(delay = FLUSH_DELAY_MS) {
1539
1613
  if (this.flushTimer)
@@ -1638,6 +1712,21 @@ var require_build = __commonJS({
1638
1712
  keepalive() {
1639
1713
  return this.isWeb ? { keepalive: true } : {};
1640
1714
  }
1715
+ get asksTrackingAuthorization() {
1716
+ var _a;
1717
+ return ((_a = this.opts.native) == null ? void 0 : _a.platform) === "ios" && typeof this.opts.native.getTrackingAuthorization === "function";
1718
+ }
1719
+ /** iOS only; the last good answer when the bridge cannot give one now. */
1720
+ async readTrackingAuthorization() {
1721
+ var _a, _b;
1722
+ try {
1723
+ const status = await ((_b = (_a = this.opts.native) == null ? void 0 : _a.getTrackingAuthorization) == null ? void 0 : _b.call(_a));
1724
+ if (TRACKING_AUTHORIZATIONS.includes(status))
1725
+ this.att = status != null ? status : null;
1726
+ } catch {
1727
+ }
1728
+ return this.att;
1729
+ }
1641
1730
  async post(body, path = "/ingest") {
1642
1731
  return await this.send(body, path) === "ok";
1643
1732
  }
@@ -1648,12 +1737,19 @@ var require_build = __commonJS({
1648
1737
  * key fixed in the console makes the same queued batch succeed.
1649
1738
  */
1650
1739
  async send(body, path = "/ingest") {
1651
- const consent = this.consent && (path === "/ingest" || path === "/ingest/session") ? { consent: this.consent } : {};
1740
+ const consent = this.consent && (path === "/ingest" || path === "/ingest/session" || path === AD_ID_PATH) ? { consent: this.consent } : {};
1741
+ const att = (path === "/ingest" || path === AD_ID_PATH) && this.asksTrackingAuthorization ? await this.readTrackingAuthorization() : null;
1652
1742
  try {
1653
1743
  const res = await fetch(`${this.opts.endpoint}${path}`, {
1654
1744
  method: "POST",
1655
1745
  headers: { "Content-Type": "application/json" },
1656
- body: JSON.stringify({ sdkKey: this.opts.sdkKey, ...body, ...consent, ...this.debugField() }),
1746
+ body: JSON.stringify({
1747
+ sdkKey: this.opts.sdkKey,
1748
+ ...body,
1749
+ ...consent,
1750
+ ...att ? { att } : {},
1751
+ ...this.debugField()
1752
+ }),
1657
1753
  ...this.keepalive()
1658
1754
  });
1659
1755
  if (res.ok)