@traqio/web 0.4.0 → 0.4.1

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
@@ -225,6 +225,66 @@ var require_skan = __commonJS({
225
225
  }
226
226
  });
227
227
 
228
+ // ../sdk-core/build/analytics.js
229
+ var require_analytics = __commonJS({
230
+ "../sdk-core/build/analytics.js"(exports) {
231
+ "use strict";
232
+ Object.defineProperty(exports, "__esModule", { value: true });
233
+ exports.MAX_PROPS_BYTES = exports.MAX_SCREEN_NAME = exports.MAX_EVENT_NAME = exports.ANALYTICS_SESSION_IDLE_MS = void 0;
234
+ exports.cleanEventName = cleanEventName;
235
+ exports.cleanScreenName = cleanScreenName;
236
+ exports.cleanProps = cleanProps;
237
+ exports.touchSession = touchSession;
238
+ exports.parseSession = parseSession;
239
+ exports.ANALYTICS_SESSION_IDLE_MS = 30 * 60 * 1e3;
240
+ exports.MAX_EVENT_NAME = 128;
241
+ exports.MAX_SCREEN_NAME = 512;
242
+ exports.MAX_PROPS_BYTES = 4096;
243
+ function cleanEventName(name) {
244
+ if (typeof name !== "string")
245
+ return null;
246
+ const n = name.trim();
247
+ if (!n || n.length > exports.MAX_EVENT_NAME || n.startsWith("$"))
248
+ return null;
249
+ return n;
250
+ }
251
+ function cleanScreenName(name) {
252
+ if (typeof name !== "string")
253
+ return null;
254
+ const n = name.trim();
255
+ return n ? n.slice(0, exports.MAX_SCREEN_NAME) : null;
256
+ }
257
+ function cleanProps(props) {
258
+ if (typeof props !== "object" || props === null || Array.isArray(props))
259
+ return void 0;
260
+ try {
261
+ const json = JSON.stringify(props);
262
+ if (!json || json === "{}" || json.length > exports.MAX_PROPS_BYTES)
263
+ return void 0;
264
+ return JSON.parse(json);
265
+ } catch {
266
+ return void 0;
267
+ }
268
+ }
269
+ function touchSession(current, now, newId2) {
270
+ if (current && typeof current.id === "string" && typeof current.last === "number" && now - current.last <= exports.ANALYTICS_SESSION_IDLE_MS && now >= current.last - 6e4) {
271
+ return { id: current.id, last: Math.max(now, current.last) };
272
+ }
273
+ return { id: newId2(), last: now };
274
+ }
275
+ function parseSession(raw) {
276
+ if (!raw)
277
+ return null;
278
+ try {
279
+ const s = JSON.parse(raw);
280
+ return typeof s.id === "string" && typeof s.last === "number" ? { id: s.id, last: s.last } : null;
281
+ } catch {
282
+ return null;
283
+ }
284
+ }
285
+ }
286
+ });
287
+
228
288
  // ../sdk-core/build/adservices.js
