@traqio/web 0.3.2 → 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/README.md +26 -0
- package/build/analytics.d.ts +73 -0
- package/build/index.cjs +524 -11
- package/build/index.cjs.map +4 -4
- package/build/index.d.ts +27 -0
- package/build/index.js +524 -11
- package/build/index.js.map +4 -4
- package/build/traqio.min.js +1 -1
- package/build/traqio.min.js.map +4 -4
- package/package.json +2 -2
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.
|
|
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,9 +770,15 @@ 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
|
-
var
|
|
781
|
+
var MAX_QUEUE2 = 500;
|
|
711
782
|
var MAX_AD_BATCH = 100;
|
|
712
783
|
var MAX_AD_QUEUE = 1e3;
|
|
713
784
|
var BASE_RETRY_MS = 1e3;
|
|
@@ -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 {
|
|
@@ -1379,11 +1542,60 @@ var require_build = __commonJS({
|
|
|
1379
1542
|
}
|
|
1380
1543
|
enqueue(e) {
|
|
1381
1544
|
this.queue.push(e);
|
|
1382
|
-
if (this.queue.length >
|
|
1383
|
-
this.queue.splice(0, this.queue.length -
|
|
1545
|
+
if (this.queue.length > MAX_QUEUE2)
|
|
1546
|
+
this.queue.splice(0, this.queue.length - MAX_QUEUE2);
|
|
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
|
}
|
|
@@ -2134,6 +2362,245 @@ var TrackIdKeeper = class {
|
|
|
2134
2362
|
}
|
|
2135
2363
|
};
|
|
2136
2364
|
|
|
2365
|
+
// src/analytics.ts
|
|
2366
|
+
var SESSION_KEY = "traq_session";
|
|
2367
|
+
var SESSION_IDLE_MS = 30 * 60 * 1e3;
|
|
2368
|
+
var FLUSH_EVERY_MS = 5e3;
|
|
2369
|
+
var FLUSH_AT = 20;
|
|
2370
|
+
var MAX_QUEUE = 200;
|
|
2371
|
+
var MAX_ERRORS_PER_PAGE = 10;
|
|
2372
|
+
var SCROLL_STEPS = [25, 50, 75, 100];
|
|
2373
|
+
var UTM = [
|
|
2374
|
+
["utm_source", "utmSource"],
|
|
2375
|
+
["utm_medium", "utmMedium"],
|
|
2376
|
+
["utm_campaign", "utmCampaign"],
|
|
2377
|
+
["utm_content", "utmContent"],
|
|
2378
|
+
["utm_term", "utmTerm"],
|
|
2379
|
+
["gclid", "gclid"],
|
|
2380
|
+
["fbclid", "fbclid"],
|
|
2381
|
+
["ttclid", "ttclid"]
|
|
2382
|
+
];
|
|
2383
|
+
function newId() {
|
|
2384
|
+
try {
|
|
2385
|
+
if (typeof crypto !== "undefined" && typeof crypto.randomUUID === "function") return crypto.randomUUID();
|
|
2386
|
+
} catch {
|
|
2387
|
+
}
|
|
2388
|
+
return `${Date.now().toString(36)}-${Math.random().toString(36).slice(2, 12)}`;
|
|
2389
|
+
}
|
|
2390
|
+
function landingSource(href, referrer) {
|
|
2391
|
+
var _a;
|
|
2392
|
+
const src = {};
|
|
2393
|
+
try {
|
|
2394
|
+
const u = new URL(href);
|
|
2395
|
+
for (const [param, key] of UTM) {
|
|
2396
|
+
const v = (_a = u.searchParams.get(param)) == null ? void 0 : _a.trim();
|
|
2397
|
+
if (v) src[key] = v.slice(0, 200);
|
|
2398
|
+
}
|
|
2399
|
+
if (referrer) {
|
|
2400
|
+
const r = new URL(referrer);
|
|
2401
|
+
if (r.hostname.replace(/^www\./, "") !== u.hostname.replace(/^www\./, "")) src.referrer = `${r.protocol}//${r.host}/`;
|
|
2402
|
+
}
|
|
2403
|
+
} catch {
|
|
2404
|
+
}
|
|
2405
|
+
return Object.keys(src).length ? src : void 0;
|
|
2406
|
+
}
|
|
2407
|
+
function bare2(href) {
|
|
2408
|
+
if (!href) return void 0;
|
|
2409
|
+
try {
|
|
2410
|
+
const u = new URL(href, typeof location !== "undefined" ? location.href : void 0);
|
|
2411
|
+
if (u.protocol !== "http:" && u.protocol !== "https:") return void 0;
|
|
2412
|
+
return `${u.origin}${u.pathname}`;
|
|
2413
|
+
} catch {
|
|
2414
|
+
return void 0;
|
|
2415
|
+
}
|
|
2416
|
+
}
|
|
2417
|
+
var AnalyticsTracker = class {
|
|
2418
|
+
constructor(deps) {
|
|
2419
|
+
this.deps = deps;
|
|
2420
|
+
this.queue = [];
|
|
2421
|
+
this.timer = null;
|
|
2422
|
+
this.lastPath = null;
|
|
2423
|
+
this.maxScroll = 0;
|
|
2424
|
+
this.scrollUrl = null;
|
|
2425
|
+
this.errorsThisPage = 0;
|
|
2426
|
+
this.stopped = false;
|
|
2427
|
+
}
|
|
2428
|
+
/** Wires the listeners the options ask for and records the first page view. */
|
|
2429
|
+
start() {
|
|
2430
|
+
const o = this.deps.options;
|
|
2431
|
+
this.timer = setInterval(() => this.flush(false), FLUSH_EVERY_MS);
|
|
2432
|
+
document.addEventListener("visibilitychange", () => {
|
|
2433
|
+
if (document.visibilityState === "hidden") this.leave();
|
|
2434
|
+
});
|
|
2435
|
+
window.addEventListener("pagehide", () => this.leave());
|
|
2436
|
+
if (o.pageviews !== false) {
|
|
2437
|
+
this.pageview();
|
|
2438
|
+
this.watchRoutes();
|
|
2439
|
+
}
|
|
2440
|
+
if (o.clicks !== false) document.addEventListener("click", (e) => this.onClick(e), true);
|
|
2441
|
+
if (o.scroll !== false) window.addEventListener("scroll", () => this.onScroll(), { passive: true });
|
|
2442
|
+
if (o.errors !== false) {
|
|
2443
|
+
window.addEventListener("error", (e) => this.onError(e));
|
|
2444
|
+
window.addEventListener("unhandledrejection", (e) => this.onRejection(e));
|
|
2445
|
+
}
|
|
2446
|
+
}
|
|
2447
|
+
/** A custom analytics event — never a conversion (that is `track()`). */
|
|
2448
|
+
capture(name, props) {
|
|
2449
|
+
if (typeof name !== "string" || !name.trim() || name.startsWith("$")) return;
|
|
2450
|
+
this.enqueue({ kind: "custom", name: name.trim(), props });
|
|
2451
|
+
}
|
|
2452
|
+
/** Records a page view of the current URL. Called by itself on route changes; exposed for routers that need it. */
|
|
2453
|
+
pageview() {
|
|
2454
|
+
const url = bare2(location.href);
|
|
2455
|
+
if (!url) return;
|
|
2456
|
+
if (url === this.lastPath) return;
|
|
2457
|
+
this.sendScroll();
|
|
2458
|
+
this.lastPath = url;
|
|
2459
|
+
this.scrollUrl = url;
|
|
2460
|
+
this.maxScroll = 0;
|
|
2461
|
+
this.errorsThisPage = 0;
|
|
2462
|
+
this.enqueue({ kind: "pageview" });
|
|
2463
|
+
this.onScroll();
|
|
2464
|
+
}
|
|
2465
|
+
/** Sends what is queued. `leaving` = the page is going away: keepalive, so the request outlives it. */
|
|
2466
|
+
flush(leaving) {
|
|
2467
|
+
if (!this.queue.length || this.stopped) return;
|
|
2468
|
+
if (this.deps.isForgotten()) {
|
|
2469
|
+
this.stop();
|
|
2470
|
+
return;
|
|
2471
|
+
}
|
|
2472
|
+
const anonymousId = this.deps.anonymousId();
|
|
2473
|
+
if (!anonymousId) return;
|
|
2474
|
+
const events = this.queue.splice(0, 100);
|
|
2475
|
+
const body = JSON.stringify({
|
|
2476
|
+
sdkKey: this.deps.sdkKey,
|
|
2477
|
+
anonymousId,
|
|
2478
|
+
userId: this.deps.userId(),
|
|
2479
|
+
debug: this.deps.isDebug() || void 0,
|
|
2480
|
+
events
|
|
2481
|
+
});
|
|
2482
|
+
try {
|
|
2483
|
+
void fetch(`${this.deps.endpoint}/ingest/analytics`, {
|
|
2484
|
+
method: "POST",
|
|
2485
|
+
headers: { "content-type": "application/json" },
|
|
2486
|
+
body,
|
|
2487
|
+
keepalive: leaving
|
|
2488
|
+
}).then((res) => {
|
|
2489
|
+
if (!res.ok && (res.status >= 500 || res.status === 429)) this.requeue(events);
|
|
2490
|
+
}).catch(() => this.requeue(events));
|
|
2491
|
+
} catch {
|
|
2492
|
+
this.requeue(events);
|
|
2493
|
+
}
|
|
2494
|
+
}
|
|
2495
|
+
/** Forget everything and stop: after `forgetMe()`, or on a server. */
|
|
2496
|
+
stop() {
|
|
2497
|
+
this.stopped = true;
|
|
2498
|
+
this.queue = [];
|
|
2499
|
+
if (this.timer) clearInterval(this.timer);
|
|
2500
|
+
this.timer = null;
|
|
2501
|
+
}
|
|
2502
|
+
// ── internals ──────────────────────────────────────────────────────────────
|
|
2503
|
+
requeue(events) {
|
|
2504
|
+
if (this.stopped) return;
|
|
2505
|
+
this.queue = [...events, ...this.queue].slice(-MAX_QUEUE);
|
|
2506
|
+
}
|
|
2507
|
+
session() {
|
|
2508
|
+
const now = Date.now();
|
|
2509
|
+
let s = null;
|
|
2510
|
+
try {
|
|
2511
|
+
const raw = this.deps.store.getSync(SESSION_KEY);
|
|
2512
|
+
s = raw ? JSON.parse(raw) : null;
|
|
2513
|
+
} catch {
|
|
2514
|
+
s = null;
|
|
2515
|
+
}
|
|
2516
|
+
if (!s || typeof s.id !== "string" || typeof s.last !== "number" || now - s.last > SESSION_IDLE_MS) {
|
|
2517
|
+
s = { id: newId(), last: now, src: landingSource(location.href, document.referrer) };
|
|
2518
|
+
} else {
|
|
2519
|
+
s.last = now;
|
|
2520
|
+
}
|
|
2521
|
+
try {
|
|
2522
|
+
this.deps.store.setSync(SESSION_KEY, JSON.stringify(s));
|
|
2523
|
+
} catch {
|
|
2524
|
+
}
|
|
2525
|
+
return s;
|
|
2526
|
+
}
|
|
2527
|
+
enqueue(e) {
|
|
2528
|
+
var _a, _b;
|
|
2529
|
+
if (this.stopped) return;
|
|
2530
|
+
const s = this.session();
|
|
2531
|
+
const title = typeof document.title === "string" && document.title ? document.title.slice(0, 200) : void 0;
|
|
2532
|
+
this.queue.push({ ...e, ts: Date.now(), url: (_b = (_a = e.url) != null ? _a : bare2(location.href)) != null ? _b : "", title, sessionId: s.id, ...s.src ? { src: s.src } : {} });
|
|
2533
|
+
if (this.queue.length > MAX_QUEUE) this.queue.shift();
|
|
2534
|
+
if (this.queue.length >= FLUSH_AT) this.flush(false);
|
|
2535
|
+
}
|
|
2536
|
+
leave() {
|
|
2537
|
+
this.sendScroll();
|
|
2538
|
+
this.flush(true);
|
|
2539
|
+
}
|
|
2540
|
+
/** Single-page apps change the URL through the history API: wrap it, and listen for back/forward. */
|
|
2541
|
+
watchRoutes() {
|
|
2542
|
+
if (typeof history === "undefined") return;
|
|
2543
|
+
const after = () => {
|
|
2544
|
+
setTimeout(() => this.pageview(), 0);
|
|
2545
|
+
};
|
|
2546
|
+
for (const method of ["pushState", "replaceState"]) {
|
|
2547
|
+
const original = history[method];
|
|
2548
|
+
if (typeof original !== "function") continue;
|
|
2549
|
+
history[method] = function(...args) {
|
|
2550
|
+
const out = original.apply(this, args);
|
|
2551
|
+
after();
|
|
2552
|
+
return out;
|
|
2553
|
+
};
|
|
2554
|
+
}
|
|
2555
|
+
window.addEventListener("popstate", after);
|
|
2556
|
+
}
|
|
2557
|
+
onScroll() {
|
|
2558
|
+
var _a;
|
|
2559
|
+
if (typeof document.documentElement === "undefined" || !document.documentElement) return;
|
|
2560
|
+
const doc2 = document.documentElement;
|
|
2561
|
+
const viewport = window.innerHeight || doc2.clientHeight || 0;
|
|
2562
|
+
const height = Math.max(doc2.scrollHeight || 0, ((_a = document.body) == null ? void 0 : _a.scrollHeight) || 0);
|
|
2563
|
+
if (!height) return;
|
|
2564
|
+
const seen = height <= viewport ? 1 : Math.min(1, ((window.scrollY || doc2.scrollTop || 0) + viewport) / height);
|
|
2565
|
+
const pct = Math.round(seen * 100);
|
|
2566
|
+
for (const step of SCROLL_STEPS) if (pct >= step && step > this.maxScroll) this.maxScroll = step;
|
|
2567
|
+
}
|
|
2568
|
+
sendScroll() {
|
|
2569
|
+
if (this.deps.options.scroll === false || !this.scrollUrl || this.maxScroll < 25) return;
|
|
2570
|
+
this.enqueue({ kind: "scroll", url: this.scrollUrl, props: { depth: this.maxScroll } });
|
|
2571
|
+
this.scrollUrl = null;
|
|
2572
|
+
this.maxScroll = 0;
|
|
2573
|
+
}
|
|
2574
|
+
onClick(e) {
|
|
2575
|
+
const target = e.target;
|
|
2576
|
+
const el = target && typeof target.closest === "function" ? target.closest("a, button, [role='button'], input[type='submit'], input[type='button']") : null;
|
|
2577
|
+
if (!el || el.closest("[data-traqio-ignore]")) return;
|
|
2578
|
+
const input = el;
|
|
2579
|
+
const text = (el.tagName === "INPUT" ? input.value : el.innerText || el.textContent || "").replace(/\s+/g, " ").trim().slice(0, 100);
|
|
2580
|
+
const href = el.tagName === "A" ? bare2(el.getAttribute("href")) : void 0;
|
|
2581
|
+
const props = { tag: el.tagName.toLowerCase() };
|
|
2582
|
+
if (text) props.text = text;
|
|
2583
|
+
if (href) props.href = href;
|
|
2584
|
+
if (el.id) props.id = el.id.slice(0, 100);
|
|
2585
|
+
this.enqueue({ kind: "click", props });
|
|
2586
|
+
}
|
|
2587
|
+
onError(e) {
|
|
2588
|
+
if (this.errorsThisPage >= MAX_ERRORS_PER_PAGE) return;
|
|
2589
|
+
const message = (e.message || (e.error instanceof Error ? e.error.message : "") || "Script error").slice(0, 300);
|
|
2590
|
+
this.errorsThisPage++;
|
|
2591
|
+
const stack = e.error instanceof Error && typeof e.error.stack === "string" ? e.error.stack.slice(0, 1500) : void 0;
|
|
2592
|
+
this.enqueue({ kind: "error", props: { message, ...stack ? { stack } : {}, ...e.filename ? { source: bare2(e.filename) } : {}, ...e.lineno ? { line: e.lineno } : {} } });
|
|
2593
|
+
}
|
|
2594
|
+
onRejection(e) {
|
|
2595
|
+
if (this.errorsThisPage >= MAX_ERRORS_PER_PAGE) return;
|
|
2596
|
+
const reason = e.reason;
|
|
2597
|
+
const message = (reason instanceof Error ? reason.message : typeof reason === "string" ? reason : "Unhandled promise rejection").slice(0, 300);
|
|
2598
|
+
this.errorsThisPage++;
|
|
2599
|
+
const stack = reason instanceof Error && typeof reason.stack === "string" ? reason.stack.slice(0, 1500) : void 0;
|
|
2600
|
+
this.enqueue({ kind: "error", props: { message, unhandledRejection: true, ...stack ? { stack } : {} } });
|
|
2601
|
+
}
|
|
2602
|
+
};
|
|
2603
|
+
|
|
2137
2604
|
// src/index.ts
|
|
2138
2605
|
var DEFAULT_ENDPOINT = "https://api.traqio.app";
|
|
2139
2606
|
var DEFAULT_TRACK_ID_DAYS = 30;
|
|
@@ -2156,6 +2623,11 @@ var TraqioWeb = class {
|
|
|
2156
2623
|
this.sources = null;
|
|
2157
2624
|
this.waitForConsent = false;
|
|
2158
2625
|
this.started = false;
|
|
2626
|
+
this.analytics = null;
|
|
2627
|
+
/** `capture()` calls made before `init()` finished — replayed once the tracker exists. */
|
|
2628
|
+
this.earlyCaptures = [];
|
|
2629
|
+
/** `init()` has finished (or will never run here): early captures are no longer kept. */
|
|
2630
|
+
this.ready = false;
|
|
2159
2631
|
}
|
|
2160
2632
|
/**
|
|
2161
2633
|
* Call once, as early as possible on every page (or once in a single-page app).
|
|
@@ -2183,9 +2655,11 @@ var TraqioWeb = class {
|
|
|
2183
2655
|
const id = randomId();
|
|
2184
2656
|
if (id) this.store.setSync(DEVICE_ID_KEY, id);
|
|
2185
2657
|
}
|
|
2658
|
+
this.userId = opts.userId;
|
|
2659
|
+
const endpoint = ((_c = opts.endpoint) != null ? _c : DEFAULT_ENDPOINT).replace(/\/+$/, "");
|
|
2186
2660
|
await this.core.init({
|
|
2187
2661
|
sdkKey: opts.sdkKey,
|
|
2188
|
-
endpoint
|
|
2662
|
+
endpoint,
|
|
2189
2663
|
storage: this.store,
|
|
2190
2664
|
platform: "web",
|
|
2191
2665
|
userId: opts.userId,
|
|
@@ -2196,6 +2670,23 @@ var TraqioWeb = class {
|
|
|
2196
2670
|
window.addEventListener("pagehide", background);
|
|
2197
2671
|
}
|
|
2198
2672
|
});
|
|
2673
|
+
if (opts.analytics) {
|
|
2674
|
+
const store = this.store;
|
|
2675
|
+
this.analytics = new AnalyticsTracker({
|
|
2676
|
+
store,
|
|
2677
|
+
endpoint,
|
|
2678
|
+
sdkKey: opts.sdkKey,
|
|
2679
|
+
anonymousId: () => store.getSync(DEVICE_ID_KEY),
|
|
2680
|
+
userId: () => this.userId,
|
|
2681
|
+
isDebug: () => this.core.isDebug,
|
|
2682
|
+
isForgotten: () => this.core.forgotten,
|
|
2683
|
+
options: opts.analytics === true ? {} : opts.analytics
|
|
2684
|
+
});
|
|
2685
|
+
this.analytics.start();
|
|
2686
|
+
for (const [name, props] of this.earlyCaptures) this.analytics.capture(name, props);
|
|
2687
|
+
}
|
|
2688
|
+
this.earlyCaptures = [];
|
|
2689
|
+
this.ready = true;
|
|
2199
2690
|
}
|
|
2200
2691
|
/** Whether this page runs in debug mode — see `WebInitOptions.debug`. Settled by `init()`. */
|
|
2201
2692
|
get isDebug() {
|
|
@@ -2205,8 +2696,27 @@ var TraqioWeb = class {
|
|
|
2205
2696
|
track(name, props) {
|
|
2206
2697
|
this.core.track(name, props);
|
|
2207
2698
|
}
|
|
2699
|
+
/**
|
|
2700
|
+
* A product-analytics event — `search_performed`, `pricing_toggled`… — read in the
|
|
2701
|
+
* console's Analytics section (events, funnels). NOT a conversion: it is never
|
|
2702
|
+
* billed nor sent to an ad network. A conversion (sign-up, purchase) is `track()`.
|
|
2703
|
+
* Needs `analytics` at init; names starting with `$` are reserved.
|
|
2704
|
+
*/
|
|
2705
|
+
capture(name, props) {
|
|
2706
|
+
if (this.analytics) this.analytics.capture(name, props);
|
|
2707
|
+
else if (!this.ready && this.earlyCaptures.length < 50) this.earlyCaptures.push([name, props]);
|
|
2708
|
+
}
|
|
2709
|
+
/**
|
|
2710
|
+
* Record a page view now. Not needed with the history API (route changes are
|
|
2711
|
+
* caught by themselves); for a router that changes the page another way.
|
|
2712
|
+
*/
|
|
2713
|
+
pageview() {
|
|
2714
|
+
var _a;
|
|
2715
|
+
(_a = this.analytics) == null ? void 0 : _a.pageview();
|
|
2716
|
+
}
|
|
2208
2717
|
/** Set the user id once known (after sign-in). */
|
|
2209
2718
|
setUserId(id) {
|
|
2719
|
+
this.userId = id;
|
|
2210
2720
|
this.core.setUserId(id);
|
|
2211
2721
|
}
|
|
2212
2722
|
/**
|
|
@@ -2255,17 +2765,20 @@ var TraqioWeb = class {
|
|
|
2255
2765
|
* Resolves false when the request did not reach the server — then nothing changed.
|
|
2256
2766
|
*/
|
|
2257
2767
|
async forgetMe() {
|
|
2258
|
-
var _a, _b;
|
|
2768
|
+
var _a, _b, _c;
|
|
2259
2769
|
const ok = await this.core.forgetMe();
|
|
2260
2770
|
if (ok) {
|
|
2261
2771
|
(_a = this.trackIds) == null ? void 0 : _a.clear();
|
|
2262
2772
|
(_b = this.sources) == null ? void 0 : _b.clear();
|
|
2773
|
+
(_c = this.analytics) == null ? void 0 : _c.stop();
|
|
2263
2774
|
}
|
|
2264
2775
|
return ok;
|
|
2265
2776
|
}
|
|
2266
2777
|
/** Send what is queued now. Already wired to tab hide and page exit. */
|
|
2267
2778
|
flush() {
|
|
2779
|
+
var _a;
|
|
2268
2780
|
this.core.flush();
|
|
2781
|
+
(_a = this.analytics) == null ? void 0 : _a.flush(false);
|
|
2269
2782
|
}
|
|
2270
2783
|
/** True once `forgetMe()` has been honoured: this browser sends nothing any more. */
|
|
2271
2784
|
get forgotten() {
|