@traqio/web 0.5.0 → 0.6.0
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 +24 -1
- package/build/analytics.d.ts +28 -0
- package/build/index.cjs +336 -18
- package/build/index.cjs.map +3 -3
- package/build/index.d.ts +49 -0
- package/build/index.js +336 -18
- package/build/index.js.map +3 -3
- package/build/storage.d.ts +6 -0
- package/build/traqio.min.js +1 -1
- package/build/traqio.min.js.map +3 -3
- package/package.json +2 -2
package/build/index.d.ts
CHANGED
|
@@ -59,6 +59,11 @@ export interface WebTrackProps {
|
|
|
59
59
|
/** Where the payment went through — `"stripe"` for Stripe. */
|
|
60
60
|
store?: "stripe" | "app_store" | "play_store" | "mac_app_store" | "amazon";
|
|
61
61
|
}
|
|
62
|
+
/**
|
|
63
|
+
* `traqio_internal` in a URL's query: `1`/`true` → true, `0`/`false` → false, absent or
|
|
64
|
+
* anything else → null (left as it was).
|
|
65
|
+
*/
|
|
66
|
+
export declare function internalParam(href: string): boolean | null;
|
|
62
67
|
export declare class TraqioWeb {
|
|
63
68
|
private readonly core;
|
|
64
69
|
private store;
|
|
@@ -82,6 +87,10 @@ export declare class TraqioWeb {
|
|
|
82
87
|
private groups;
|
|
83
88
|
/** Group entries made before `init()` — replayed once the tracker exists. */
|
|
84
89
|
private earlyGroups;
|
|
90
|
+
/** `alias()` calls made before `init()`: [userId, previousId]. */
|
|
91
|
+
private earlyAliases;
|
|
92
|
+
/** A `reset()` before `init()`: what earlier pages kept (super-properties, accounts) is dropped, not restored. */
|
|
93
|
+
private resetBeforeInit;
|
|
85
94
|
/**
|
|
86
95
|
* Call once, as early as possible on every page (or once in a single-page app).
|
|
87
96
|
* Reads the trackID from the current URL first, synchronously, so even a visitor
|
|
@@ -89,6 +98,16 @@ export declare class TraqioWeb {
|
|
|
89
98
|
* does nothing there.
|
|
90
99
|
*/
|
|
91
100
|
init(opts: WebInitOptions): Promise<void>;
|
|
101
|
+
/**
|
|
102
|
+
* Mark this browser as the team's own (`true`) or as a visitor again (`false`) — what
|
|
103
|
+
* opening the site with `?traqio_internal=1` / `=0` does. Its analytics events are
|
|
104
|
+
* still sent, flagged: the console leaves them out of every figure by default and
|
|
105
|
+
* shows them under « Internal traffic only ». Conversions (`track()`) are not
|
|
106
|
+
* affected. Kept on the device, like the visitor id (and under the same consent).
|
|
107
|
+
*/
|
|
108
|
+
setInternal(on: boolean): void;
|
|
109
|
+
/** True when this browser opted itself out as internal traffic — see `setInternal()`. */
|
|
110
|
+
get isInternal(): boolean;
|
|
92
111
|
/** Whether this page runs in debug mode — see `WebInitOptions.debug`. Settled by `init()`. */
|
|
93
112
|
get isDebug(): boolean;
|
|
94
113
|
/** Record an event — `signup`, `purchase`, `add_to_cart`… Batched, retried, kept across page loads. */
|
|
@@ -149,6 +168,36 @@ export declare class TraqioWeb {
|
|
|
149
168
|
resetGroups(): void;
|
|
150
169
|
/** The accounts `group()` set, by type — a copy. */
|
|
151
170
|
getGroups(): Groups;
|
|
171
|
+
/**
|
|
172
|
+
* Two ids, one person: the user known so far as `previousId` (a guest id, an id
|
|
173
|
+
* from before sign-up) IS the current user (`setUserId` / `identify`). Their
|
|
174
|
+
* analytics history and properties are merged under the current id — its own
|
|
175
|
+
* properties win. From the browser only what THIS visitor sent under `previousId`
|
|
176
|
+
* is merged; a backend (`@traqio/node`, secret key) merges everything.
|
|
177
|
+
*
|
|
178
|
+
* Traqio.identify("u_123");
|
|
179
|
+
* Traqio.alias("guest_8f2c");
|
|
180
|
+
*
|
|
181
|
+
* Ignored without a current user id, or when `previousId` is the current one. Needs `analytics` at init.
|
|
182
|
+
*/
|
|
183
|
+
alias(previousId: string): void;
|
|
184
|
+
/**
|
|
185
|
+
* Sign-out: forget who the user was in this browser — the user id, the
|
|
186
|
+
* super-properties, the accounts — and end the analytics session, so the next
|
|
187
|
+
* person's visit starts clean. Events already queued keep the user they were
|
|
188
|
+
* captured under. The visitor id is KEPT: it carries the visitor's attribution
|
|
189
|
+
* (the click, the campaign) for the next sign-up.
|
|
190
|
+
*/
|
|
191
|
+
reset(): void;
|
|
192
|
+
/**
|
|
193
|
+
* An experiment exposure: the visitor SAW `variant` of `experiment` — what the
|
|
194
|
+
* console's experiment analysis reads (uplift, significance per variant). Your site
|
|
195
|
+
* assigns the variant (its own flags, an edge split, a cookie); call this where the
|
|
196
|
+
* visitor first meets it. Sends `$exposure { experiment, variant, ...props }`.
|
|
197
|
+
*
|
|
198
|
+
* Traqio.exposure("pricing_page", "annual_first");
|
|
199
|
+
*/
|
|
200
|
+
exposure(experiment: string, variant: string | number | boolean, props?: Record<string, unknown>): void;
|
|
152
201
|
private persistGroups;
|
|
153
202
|
private persistSuperProps;
|
|
154
203
|
/**
|
package/build/index.js
CHANGED
|
@@ -231,6 +231,10 @@ var require_analytics = __commonJS({
|
|
|
231
231
|
"use strict";
|
|
232
232
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
233
233
|
exports.EXPOSURE_EVENT = exports.MAX_PROPS_BYTES = exports.MAX_SCREEN_NAME = exports.MAX_EVENT_NAME = exports.ANALYTICS_SESSION_IDLE_MS = exports.MAX_TRAITS = exports.MAX_GROUP_TYPES = void 0;
|
|
234
|
+
exports.cleanUserId = cleanUserId2;
|
|
235
|
+
exports.isEntry = isEntry2;
|
|
236
|
+
exports.nextAnalyticsBatch = nextAnalyticsBatch;
|
|
237
|
+
exports.exposureProps = exposureProps2;
|
|
234
238
|
exports.cleanGroupType = cleanGroupType2;
|
|
235
239
|
exports.cleanGroupKey = cleanGroupKey2;
|
|
236
240
|
exports.cleanGroups = cleanGroups2;
|
|
@@ -242,6 +246,49 @@ var require_analytics = __commonJS({
|
|
|
242
246
|
exports.cleanProps = cleanProps;
|
|
243
247
|
exports.touchSession = touchSession;
|
|
244
248
|
exports.parseSession = parseSession;
|
|
249
|
+
function cleanUserId2(v) {
|
|
250
|
+
if (typeof v === "number" && Number.isFinite(v))
|
|
251
|
+
return String(v);
|
|
252
|
+
if (typeof v !== "string")
|
|
253
|
+
return null;
|
|
254
|
+
const t = v.trim();
|
|
255
|
+
return t && t.length <= 256 ? t : null;
|
|
256
|
+
}
|
|
257
|
+
function isEntry2(kind) {
|
|
258
|
+
return kind === "identify" || kind === "group" || kind === "alias";
|
|
259
|
+
}
|
|
260
|
+
function nextAnalyticsBatch(queue, current, max) {
|
|
261
|
+
let userId;
|
|
262
|
+
let fixed = false;
|
|
263
|
+
const events = [];
|
|
264
|
+
for (const e of queue) {
|
|
265
|
+
if (events.length >= max)
|
|
266
|
+
break;
|
|
267
|
+
if (!isEntry2(e.kind)) {
|
|
268
|
+
const who = e.asUser !== void 0 ? e.asUser || void 0 : current;
|
|
269
|
+
if (!fixed) {
|
|
270
|
+
userId = who;
|
|
271
|
+
fixed = true;
|
|
272
|
+
} else if (who !== userId)
|
|
273
|
+
break;
|
|
274
|
+
}
|
|
275
|
+
const { asUser: _asUser, ...wire } = e;
|
|
276
|
+
events.push(wire);
|
|
277
|
+
}
|
|
278
|
+
return { events, userId: fixed ? userId : current, taken: events.length };
|
|
279
|
+
}
|
|
280
|
+
var MAX_EXPOSURE_TEXT = 200;
|
|
281
|
+
function exposureText2(v) {
|
|
282
|
+
const t = typeof v === "string" ? v.trim() : typeof v === "number" && Number.isFinite(v) || typeof v === "boolean" ? String(v) : "";
|
|
283
|
+
return t && t.length <= MAX_EXPOSURE_TEXT ? t : null;
|
|
284
|
+
}
|
|
285
|
+
function exposureProps2(experiment, variant, props) {
|
|
286
|
+
const e = exposureText2(experiment);
|
|
287
|
+
const v = exposureText2(variant);
|
|
288
|
+
if (!e || !v)
|
|
289
|
+
return null;
|
|
290
|
+
return { ...props && typeof props === "object" && !Array.isArray(props) ? props : {}, experiment: e, variant: v };
|
|
291
|
+
}
|
|
245
292
|
var GROUP_TYPE2 = /^[a-z][a-z0-9_]{0,29}$/;
|
|
246
293
|
exports.MAX_GROUP_TYPES = 5;
|
|
247
294
|
var MAX_GROUP_KEY = 256;
|
|
@@ -772,7 +819,7 @@ var require_build = __commonJS({
|
|
|
772
819
|
var adservices_1 = require_adservices();
|
|
773
820
|
var deeplink_1 = require_deeplink();
|
|
774
821
|
var adRevenue_1 = require_adRevenue();
|
|
775
|
-
exports.SDK_VERSION = "0.
|
|
822
|
+
exports.SDK_VERSION = "0.9.0";
|
|
776
823
|
var skan_2 = require_skan();
|
|
777
824
|
Object.defineProperty(exports, "Skan", { enumerable: true, get: function() {
|
|
778
825
|
return skan_2.Skan;
|
|
@@ -970,6 +1017,7 @@ var require_build = __commonJS({
|
|
|
970
1017
|
this.currentScreen = null;
|
|
971
1018
|
this.superProps = {};
|
|
972
1019
|
this.groups = {};
|
|
1020
|
+
this.resetBeforeInit = false;
|
|
973
1021
|
this.maxRevenuePaid = (adInfo) => {
|
|
974
1022
|
try {
|
|
975
1023
|
const max = (0, adRevenue_1.fromMaxAdInfo)(adInfo);
|
|
@@ -1018,7 +1066,10 @@ var require_build = __commonJS({
|
|
|
1018
1066
|
this.wireDeepLinks();
|
|
1019
1067
|
}
|
|
1020
1068
|
}
|
|
1021
|
-
|
|
1069
|
+
if (this.isWeb)
|
|
1070
|
+
void this.maybeStartSession(false);
|
|
1071
|
+
else
|
|
1072
|
+
await this.maybeStartSession(true);
|
|
1022
1073
|
await this.initSkan();
|
|
1023
1074
|
await this.initAppleAds();
|
|
1024
1075
|
this.wireLifecycle();
|
|
@@ -1184,6 +1235,81 @@ var require_build = __commonJS({
|
|
|
1184
1235
|
getGroups() {
|
|
1185
1236
|
return { ...this.groups };
|
|
1186
1237
|
}
|
|
1238
|
+
/**
|
|
1239
|
+
* Two ids, one person: the user known so far as `previousId` (a guest id, an id
|
|
1240
|
+
* from before sign-up, an older system's) IS the current user (`setUserId` /
|
|
1241
|
+
* `identify`). Their analytics history and properties are merged under the current
|
|
1242
|
+
* id — the current id's own properties win. From an app, only what THIS device sent
|
|
1243
|
+
* under `previousId` is merged; a backend (secret key) merges everything.
|
|
1244
|
+
*
|
|
1245
|
+
* Traqio.identify("u_123");
|
|
1246
|
+
* Traqio.alias("guest_8f2c");
|
|
1247
|
+
*
|
|
1248
|
+
* Ignored without a current user id, or when `previousId` is the current one.
|
|
1249
|
+
*/
|
|
1250
|
+
alias(previousId) {
|
|
1251
|
+
try {
|
|
1252
|
+
const userId = this.opts ? this.opts.userId : this.earlyUserId;
|
|
1253
|
+
const prev = (0, analytics_1.cleanUserId)(previousId);
|
|
1254
|
+
if (!userId || !prev || prev === userId)
|
|
1255
|
+
return;
|
|
1256
|
+
this.queueAnalytics({ kind: "alias", ts: Date.now(), userId, previousId: prev });
|
|
1257
|
+
} catch {
|
|
1258
|
+
}
|
|
1259
|
+
}
|
|
1260
|
+
/**
|
|
1261
|
+
* Sign-out: forget who the user was on this device — the user id, the
|
|
1262
|
+
* super-properties (`register`), the accounts (`group`) — and end the analytics
|
|
1263
|
+
* session, so the next person's visit starts clean. Events already queued keep the
|
|
1264
|
+
* user they were captured under. The device id is KEPT: it is the install's
|
|
1265
|
+
* attribution identity, and a new one would read as a new install.
|
|
1266
|
+
*/
|
|
1267
|
+
reset() {
|
|
1268
|
+
try {
|
|
1269
|
+
const userId = this.opts ? this.opts.userId : this.earlyUserId;
|
|
1270
|
+
if (userId) {
|
|
1271
|
+
for (const e of this.analyticsQueue)
|
|
1272
|
+
if (!(0, analytics_1.isEntry)(e.kind) && e.asUser === void 0)
|
|
1273
|
+
e.asUser = userId;
|
|
1274
|
+
for (const e of this.earlyAnalytics)
|
|
1275
|
+
if (!(0, analytics_1.isEntry)(e.kind) && e.asUser === void 0)
|
|
1276
|
+
e.asUser = userId;
|
|
1277
|
+
}
|
|
1278
|
+
if (this.opts)
|
|
1279
|
+
this.opts.userId = void 0;
|
|
1280
|
+
else {
|
|
1281
|
+
this.earlyUserId = void 0;
|
|
1282
|
+
this.resetBeforeInit = true;
|
|
1283
|
+
}
|
|
1284
|
+
this.superProps = {};
|
|
1285
|
+
this.groups = {};
|
|
1286
|
+
this.analyticsSession = null;
|
|
1287
|
+
this.currentScreen = null;
|
|
1288
|
+
if (this.opts && this.isWeb)
|
|
1289
|
+
return;
|
|
1290
|
+
void this.persistSuperProps();
|
|
1291
|
+
void this.persistGroups();
|
|
1292
|
+
void this.forgetAnalyticsSession();
|
|
1293
|
+
} catch {
|
|
1294
|
+
}
|
|
1295
|
+
}
|
|
1296
|
+
/**
|
|
1297
|
+
* An experiment exposure: the user SAW `variant` of `experiment` — what the console's
|
|
1298
|
+
* experiment analysis reads (uplift and significance per variant). Your app assigns
|
|
1299
|
+
* the variant (its own flags, a remote config, a hash); call this where the user
|
|
1300
|
+
* first meets it. Sends `$exposure { experiment, variant, ...props }`.
|
|
1301
|
+
*
|
|
1302
|
+
* Traqio.exposure("paywall_copy", "b");
|
|
1303
|
+
*/
|
|
1304
|
+
exposure(experiment, variant, props) {
|
|
1305
|
+
var _a;
|
|
1306
|
+
try {
|
|
1307
|
+
const p = (0, analytics_1.exposureProps)(experiment, variant, props);
|
|
1308
|
+
if (p)
|
|
1309
|
+
this.capture(analytics_1.EXPOSURE_EVENT, (_a = (0, analytics_1.cleanProps)(p)) != null ? _a : { experiment: p.experiment, variant: p.variant });
|
|
1310
|
+
} catch {
|
|
1311
|
+
}
|
|
1312
|
+
}
|
|
1187
1313
|
/**
|
|
1188
1314
|
* A screen view — the app's page view, read under « Top pages » and usable as a
|
|
1189
1315
|
* funnel step. Call it when a screen gets the focus, or let `trackScreens()` (React
|
|
@@ -1733,7 +1859,7 @@ var require_build = __commonJS({
|
|
|
1733
1859
|
if (this.erased)
|
|
1734
1860
|
return;
|
|
1735
1861
|
let e = event;
|
|
1736
|
-
if (
|
|
1862
|
+
if (!(0, analytics_1.isEntry)(event.kind)) {
|
|
1737
1863
|
const screen = (_b = (_a = event.screen) != null ? _a : this.currentScreen) != null ? _b : void 0;
|
|
1738
1864
|
const props = (0, analytics_1.withSuperProps)(event.props, this.superProps);
|
|
1739
1865
|
const groups = Object.keys(this.groups).length ? { ...this.groups } : void 0;
|
|
@@ -1749,12 +1875,12 @@ var require_build = __commonJS({
|
|
|
1749
1875
|
this.pushAnalytics(e);
|
|
1750
1876
|
}
|
|
1751
1877
|
pushAnalytics(e, schedule = true) {
|
|
1752
|
-
var _a, _b;
|
|
1753
|
-
if (
|
|
1878
|
+
var _a, _b, _c;
|
|
1879
|
+
if ((0, analytics_1.isEntry)(e.kind))
|
|
1754
1880
|
this.analyticsQueue.push({ ...e, sessionId: (_b = (_a = this.analyticsSession) == null ? void 0 : _a.id) != null ? _b : "" });
|
|
1755
1881
|
else {
|
|
1756
1882
|
this.analyticsSession = (0, analytics_1.touchSession)(this.analyticsSession, e.ts, uuid);
|
|
1757
|
-
this.analyticsQueue.push({ ...e, sessionId: this.analyticsSession.id });
|
|
1883
|
+
this.analyticsQueue.push({ ...e, id: (_c = e.id) != null ? _c : uuid(), sessionId: this.analyticsSession.id });
|
|
1758
1884
|
}
|
|
1759
1885
|
if (this.analyticsQueue.length > MAX_ANALYTICS_QUEUE)
|
|
1760
1886
|
this.analyticsQueue.splice(0, this.analyticsQueue.length - MAX_ANALYTICS_QUEUE);
|
|
@@ -1773,9 +1899,17 @@ var require_build = __commonJS({
|
|
|
1773
1899
|
} catch {
|
|
1774
1900
|
}
|
|
1775
1901
|
try {
|
|
1776
|
-
|
|
1902
|
+
if (!this.resetBeforeInit)
|
|
1903
|
+
(_c = this.analyticsSession) != null ? _c : this.analyticsSession = (0, analytics_1.parseSession)(await ((_b = this.opts.storage) == null ? void 0 : _b.getItem(ANALYTICS_SESSION_KEY)));
|
|
1777
1904
|
} catch {
|
|
1778
1905
|
}
|
|
1906
|
+
if (this.resetBeforeInit) {
|
|
1907
|
+
this.resetBeforeInit = false;
|
|
1908
|
+
await this.persistSuperProps();
|
|
1909
|
+
await this.persistGroups();
|
|
1910
|
+
await this.forgetAnalyticsSession();
|
|
1911
|
+
return;
|
|
1912
|
+
}
|
|
1779
1913
|
try {
|
|
1780
1914
|
const raw = await ((_d = this.opts.storage) == null ? void 0 : _d.getItem(SUPER_PROPS_KEY2));
|
|
1781
1915
|
const early = Object.keys(this.superProps).length > 0;
|
|
@@ -1795,6 +1929,16 @@ var require_build = __commonJS({
|
|
|
1795
1929
|
} catch {
|
|
1796
1930
|
}
|
|
1797
1931
|
}
|
|
1932
|
+
async forgetAnalyticsSession() {
|
|
1933
|
+
var _a;
|
|
1934
|
+
if (!this.opts)
|
|
1935
|
+
return;
|
|
1936
|
+
try {
|
|
1937
|
+
await ((_a = this.opts.storage) == null ? void 0 : _a.setItem(ANALYTICS_SESSION_KEY, ""));
|
|
1938
|
+
await this.persistAnalyticsQueue();
|
|
1939
|
+
} catch {
|
|
1940
|
+
}
|
|
1941
|
+
}
|
|
1798
1942
|
async persistGroups() {
|
|
1799
1943
|
var _a;
|
|
1800
1944
|
if (!this.opts)
|
|
@@ -2101,18 +2245,18 @@ var require_build = __commonJS({
|
|
|
2101
2245
|
this.retryMs = BASE_RETRY_MS;
|
|
2102
2246
|
}
|
|
2103
2247
|
while (this.analyticsQueue.length) {
|
|
2104
|
-
const batch = this.analyticsQueue.
|
|
2248
|
+
const batch = (0, analytics_1.nextAnalyticsBatch)(this.analyticsQueue, this.opts.userId, MAX_ANALYTICS_BATCH);
|
|
2105
2249
|
const outcome = await this.send({
|
|
2106
2250
|
anonymousId: (_b = this.context.deviceId) != null ? _b : await this.getOrCreateDeviceId(),
|
|
2107
|
-
userId:
|
|
2251
|
+
userId: batch.userId,
|
|
2108
2252
|
platform: this.hostPlatform,
|
|
2109
|
-
events: batch
|
|
2253
|
+
events: batch.events
|
|
2110
2254
|
}, ANALYTICS_PATH);
|
|
2111
2255
|
if (outcome === "retry")
|
|
2112
2256
|
return this.backOff();
|
|
2113
2257
|
if (outcome === "refused")
|
|
2114
|
-
console.warn(`[traqio] the server refused ${batch.
|
|
2115
|
-
this.analyticsQueue.splice(0, batch.
|
|
2258
|
+
console.warn(`[traqio] the server refused ${batch.taken} analytics event(s) as malformed \u2014 dropped, not retried`);
|
|
2259
|
+
this.analyticsQueue.splice(0, batch.taken);
|
|
2116
2260
|
await this.persistAnalyticsQueue();
|
|
2117
2261
|
this.retryMs = BASE_RETRY_MS;
|
|
2118
2262
|
}
|
|
@@ -2294,7 +2438,8 @@ function deleteCookie(name, domain) {
|
|
|
2294
2438
|
|
|
2295
2439
|
// src/storage.ts
|
|
2296
2440
|
var DEVICE_ID_KEY = "traq_device_id";
|
|
2297
|
-
var
|
|
2441
|
+
var INTERNAL_KEY = "traq_internal";
|
|
2442
|
+
var MIRRORED = /* @__PURE__ */ new Set([DEVICE_ID_KEY, INTERNAL_KEY]);
|
|
2298
2443
|
var MIRROR_MAX_AGE_SECONDS = 395 * 24 * 60 * 60;
|
|
2299
2444
|
function local() {
|
|
2300
2445
|
try {
|
|
@@ -2734,6 +2879,7 @@ function watchVitals(c) {
|
|
|
2734
2879
|
}
|
|
2735
2880
|
|
|
2736
2881
|
// src/analytics.ts
|
|
2882
|
+
var isEntry = (e) => e.kind === "identify" || e.kind === "group" || e.kind === "alias";
|
|
2737
2883
|
var GROUP_TYPE = /^[a-z][a-z0-9_]{0,29}$/;
|
|
2738
2884
|
var MAX_GROUP_TYPES = 5;
|
|
2739
2885
|
function cleanGroupType(v) {
|
|
@@ -2793,11 +2939,46 @@ function withSuperProps(own, sup) {
|
|
|
2793
2939
|
}
|
|
2794
2940
|
}
|
|
2795
2941
|
var SESSION_KEY = "traq_session";
|
|
2942
|
+
function cleanUserId(v) {
|
|
2943
|
+
if (typeof v === "number" && Number.isFinite(v)) return String(v);
|
|
2944
|
+
if (typeof v !== "string") return null;
|
|
2945
|
+
const t = v.trim();
|
|
2946
|
+
return t && t.length <= 256 ? t : null;
|
|
2947
|
+
}
|
|
2948
|
+
function exposureText(v) {
|
|
2949
|
+
const t = typeof v === "string" ? v.trim() : typeof v === "number" && Number.isFinite(v) || typeof v === "boolean" ? String(v) : "";
|
|
2950
|
+
return t && t.length <= 200 ? t : null;
|
|
2951
|
+
}
|
|
2952
|
+
function exposureProps(experiment, variant, props) {
|
|
2953
|
+
const e = exposureText(experiment);
|
|
2954
|
+
const v = exposureText(variant);
|
|
2955
|
+
if (!e || !v) return null;
|
|
2956
|
+
return { ...props && typeof props === "object" && !Array.isArray(props) ? props : {}, experiment: e, variant: v };
|
|
2957
|
+
}
|
|
2958
|
+
function takeBatch(queue, current, max) {
|
|
2959
|
+
let n = 0;
|
|
2960
|
+
let userId = current;
|
|
2961
|
+
let fixed = false;
|
|
2962
|
+
for (const e of queue) {
|
|
2963
|
+
if (n >= max) break;
|
|
2964
|
+
const entry = e.kind === "identify" || e.kind === "group" || e.kind === "alias";
|
|
2965
|
+
if (!entry) {
|
|
2966
|
+
const who = e.asUser !== void 0 ? e.asUser || void 0 : current;
|
|
2967
|
+
if (!fixed) {
|
|
2968
|
+
userId = who;
|
|
2969
|
+
fixed = true;
|
|
2970
|
+
} else if (who !== userId) break;
|
|
2971
|
+
}
|
|
2972
|
+
n++;
|
|
2973
|
+
}
|
|
2974
|
+
return { events: queue.splice(0, n), userId };
|
|
2975
|
+
}
|
|
2796
2976
|
var SESSION_IDLE_MS = 30 * 60 * 1e3;
|
|
2797
2977
|
var FLUSH_EVERY_MS = 5e3;
|
|
2798
2978
|
var FLUSH_AT = 20;
|
|
2799
2979
|
var MAX_QUEUE = 200;
|
|
2800
2980
|
var MAX_ERRORS_PER_PAGE = 10;
|
|
2981
|
+
var LINK_SCANNER_ERROR = /^Object Not Found Matching Id:\d+, MethodName:\w+, ParamCount:\d+$/;
|
|
2801
2982
|
var SCROLL_STEPS = [25, 50, 75, 100];
|
|
2802
2983
|
var UTM = [
|
|
2803
2984
|
["utm_source", "utmSource"],
|
|
@@ -2874,6 +3055,8 @@ var AnalyticsTracker = class {
|
|
|
2874
3055
|
this.scrollUrl = null;
|
|
2875
3056
|
this.errorsThisPage = 0;
|
|
2876
3057
|
this.stopped = false;
|
|
3058
|
+
/** This page is driven by a mail link scanner: every later batch says so (the server keeps it out of the figures). */
|
|
3059
|
+
this.scanner = false;
|
|
2877
3060
|
this.rage = new RageClickDetector();
|
|
2878
3061
|
this.vitals = null;
|
|
2879
3062
|
this.vitalsUrl = null;
|
|
@@ -2934,6 +3117,26 @@ var AnalyticsTracker = class {
|
|
|
2934
3117
|
if (this.queue.length > MAX_QUEUE) this.queue.shift();
|
|
2935
3118
|
if (this.queue.length >= FLUSH_AT) this.flush(false);
|
|
2936
3119
|
}
|
|
3120
|
+
/** Two ids, one person (`alias(previousId)`): one entry in the batch, under the current user. Not activity. */
|
|
3121
|
+
alias(userId, previousId) {
|
|
3122
|
+
if (this.stopped) return;
|
|
3123
|
+
this.queue.push({ kind: "alias", ts: Date.now(), userId, previousId });
|
|
3124
|
+
if (this.queue.length > MAX_QUEUE) this.queue.shift();
|
|
3125
|
+
if (this.queue.length >= FLUSH_AT) this.flush(false);
|
|
3126
|
+
}
|
|
3127
|
+
/**
|
|
3128
|
+
* Sign-out (`reset()`): what is queued keeps `userId`, the user it was captured
|
|
3129
|
+
* under, and the session ends — the next event opens a new one.
|
|
3130
|
+
*/
|
|
3131
|
+
reset(userId) {
|
|
3132
|
+
if (userId) {
|
|
3133
|
+
for (const e of this.queue) if (!isEntry(e) && e.asUser === void 0) e.asUser = userId;
|
|
3134
|
+
}
|
|
3135
|
+
try {
|
|
3136
|
+
this.deps.store.removeSync(SESSION_KEY);
|
|
3137
|
+
} catch {
|
|
3138
|
+
}
|
|
3139
|
+
}
|
|
2937
3140
|
/** Records a page view of the current URL. Called by itself on route changes; exposed for routers that need it. */
|
|
2938
3141
|
pageview() {
|
|
2939
3142
|
const url = bare2(location.href);
|
|
@@ -2949,6 +3152,7 @@ var AnalyticsTracker = class {
|
|
|
2949
3152
|
}
|
|
2950
3153
|
/** Sends what is queued. `leaving` = the page is going away: keepalive, so the request outlives it. */
|
|
2951
3154
|
flush(leaving) {
|
|
3155
|
+
var _a, _b;
|
|
2952
3156
|
if (!this.queue.length || this.stopped) return;
|
|
2953
3157
|
if (this.deps.isForgotten()) {
|
|
2954
3158
|
this.stop();
|
|
@@ -2956,13 +3160,17 @@ var AnalyticsTracker = class {
|
|
|
2956
3160
|
}
|
|
2957
3161
|
const anonymousId = this.deps.anonymousId();
|
|
2958
3162
|
if (!anonymousId) return;
|
|
2959
|
-
const
|
|
3163
|
+
const taken = takeBatch(this.queue, this.deps.userId(), 100);
|
|
3164
|
+
const userId = taken.userId;
|
|
3165
|
+
const events = userId ? taken.events.map((e) => isEntry(e) || e.asUser !== void 0 ? e : { ...e, asUser: userId }) : taken.events;
|
|
2960
3166
|
const body = JSON.stringify({
|
|
2961
3167
|
sdkKey: this.deps.sdkKey,
|
|
2962
3168
|
anonymousId,
|
|
2963
|
-
userId
|
|
3169
|
+
userId,
|
|
2964
3170
|
debug: this.deps.isDebug() || void 0,
|
|
2965
|
-
|
|
3171
|
+
internal: ((_b = (_a = this.deps).isInternal) == null ? void 0 : _b.call(_a)) || void 0,
|
|
3172
|
+
scanner: this.scanner || void 0,
|
|
3173
|
+
events: events.map((e) => isEntry(e) ? e : { ...e, asUser: void 0 })
|
|
2966
3174
|
});
|
|
2967
3175
|
try {
|
|
2968
3176
|
void fetch(`${this.deps.endpoint}/ingest/analytics`, {
|
|
@@ -3017,7 +3225,7 @@ var AnalyticsTracker = class {
|
|
|
3017
3225
|
const props = e.kind === "scroll" || e.kind === "vitals" ? e.props : withSuperProps(e.props, (_c = (_b = (_a = this.deps).superProps) == null ? void 0 : _b.call(_a)) != null ? _c : {});
|
|
3018
3226
|
const g = (_f = (_e = (_d = this.deps).groups) == null ? void 0 : _e.call(_d)) != null ? _f : {};
|
|
3019
3227
|
const groups = Object.keys(g).length ? { ...g } : void 0;
|
|
3020
|
-
this.queue.push({ ...e, ...props ? { props } : {}, ...groups ? { groups } : {}, ts: Date.now(), url: (_h = (_g = e.url) != null ? _g : bare2(location.href)) != null ? _h : "", title, sessionId: s.id, ...s.src ? { src: s.src } : {} });
|
|
3228
|
+
this.queue.push({ ...e, ...props ? { props } : {}, ...groups ? { groups } : {}, id: newId(), ts: Date.now(), url: (_h = (_g = e.url) != null ? _g : bare2(location.href)) != null ? _h : "", title, sessionId: s.id, ...s.src ? { src: s.src } : {} });
|
|
3021
3229
|
if (this.queue.length > MAX_QUEUE) this.queue.shift();
|
|
3022
3230
|
if (this.queue.length >= FLUSH_AT) this.flush(false);
|
|
3023
3231
|
}
|
|
@@ -3101,6 +3309,7 @@ var AnalyticsTracker = class {
|
|
|
3101
3309
|
onError(e) {
|
|
3102
3310
|
if (this.errorsThisPage >= MAX_ERRORS_PER_PAGE) return;
|
|
3103
3311
|
const message = (e.message || (e.error instanceof Error ? e.error.message : "") || "Script error").slice(0, 300);
|
|
3312
|
+
if (LINK_SCANNER_ERROR.test(message.replace(/^Uncaught (?:Error: )?/, ""))) this.scanner = true;
|
|
3104
3313
|
this.errorsThisPage++;
|
|
3105
3314
|
const stack = e.error instanceof Error && typeof e.error.stack === "string" ? e.error.stack.slice(0, 1500) : void 0;
|
|
3106
3315
|
this.enqueue({ kind: "error", props: { message, ...stack ? { stack } : {}, ...e.filename ? { source: bare2(e.filename) } : {}, ...e.lineno ? { line: e.lineno } : {} } });
|
|
@@ -3109,6 +3318,7 @@ var AnalyticsTracker = class {
|
|
|
3109
3318
|
if (this.errorsThisPage >= MAX_ERRORS_PER_PAGE) return;
|
|
3110
3319
|
const reason = e.reason;
|
|
3111
3320
|
const message = (reason instanceof Error ? reason.message : typeof reason === "string" ? reason : "Unhandled promise rejection").slice(0, 300);
|
|
3321
|
+
if (LINK_SCANNER_ERROR.test(message)) this.scanner = true;
|
|
3112
3322
|
this.errorsThisPage++;
|
|
3113
3323
|
const stack = reason instanceof Error && typeof reason.stack === "string" ? reason.stack.slice(0, 1500) : void 0;
|
|
3114
3324
|
this.enqueue({ kind: "error", props: { message, unhandledRejection: true, ...stack ? { stack } : {} } });
|
|
@@ -3131,6 +3341,15 @@ function randomId() {
|
|
|
3131
3341
|
return null;
|
|
3132
3342
|
}
|
|
3133
3343
|
}
|
|
3344
|
+
function internalParam(href) {
|
|
3345
|
+
try {
|
|
3346
|
+
const v = new URL(href).searchParams.get("traqio_internal");
|
|
3347
|
+
if (v === "1" || v === "true") return true;
|
|
3348
|
+
if (v === "0" || v === "false") return false;
|
|
3349
|
+
} catch {
|
|
3350
|
+
}
|
|
3351
|
+
return null;
|
|
3352
|
+
}
|
|
3134
3353
|
var TraqioWeb = class {
|
|
3135
3354
|
constructor() {
|
|
3136
3355
|
this.core = new import_core.Client();
|
|
@@ -3152,6 +3371,10 @@ var TraqioWeb = class {
|
|
|
3152
3371
|
this.groups = {};
|
|
3153
3372
|
/** Group entries made before `init()` — replayed once the tracker exists. */
|
|
3154
3373
|
this.earlyGroups = [];
|
|
3374
|
+
/** `alias()` calls made before `init()`: [userId, previousId]. */
|
|
3375
|
+
this.earlyAliases = [];
|
|
3376
|
+
/** A `reset()` before `init()`: what earlier pages kept (super-properties, accounts) is dropped, not restored. */
|
|
3377
|
+
this.resetBeforeInit = false;
|
|
3155
3378
|
}
|
|
3156
3379
|
/**
|
|
3157
3380
|
* Call once, as early as possible on every page (or once in a single-page app).
|
|
@@ -3175,11 +3398,17 @@ var TraqioWeb = class {
|
|
|
3175
3398
|
this.trackIds.capture(location.href);
|
|
3176
3399
|
this.sources = new SourceKeeper(this.store, keepMs, cookieDomain, cookies);
|
|
3177
3400
|
this.sources.capture(location.href, document.referrer);
|
|
3401
|
+
const optOut = internalParam(location.href);
|
|
3402
|
+
if (optOut !== null) this.setInternal(optOut);
|
|
3178
3403
|
if (!this.store.getSync(DEVICE_ID_KEY)) {
|
|
3179
3404
|
const id = randomId();
|
|
3180
3405
|
if (id) this.store.setSync(DEVICE_ID_KEY, id);
|
|
3181
3406
|
}
|
|
3182
3407
|
this.userId = (_c = opts.userId) != null ? _c : this.userId;
|
|
3408
|
+
if (this.resetBeforeInit) {
|
|
3409
|
+
this.store.removeSync(SUPER_PROPS_KEY);
|
|
3410
|
+
this.store.removeSync(GROUPS_KEY);
|
|
3411
|
+
}
|
|
3183
3412
|
try {
|
|
3184
3413
|
const raw = this.store.getSync(SUPER_PROPS_KEY);
|
|
3185
3414
|
const early = Object.keys(this.superProps).length > 0;
|
|
@@ -3220,18 +3449,45 @@ var TraqioWeb = class {
|
|
|
3220
3449
|
groups: () => this.groups,
|
|
3221
3450
|
isDebug: () => this.core.isDebug,
|
|
3222
3451
|
isForgotten: () => this.core.forgotten,
|
|
3452
|
+
isInternal: () => this.isInternal,
|
|
3223
3453
|
options: opts.analytics === true ? {} : opts.analytics
|
|
3224
3454
|
});
|
|
3225
3455
|
this.analytics.start();
|
|
3226
3456
|
for (const [id, set, once] of this.earlyIdentifies) this.analytics.identify(id, set, once);
|
|
3227
3457
|
for (const [type, key, set, once] of this.earlyGroups) this.analytics.group(type, key, set, once);
|
|
3458
|
+
for (const [user, prev] of this.earlyAliases) this.analytics.alias(user, prev);
|
|
3228
3459
|
for (const [name, props] of this.earlyCaptures) this.analytics.capture(name, props);
|
|
3229
3460
|
}
|
|
3230
3461
|
this.earlyCaptures = [];
|
|
3231
3462
|
this.earlyIdentifies = [];
|
|
3232
3463
|
this.earlyGroups = [];
|
|
3464
|
+
this.earlyAliases = [];
|
|
3233
3465
|
this.ready = true;
|
|
3234
3466
|
}
|
|
3467
|
+
/**
|
|
3468
|
+
* Mark this browser as the team's own (`true`) or as a visitor again (`false`) — what
|
|
3469
|
+
* opening the site with `?traqio_internal=1` / `=0` does. Its analytics events are
|
|
3470
|
+
* still sent, flagged: the console leaves them out of every figure by default and
|
|
3471
|
+
* shows them under « Internal traffic only ». Conversions (`track()`) are not
|
|
3472
|
+
* affected. Kept on the device, like the visitor id (and under the same consent).
|
|
3473
|
+
*/
|
|
3474
|
+
setInternal(on) {
|
|
3475
|
+
if (!this.store) return;
|
|
3476
|
+
try {
|
|
3477
|
+
if (on) this.store.setSync(INTERNAL_KEY, "1");
|
|
3478
|
+
else this.store.removeSync(INTERNAL_KEY);
|
|
3479
|
+
} catch {
|
|
3480
|
+
}
|
|
3481
|
+
}
|
|
3482
|
+
/** True when this browser opted itself out as internal traffic — see `setInternal()`. */
|
|
3483
|
+
get isInternal() {
|
|
3484
|
+
var _a;
|
|
3485
|
+
try {
|
|
3486
|
+
return ((_a = this.store) == null ? void 0 : _a.getSync(INTERNAL_KEY)) === "1";
|
|
3487
|
+
} catch {
|
|
3488
|
+
return false;
|
|
3489
|
+
}
|
|
3490
|
+
}
|
|
3235
3491
|
/** Whether this page runs in debug mode — see `WebInitOptions.debug`. Settled by `init()`. */
|
|
3236
3492
|
get isDebug() {
|
|
3237
3493
|
return this.core.isDebug;
|
|
@@ -3340,6 +3596,67 @@ var TraqioWeb = class {
|
|
|
3340
3596
|
getGroups() {
|
|
3341
3597
|
return { ...this.groups };
|
|
3342
3598
|
}
|
|
3599
|
+
/**
|
|
3600
|
+
* Two ids, one person: the user known so far as `previousId` (a guest id, an id
|
|
3601
|
+
* from before sign-up) IS the current user (`setUserId` / `identify`). Their
|
|
3602
|
+
* analytics history and properties are merged under the current id — its own
|
|
3603
|
+
* properties win. From the browser only what THIS visitor sent under `previousId`
|
|
3604
|
+
* is merged; a backend (`@traqio/node`, secret key) merges everything.
|
|
3605
|
+
*
|
|
3606
|
+
* Traqio.identify("u_123");
|
|
3607
|
+
* Traqio.alias("guest_8f2c");
|
|
3608
|
+
*
|
|
3609
|
+
* Ignored without a current user id, or when `previousId` is the current one. Needs `analytics` at init.
|
|
3610
|
+
*/
|
|
3611
|
+
alias(previousId) {
|
|
3612
|
+
const prev = cleanUserId(previousId);
|
|
3613
|
+
const user = this.userId;
|
|
3614
|
+
if (!user || !prev || prev === user) return;
|
|
3615
|
+
if (this.analytics) this.analytics.alias(user, prev);
|
|
3616
|
+
else if (!this.ready && this.earlyAliases.length < 10) this.earlyAliases.push([user, prev]);
|
|
3617
|
+
}
|
|
3618
|
+
/**
|
|
3619
|
+
* Sign-out: forget who the user was in this browser — the user id, the
|
|
3620
|
+
* super-properties, the accounts — and end the analytics session, so the next
|
|
3621
|
+
* person's visit starts clean. Events already queued keep the user they were
|
|
3622
|
+
* captured under. The visitor id is KEPT: it carries the visitor's attribution
|
|
3623
|
+
* (the click, the campaign) for the next sign-up.
|
|
3624
|
+
*/
|
|
3625
|
+
reset() {
|
|
3626
|
+
var _a;
|
|
3627
|
+
const user = this.userId;
|
|
3628
|
+
(_a = this.analytics) == null ? void 0 : _a.reset(user);
|
|
3629
|
+
this.userId = void 0;
|
|
3630
|
+
this.core.reset();
|
|
3631
|
+
this.superProps = {};
|
|
3632
|
+
this.groups = {};
|
|
3633
|
+
this.earlyIdentifies = [];
|
|
3634
|
+
this.earlyGroups = [];
|
|
3635
|
+
this.earlyAliases = [];
|
|
3636
|
+
if (this.store) {
|
|
3637
|
+
this.persistSuperProps();
|
|
3638
|
+
this.persistGroups();
|
|
3639
|
+
} else this.resetBeforeInit = true;
|
|
3640
|
+
}
|
|
3641
|
+
/**
|
|
3642
|
+
* An experiment exposure: the visitor SAW `variant` of `experiment` — what the
|
|
3643
|
+
* console's experiment analysis reads (uplift, significance per variant). Your site
|
|
3644
|
+
* assigns the variant (its own flags, an edge split, a cookie); call this where the
|
|
3645
|
+
* visitor first meets it. Sends `$exposure { experiment, variant, ...props }`.
|
|
3646
|
+
*
|
|
3647
|
+
* Traqio.exposure("pricing_page", "annual_first");
|
|
3648
|
+
*/
|
|
3649
|
+
exposure(experiment, variant, props) {
|
|
3650
|
+
const p = exposureProps(experiment, variant, props);
|
|
3651
|
+
if (!p) return;
|
|
3652
|
+
let fits = false;
|
|
3653
|
+
try {
|
|
3654
|
+
fits = JSON.stringify(p).length <= 4096;
|
|
3655
|
+
} catch {
|
|
3656
|
+
fits = false;
|
|
3657
|
+
}
|
|
3658
|
+
this.capture("$exposure", fits ? p : { experiment: p.experiment, variant: p.variant });
|
|
3659
|
+
}
|
|
3343
3660
|
persistGroups() {
|
|
3344
3661
|
try {
|
|
3345
3662
|
if (!this.store) return;
|
|
@@ -3430,6 +3747,7 @@ var Traqio = new TraqioWeb();
|
|
|
3430
3747
|
export {
|
|
3431
3748
|
DEFAULT_ENDPOINT,
|
|
3432
3749
|
Traqio,
|
|
3433
|
-
TraqioWeb
|
|
3750
|
+
TraqioWeb,
|
|
3751
|
+
internalParam
|
|
3434
3752
|
};
|
|
3435
3753
|
//# sourceMappingURL=index.js.map
|