@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.cjs CHANGED
@@ -230,6 +230,66 @@ var require_skan = __commonJS({
230
230
  }
231
231
  });
232
232
 
233
+ // ../sdk-core/build/analytics.js
234
+ var require_analytics = __commonJS({
235
+ "../sdk-core/build/analytics.js"(exports) {
236
+ "use strict";
237
+ Object.defineProperty(exports, "__esModule", { value: true });
238
+ exports.MAX_PROPS_BYTES = exports.MAX_SCREEN_NAME = exports.MAX_EVENT_NAME = exports.ANALYTICS_SESSION_IDLE_MS = void 0;
239
+ exports.cleanEventName = cleanEventName;
240
+ exports.cleanScreenName = cleanScreenName;
241
+ exports.cleanProps = cleanProps;
242
+ exports.touchSession = touchSession;
243
+ exports.parseSession = parseSession;
244
+ exports.ANALYTICS_SESSION_IDLE_MS = 30 * 60 * 1e3;
245
+ exports.MAX_EVENT_NAME = 128;
246
+ exports.MAX_SCREEN_NAME = 512;
247
+ exports.MAX_PROPS_BYTES = 4096;
248
+ function cleanEventName(name) {
249
+ if (typeof name !== "string")
250
+ return null;
251
+ const n = name.trim();
252
+ if (!n || n.length > exports.MAX_EVENT_NAME || n.startsWith("$"))
253
+ return null;
254
+ return n;
255
+ }
256
+ function cleanScreenName(name) {
257
+ if (typeof name !== "string")
258
+ return null;
259
+ const n = name.trim();
260
+ return n ? n.slice(0, exports.MAX_SCREEN_NAME) : null;
261
+ }
262
+ function cleanProps(props) {
263
+ if (typeof props !== "object" || props === null || Array.isArray(props))
264
+ return void 0;
265
+ try {
266
+ const json = JSON.stringify(props);
267
+ if (!json || json === "{}" || json.length > exports.MAX_PROPS_BYTES)
268
+ return void 0;
269
+ return JSON.parse(json);
270
+ } catch {
271
+ return void 0;
272
+ }
273
+ }
274
+ function touchSession(current, now, newId2) {
275
+ if (current && typeof current.id === "string" && typeof current.last === "number" && now - current.last <= exports.ANALYTICS_SESSION_IDLE_MS && now >= current.last - 6e4) {
276
+ return { id: current.id, last: Math.max(now, current.last) };
277
+ }
278
+ return { id: newId2(), last: now };
279
+ }
280
+ function parseSession(raw) {
281
+ if (!raw)
282
+ return null;
283
+ try {
284
+ const s = JSON.parse(raw);
285
+ return typeof s.id === "string" && typeof s.last === "number" ? { id: s.id, last: s.last } : null;
286
+ } catch {
287
+ return null;
288
+ }
289
+ }
290
+ }
291
+ });
292
+
233
293
  // ../sdk-core/build/adservices.js
