@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/README.md
CHANGED
|
@@ -130,6 +130,12 @@ Traqio.capture("search_performed", { results: 12 }); // an analytics event, for
|
|
|
130
130
|
defaults to `0.5` (half the loads); `webVitals: { sampleRate: 1 }` measures every load,
|
|
131
131
|
`webVitals: false` none.
|
|
132
132
|
- URLs are stored **without their query string**: tokens and emails live there.
|
|
133
|
+
- **Your own visits** — open your site once with `?traqio_internal=1` at the end of the
|
|
134
|
+
address: from then on that browser's analytics events are flagged as internal traffic
|
|
135
|
+
(on every page and subdomain), left out of the console's figures by default and shown
|
|
136
|
+
under « Internal traffic only ». `?traqio_internal=0` undoes it;
|
|
137
|
+
`Traqio.setInternal(true | false)` does the same from code, `Traqio.isInternal` reads it.
|
|
138
|
+
Conversions (`track()`) are not affected.
|
|
133
139
|
- **User properties** — `Traqio.identify(user.id, { plan: "pro", seats: 12 }, { signup_source: "webinar" })`
|
|
134
140
|
sets the user id and the user's properties: the second object overwrites, the third is
|
|
135
141
|
written only where the user has no value yet. Read them as a breakdown (`user:plan`), a
|
|
@@ -154,7 +160,7 @@ your accounts with their properties. Needs `analytics` at init.
|
|
|
154
160
|
```ts
|
|
155
161
|
Traqio.group("company", account.id, { plan: "enterprise", seats: 40 }); // traits optional
|
|
156
162
|
Traqio.getGroups(); // { company: "acc_42" }
|
|
157
|
-
Traqio.resetGroups(); // on sign-out
|
|
163
|
+
Traqio.resetGroups(); // an account switch; on sign-out, reset() clears them too
|
|
158
164
|
```
|
|
159
165
|
|
|
160
166
|
The type is lowercase (`company`, `workspace`, `team`: a letter, then letters, digits or
|
|
@@ -163,6 +169,23 @@ characters — never an email. Traits follow the identify rules (a 4th argument
|
|
|
163
169
|
what is unset). The same key again with no traits sends nothing new, so calling it on
|
|
164
170
|
every page is free. `forgetMe()` forgets the groups too; an invalid call is ignored.
|
|
165
171
|
|
|
172
|
+
## Alias, sign-out and experiments
|
|
173
|
+
|
|
174
|
+
```ts
|
|
175
|
+
Traqio.identify("u_123");
|
|
176
|
+
Traqio.alias("guest_8f2c"); // the guest id IS u_123: history and properties merged
|
|
177
|
+
Traqio.reset(); // on sign-out
|
|
178
|
+
Traqio.exposure("paywall_copy", "b"); // $exposure { experiment, variant }
|
|
179
|
+
```
|
|
180
|
+
|
|
181
|
+
`alias(previousId)` merges what THIS browser sent under `previousId` (a guest id, an id
|
|
182
|
+
from before sign-up) into the current user — the current user's properties win; ignored
|
|
183
|
+
without a current user id. A backend (`@traqio/node`, secret key) merges everything.
|
|
184
|
+
`reset()` forgets the user id, the super-properties and the groups and ends the analytics
|
|
185
|
+
session; events already queued keep their user, and the visitor id is kept (it carries the
|
|
186
|
+
attribution). `exposure(experiment, variant, props?)` sends `$exposure`, what the console's
|
|
187
|
+
experiment analysis reads — your site assigns the variant itself.
|
|
188
|
+
|
|
166
189
|
## Frameworks
|
|
167
190
|
|
|
168
191
|
- **React / Next.js** — call `init()` in a `useEffect` of a client component mounted
|
package/build/analytics.d.ts
CHANGED
|
@@ -67,6 +67,7 @@ export interface AnalyticsDeps {
|
|
|
67
67
|
store: KeyValueStore & {
|
|
68
68
|
getSync(key: string): string | null;
|
|
69
69
|
setSync(key: string, value: string): void;
|
|
70
|
+
removeSync(key: string): void;
|
|
70
71
|
};
|
|
71
72
|
endpoint: string;
|
|
72
73
|
sdkKey: string;
|
|
@@ -78,9 +79,27 @@ export interface AnalyticsDeps {
|
|
|
78
79
|
superProps?: () => Traits;
|
|
79
80
|
isDebug: () => boolean;
|
|
80
81
|
isForgotten: () => boolean;
|
|
82
|
+
/** The browser opted itself out as the team's own (`?traqio_internal=1`): batches say so. */
|
|
83
|
+
isInternal?: () => boolean;
|
|
81
84
|
options: AnalyticsOptions;
|
|
82
85
|
}
|
|
83
86
|
export declare const SESSION_KEY = "traq_session";
|
|
87
|
+
/** A user id as the server takes it (alias.ts): trimmed text (or a number) of 1–256 characters, or null. */
|
|
88
|
+
export declare function cleanUserId(v: unknown): string | null;
|
|
89
|
+
/** `exposure()`'s properties — `{ ...props, experiment, variant }`, the two named ones winning — or null to ignore the call. */
|
|
90
|
+
export declare function exposureProps(experiment: unknown, variant: unknown, props?: Record<string, unknown>): Record<string, unknown> | null;
|
|
91
|
+
/**
|
|
92
|
+
* Takes (splices) the next batch off `queue`: the leading events captured under ONE
|
|
93
|
+
* user (`asUser`, else `current`) — entries ride with any batch — and that user. The
|
|
94
|
+
* events keep their stamp, so a requeued batch still knows its user; the request drops it.
|
|
95
|
+
*/
|
|
96
|
+
export declare function takeBatch<T extends {
|
|
97
|
+
kind: string;
|
|
98
|
+
asUser?: string;
|
|
99
|
+
}>(queue: T[], current: string | undefined, max: number): {
|
|
100
|
+
events: T[];
|
|
101
|
+
userId: string | undefined;
|
|
102
|
+
};
|
|
84
103
|
/** 30 minutes without activity end a session — the convention PostHog and GA share. */
|
|
85
104
|
export declare const SESSION_IDLE_MS: number;
|
|
86
105
|
/** The landing's source: the URL's utm_* and click ids, and the referrer when it is another site. */
|
|
@@ -108,6 +127,8 @@ export declare class AnalyticsTracker {
|
|
|
108
127
|
private scrollUrl;
|
|
109
128
|
private errorsThisPage;
|
|
110
129
|
private stopped;
|
|
130
|
+
/** This page is driven by a mail link scanner: every later batch says so (the server keeps it out of the figures). */
|
|
131
|
+
private scanner;
|
|
111
132
|
private readonly rage;
|
|
112
133
|
private vitals;
|
|
113
134
|
private vitalsUrl;
|
|
@@ -123,6 +144,13 @@ export declare class AnalyticsTracker {
|
|
|
123
144
|
identify(userId: string, set: Traits, setOnce: Traits): void;
|
|
124
145
|
/** An account's properties (`group(type, key, traits)`): one entry in the batch. Not activity either. */
|
|
125
146
|
group(groupType: string, groupKey: string, set: Traits, setOnce: Traits): void;
|
|
147
|
+
/** Two ids, one person (`alias(previousId)`): one entry in the batch, under the current user. Not activity. */
|
|
148
|
+
alias(userId: string, previousId: string): void;
|
|
149
|
+
/**
|
|
150
|
+
* Sign-out (`reset()`): what is queued keeps `userId`, the user it was captured
|
|
151
|
+
* under, and the session ends — the next event opens a new one.
|
|
152
|
+
*/
|
|
153
|
+
reset(userId: string | undefined): void;
|
|
126
154
|
/** Records a page view of the current URL. Called by itself on route changes; exposed for routers that need it. */
|
|
127
155
|
pageview(): void;
|
|
128
156
|
/** Sends what is queued. `leaving` = the page is going away: keepalive, so the request outlives it. */
|
package/build/index.cjs
CHANGED
|
@@ -236,6 +236,10 @@ var require_analytics = __commonJS({
|
|
|
236
236
|
"use strict";
|
|
237
237
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
238
238
|
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;
|
|
239
|
+
exports.cleanUserId = cleanUserId2;
|
|
240
|
+
exports.isEntry = isEntry2;
|
|
241
|
+
exports.nextAnalyticsBatch = nextAnalyticsBatch;
|
|
242
|
+
exports.exposureProps = exposureProps2;
|
|
239
243
|
exports.cleanGroupType = cleanGroupType2;
|
|
240
244
|
exports.cleanGroupKey = cleanGroupKey2;
|
|
241
245
|
exports.cleanGroups = cleanGroups2;
|
|
@@ -247,6 +251,49 @@ var require_analytics = __commonJS({
|
|
|
247
251
|
exports.cleanProps = cleanProps;
|
|
248
252
|
exports.touchSession = touchSession;
|
|
249
253
|
exports.parseSession = parseSession;
|
|
254
|
+
function cleanUserId2(v) {
|
|
255
|
+
if (typeof v === "number" && Number.isFinite(v))
|
|
256
|
+
return String(v);
|
|
257
|
+
if (typeof v !== "string")
|
|
258
|
+
return null;
|
|
259
|
+
const t = v.trim();
|
|
260
|
+
return t && t.length <= 256 ? t : null;
|
|
261
|
+
}
|
|
262
|
+
function isEntry2(kind) {
|
|
263
|
+
return kind === "identify" || kind === "group" || kind === "alias";
|
|
264
|
+
}
|
|
265
|
+
function nextAnalyticsBatch(queue, current, max) {
|
|
266
|
+
let userId;
|
|
267
|
+
let fixed = false;
|
|
268
|
+
const events = [];
|
|
269
|
+
for (const e of queue) {
|
|
270
|
+
if (events.length >= max)
|
|
271
|
+
break;
|
|
272
|
+
if (!isEntry2(e.kind)) {
|
|
273
|
+
const who = e.asUser !== void 0 ? e.asUser || void 0 : current;
|
|
274
|
+
if (!fixed) {
|
|
275
|
+
userId = who;
|
|
276
|
+
fixed = true;
|
|
277
|
+
} else if (who !== userId)
|
|
278
|
+
break;
|
|
279
|
+
}
|
|
280
|
+
const { asUser: _asUser, ...wire } = e;
|
|
281
|
+
events.push(wire);
|
|
282
|
+
}
|
|
283
|
+
return { events, userId: fixed ? userId : current, taken: events.length };
|
|
284
|
+
}
|
|
285
|
+
var MAX_EXPOSURE_TEXT = 200;
|
|
286
|
+
function exposureText2(v) {
|
|
287
|
+
const t = typeof v === "string" ? v.trim() : typeof v === "number" && Number.isFinite(v) || typeof v === "boolean" ? String(v) : "";
|
|
288
|
+
return t && t.length <= MAX_EXPOSURE_TEXT ? t : null;
|
|
289
|
+
}
|
|
290
|
+
function exposureProps2(experiment, variant, props) {
|
|
291
|
+
const e = exposureText2(experiment);
|
|
292
|
+
const v = exposureText2(variant);
|
|
293
|
+
if (!e || !v)
|
|
294
|
+
return null;
|
|
295
|
+
return { ...props && typeof props === "object" && !Array.isArray(props) ? props : {}, experiment: e, variant: v };
|
|
296
|
+
}
|
|
250
297
|
var GROUP_TYPE2 = /^[a-z][a-z0-9_]{0,29}$/;
|
|
251
298
|
exports.MAX_GROUP_TYPES = 5;
|
|
252
299
|
var MAX_GROUP_KEY = 256;
|
|
@@ -777,7 +824,7 @@ var require_build = __commonJS({
|
|
|
777
824
|
var adservices_1 = require_adservices();
|
|
778
825
|
var deeplink_1 = require_deeplink();
|
|
779
826
|
var adRevenue_1 = require_adRevenue();
|
|
780
|
-
exports.SDK_VERSION = "0.
|
|
827
|
+
exports.SDK_VERSION = "0.9.0";
|
|
781
828
|
var skan_2 = require_skan();
|
|
782
829
|
Object.defineProperty(exports, "Skan", { enumerable: true, get: function() {
|
|
783
830
|
return skan_2.Skan;
|
|
@@ -975,6 +1022,7 @@ var require_build = __commonJS({
|
|
|
975
1022
|
this.currentScreen = null;
|
|
976
1023
|
this.superProps = {};
|
|
977
1024
|
this.groups = {};
|
|
1025
|
+
this.resetBeforeInit = false;
|
|
978
1026
|
this.maxRevenuePaid = (adInfo) => {
|
|
979
1027
|
try {
|
|
980
1028
|
const max = (0, adRevenue_1.fromMaxAdInfo)(adInfo);
|
|
@@ -1023,7 +1071,10 @@ var require_build = __commonJS({
|
|
|
1023
1071
|
this.wireDeepLinks();
|
|
1024
1072
|
}
|
|
1025
1073
|
}
|
|
1026
|
-
|
|
1074
|
+
if (this.isWeb)
|
|
1075
|
+
void this.maybeStartSession(false);
|
|
1076
|
+
else
|
|
1077
|
+
await this.maybeStartSession(true);
|
|
1027
1078
|
await this.initSkan();
|
|
1028
1079
|
await this.initAppleAds();
|
|
1029
1080
|
this.wireLifecycle();
|
|
@@ -1189,6 +1240,81 @@ var require_build = __commonJS({
|
|
|
1189
1240
|
getGroups() {
|
|
1190
1241
|
return { ...this.groups };
|
|
1191
1242
|
}
|
|
1243
|
+
/**
|
|
1244
|
+
* Two ids, one person: the user known so far as `previousId` (a guest id, an id
|
|
1245
|
+
* from before sign-up, an older system's) IS the current user (`setUserId` /
|
|
1246
|
+
* `identify`). Their analytics history and properties are merged under the current
|
|
1247
|
+
* id — the current id's own properties win. From an app, only what THIS device sent
|
|
1248
|
+
* under `previousId` is merged; a backend (secret key) merges everything.
|
|
1249
|
+
*
|
|
1250
|
+
* Traqio.identify("u_123");
|
|
1251
|
+
* Traqio.alias("guest_8f2c");
|
|
1252
|
+
*
|
|
1253
|
+
* Ignored without a current user id, or when `previousId` is the current one.
|
|
1254
|
+
*/
|
|
1255
|
+
alias(previousId) {
|
|
1256
|
+
try {
|
|
1257
|
+
const userId = this.opts ? this.opts.userId : this.earlyUserId;
|
|
1258
|
+
const prev = (0, analytics_1.cleanUserId)(previousId);
|
|
1259
|
+
if (!userId || !prev || prev === userId)
|
|
1260
|
+
return;
|
|
1261
|
+
this.queueAnalytics({ kind: "alias", ts: Date.now(), userId, previousId: prev });
|
|
1262
|
+
} catch {
|
|
1263
|
+
}
|
|
1264
|
+
}
|
|
1265
|
+
/**
|
|
1266
|
+
* Sign-out: forget who the user was on this device — the user id, the
|
|
1267
|
+
* super-properties (`register`), the accounts (`group`) — and end the analytics
|
|
1268
|
+
* session, so the next person's visit starts clean. Events already queued keep the
|
|
1269
|
+
* user they were captured under. The device id is KEPT: it is the install's
|
|
1270
|
+
* attribution identity, and a new one would read as a new install.
|
|
1271
|
+
*/
|
|
1272
|
+
reset() {
|
|
1273
|
+
try {
|
|
1274
|
+
const userId = this.opts ? this.opts.userId : this.earlyUserId;
|
|
1275
|
+
if (userId) {
|
|
1276
|
+
for (const e of this.analyticsQueue)
|
|
1277
|
+
if (!(0, analytics_1.isEntry)(e.kind) && e.asUser === void 0)
|
|
1278
|
+
e.asUser = userId;
|
|
1279
|
+
for (const e of this.earlyAnalytics)
|
|
1280
|
+
if (!(0, analytics_1.isEntry)(e.kind) && e.asUser === void 0)
|
|
1281
|
+
e.asUser = userId;
|
|
1282
|
+
}
|
|
1283
|
+
if (this.opts)
|
|
1284
|
+
this.opts.userId = void 0;
|
|
1285
|
+
else {
|
|
1286
|
+
this.earlyUserId = void 0;
|
|
1287
|
+
this.resetBeforeInit = true;
|
|
1288
|
+
}
|
|
1289
|
+
this.superProps = {};
|
|
1290
|
+
this.groups = {};
|
|
1291
|
+
this.analyticsSession = null;
|
|
1292
|
+
this.currentScreen = null;
|
|
1293
|
+
if (this.opts && this.isWeb)
|
|
1294
|
+
return;
|
|
1295
|
+
void this.persistSuperProps();
|
|
1296
|
+
void this.persistGroups();
|
|
1297
|
+
void this.forgetAnalyticsSession();
|
|
1298
|
+
} catch {
|
|
1299
|
+
}
|
|
1300
|
+
}
|
|
1301
|
+
/**
|
|
1302
|
+
* An experiment exposure: the user SAW `variant` of `experiment` — what the console's
|
|
1303
|
+
* experiment analysis reads (uplift and significance per variant). Your app assigns
|
|
1304
|
+
* the variant (its own flags, a remote config, a hash); call this where the user
|
|
1305
|
+
* first meets it. Sends `$exposure { experiment, variant, ...props }`.
|
|
1306
|
+
*
|
|
1307
|
+
* Traqio.exposure("paywall_copy", "b");
|
|
1308
|
+
*/
|
|
1309
|
+
exposure(experiment, variant, props) {
|
|
1310
|
+
var _a;
|
|
1311
|
+
try {
|
|
1312
|
+
const p = (0, analytics_1.exposureProps)(experiment, variant, props);
|
|
1313
|
+
if (p)
|
|
1314
|
+
this.capture(analytics_1.EXPOSURE_EVENT, (_a = (0, analytics_1.cleanProps)(p)) != null ? _a : { experiment: p.experiment, variant: p.variant });
|
|
1315
|
+
} catch {
|
|
1316
|
+
}
|
|
1317
|
+
}
|
|
1192
1318
|
/**
|
|
1193
1319
|
* A screen view — the app's page view, read under « Top pages » and usable as a
|
|
1194
1320
|
* funnel step. Call it when a screen gets the focus, or let `trackScreens()` (React
|
|
@@ -1738,7 +1864,7 @@ var require_build = __commonJS({
|
|
|
1738
1864
|
if (this.erased)
|
|
1739
1865
|
return;
|
|
1740
1866
|
let e = event;
|
|
1741
|
-
if (
|
|
1867
|
+
if (!(0, analytics_1.isEntry)(event.kind)) {
|
|
1742
1868
|
const screen = (_b = (_a = event.screen) != null ? _a : this.currentScreen) != null ? _b : void 0;
|
|
1743
1869
|
const props = (0, analytics_1.withSuperProps)(event.props, this.superProps);
|
|
1744
1870
|
const groups = Object.keys(this.groups).length ? { ...this.groups } : void 0;
|
|
@@ -1754,12 +1880,12 @@ var require_build = __commonJS({
|
|
|
1754
1880
|
this.pushAnalytics(e);
|
|
1755
1881
|
}
|
|
1756
1882
|
pushAnalytics(e, schedule = true) {
|
|
1757
|
-
var _a, _b;
|
|
1758
|
-
if (
|
|
1883
|
+
var _a, _b, _c;
|
|
1884
|
+
if ((0, analytics_1.isEntry)(e.kind))
|
|
1759
1885
|
this.analyticsQueue.push({ ...e, sessionId: (_b = (_a = this.analyticsSession) == null ? void 0 : _a.id) != null ? _b : "" });
|
|
1760
1886
|
else {
|
|
1761
1887
|
this.analyticsSession = (0, analytics_1.touchSession)(this.analyticsSession, e.ts, uuid);
|
|
1762
|
-
this.analyticsQueue.push({ ...e, sessionId: this.analyticsSession.id });
|
|
1888
|
+
this.analyticsQueue.push({ ...e, id: (_c = e.id) != null ? _c : uuid(), sessionId: this.analyticsSession.id });
|
|
1763
1889
|
}
|
|
1764
1890
|
if (this.analyticsQueue.length > MAX_ANALYTICS_QUEUE)
|
|
1765
1891
|
this.analyticsQueue.splice(0, this.analyticsQueue.length - MAX_ANALYTICS_QUEUE);
|
|
@@ -1778,9 +1904,17 @@ var require_build = __commonJS({
|
|
|
1778
1904
|
} catch {
|
|
1779
1905
|
}
|
|
1780
1906
|
try {
|
|
1781
|
-
|
|
1907
|
+
if (!this.resetBeforeInit)
|
|
1908
|
+
(_c = this.analyticsSession) != null ? _c : this.analyticsSession = (0, analytics_1.parseSession)(await ((_b = this.opts.storage) == null ? void 0 : _b.getItem(ANALYTICS_SESSION_KEY)));
|
|
1782
1909
|
} catch {
|
|
1783
1910
|
}
|
|
1911
|
+
if (this.resetBeforeInit) {
|
|
1912
|
+
this.resetBeforeInit = false;
|
|
1913
|
+
await this.persistSuperProps();
|
|
1914
|
+
await this.persistGroups();
|
|
1915
|
+
await this.forgetAnalyticsSession();
|
|
1916
|
+
return;
|
|
1917
|
+
}
|
|
1784
1918
|
try {
|
|
1785
1919
|
const raw = await ((_d = this.opts.storage) == null ? void 0 : _d.getItem(SUPER_PROPS_KEY2));
|
|
1786
1920
|
const early = Object.keys(this.superProps).length > 0;
|
|
@@ -1800,6 +1934,16 @@ var require_build = __commonJS({
|
|
|
1800
1934
|
} catch {
|
|
1801
1935
|
}
|
|
1802
1936
|
}
|
|
1937
|
+
async forgetAnalyticsSession() {
|
|
1938
|
+
var _a;
|
|
1939
|
+
if (!this.opts)
|
|
1940
|
+
return;
|
|
1941
|
+
try {
|
|
1942
|
+
await ((_a = this.opts.storage) == null ? void 0 : _a.setItem(ANALYTICS_SESSION_KEY, ""));
|
|
1943
|
+
await this.persistAnalyticsQueue();
|
|
1944
|
+
} catch {
|
|
1945
|
+
}
|
|
1946
|
+
}
|
|
1803
1947
|
async persistGroups() {
|
|
1804
1948
|
var _a;
|
|
1805
1949
|
if (!this.opts)
|
|
@@ -2106,18 +2250,18 @@ var require_build = __commonJS({
|
|
|
2106
2250
|
this.retryMs = BASE_RETRY_MS;
|
|
2107
2251
|
}
|
|
2108
2252
|
while (this.analyticsQueue.length) {
|
|
2109
|
-
const batch = this.analyticsQueue.
|
|
2253
|
+
const batch = (0, analytics_1.nextAnalyticsBatch)(this.analyticsQueue, this.opts.userId, MAX_ANALYTICS_BATCH);
|
|
2110
2254
|
const outcome = await this.send({
|
|
2111
2255
|
anonymousId: (_b = this.context.deviceId) != null ? _b : await this.getOrCreateDeviceId(),
|
|
2112
|
-
userId:
|
|
2256
|
+
userId: batch.userId,
|
|
2113
2257
|
platform: this.hostPlatform,
|
|
2114
|
-
events: batch
|
|
2258
|
+
events: batch.events
|
|
2115
2259
|
}, ANALYTICS_PATH);
|
|
2116
2260
|
if (outcome === "retry")
|
|
2117
2261
|
return this.backOff();
|
|
2118
2262
|
if (outcome === "refused")
|
|
2119
|
-
console.warn(`[traqio] the server refused ${batch.
|
|
2120
|
-
this.analyticsQueue.splice(0, batch.
|
|
2263
|
+
console.warn(`[traqio] the server refused ${batch.taken} analytics event(s) as malformed \u2014 dropped, not retried`);
|
|
2264
|
+
this.analyticsQueue.splice(0, batch.taken);
|
|
2121
2265
|
await this.persistAnalyticsQueue();
|
|
2122
2266
|
this.retryMs = BASE_RETRY_MS;
|
|
2123
2267
|
}
|
|
@@ -2246,7 +2390,8 @@ var index_exports = {};
|
|
|
2246
2390
|
__export(index_exports, {
|
|
2247
2391
|
DEFAULT_ENDPOINT: () => DEFAULT_ENDPOINT,
|
|
2248
2392
|
Traqio: () => Traqio,
|
|
2249
|
-
TraqioWeb: () => TraqioWeb
|
|
2393
|
+
TraqioWeb: () => TraqioWeb,
|
|
2394
|
+
internalParam: () => internalParam
|
|
2250
2395
|
});
|
|
2251
2396
|
module.exports = __toCommonJS(index_exports);
|
|
2252
2397
|
var import_core = __toESM(require_build(), 1);
|
|
@@ -2306,7 +2451,8 @@ function deleteCookie(name, domain) {
|
|
|
2306
2451
|
|
|
2307
2452
|
// src/storage.ts
|
|
2308
2453
|
var DEVICE_ID_KEY = "traq_device_id";
|
|
2309
|
-
var
|
|
2454
|
+
var INTERNAL_KEY = "traq_internal";
|
|
2455
|
+
var MIRRORED = /* @__PURE__ */ new Set([DEVICE_ID_KEY, INTERNAL_KEY]);
|
|
2310
2456
|
var MIRROR_MAX_AGE_SECONDS = 395 * 24 * 60 * 60;
|
|
2311
2457
|
function local() {
|
|
2312
2458
|
try {
|
|
@@ -2746,6 +2892,7 @@ function watchVitals(c) {
|
|
|
2746
2892
|
}
|
|
2747
2893
|
|
|
2748
2894
|
// src/analytics.ts
|
|
2895
|
+
var isEntry = (e) => e.kind === "identify" || e.kind === "group" || e.kind === "alias";
|
|
2749
2896
|
var GROUP_TYPE = /^[a-z][a-z0-9_]{0,29}$/;
|
|
2750
2897
|
var MAX_GROUP_TYPES = 5;
|
|
2751
2898
|
function cleanGroupType(v) {
|
|
@@ -2805,11 +2952,46 @@ function withSuperProps(own, sup) {
|
|
|
2805
2952
|
}
|
|
2806
2953
|
}
|
|
2807
2954
|
var SESSION_KEY = "traq_session";
|
|
2955
|
+
function cleanUserId(v) {
|
|
2956
|
+
if (typeof v === "number" && Number.isFinite(v)) return String(v);
|
|
2957
|
+
if (typeof v !== "string") return null;
|
|
2958
|
+
const t = v.trim();
|
|
2959
|
+
return t && t.length <= 256 ? t : null;
|
|
2960
|
+
}
|
|
2961
|
+
function exposureText(v) {
|
|
2962
|
+
const t = typeof v === "string" ? v.trim() : typeof v === "number" && Number.isFinite(v) || typeof v === "boolean" ? String(v) : "";
|
|
2963
|
+
return t && t.length <= 200 ? t : null;
|
|
2964
|
+
}
|
|
2965
|
+
function exposureProps(experiment, variant, props) {
|
|
2966
|
+
const e = exposureText(experiment);
|
|
2967
|
+
const v = exposureText(variant);
|
|
2968
|
+
if (!e || !v) return null;
|
|
2969
|
+
return { ...props && typeof props === "object" && !Array.isArray(props) ? props : {}, experiment: e, variant: v };
|
|
2970
|
+
}
|
|
2971
|
+
function takeBatch(queue, current, max) {
|
|
2972
|
+
let n = 0;
|
|
2973
|
+
let userId = current;
|
|
2974
|
+
let fixed = false;
|
|
2975
|
+
for (const e of queue) {
|
|
2976
|
+
if (n >= max) break;
|
|
2977
|
+
const entry = e.kind === "identify" || e.kind === "group" || e.kind === "alias";
|
|
2978
|
+
if (!entry) {
|
|
2979
|
+
const who = e.asUser !== void 0 ? e.asUser || void 0 : current;
|
|
2980
|
+
if (!fixed) {
|
|
2981
|
+
userId = who;
|
|
2982
|
+
fixed = true;
|
|
2983
|
+
} else if (who !== userId) break;
|
|
2984
|
+
}
|
|
2985
|
+
n++;
|
|
2986
|
+
}
|
|
2987
|
+
return { events: queue.splice(0, n), userId };
|
|
2988
|
+
}
|
|
2808
2989
|
var SESSION_IDLE_MS = 30 * 60 * 1e3;
|
|
2809
2990
|
var FLUSH_EVERY_MS = 5e3;
|
|
2810
2991
|
var FLUSH_AT = 20;
|
|
2811
2992
|
var MAX_QUEUE = 200;
|
|
2812
2993
|
var MAX_ERRORS_PER_PAGE = 10;
|
|
2994
|
+
var LINK_SCANNER_ERROR = /^Object Not Found Matching Id:\d+, MethodName:\w+, ParamCount:\d+$/;
|
|
2813
2995
|
var SCROLL_STEPS = [25, 50, 75, 100];
|
|
2814
2996
|
var UTM = [
|
|
2815
2997
|
["utm_source", "utmSource"],
|
|
@@ -2886,6 +3068,8 @@ var AnalyticsTracker = class {
|
|
|
2886
3068
|
this.scrollUrl = null;
|
|
2887
3069
|
this.errorsThisPage = 0;
|
|
2888
3070
|
this.stopped = false;
|
|
3071
|
+
/** This page is driven by a mail link scanner: every later batch says so (the server keeps it out of the figures). */
|
|
3072
|
+
this.scanner = false;
|
|
2889
3073
|
this.rage = new RageClickDetector();
|
|
2890
3074
|
this.vitals = null;
|
|
2891
3075
|
this.vitalsUrl = null;
|
|
@@ -2946,6 +3130,26 @@ var AnalyticsTracker = class {
|
|
|
2946
3130
|
if (this.queue.length > MAX_QUEUE) this.queue.shift();
|
|
2947
3131
|
if (this.queue.length >= FLUSH_AT) this.flush(false);
|
|
2948
3132
|
}
|
|
3133
|
+
/** Two ids, one person (`alias(previousId)`): one entry in the batch, under the current user. Not activity. */
|
|
3134
|
+
alias(userId, previousId) {
|
|
3135
|
+
if (this.stopped) return;
|
|
3136
|
+
this.queue.push({ kind: "alias", ts: Date.now(), userId, previousId });
|
|
3137
|
+
if (this.queue.length > MAX_QUEUE) this.queue.shift();
|
|
3138
|
+
if (this.queue.length >= FLUSH_AT) this.flush(false);
|
|
3139
|
+
}
|
|
3140
|
+
/**
|
|
3141
|
+
* Sign-out (`reset()`): what is queued keeps `userId`, the user it was captured
|
|
3142
|
+
* under, and the session ends — the next event opens a new one.
|
|
3143
|
+
*/
|
|
3144
|
+
reset(userId) {
|
|
3145
|
+
if (userId) {
|
|
3146
|
+
for (const e of this.queue) if (!isEntry(e) && e.asUser === void 0) e.asUser = userId;
|
|
3147
|
+
}
|
|
3148
|
+
try {
|
|
3149
|
+
this.deps.store.removeSync(SESSION_KEY);
|
|
3150
|
+
} catch {
|
|
3151
|
+
}
|
|
3152
|
+
}
|
|
2949
3153
|
/** Records a page view of the current URL. Called by itself on route changes; exposed for routers that need it. */
|
|
2950
3154
|
pageview() {
|
|
2951
3155
|
const url = bare2(location.href);
|
|
@@ -2961,6 +3165,7 @@ var AnalyticsTracker = class {
|
|
|
2961
3165
|
}
|
|
2962
3166
|
/** Sends what is queued. `leaving` = the page is going away: keepalive, so the request outlives it. */
|
|
2963
3167
|
flush(leaving) {
|
|
3168
|
+
var _a, _b;
|
|
2964
3169
|
if (!this.queue.length || this.stopped) return;
|
|
2965
3170
|
if (this.deps.isForgotten()) {
|
|
2966
3171
|
this.stop();
|
|
@@ -2968,13 +3173,17 @@ var AnalyticsTracker = class {
|
|
|
2968
3173
|
}
|
|
2969
3174
|
const anonymousId = this.deps.anonymousId();
|
|
2970
3175
|
if (!anonymousId) return;
|
|
2971
|
-
const
|
|
3176
|
+
const taken = takeBatch(this.queue, this.deps.userId(), 100);
|
|
3177
|
+
const userId = taken.userId;
|
|
3178
|
+
const events = userId ? taken.events.map((e) => isEntry(e) || e.asUser !== void 0 ? e : { ...e, asUser: userId }) : taken.events;
|
|
2972
3179
|
const body = JSON.stringify({
|
|
2973
3180
|
sdkKey: this.deps.sdkKey,
|
|
2974
3181
|
anonymousId,
|
|
2975
|
-
userId
|
|
3182
|
+
userId,
|
|
2976
3183
|
debug: this.deps.isDebug() || void 0,
|
|
2977
|
-
|
|
3184
|
+
internal: ((_b = (_a = this.deps).isInternal) == null ? void 0 : _b.call(_a)) || void 0,
|
|
3185
|
+
scanner: this.scanner || void 0,
|
|
3186
|
+
events: events.map((e) => isEntry(e) ? e : { ...e, asUser: void 0 })
|
|
2978
3187
|
});
|
|
2979
3188
|
try {
|
|
2980
3189
|
void fetch(`${this.deps.endpoint}/ingest/analytics`, {
|
|
@@ -3029,7 +3238,7 @@ var AnalyticsTracker = class {
|
|
|
3029
3238
|
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 : {});
|
|
3030
3239
|
const g = (_f = (_e = (_d = this.deps).groups) == null ? void 0 : _e.call(_d)) != null ? _f : {};
|
|
3031
3240
|
const groups = Object.keys(g).length ? { ...g } : void 0;
|
|
3032
|
-
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 } : {} });
|
|
3241
|
+
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 } : {} });
|
|
3033
3242
|
if (this.queue.length > MAX_QUEUE) this.queue.shift();
|
|
3034
3243
|
if (this.queue.length >= FLUSH_AT) this.flush(false);
|
|
3035
3244
|
}
|
|
@@ -3113,6 +3322,7 @@ var AnalyticsTracker = class {
|
|
|
3113
3322
|
onError(e) {
|
|
3114
3323
|
if (this.errorsThisPage >= MAX_ERRORS_PER_PAGE) return;
|
|
3115
3324
|
const message = (e.message || (e.error instanceof Error ? e.error.message : "") || "Script error").slice(0, 300);
|
|
3325
|
+
if (LINK_SCANNER_ERROR.test(message.replace(/^Uncaught (?:Error: )?/, ""))) this.scanner = true;
|
|
3116
3326
|
this.errorsThisPage++;
|
|
3117
3327
|
const stack = e.error instanceof Error && typeof e.error.stack === "string" ? e.error.stack.slice(0, 1500) : void 0;
|
|
3118
3328
|
this.enqueue({ kind: "error", props: { message, ...stack ? { stack } : {}, ...e.filename ? { source: bare2(e.filename) } : {}, ...e.lineno ? { line: e.lineno } : {} } });
|
|
@@ -3121,6 +3331,7 @@ var AnalyticsTracker = class {
|
|
|
3121
3331
|
if (this.errorsThisPage >= MAX_ERRORS_PER_PAGE) return;
|
|
3122
3332
|
const reason = e.reason;
|
|
3123
3333
|
const message = (reason instanceof Error ? reason.message : typeof reason === "string" ? reason : "Unhandled promise rejection").slice(0, 300);
|
|
3334
|
+
if (LINK_SCANNER_ERROR.test(message)) this.scanner = true;
|
|
3124
3335
|
this.errorsThisPage++;
|
|
3125
3336
|
const stack = reason instanceof Error && typeof reason.stack === "string" ? reason.stack.slice(0, 1500) : void 0;
|
|
3126
3337
|
this.enqueue({ kind: "error", props: { message, unhandledRejection: true, ...stack ? { stack } : {} } });
|
|
@@ -3143,6 +3354,15 @@ function randomId() {
|
|
|
3143
3354
|
return null;
|
|
3144
3355
|
}
|
|
3145
3356
|
}
|
|
3357
|
+
function internalParam(href) {
|
|
3358
|
+
try {
|
|
3359
|
+
const v = new URL(href).searchParams.get("traqio_internal");
|
|
3360
|
+
if (v === "1" || v === "true") return true;
|
|
3361
|
+
if (v === "0" || v === "false") return false;
|
|
3362
|
+
} catch {
|
|
3363
|
+
}
|
|
3364
|
+
return null;
|
|
3365
|
+
}
|
|
3146
3366
|
var TraqioWeb = class {
|
|
3147
3367
|
constructor() {
|
|
3148
3368
|
this.core = new import_core.Client();
|
|
@@ -3164,6 +3384,10 @@ var TraqioWeb = class {
|
|
|
3164
3384
|
this.groups = {};
|
|
3165
3385
|
/** Group entries made before `init()` — replayed once the tracker exists. */
|
|
3166
3386
|
this.earlyGroups = [];
|
|
3387
|
+
/** `alias()` calls made before `init()`: [userId, previousId]. */
|
|
3388
|
+
this.earlyAliases = [];
|
|
3389
|
+
/** A `reset()` before `init()`: what earlier pages kept (super-properties, accounts) is dropped, not restored. */
|
|
3390
|
+
this.resetBeforeInit = false;
|
|
3167
3391
|
}
|
|
3168
3392
|
/**
|
|
3169
3393
|
* Call once, as early as possible on every page (or once in a single-page app).
|
|
@@ -3187,11 +3411,17 @@ var TraqioWeb = class {
|
|
|
3187
3411
|
this.trackIds.capture(location.href);
|
|
3188
3412
|
this.sources = new SourceKeeper(this.store, keepMs, cookieDomain, cookies);
|
|
3189
3413
|
this.sources.capture(location.href, document.referrer);
|
|
3414
|
+
const optOut = internalParam(location.href);
|
|
3415
|
+
if (optOut !== null) this.setInternal(optOut);
|
|
3190
3416
|
if (!this.store.getSync(DEVICE_ID_KEY)) {
|
|
3191
3417
|
const id = randomId();
|
|
3192
3418
|
if (id) this.store.setSync(DEVICE_ID_KEY, id);
|
|
3193
3419
|
}
|
|
3194
3420
|
this.userId = (_c = opts.userId) != null ? _c : this.userId;
|
|
3421
|
+
if (this.resetBeforeInit) {
|
|
3422
|
+
this.store.removeSync(SUPER_PROPS_KEY);
|
|
3423
|
+
this.store.removeSync(GROUPS_KEY);
|
|
3424
|
+
}
|
|
3195
3425
|
try {
|
|
3196
3426
|
const raw = this.store.getSync(SUPER_PROPS_KEY);
|
|
3197
3427
|
const early = Object.keys(this.superProps).length > 0;
|
|
@@ -3232,18 +3462,45 @@ var TraqioWeb = class {
|
|
|
3232
3462
|
groups: () => this.groups,
|
|
3233
3463
|
isDebug: () => this.core.isDebug,
|
|
3234
3464
|
isForgotten: () => this.core.forgotten,
|
|
3465
|
+
isInternal: () => this.isInternal,
|
|
3235
3466
|
options: opts.analytics === true ? {} : opts.analytics
|
|
3236
3467
|
});
|
|
3237
3468
|
this.analytics.start();
|
|
3238
3469
|
for (const [id, set, once] of this.earlyIdentifies) this.analytics.identify(id, set, once);
|
|
3239
3470
|
for (const [type, key, set, once] of this.earlyGroups) this.analytics.group(type, key, set, once);
|
|
3471
|
+
for (const [user, prev] of this.earlyAliases) this.analytics.alias(user, prev);
|
|
3240
3472
|
for (const [name, props] of this.earlyCaptures) this.analytics.capture(name, props);
|
|
3241
3473
|
}
|
|
3242
3474
|
this.earlyCaptures = [];
|
|
3243
3475
|
this.earlyIdentifies = [];
|
|
3244
3476
|
this.earlyGroups = [];
|
|
3477
|
+
this.earlyAliases = [];
|
|
3245
3478
|
this.ready = true;
|
|
3246
3479
|
}
|
|
3480
|
+
/**
|
|
3481
|
+
* Mark this browser as the team's own (`true`) or as a visitor again (`false`) — what
|
|
3482
|
+
* opening the site with `?traqio_internal=1` / `=0` does. Its analytics events are
|
|
3483
|
+
* still sent, flagged: the console leaves them out of every figure by default and
|
|
3484
|
+
* shows them under « Internal traffic only ». Conversions (`track()`) are not
|
|
3485
|
+
* affected. Kept on the device, like the visitor id (and under the same consent).
|
|
3486
|
+
*/
|
|
3487
|
+
setInternal(on) {
|
|
3488
|
+
if (!this.store) return;
|
|
3489
|
+
try {
|
|
3490
|
+
if (on) this.store.setSync(INTERNAL_KEY, "1");
|
|
3491
|
+
else this.store.removeSync(INTERNAL_KEY);
|
|
3492
|
+
} catch {
|
|
3493
|
+
}
|
|
3494
|
+
}
|
|
3495
|
+
/** True when this browser opted itself out as internal traffic — see `setInternal()`. */
|
|
3496
|
+
get isInternal() {
|
|
3497
|
+
var _a;
|
|
3498
|
+
try {
|
|
3499
|
+
return ((_a = this.store) == null ? void 0 : _a.getSync(INTERNAL_KEY)) === "1";
|
|
3500
|
+
} catch {
|
|
3501
|
+
return false;
|
|
3502
|
+
}
|
|
3503
|
+
}
|
|
3247
3504
|
/** Whether this page runs in debug mode — see `WebInitOptions.debug`. Settled by `init()`. */
|
|
3248
3505
|
get isDebug() {
|
|
3249
3506
|
return this.core.isDebug;
|
|
@@ -3352,6 +3609,67 @@ var TraqioWeb = class {
|
|
|
3352
3609
|
getGroups() {
|
|
3353
3610
|
return { ...this.groups };
|
|
3354
3611
|
}
|
|
3612
|
+
/**
|
|
3613
|
+
* Two ids, one person: the user known so far as `previousId` (a guest id, an id
|
|
3614
|
+
* from before sign-up) IS the current user (`setUserId` / `identify`). Their
|
|
3615
|
+
* analytics history and properties are merged under the current id — its own
|
|
3616
|
+
* properties win. From the browser only what THIS visitor sent under `previousId`
|
|
3617
|
+
* is merged; a backend (`@traqio/node`, secret key) merges everything.
|
|
3618
|
+
*
|
|
3619
|
+
* Traqio.identify("u_123");
|
|
3620
|
+
* Traqio.alias("guest_8f2c");
|
|
3621
|
+
*
|
|
3622
|
+
* Ignored without a current user id, or when `previousId` is the current one. Needs `analytics` at init.
|
|
3623
|
+
*/
|
|
3624
|
+
alias(previousId) {
|
|
3625
|
+
const prev = cleanUserId(previousId);
|
|
3626
|
+
const user = this.userId;
|
|
3627
|
+
if (!user || !prev || prev === user) return;
|
|
3628
|
+
if (this.analytics) this.analytics.alias(user, prev);
|
|
3629
|
+
else if (!this.ready && this.earlyAliases.length < 10) this.earlyAliases.push([user, prev]);
|
|
3630
|
+
}
|
|
3631
|
+
/**
|
|
3632
|
+
* Sign-out: forget who the user was in this browser — the user id, the
|
|
3633
|
+
* super-properties, the accounts — and end the analytics session, so the next
|
|
3634
|
+
* person's visit starts clean. Events already queued keep the user they were
|
|
3635
|
+
* captured under. The visitor id is KEPT: it carries the visitor's attribution
|
|
3636
|
+
* (the click, the campaign) for the next sign-up.
|
|
3637
|
+
*/
|
|
3638
|
+
reset() {
|
|
3639
|
+
var _a;
|
|
3640
|
+
const user = this.userId;
|
|
3641
|
+
(_a = this.analytics) == null ? void 0 : _a.reset(user);
|
|
3642
|
+
this.userId = void 0;
|
|
3643
|
+
this.core.reset();
|
|
3644
|
+
this.superProps = {};
|
|
3645
|
+
this.groups = {};
|
|
3646
|
+
this.earlyIdentifies = [];
|
|
3647
|
+
this.earlyGroups = [];
|
|
3648
|
+
this.earlyAliases = [];
|
|
3649
|
+
if (this.store) {
|
|
3650
|
+
this.persistSuperProps();
|
|
3651
|
+
this.persistGroups();
|
|
3652
|
+
} else this.resetBeforeInit = true;
|
|
3653
|
+
}
|
|
3654
|
+
/**
|
|
3655
|
+
* An experiment exposure: the visitor SAW `variant` of `experiment` — what the
|
|
3656
|
+
* console's experiment analysis reads (uplift, significance per variant). Your site
|
|
3657
|
+
* assigns the variant (its own flags, an edge split, a cookie); call this where the
|
|
3658
|
+
* visitor first meets it. Sends `$exposure { experiment, variant, ...props }`.
|
|
3659
|
+
*
|
|
3660
|
+
* Traqio.exposure("pricing_page", "annual_first");
|
|
3661
|
+
*/
|
|
3662
|
+
exposure(experiment, variant, props) {
|
|
3663
|
+
const p = exposureProps(experiment, variant, props);
|
|
3664
|
+
if (!p) return;
|
|
3665
|
+
let fits = false;
|
|
3666
|
+
try {
|
|
3667
|
+
fits = JSON.stringify(p).length <= 4096;
|
|
3668
|
+
} catch {
|
|
3669
|
+
fits = false;
|
|
3670
|
+
}
|
|
3671
|
+
this.capture("$exposure", fits ? p : { experiment: p.experiment, variant: p.variant });
|
|
3672
|
+
}
|
|
3355
3673
|
persistGroups() {
|
|
3356
3674
|
try {
|
|
3357
3675
|
if (!this.store) return;
|