229
289
  var require_adservices = __commonJS({
230
290
  "../sdk-core/build/adservices.js"(exports) {
@@ -627,16 +687,17 @@ var require_build = __commonJS({
627
687
  "../sdk-core/build/index.js"(exports) {
628
688
  "use strict";
629
689
  Object.defineProperty(exports, "__esModule", { value: true });
630
- exports.Traqio = exports.Client = exports.AdRevenueGate = exports.watchReactNativeAds = exports.fromMaxAdInfo = exports.fromGoogleMobileAdsPaid = exports.fromGoogleMobileAdsNativeAd = exports.fromGoogleMobileAdsChannel = exports.fromCapacitorAdMob = exports.MAX_RN_REVENUE_EVENTS = exports.MAX_RN_MIN_VERSION = exports.GMA_MIN_VERSION = exports.GMA_FULLSCREEN_CHANNELS = exports.CAPACITOR_ADMOB_PAID_EVENTS = exports.isTrackingUrl = exports.isOpenUrl = exports.reportAdServicesToken = exports.Skan = exports.SDK_VERSION = void 0;
690
+ exports.Traqio = exports.Client = exports.AdRevenueGate = exports.watchReactNativeAds = exports.fromMaxAdInfo = exports.fromGoogleMobileAdsPaid = exports.fromGoogleMobileAdsNativeAd = exports.fromGoogleMobileAdsChannel = exports.fromCapacitorAdMob = exports.MAX_RN_REVENUE_EVENTS = exports.MAX_RN_MIN_VERSION = exports.GMA_MIN_VERSION = exports.GMA_FULLSCREEN_CHANNELS = exports.CAPACITOR_ADMOB_PAID_EVENTS = exports.isTrackingUrl = exports.isOpenUrl = exports.ANALYTICS_SESSION_IDLE_MS = exports.reportAdServicesToken = exports.Skan = exports.SDK_VERSION = void 0;
631
691
  exports.normalizeAdvertisingId = normalizeAdvertisingId;
632
692
  exports.fromMaxAd = fromMaxAd;
633
693
  exports.detectDebug = detectDebug;
634
694
  exports.isLocalPage = isLocalPage;
635
695
  var skan_1 = require_skan();
696
+ var analytics_1 = require_analytics();
636
697
  var adservices_1 = require_adservices();
637
698
  var deeplink_1 = require_deeplink();
638
699
  var adRevenue_1 = require_adRevenue();
639
- exports.SDK_VERSION = "0.6.0";
700
+ exports.SDK_VERSION = "0.7.0";
640
701
  var skan_2 = require_skan();
641
702
  Object.defineProperty(exports, "Skan", { enumerable: true, get: function() {
642
703
  return skan_2.Skan;
@@ -645,6 +706,10 @@ var require_build = __commonJS({
645
706
  Object.defineProperty(exports, "reportAdServicesToken", { enumerable: true, get: function() {
646
707
  return adservices_2.reportAdServicesToken;
647
708
  } });
709
+ var analytics_2 = require_analytics();
710
+ Object.defineProperty(exports, "ANALYTICS_SESSION_IDLE_MS", { enumerable: true, get: function() {
711
+ return analytics_2.ANALYTICS_SESSION_IDLE_MS;
712
+ } });
648
713
  var deeplink_2 = require_deeplink();
649
714
  Object.defineProperty(exports, "isOpenUrl", { enumerable: true, get: function() {
650
715
  return deeplink_2.isOpenUrl;
@@ -705,6 +770,12 @@ var require_build = __commonJS({
705
770
  var DEFERRED_RETRY_KEY = "traq_deferred_consent_retry_until";
706
771
  var DEFERRED_RETRY_WINDOW_MS = 60 * 6e4;
707
772
  var CONSENT_KEY = "traq_consent";
773
+ var ANALYTICS_QUEUE_KEY = "traq_analytics_queue";
774
+ var ANALYTICS_SESSION_KEY = "traq_analytics_session";
775
+ var ANALYTICS_PATH = "/ingest/analytics";
776
+ var MAX_ANALYTICS_BATCH = 50;
777
+ var MAX_ANALYTICS_QUEUE = 1e3;
778
+ var MAX_EARLY_ANALYTICS = 100;
708
779
  var MAX_BATCH = 20;
709
780
  var NON_RETRYABLE_STATUS = /* @__PURE__ */ new Set([400, 413, 422]);
710
781
  var MAX_QUEUE2 = 500;
@@ -816,6 +887,10 @@ var require_build = __commonJS({
816
887
  this.att = null;
817
888
  this.adIdSync = null;
818
889
  this.debug = false;
890
+ this.analyticsQueue = [];
891
+ this.analyticsSession = null;
892
+ this.earlyAnalytics = [];
893
+ this.currentScreen = null;
819
894
  this.maxRevenuePaid = (adInfo) => {
820
895
  try {
821
896
  const max = (0, adRevenue_1.fromMaxAdInfo)(adInfo);
@@ -843,9 +918,18 @@ var require_build = __commonJS({
843
918
  await this.restoreConsent(opts.consent);
844
919
  await this.restoreQueue();
845
920
  await this.restoreAdQueue();
921
+ await this.restoreAnalytics();
846
922
  await this.buildContext();
847
- if (await this.restoreErased())
923
+ if (await this.restoreErased()) {
924
+ this.earlyAnalytics = [];
848
925
  return;
926
+ }
927
+ if (this.isWeb) {
928
+ this.earlyAnalytics = [];
929
+ } else {
930
+ for (const e of this.earlyAnalytics.splice(0))
931
+ this.pushAnalytics(e, false);
932
+ }
849
933
  if (!this.isWeb) {
850
934
  const freshInstall = Boolean(this.opts.storage) && !await this.opts.storage.getItem(this.installSentKey).catch(() => "1");
851
935
  await this.sendInstallOnce();
@@ -860,7 +944,7 @@ var require_build = __commonJS({
860
944
  await this.initAppleAds();
861
945
  this.wireLifecycle();
862
946
  this.wireAdRevenue();
863
- if (this.queue.length || this.adQueue.length)
947
+ if (this.queue.length || this.adQueue.length || this.analyticsQueue.length)
864
948
  this.scheduleFlush(0);
865
949
  }
866
950
  // `InitOptions.adRevenue`: hand the ad SDKs' own paid events to the trackers.
@@ -902,6 +986,82 @@ var require_build = __commonJS({
902
986
  if (props == null ? void 0 : props.value)
903
987
  this.skan.addRevenue(props.value);
904
988
  }
989
+ /**
990
+ * A product-analytics event — `paywall_viewed`, `deck_built`, `search_performed`…
991
+ * — read in the console's Analytics section (events, funnels, property splits).
992
+ * NOT a conversion: never billed, never sent to an ad network (that is `track()`).
993
+ *
994
+ * Batched, persisted while offline and retried like every other call; it never
995
+ * throws. Names starting with `$` are reserved; properties must survive JSON and
996
+ * stay under 4 KB, else they are dropped (the event is kept). Callable before
997
+ * `init()`: the first 100 calls wait for it.
998
+ *
999
+ * Traqio.capture("lead_captured", { magnet: "starter-deck" });
1000
+ */
1001
+ capture(name, props) {
1002
+ try {
1003
+ const n = (0, analytics_1.cleanEventName)(name);
1004
+ if (!n)
1005
+ return;
1006
+ const p = (0, analytics_1.cleanProps)(props);
1007
+ this.queueAnalytics({ kind: "custom", name: n, ts: Date.now(), ...p ? { props: p } : {} });
1008
+ } catch {
1009
+ }
1010
+ }
1011
+ /**
1012
+ * A screen view — the app's page view, read under « Top pages » and usable as a
1013
+ * funnel step. Call it when a screen gets the focus, or let `trackScreens()` (React
1014
+ * Navigation) or Expo Router's `usePathname()` do it (docs: /docs/sdks/react-native).
1015
+ * The same screen reported twice in a row inside a session counts once (re-renders).
1016
+ * Custom events captured afterwards carry this screen as their page.
1017
+ */
1018
+ screen(name, props) {
1019
+ try {
1020
+ const s = (0, analytics_1.cleanScreenName)(name);
1021
+ if (!s)
1022
+ return;
1023
+ const now = Date.now();
1024
+ const sameVisit = this.analyticsSession !== null && now - this.analyticsSession.last <= analytics_1.ANALYTICS_SESSION_IDLE_MS;
1025
+ if (s === this.currentScreen && sameVisit)
1026
+ return;
1027
+ this.currentScreen = s;
1028
+ const p = (0, analytics_1.cleanProps)(props);
1029
+ this.queueAnalytics({ kind: "pageview", ts: now, screen: s, ...p ? { props: p } : {} });
1030
+ } catch {
1031
+ }
1032
+ }
1033
+ /**
1034
+ * Screen views from a React Navigation container, by themselves:
1035
+ *
1036
+ * const navigationRef = useNavigationContainerRef();
1037
+ * useEffect(() => Traqio.trackScreens(navigationRef), [navigationRef]);
1038
+ *
1039
+ * Returns the unsubscribe. Typed by shape: no navigation library is a dependency.
1040
+ */
1041
+ trackScreens(navigation) {
1042
+ const report = () => {
1043
+ try {
1044
+ const route = navigation.getCurrentRoute();
1045
+ if (route == null ? void 0 : route.name)
1046
+ this.screen(route.name);
1047
+ } catch {
1048
+ }
1049
+ };
1050
+ let off;
1051
+ try {
1052
+ off = navigation.addListener("state", report);
1053
+ } catch {
1054
+ off = void 0;
1055
+ }
1056
+ report();
1057
+ return () => {
1058
+ try {
1059
+ if (typeof off === "function")
1060
+ off();
1061
+ } catch {
1062
+ }
1063
+ };
1064
+ }
905
1065
  /**
906
1066
  * The store this app runs on: the native bridge's word, else React Native's
907
1067
  * `Platform.OS`, else UNKNOWN. Never a guess — until 2026-11 a missing bridge meant
@@ -1370,8 +1530,11 @@ var require_build = __commonJS({
1370
1530
  this.queue = [];
1371
1531
  this.adQueue = [];
1372
1532
  this.adBatchId = null;
1533
+ this.analyticsQueue = [];
1534
+ this.earlyAnalytics = [];
1373
1535
  await this.persistQueue();
1374
1536
  await this.persistAdQueue();
1537
+ await this.persistAnalyticsQueue();
1375
1538
  try {
1376
1539
  await ((_a = this.opts.storage) == null ? void 0 : _a.setItem(ERASED_KEY, "1"));
1377
1540
  } catch {
@@ -1384,6 +1547,55 @@ var require_build = __commonJS({
1384
1547
  void this.persistQueue();
1385
1548
  this.scheduleFlush();
1386
1549
  }
1550
+ /** Before `init()` the call waits in memory; after it, it is stamped and queued. */
1551
+ queueAnalytics(event) {
1552
+ var _a, _b;
1553
+ if (this.erased)
1554
+ return;
1555
+ const screen = (_b = (_a = event.screen) != null ? _a : this.currentScreen) != null ? _b : void 0;
1556
+ const e = screen ? { ...event, screen } : event;
1557
+ if (!this.started) {
1558
+ if (this.earlyAnalytics.length < MAX_EARLY_ANALYTICS)
1559
+ this.earlyAnalytics.push(e);
1560
+ return;
1561
+ }
1562
+ if (this.isWeb)
1563
+ return;
1564
+ this.pushAnalytics(e);
1565
+ }
1566
+ pushAnalytics(e, schedule = true) {
1567
+ this.analyticsSession = (0, analytics_1.touchSession)(this.analyticsSession, e.ts, uuid);
1568
+ this.analyticsQueue.push({ ...e, sessionId: this.analyticsSession.id });
1569
+ if (this.analyticsQueue.length > MAX_ANALYTICS_QUEUE)
1570
+ this.analyticsQueue.splice(0, this.analyticsQueue.length - MAX_ANALYTICS_QUEUE);
1571
+ void this.persistAnalyticsQueue();
1572
+ if (schedule)
1573
+ this.scheduleFlush();
1574
+ }
1575
+ /** The persisted queue first (older), then whatever was captured while it was being read. */
1576
+ async restoreAnalytics() {
1577
+ var _a, _b, _c;
1578
+ try {
1579
+ const raw = await ((_a = this.opts.storage) == null ? void 0 : _a.getItem(ANALYTICS_QUEUE_KEY));
1580
+ const stored = raw ? JSON.parse(raw) : null;
1581
+ if (Array.isArray(stored))
1582
+ this.analyticsQueue = [...stored, ...this.analyticsQueue].slice(-MAX_ANALYTICS_QUEUE);
1583
+ } catch {
1584
+ }
1585
+ try {
1586
+ (_c = this.analyticsSession) != null ? _c : this.analyticsSession = (0, analytics_1.parseSession)(await ((_b = this.opts.storage) == null ? void 0 : _b.getItem(ANALYTICS_SESSION_KEY)));
1587
+ } catch {
1588
+ }
1589
+ }
1590
+ async persistAnalyticsQueue() {
1591
+ var _a, _b;
1592
+ try {
1593
+ await ((_a = this.opts.storage) == null ? void 0 : _a.setItem(ANALYTICS_QUEUE_KEY, JSON.stringify(this.analyticsQueue)));
1594
+ if (this.analyticsSession)
1595
+ await ((_b = this.opts.storage) == null ? void 0 : _b.setItem(ANALYTICS_SESSION_KEY, JSON.stringify(this.analyticsSession)));
1596
+ } catch {
1597
+ }
1598
+ }
1387
1599
  async restoreQueue() {
1388
1600
  var _a;
1389
1601
  try {
@@ -1613,10 +1825,10 @@ var require_build = __commonJS({
1613
1825
  }, delay);
1614
1826
  }
1615
1827
  async drain() {
1616
- var _a;
1828
+ var _a, _b;
1617
1829
  if (this.erased)
1618
1830
  return;
1619
- if (this.flushing || !this.queue.length && !this.adQueue.length)
1831
+ if (this.flushing || !this.queue.length && !this.adQueue.length && !this.analyticsQueue.length)
1620
1832
  return;
1621
1833
  this.flushing = true;
1622
1834
  try {
@@ -1662,6 +1874,22 @@ var require_build = __commonJS({
1662
1874
  await this.persistAdQueue();
1663
1875
  this.retryMs = BASE_RETRY_MS;
1664
1876
  }
1877
+ while (this.analyticsQueue.length) {
1878
+ const batch = this.analyticsQueue.slice(0, MAX_ANALYTICS_BATCH);
1879
+ const outcome = await this.send({
1880
+ anonymousId: (_b = this.context.deviceId) != null ? _b : await this.getOrCreateDeviceId(),
1881
+ userId: this.opts.userId,
1882
+ platform: this.hostPlatform,
1883
+ events: batch
1884
+ }, ANALYTICS_PATH);
1885
+ if (outcome === "retry")
1886
+ return this.backOff();
1887
+ if (outcome === "refused")
1888
+ console.warn(`[traqio] the server refused ${batch.length} analytics event(s) as malformed \u2014 dropped, not retried`);
1889
+ this.analyticsQueue.splice(0, batch.length);
1890
+ await this.persistAnalyticsQueue();
1891
+ this.retryMs = BASE_RETRY_MS;
1892
+ }
1665
1893
  } finally {
1666
1894
  this.flushing = false;
1667
1895
  }