234
294
  var require_adservices = __commonJS({
235
295
  "../sdk-core/build/adservices.js"(exports) {
@@ -632,16 +692,17 @@ var require_build = __commonJS({
632
692
  "../sdk-core/build/index.js"(exports) {
633
693
  "use strict";
634
694
  Object.defineProperty(exports, "__esModule", { value: true });
635
- 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;
695
+ 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;
636
696
  exports.normalizeAdvertisingId = normalizeAdvertisingId;
637
697
  exports.fromMaxAd = fromMaxAd;
638
698
  exports.detectDebug = detectDebug;
639
699
  exports.isLocalPage = isLocalPage;
640
700
  var skan_1 = require_skan();
701
+ var analytics_1 = require_analytics();
641
702
  var adservices_1 = require_adservices();
642
703
  var deeplink_1 = require_deeplink();
643
704
  var adRevenue_1 = require_adRevenue();
644
- exports.SDK_VERSION = "0.6.0";
705
+ exports.SDK_VERSION = "0.7.0";
645
706
  var skan_2 = require_skan();
646
707
  Object.defineProperty(exports, "Skan", { enumerable: true, get: function() {
647
708
  return skan_2.Skan;
@@ -650,6 +711,10 @@ var require_build = __commonJS({
650
711
  Object.defineProperty(exports, "reportAdServicesToken", { enumerable: true, get: function() {
651
712
  return adservices_2.reportAdServicesToken;
652
713
  } });
714
+ var analytics_2 = require_analytics();
715
+ Object.defineProperty(exports, "ANALYTICS_SESSION_IDLE_MS", { enumerable: true, get: function() {
716
+ return analytics_2.ANALYTICS_SESSION_IDLE_MS;
717
+ } });
653
718
  var deeplink_2 = require_deeplink();
654
719
  Object.defineProperty(exports, "isOpenUrl", { enumerable: true, get: function() {
655
720
  return deeplink_2.isOpenUrl;
@@ -710,6 +775,12 @@ var require_build = __commonJS({
710
775
  var DEFERRED_RETRY_KEY = "traq_deferred_consent_retry_until";
711
776
  var DEFERRED_RETRY_WINDOW_MS = 60 * 6e4;
712
777
  var CONSENT_KEY = "traq_consent";
778
+ var ANALYTICS_QUEUE_KEY = "traq_analytics_queue";
779
+ var ANALYTICS_SESSION_KEY = "traq_analytics_session";
780
+ var ANALYTICS_PATH = "/ingest/analytics";
781
+ var MAX_ANALYTICS_BATCH = 50;
782
+ var MAX_ANALYTICS_QUEUE = 1e3;
783
+ var MAX_EARLY_ANALYTICS = 100;
713
784
  var MAX_BATCH = 20;
714
785
  var NON_RETRYABLE_STATUS = /* @__PURE__ */ new Set([400, 413, 422]);
715
786
  var MAX_QUEUE2 = 500;
@@ -821,6 +892,10 @@ var require_build = __commonJS({
821
892
  this.att = null;
822
893
  this.adIdSync = null;
823
894
  this.debug = false;
895
+ this.analyticsQueue = [];
896
+ this.analyticsSession = null;
897
+ this.earlyAnalytics = [];
898
+ this.currentScreen = null;
824
899
  this.maxRevenuePaid = (adInfo) => {
825
900
  try {
826
901
  const max = (0, adRevenue_1.fromMaxAdInfo)(adInfo);
@@ -848,9 +923,18 @@ var require_build = __commonJS({
848
923
  await this.restoreConsent(opts.consent);
849
924
  await this.restoreQueue();
850
925
  await this.restoreAdQueue();
926
+ await this.restoreAnalytics();
851
927
  await this.buildContext();
852
- if (await this.restoreErased())
928
+ if (await this.restoreErased()) {
929
+ this.earlyAnalytics = [];
853
930
  return;
931
+ }
932
+ if (this.isWeb) {
933
+ this.earlyAnalytics = [];
934
+ } else {
935
+ for (const e of this.earlyAnalytics.splice(0))
936
+ this.pushAnalytics(e, false);
937
+ }
854
938
  if (!this.isWeb) {
855
939
  const freshInstall = Boolean(this.opts.storage) && !await this.opts.storage.getItem(this.installSentKey).catch(() => "1");
856
940
  await this.sendInstallOnce();
@@ -865,7 +949,7 @@ var require_build = __commonJS({
865
949
  await this.initAppleAds();
866
950
  this.wireLifecycle();
867
951
  this.wireAdRevenue();
868
- if (this.queue.length || this.adQueue.length)
952
+ if (this.queue.length || this.adQueue.length || this.analyticsQueue.length)
869
953
  this.scheduleFlush(0);
870
954
  }
871
955
  // `InitOptions.adRevenue`: hand the ad SDKs' own paid events to the trackers.
@@ -907,6 +991,82 @@ var require_build = __commonJS({
907
991
  if (props == null ? void 0 : props.value)
908
992
  this.skan.addRevenue(props.value);
909
993
  }
994
+ /**
995
+ * A product-analytics event — `paywall_viewed`, `deck_built`, `search_performed`…
996
+ * — read in the console's Analytics section (events, funnels, property splits).
997
+ * NOT a conversion: never billed, never sent to an ad network (that is `track()`).
998
+ *
999
+ * Batched, persisted while offline and retried like every other call; it never
1000
+ * throws. Names starting with `$` are reserved; properties must survive JSON and
1001
+ * stay under 4 KB, else they are dropped (the event is kept). Callable before
1002
+ * `init()`: the first 100 calls wait for it.
1003
+ *
1004
+ * Traqio.capture("lead_captured", { magnet: "starter-deck" });
1005
+ */
1006
+ capture(name, props) {
1007
+ try {
1008
+ const n = (0, analytics_1.cleanEventName)(name);
1009
+ if (!n)
1010
+ return;
1011
+ const p = (0, analytics_1.cleanProps)(props);
1012
+ this.queueAnalytics({ kind: "custom", name: n, ts: Date.now(), ...p ? { props: p } : {} });
1013
+ } catch {
1014
+ }
1015
+ }
1016
+ /**
1017
+ * A screen view — the app's page view, read under « Top pages » and usable as a
1018
+ * funnel step. Call it when a screen gets the focus, or let `trackScreens()` (React
1019
+ * Navigation) or Expo Router's `usePathname()` do it (docs: /docs/sdks/react-native).
1020
+ * The same screen reported twice in a row inside a session counts once (re-renders).
1021
+ * Custom events captured afterwards carry this screen as their page.
1022
+ */
1023
+ screen(name, props) {
1024
+ try {
1025
+ const s = (0, analytics_1.cleanScreenName)(name);
1026
+ if (!s)
1027
+ return;
1028
+ const now = Date.now();
1029
+ const sameVisit = this.analyticsSession !== null && now - this.analyticsSession.last <= analytics_1.ANALYTICS_SESSION_IDLE_MS;
1030
+ if (s === this.currentScreen && sameVisit)
1031
+ return;
1032
+ this.currentScreen = s;
1033
+ const p = (0, analytics_1.cleanProps)(props);
1034
+ this.queueAnalytics({ kind: "pageview", ts: now, screen: s, ...p ? { props: p } : {} });
1035
+ } catch {
1036
+ }
1037
+ }
1038
+ /**
1039
+ * Screen views from a React Navigation container, by themselves:
1040
+ *
1041
+ * const navigationRef = useNavigationContainerRef();
1042
+ * useEffect(() => Traqio.trackScreens(navigationRef), [navigationRef]);
1043
+ *
1044
+ * Returns the unsubscribe. Typed by shape: no navigation library is a dependency.
1045
+ */
1046
+ trackScreens(navigation) {
1047
+ const report = () => {
1048
+ try {
1049
+ const route = navigation.getCurrentRoute();
1050
+ if (route == null ? void 0 : route.name)
1051
+ this.screen(route.name);
1052
+ } catch {
1053
+ }
1054
+ };
1055
+ let off;
1056
+ try {
1057
+ off = navigation.addListener("state", report);
1058
+ } catch {
1059
+ off = void 0;
1060
+ }
1061
+ report();
1062
+ return () => {
1063
+ try {
1064
+ if (typeof off === "function")
1065
+ off();
1066
+ } catch {
1067
+ }
1068
+ };
1069
+ }
910
1070
  /**
911
1071
  * The store this app runs on: the native bridge's word, else React Native's
912
1072
  * `Platform.OS`, else UNKNOWN. Never a guess — until 2026-11 a missing bridge meant
@@ -1375,8 +1535,11 @@ var require_build = __commonJS({
1375
1535
  this.queue = [];
1376
1536
  this.adQueue = [];
1377
1537
  this.adBatchId = null;
1538
+ this.analyticsQueue = [];
1539
+ this.earlyAnalytics = [];
1378
1540
  await this.persistQueue();
1379
1541
  await this.persistAdQueue();
1542
+ await this.persistAnalyticsQueue();
1380
1543
  try {
1381
1544
  await ((_a = this.opts.storage) == null ? void 0 : _a.setItem(ERASED_KEY, "1"));
1382
1545
  } catch {
@@ -1389,6 +1552,55 @@ var require_build = __commonJS({
1389
1552
  void this.persistQueue();
1390
1553
  this.scheduleFlush();
1391
1554
  }
1555
+ /** Before `init()` the call waits in memory; after it, it is stamped and queued. */
1556
+ queueAnalytics(event) {
1557
+ var _a, _b;
1558
+ if (this.erased)
1559
+ return;
1560
+ const screen = (_b = (_a = event.screen) != null ? _a : this.currentScreen) != null ? _b : void 0;
1561
+ const e = screen ? { ...event, screen } : event;
1562
+ if (!this.started) {
1563
+ if (this.earlyAnalytics.length < MAX_EARLY_ANALYTICS)
1564
+ this.earlyAnalytics.push(e);
1565
+ return;
1566
+ }
1567
+ if (this.isWeb)
1568
+ return;
1569
+ this.pushAnalytics(e);
1570
+ }
1571
+ pushAnalytics(e, schedule = true) {
1572
+ this.analyticsSession = (0, analytics_1.touchSession)(this.analyticsSession, e.ts, uuid);
1573
+ this.analyticsQueue.push({ ...e, sessionId: this.analyticsSession.id });
1574
+ if (this.analyticsQueue.length > MAX_ANALYTICS_QUEUE)
1575
+ this.analyticsQueue.splice(0, this.analyticsQueue.length - MAX_ANALYTICS_QUEUE);
1576
+ void this.persistAnalyticsQueue();
1577
+ if (schedule)
1578
+ this.scheduleFlush();
1579
+ }
1580
+ /** The persisted queue first (older), then whatever was captured while it was being read. */
1581
+ async restoreAnalytics() {
1582
+ var _a, _b, _c;
1583
+ try {
1584
+ const raw = await ((_a = this.opts.storage) == null ? void 0 : _a.getItem(ANALYTICS_QUEUE_KEY));
1585
+ const stored = raw ? JSON.parse(raw) : null;
1586
+ if (Array.isArray(stored))
1587
+ this.analyticsQueue = [...stored, ...this.analyticsQueue].slice(-MAX_ANALYTICS_QUEUE);
1588
+ } catch {
1589
+ }
1590
+ try {
1591
+ (_c = this.analyticsSession) != null ? _c : this.analyticsSession = (0, analytics_1.parseSession)(await ((_b = this.opts.storage) == null ? void 0 : _b.getItem(ANALYTICS_SESSION_KEY)));
1592
+ } catch {
1593
+ }
1594
+ }
1595
+ async persistAnalyticsQueue() {
1596
+ var _a, _b;
1597
+ try {
1598
+ await ((_a = this.opts.storage) == null ? void 0 : _a.setItem(ANALYTICS_QUEUE_KEY, JSON.stringify(this.analyticsQueue)));
1599
+ if (this.analyticsSession)
1600
+ await ((_b = this.opts.storage) == null ? void 0 : _b.setItem(ANALYTICS_SESSION_KEY, JSON.stringify(this.analyticsSession)));
1601
+ } catch {
1602
+ }
1603
+ }
1392
1604
  async restoreQueue() {
1393
1605
  var _a;
1394
1606
  try {
@@ -1618,10 +1830,10 @@ var require_build = __commonJS({
1618
1830
  }, delay);
1619
1831
  }
1620
1832
  async drain() {
1621
- var _a;
1833
+ var _a, _b;
1622
1834
  if (this.erased)
1623
1835
  return;
1624
- if (this.flushing || !this.queue.length && !this.adQueue.length)
1836
+ if (this.flushing || !this.queue.length && !this.adQueue.length && !this.analyticsQueue.length)
1625
1837
  return;
1626
1838
  this.flushing = true;
1627
1839
  try {
@@ -1667,6 +1879,22 @@ var require_build = __commonJS({
1667
1879
  await this.persistAdQueue();
1668
1880
  this.retryMs = BASE_RETRY_MS;
1669
1881
  }
1882
+ while (this.analyticsQueue.length) {
1883
+ const batch = this.analyticsQueue.slice(0, MAX_ANALYTICS_BATCH);
1884
+ const outcome = await this.send({
1885
+ anonymousId: (_b = this.context.deviceId) != null ? _b : await this.getOrCreateDeviceId(),
1886
+ userId: this.opts.userId,
1887
+ platform: this.hostPlatform,
1888
+ events: batch
1889
+ }, ANALYTICS_PATH);
1890
+ if (outcome === "retry")
1891
+ return this.backOff();
1892
+ if (outcome === "refused")
1893
+ console.warn(`[traqio] the server refused ${batch.length} analytics event(s) as malformed \u2014 dropped, not retried`);
1894
+ this.analyticsQueue.splice(0, batch.length);
1895
+ await this.persistAnalyticsQueue();
1896
+ this.retryMs = BASE_RETRY_MS;
1897
+ }
1670
1898
  } finally {
1671
1899
  this.flushing = false;
1672
1900
  }