@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.js CHANGED
@@ -636,7 +636,7 @@ var require_build = __commonJS({
636
636
  var adservices_1 = require_adservices();
637
637
  var deeplink_1 = require_deeplink();
638
638
  var adRevenue_1 = require_adRevenue();
639
- exports.SDK_VERSION = "0.5.0";
639
+ exports.SDK_VERSION = "0.6.0";
640
640
  var skan_2 = require_skan();
641
641
  Object.defineProperty(exports, "Skan", { enumerable: true, get: function() {
642
642
  return skan_2.Skan;
@@ -692,6 +692,9 @@ var require_build = __commonJS({
692
692
  var INSTALL_SENT_KEY = "traq_install_sent";
693
693
  var INSTALL_SENT_DEBUG_KEY = "traq_install_sent_debug";
694
694
  var INSTALL_ATTEMPTS_KEY = "traq_install_attempts";
695
+ var AD_ID_SENT_KEY = "traq_ad_id_sent";
696
+ var AD_ID_SENT_DEBUG_KEY = "traq_ad_id_sent_debug";
697
+ var AD_ID_PATH = "/ingest/advertising-id";
695
698
  var REFERRER_KEY = "traq_referrer";
696
699
  var QUEUE_KEY = "traq_queue";
697
700
  var AD_QUEUE_KEY = "traq_ad_queue";
@@ -712,6 +715,7 @@ var require_build = __commonJS({
712
715
  var FLUSH_DELAY_MS = 3e3;
713
716
  var MAX_INSTALL_ATTEMPTS = 3;
714
717
  var SESSION_TIMEOUT_MS = 30 * 60 * 1e3;
718
+ var TRACKING_AUTHORIZATIONS = ["not_determined", "restricted", "denied", "authorized"];
715
719
  var MAX_PRECISION = {
716
720
  exact: "precise",
717
721
  publisher_defined: "publisher_provided",
@@ -782,6 +786,15 @@ var require_build = __commonJS({
782
786
  }
783
787
  return void 0;
784
788
  }
789
+ function reactNativeOs() {
790
+ var _a, _b;
791
+ try {
792
+ const os = (_b = (_a = (init_empty(), __toCommonJS(empty_exports))) == null ? void 0 : _a.Platform) == null ? void 0 : _b.OS;
793
+ return os === "ios" || os === "android" ? os : void 0;
794
+ } catch {
795
+ return void 0;
796
+ }
797
+ }
785
798
  var Client2 = class {
786
799
  constructor() {
787
800
  this.queue = [];
@@ -800,6 +813,8 @@ var require_build = __commonJS({
800
813
  this.undelivered = [];
801
814
  this.deferred = null;
802
815
  this.consent = null;
816
+ this.att = null;
817
+ this.adIdSync = null;
803
818
  this.debug = false;
804
819
  this.maxRevenuePaid = (adInfo) => {
805
820
  try {
@@ -834,6 +849,7 @@ var require_build = __commonJS({
834
849
  if (!this.isWeb) {
835
850
  const freshInstall = Boolean(this.opts.storage) && !await this.opts.storage.getItem(this.installSentKey).catch(() => "1");
836
851
  await this.sendInstallOnce();
852
+ void this.syncAdvertisingId();
837
853
  if (this.opts.deepLinking !== false) {
838
854
  this.deferred = this.resolveDeferredOnce(freshInstall).then((link) => link != null ? link : this.retryDeferredWithConsent());
839
855
  this.wireDeepLinks();
@@ -886,9 +902,18 @@ var require_build = __commonJS({
886
902
  if (props == null ? void 0 : props.value)
887
903
  this.skan.addRevenue(props.value);
888
904
  }
905
+ /**
906
+ * The store this app runs on: the native bridge's word, else React Native's
907
+ * `Platform.OS`, else UNKNOWN. Never a guess — until 2026-11 a missing bridge meant
908
+ * "android", and every bridgeless iPhone was filed as an Android install whose
909
+ * purchases then went to Meta with the Android package (todo cmwl6okn).
910
+ */
911
+ get hostPlatform() {
912
+ var _a, _b;
913
+ return (_b = (_a = this.opts.native) == null ? void 0 : _a.platform) != null ? _b : reactNativeOs();
914
+ }
889
915
  defaultStore() {
890
- var _a;
891
- const platform = (_a = this.opts.native) == null ? void 0 : _a.platform;
916
+ const platform = this.hostPlatform;
892
917
  return platform === "ios" ? "app_store" : platform === "android" ? "play_store" : void 0;
893
918
  }
894
919
  /**
@@ -1127,6 +1152,7 @@ var require_build = __commonJS({
1127
1152
  if (!this.started || this.erased)
1128
1153
  return;
1129
1154
  void this.maybeStartSession();
1155
+ void this.syncAdvertisingId();
1130
1156
  }
1131
1157
  /** The conversion-value mapping in force on this device, once fetched. */
1132
1158
  get skanMapping() {
@@ -1178,7 +1204,6 @@ var require_build = __commonJS({
1178
1204
  * (still counted as fresh, since the flag is only written on an answer).
1179
1205
  */
1180
1206
  async resolveDeferredOnce(freshInstall) {
1181
- var _a, _b;
1182
1207
  const store = this.opts.storage;
1183
1208
  try {
1184
1209
  const cached = await (store == null ? void 0 : store.getItem(DEFERRED_KEY));
@@ -1188,7 +1213,7 @@ var require_build = __commonJS({
1188
1213
  }
1189
1214
  if (!freshInstall && !await (store == null ? void 0 : store.getItem(`${DEFERRED_KEY}_pending`).catch(() => null)))
1190
1215
  return null;
1191
- const platform = (_b = (_a = this.opts.native) == null ? void 0 : _a.platform) != null ? _b : "android";
1216
+ const platform = this.hostPlatform;
1192
1217
  const referrer = platform === "android" ? await this.readReferrer() : null;
1193
1218
  await (store == null ? void 0 : store.setItem(`${DEFERRED_KEY}_pending`, "1").catch(() => void 0));
1194
1219
  const answer = await this.askDeferred(platform, referrer == null ? void 0 : referrer.referrer);
@@ -1225,7 +1250,7 @@ var require_build = __commonJS({
1225
1250
  * Never when a link is already known, never twice.
1226
1251
  */
1227
1252
  async retryDeferredWithConsent() {
1228
- var _a, _b, _c, _d;
1253
+ var _a, _b;
1229
1254
  const store = (_a = this.opts) == null ? void 0 : _a.storage;
1230
1255
  if (!store || this.consent !== "granted" || this.opts.deepLinking === false || this.isWeb)
1231
1256
  return null;
@@ -1238,10 +1263,10 @@ var require_build = __commonJS({
1238
1263
  const known = await this.storedDeferred();
1239
1264
  if (known)
1240
1265
  return known;
1241
- 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);
1266
+ const link = (0, deeplink_1.toDeepLink)(await this.askDeferred(this.hostPlatform, void 0), true);
1242
1267
  if (!link)
1243
1268
  return null;
1244
- await ((_d = this.opts.storage) == null ? void 0 : _d.setItem(DEFERRED_KEY, JSON.stringify(link)).catch(() => void 0));
1269
+ await ((_b = this.opts.storage) == null ? void 0 : _b.setItem(DEFERRED_KEY, JSON.stringify(link)).catch(() => void 0));
1245
1270
  this.emitDeepLink(link);
1246
1271
  return link;
1247
1272
  }
@@ -1292,6 +1317,9 @@ var require_build = __commonJS({
1292
1317
  get installSentKey() {
1293
1318
  return this.debug ? INSTALL_SENT_DEBUG_KEY : INSTALL_SENT_KEY;
1294
1319
  }
1320
+ get adIdSentKey() {
1321
+ return this.debug ? AD_ID_SENT_DEBUG_KEY : AD_ID_SENT_KEY;
1322
+ }
1295
1323
  /** Spread into every body: present only in debug mode, so a release build's payload is unchanged. */
1296
1324
  debugField() {
1297
1325
  return this.debug ? { debug: true } : {};
@@ -1315,6 +1343,7 @@ var require_build = __commonJS({
1315
1343
  return;
1316
1344
  }
1317
1345
  void this.maybeStartSession();
1346
+ void this.syncAdvertisingId();
1318
1347
  });
1319
1348
  } catch {
1320
1349
  }
@@ -1505,11 +1534,17 @@ var require_build = __commonJS({
1505
1534
  }
1506
1535
  }
1507
1536
  async sendInstallOnce() {
1508
- var _a, _b;
1509
1537
  const store = this.opts.storage;
1510
1538
  if (store && await store.getItem(this.installSentKey))
1511
1539
  return;
1512
- const platform = (_b = (_a = this.opts.native) == null ? void 0 : _a.platform) != null ? _b : "android";
1540
+ const platform = this.hostPlatform;
1541
+ if (!platform) {
1542
+ 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().");
1543
+ return;
1544
+ }
1545
+ if (!this.opts.native) {
1546
+ 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.`);
1547
+ }
1513
1548
  const referrer = await this.readReferrer();
1514
1549
  if (referrer.retryable && store) {
1515
1550
  const attempts = Number(await store.getItem(INSTALL_ATTEMPTS_KEY)) || 0;
@@ -1518,17 +1553,56 @@ var require_build = __commonJS({
1518
1553
  return;
1519
1554
  }
1520
1555
  }
1556
+ const advertisingId = await this.readAdvertisingId();
1521
1557
  const ok = await this.post({
1522
1558
  install: {
1523
1559
  platform,
1524
1560
  installReferrer: referrer.referrer,
1525
1561
  appUserId: this.opts.userId,
1526
- advertisingId: await this.readAdvertisingId(),
1562
+ advertisingId,
1527
1563
  context: this.context
1528
1564
  }
1529
1565
  });
1530
- if (ok)
1566
+ if (ok) {
1531
1567
  await (store == null ? void 0 : store.setItem(this.installSentKey, "1"));
1568
+ if (advertisingId)
1569
+ await (store == null ? void 0 : store.setItem(this.adIdSentKey, advertisingId).catch(() => void 0));
1570
+ }
1571
+ }
1572
+ /**
1573
+ * The opted-in advertising id the install could not carry (todo cmwlauev). The
1574
+ * install leaves at first launch, before an app shows its own ATT prompt, so on iOS
1575
+ * it almost never has the IDFA. Re-read at every start and foreground — reading
1576
+ * never prompts (decision cmvjohk6) — and posted ONCE when it differs from the one
1577
+ * the server last acknowledged; a failure is retried at the next foreground.
1578
+ *
1579
+ * Only after the install is reported: the install itself carries an id it can read.
1580
+ */
1581
+ syncAdvertisingId() {
1582
+ if (this.adIdSync)
1583
+ return this.adIdSync;
1584
+ this.adIdSync = this.postAdvertisingIdIfChanged().finally(() => {
1585
+ this.adIdSync = null;
1586
+ });
1587
+ return this.adIdSync;
1588
+ }
1589
+ async postAdvertisingIdIfChanged() {
1590
+ var _a;
1591
+ const store = this.opts.storage;
1592
+ if (this.isWeb || this.erased || !store || !this.opts.collectAdvertisingId)
1593
+ return;
1594
+ try {
1595
+ if (!await store.getItem(this.installSentKey))
1596
+ return;
1597
+ const advertisingId = await this.readAdvertisingId();
1598
+ if (!advertisingId || await store.getItem(this.adIdSentKey) === advertisingId)
1599
+ return;
1600
+ const deviceId = (_a = this.context.deviceId) != null ? _a : await this.getOrCreateDeviceId();
1601
+ if (await this.post({ deviceId, appUserId: this.opts.userId, advertisingId }, AD_ID_PATH)) {
1602
+ await store.setItem(this.adIdSentKey, advertisingId);
1603
+ }
1604
+ } catch {
1605
+ }
1532
1606
  }
1533
1607
  scheduleFlush(delay = FLUSH_DELAY_MS) {
1534
1608
  if (this.flushTimer)
@@ -1633,6 +1707,21 @@ var require_build = __commonJS({
1633
1707
  keepalive() {
1634
1708
  return this.isWeb ? { keepalive: true } : {};
1635
1709
  }
1710
+ get asksTrackingAuthorization() {
1711
+ var _a;
1712
+ return ((_a = this.opts.native) == null ? void 0 : _a.platform) === "ios" && typeof this.opts.native.getTrackingAuthorization === "function";
1713
+ }
1714
+ /** iOS only; the last good answer when the bridge cannot give one now. */
1715
+ async readTrackingAuthorization() {
1716
+ var _a, _b;
1717
+ try {
1718
+ const status = await ((_b = (_a = this.opts.native) == null ? void 0 : _a.getTrackingAuthorization) == null ? void 0 : _b.call(_a));
1719
+ if (TRACKING_AUTHORIZATIONS.includes(status))
1720
+ this.att = status != null ? status : null;
1721
+ } catch {
1722
+ }
1723
+ return this.att;
1724
+ }
1636
1725
  async post(body, path = "/ingest") {
1637
1726
  return await this.send(body, path) === "ok";
1638
1727
  }
@@ -1643,12 +1732,19 @@ var require_build = __commonJS({
1643
1732
  * key fixed in the console makes the same queued batch succeed.
1644
1733
  */
1645
1734
  async send(body, path = "/ingest") {
1646
- const consent = this.consent && (path === "/ingest" || path === "/ingest/session") ? { consent: this.consent } : {};
1735
+ const consent = this.consent && (path === "/ingest" || path === "/ingest/session" || path === AD_ID_PATH) ? { consent: this.consent } : {};
1736
+ const att = (path === "/ingest" || path === AD_ID_PATH) && this.asksTrackingAuthorization ? await this.readTrackingAuthorization() : null;
1647
1737
  try {
1648
1738
  const res = await fetch(`${this.opts.endpoint}${path}`, {
1649
1739
  method: "POST",
1650
1740
  headers: { "Content-Type": "application/json" },
1651
- body: JSON.stringify({ sdkKey: this.opts.sdkKey, ...body, ...consent, ...this.debugField() }),
1741
+ body: JSON.stringify({
1742
+ sdkKey: this.opts.sdkKey,
1743
+ ...body,
1744
+ ...consent,
1745
+ ...att ? { att } : {},
1746
+ ...this.debugField()
1747
+ }),
1652
1748
  ...this.keepalive()
1653
1749
  });
1654
1750
  if (res.ok)