@traqio/web 0.4.1 → 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 +63 -3
- package/build/analytics.d.ts +102 -0
- package/build/index.cjs +990 -31
- package/build/index.cjs.map +4 -4
- package/build/index.d.ts +111 -7
- package/build/index.js +990 -31
- package/build/index.js.map +4 -4
- package/build/storage.d.ts +6 -0
- package/build/traqio.min.js +1 -1
- package/build/traqio.min.js.map +4 -4
- package/build/vitals.d.ts +58 -0
- package/package.json +2 -2
package/build/index.cjs
CHANGED
|
@@ -235,21 +235,143 @@ var require_analytics = __commonJS({
|
|
|
235
235
|
"../sdk-core/build/analytics.js"(exports) {
|
|
236
236
|
"use strict";
|
|
237
237
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
238
|
-
exports.MAX_PROPS_BYTES = exports.MAX_SCREEN_NAME = exports.MAX_EVENT_NAME = exports.ANALYTICS_SESSION_IDLE_MS = void 0;
|
|
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;
|
|
243
|
+
exports.cleanGroupType = cleanGroupType2;
|
|
244
|
+
exports.cleanGroupKey = cleanGroupKey2;
|
|
245
|
+
exports.cleanGroups = cleanGroups2;
|
|
246
|
+
exports.withGroup = withGroup2;
|
|
247
|
+
exports.cleanTraits = cleanTraits2;
|
|
248
|
+
exports.withSuperProps = withSuperProps2;
|
|
239
249
|
exports.cleanEventName = cleanEventName;
|
|
240
250
|
exports.cleanScreenName = cleanScreenName;
|
|
241
251
|
exports.cleanProps = cleanProps;
|
|
242
252
|
exports.touchSession = touchSession;
|
|
243
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
|
+
}
|
|
297
|
+
var GROUP_TYPE2 = /^[a-z][a-z0-9_]{0,29}$/;
|
|
298
|
+
exports.MAX_GROUP_TYPES = 5;
|
|
299
|
+
var MAX_GROUP_KEY = 256;
|
|
300
|
+
function cleanGroupType2(v) {
|
|
301
|
+
if (typeof v !== "string")
|
|
302
|
+
return null;
|
|
303
|
+
const t = v.trim();
|
|
304
|
+
return GROUP_TYPE2.test(t) ? t : null;
|
|
305
|
+
}
|
|
306
|
+
function cleanGroupKey2(v) {
|
|
307
|
+
if (typeof v === "number" && Number.isFinite(v))
|
|
308
|
+
return String(v);
|
|
309
|
+
if (typeof v !== "string")
|
|
310
|
+
return null;
|
|
311
|
+
const t = v.trim();
|
|
312
|
+
return t && t.length <= MAX_GROUP_KEY ? t : null;
|
|
313
|
+
}
|
|
314
|
+
function cleanGroups2(v) {
|
|
315
|
+
const out = {};
|
|
316
|
+
if (typeof v !== "object" || v === null || Array.isArray(v))
|
|
317
|
+
return out;
|
|
318
|
+
for (const [t, k] of Object.entries(v)) {
|
|
319
|
+
if (Object.keys(out).length >= exports.MAX_GROUP_TYPES)
|
|
320
|
+
break;
|
|
321
|
+
const type = cleanGroupType2(t);
|
|
322
|
+
const key = cleanGroupKey2(k);
|
|
323
|
+
if (type && key)
|
|
324
|
+
out[type] = key;
|
|
325
|
+
}
|
|
326
|
+
return out;
|
|
327
|
+
}
|
|
328
|
+
function withGroup2(groups, type, key) {
|
|
329
|
+
const t = cleanGroupType2(type);
|
|
330
|
+
const k = cleanGroupKey2(key);
|
|
331
|
+
if (!t || !k)
|
|
332
|
+
return null;
|
|
333
|
+
if (!(t in groups) && Object.keys(groups).length >= exports.MAX_GROUP_TYPES)
|
|
334
|
+
return null;
|
|
335
|
+
return { groups: { ...groups, [t]: k }, type: t, key: k, changed: groups[t] !== k };
|
|
336
|
+
}
|
|
337
|
+
var TRAIT_KEY2 = /^[A-Za-z0-9_][A-Za-z0-9_.-]{0,63}$/;
|
|
338
|
+
exports.MAX_TRAITS = 50;
|
|
339
|
+
var MAX_TRAIT_TEXT = 200;
|
|
340
|
+
function cleanTraits2(v) {
|
|
341
|
+
const out = {};
|
|
342
|
+
if (typeof v !== "object" || v === null || Array.isArray(v))
|
|
343
|
+
return out;
|
|
344
|
+
for (const [k, raw] of Object.entries(v)) {
|
|
345
|
+
if (Object.keys(out).length >= exports.MAX_TRAITS)
|
|
346
|
+
break;
|
|
347
|
+
if (!TRAIT_KEY2.test(k))
|
|
348
|
+
continue;
|
|
349
|
+
if (typeof raw === "string") {
|
|
350
|
+
const t = raw.trim();
|
|
351
|
+
if (t)
|
|
352
|
+
out[k] = t.slice(0, MAX_TRAIT_TEXT);
|
|
353
|
+
} else if (typeof raw === "number" && Number.isFinite(raw) || typeof raw === "boolean") {
|
|
354
|
+
out[k] = raw;
|
|
355
|
+
}
|
|
356
|
+
}
|
|
357
|
+
return out;
|
|
358
|
+
}
|
|
359
|
+
function withSuperProps2(own, sup) {
|
|
360
|
+
var _a;
|
|
361
|
+
if (!Object.keys(sup).length)
|
|
362
|
+
return own;
|
|
363
|
+
return (_a = cleanProps({ ...sup, ...own != null ? own : {} })) != null ? _a : own;
|
|
364
|
+
}
|
|
244
365
|
exports.ANALYTICS_SESSION_IDLE_MS = 30 * 60 * 1e3;
|
|
245
366
|
exports.MAX_EVENT_NAME = 128;
|
|
246
367
|
exports.MAX_SCREEN_NAME = 512;
|
|
247
368
|
exports.MAX_PROPS_BYTES = 4096;
|
|
369
|
+
exports.EXPOSURE_EVENT = "$exposure";
|
|
248
370
|
function cleanEventName(name) {
|
|
249
371
|
if (typeof name !== "string")
|
|
250
372
|
return null;
|
|
251
373
|
const n = name.trim();
|
|
252
|
-
if (!n || n.length > exports.MAX_EVENT_NAME || n.startsWith("$"))
|
|
374
|
+
if (!n || n.length > exports.MAX_EVENT_NAME || n.startsWith("$") && n !== exports.EXPOSURE_EVENT)
|
|
253
375
|
return null;
|
|
254
376
|
return n;
|
|
255
377
|
}
|
|
@@ -702,7 +824,7 @@ var require_build = __commonJS({
|
|
|
702
824
|
var adservices_1 = require_adservices();
|
|
703
825
|
var deeplink_1 = require_deeplink();
|
|
704
826
|
var adRevenue_1 = require_adRevenue();
|
|
705
|
-
exports.SDK_VERSION = "0.
|
|
827
|
+
exports.SDK_VERSION = "0.9.0";
|
|
706
828
|
var skan_2 = require_skan();
|
|
707
829
|
Object.defineProperty(exports, "Skan", { enumerable: true, get: function() {
|
|
708
830
|
return skan_2.Skan;
|
|
@@ -777,6 +899,8 @@ var require_build = __commonJS({
|
|
|
777
899
|
var CONSENT_KEY = "traq_consent";
|
|
778
900
|
var ANALYTICS_QUEUE_KEY = "traq_analytics_queue";
|
|
779
901
|
var ANALYTICS_SESSION_KEY = "traq_analytics_session";
|
|
902
|
+
var SUPER_PROPS_KEY2 = "traq_super_props";
|
|
903
|
+
var GROUPS_KEY2 = "traq_groups";
|
|
780
904
|
var ANALYTICS_PATH = "/ingest/analytics";
|
|
781
905
|
var MAX_ANALYTICS_BATCH = 50;
|
|
782
906
|
var MAX_ANALYTICS_QUEUE = 1e3;
|
|
@@ -896,6 +1020,9 @@ var require_build = __commonJS({
|
|
|
896
1020
|
this.analyticsSession = null;
|
|
897
1021
|
this.earlyAnalytics = [];
|
|
898
1022
|
this.currentScreen = null;
|
|
1023
|
+
this.superProps = {};
|
|
1024
|
+
this.groups = {};
|
|
1025
|
+
this.resetBeforeInit = false;
|
|
899
1026
|
this.maxRevenuePaid = (adInfo) => {
|
|
900
1027
|
try {
|
|
901
1028
|
const max = (0, adRevenue_1.fromMaxAdInfo)(adInfo);
|
|
@@ -911,11 +1038,11 @@ var require_build = __commonJS({
|
|
|
911
1038
|
return this.debug;
|
|
912
1039
|
}
|
|
913
1040
|
async init(opts) {
|
|
914
|
-
var _a;
|
|
1041
|
+
var _a, _b;
|
|
915
1042
|
if (this.started)
|
|
916
1043
|
return;
|
|
917
1044
|
this.started = true;
|
|
918
|
-
this.opts = { collectContext: true, ...opts,
|
|
1045
|
+
this.opts = { collectContext: true, ...opts, userId: (_a = opts.userId) != null ? _a : this.earlyUserId, storage: (_b = opts.storage) != null ? _b : defaultStorage() };
|
|
919
1046
|
if (!this.opts.storage) {
|
|
920
1047
|
console.warn("[traqio] no storage available \u2014 pass `storage` to init() or install @react-native-async-storage/async-storage, otherwise the install is re-sent at every launch.");
|
|
921
1048
|
}
|
|
@@ -944,7 +1071,10 @@ var require_build = __commonJS({
|
|
|
944
1071
|
this.wireDeepLinks();
|
|
945
1072
|
}
|
|
946
1073
|
}
|
|
947
|
-
|
|
1074
|
+
if (this.isWeb)
|
|
1075
|
+
void this.maybeStartSession(false);
|
|
1076
|
+
else
|
|
1077
|
+
await this.maybeStartSession(true);
|
|
948
1078
|
await this.initSkan();
|
|
949
1079
|
await this.initAppleAds();
|
|
950
1080
|
this.wireLifecycle();
|
|
@@ -997,7 +1127,7 @@ var require_build = __commonJS({
|
|
|
997
1127
|
* NOT a conversion: never billed, never sent to an ad network (that is `track()`).
|
|
998
1128
|
*
|
|
999
1129
|
* Batched, persisted while offline and retried like every other call; it never
|
|
1000
|
-
* throws. Names starting with `$` are reserved; properties must survive JSON and
|
|
1130
|
+
* throws. Names starting with `$` are reserved (but `$exposure { experiment, variant }`, an experiment exposure); properties must survive JSON and
|
|
1001
1131
|
* stay under 4 KB, else they are dropped (the event is kept). Callable before
|
|
1002
1132
|
* `init()`: the first 100 calls wait for it.
|
|
1003
1133
|
*
|
|
@@ -1013,6 +1143,178 @@ var require_build = __commonJS({
|
|
|
1013
1143
|
} catch {
|
|
1014
1144
|
}
|
|
1015
1145
|
}
|
|
1146
|
+
/**
|
|
1147
|
+
* Who the user is, and what they are: sets the user id (like `setUserId`) and, with
|
|
1148
|
+
* traits, the user's PROPERTIES — `plan`, `role`, `company_size` — read in the
|
|
1149
|
+
* console as a breakdown, a report dimension and a cohort condition.
|
|
1150
|
+
*
|
|
1151
|
+
* Traqio.identify("u_123", { plan: "pro", seats: 12 }, { signup_platform: "ios" });
|
|
1152
|
+
*
|
|
1153
|
+
* `traits` overwrite what was set before; `traitsOnce` are written only where the
|
|
1154
|
+
* user has no value yet (a first plan, a sign-up source). Values are text, numbers
|
|
1155
|
+
* or booleans — never an email or a name: the docs say why. At most 50 keys.
|
|
1156
|
+
*/
|
|
1157
|
+
identify(userId, traits, traitsOnce) {
|
|
1158
|
+
try {
|
|
1159
|
+
const id = typeof userId === "string" ? userId.trim() : "";
|
|
1160
|
+
if (!id)
|
|
1161
|
+
return;
|
|
1162
|
+
if (this.opts)
|
|
1163
|
+
this.setUserId(id);
|
|
1164
|
+
else
|
|
1165
|
+
this.earlyUserId = id;
|
|
1166
|
+
const set = (0, analytics_1.cleanTraits)(traits);
|
|
1167
|
+
const setOnce = (0, analytics_1.cleanTraits)(traitsOnce);
|
|
1168
|
+
if (!Object.keys(set).length && !Object.keys(setOnce).length)
|
|
1169
|
+
return;
|
|
1170
|
+
this.queueAnalytics({ kind: "identify", ts: Date.now(), userId: id, ...Object.keys(set).length ? { set } : {}, ...Object.keys(setOnce).length ? { setOnce } : {} });
|
|
1171
|
+
} catch {
|
|
1172
|
+
}
|
|
1173
|
+
}
|
|
1174
|
+
/**
|
|
1175
|
+
* Super-properties: added to EVERY analytics event from now on (and across
|
|
1176
|
+
* launches) — an app version flavour, an A/B arm, a theme. An event's own property
|
|
1177
|
+
* of the same name wins. `unregister(key)` removes one.
|
|
1178
|
+
*
|
|
1179
|
+
* Traqio.register({ app_flavor: "beta", theme: "dark" });
|
|
1180
|
+
*/
|
|
1181
|
+
register(props) {
|
|
1182
|
+
try {
|
|
1183
|
+
this.superProps = (0, analytics_1.cleanTraits)({ ...this.superProps, ...(0, analytics_1.cleanTraits)(props) });
|
|
1184
|
+
void this.persistSuperProps();
|
|
1185
|
+
} catch {
|
|
1186
|
+
}
|
|
1187
|
+
}
|
|
1188
|
+
/** Stop adding one super-property to the events. */
|
|
1189
|
+
unregister(key) {
|
|
1190
|
+
if (typeof key !== "string" || !(key in this.superProps))
|
|
1191
|
+
return;
|
|
1192
|
+
const rest = { ...this.superProps };
|
|
1193
|
+
delete rest[key];
|
|
1194
|
+
this.superProps = rest;
|
|
1195
|
+
void this.persistSuperProps();
|
|
1196
|
+
}
|
|
1197
|
+
/** The super-properties `register()` set — a copy. */
|
|
1198
|
+
getSuperProperties() {
|
|
1199
|
+
return { ...this.superProps };
|
|
1200
|
+
}
|
|
1201
|
+
/**
|
|
1202
|
+
* The B2B ACCOUNT the user works in (group analytics): every analytics event from now
|
|
1203
|
+
* on (and across launches) counts for it, so the console can count accounts — unique
|
|
1204
|
+
* accounts, funnels, retention and cohorts « by company » — and list them with their
|
|
1205
|
+
* traits. Call it after sign-in, and again when the user switches account.
|
|
1206
|
+
*
|
|
1207
|
+
* Traqio.group("company", "acc_42", { plan: "enterprise", seats: 40 });
|
|
1208
|
+
*
|
|
1209
|
+
* `groupType` is lowercase (`company`, `workspace`, `team`; 5 types at most),
|
|
1210
|
+
* `groupKey` your own account id — never an email. Traits follow identify's rules.
|
|
1211
|
+
*/
|
|
1212
|
+
group(groupType, groupKey, traits, traitsOnce) {
|
|
1213
|
+
try {
|
|
1214
|
+
const next = (0, analytics_1.withGroup)(this.groups, groupType, groupKey);
|
|
1215
|
+
if (!next)
|
|
1216
|
+
return;
|
|
1217
|
+
this.groups = next.groups;
|
|
1218
|
+
void this.persistGroups();
|
|
1219
|
+
const set = (0, analytics_1.cleanTraits)(traits);
|
|
1220
|
+
const setOnce = (0, analytics_1.cleanTraits)(traitsOnce);
|
|
1221
|
+
if (!next.changed && !Object.keys(set).length && !Object.keys(setOnce).length)
|
|
1222
|
+
return;
|
|
1223
|
+
this.queueAnalytics({
|
|
1224
|
+
kind: "group",
|
|
1225
|
+
ts: Date.now(),
|
|
1226
|
+
groupType: next.type,
|
|
1227
|
+
groupKey: next.key,
|
|
1228
|
+
...Object.keys(set).length ? { set } : {},
|
|
1229
|
+
...Object.keys(setOnce).length ? { setOnce } : {}
|
|
1230
|
+
});
|
|
1231
|
+
} catch {
|
|
1232
|
+
}
|
|
1233
|
+
}
|
|
1234
|
+
/** Stop stamping the events with any account — call it on sign-out. */
|
|
1235
|
+
resetGroups() {
|
|
1236
|
+
this.groups = {};
|
|
1237
|
+
void this.persistGroups();
|
|
1238
|
+
}
|
|
1239
|
+
/** The accounts `group()` set, by type — a copy. */
|
|
1240
|
+
getGroups() {
|
|
1241
|
+
return { ...this.groups };
|
|
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
|
+
}
|
|
1016
1318
|
/**
|
|
1017
1319
|
* A screen view — the app's page view, read under « Top pages » and usable as a
|
|
1018
1320
|
* funnel step. Call it when a screen gets the focus, or let `trackScreens()` (React
|
|
@@ -1537,6 +1839,10 @@ var require_build = __commonJS({
|
|
|
1537
1839
|
this.adBatchId = null;
|
|
1538
1840
|
this.analyticsQueue = [];
|
|
1539
1841
|
this.earlyAnalytics = [];
|
|
1842
|
+
this.superProps = {};
|
|
1843
|
+
this.groups = {};
|
|
1844
|
+
await this.persistSuperProps();
|
|
1845
|
+
await this.persistGroups();
|
|
1540
1846
|
await this.persistQueue();
|
|
1541
1847
|
await this.persistAdQueue();
|
|
1542
1848
|
await this.persistAnalyticsQueue();
|
|
@@ -1557,8 +1863,13 @@ var require_build = __commonJS({
|
|
|
1557
1863
|
var _a, _b;
|
|
1558
1864
|
if (this.erased)
|
|
1559
1865
|
return;
|
|
1560
|
-
|
|
1561
|
-
|
|
1866
|
+
let e = event;
|
|
1867
|
+
if (!(0, analytics_1.isEntry)(event.kind)) {
|
|
1868
|
+
const screen = (_b = (_a = event.screen) != null ? _a : this.currentScreen) != null ? _b : void 0;
|
|
1869
|
+
const props = (0, analytics_1.withSuperProps)(event.props, this.superProps);
|
|
1870
|
+
const groups = Object.keys(this.groups).length ? { ...this.groups } : void 0;
|
|
1871
|
+
e = { ...event, ...screen ? { screen } : {}, ...props ? { props } : {}, ...groups ? { groups } : {} };
|
|
1872
|
+
}
|
|
1562
1873
|
if (!this.started) {
|
|
1563
1874
|
if (this.earlyAnalytics.length < MAX_EARLY_ANALYTICS)
|
|
1564
1875
|
this.earlyAnalytics.push(e);
|
|
@@ -1569,8 +1880,13 @@ var require_build = __commonJS({
|
|
|
1569
1880
|
this.pushAnalytics(e);
|
|
1570
1881
|
}
|
|
1571
1882
|
pushAnalytics(e, schedule = true) {
|
|
1572
|
-
|
|
1573
|
-
|
|
1883
|
+
var _a, _b, _c;
|
|
1884
|
+
if ((0, analytics_1.isEntry)(e.kind))
|
|
1885
|
+
this.analyticsQueue.push({ ...e, sessionId: (_b = (_a = this.analyticsSession) == null ? void 0 : _a.id) != null ? _b : "" });
|
|
1886
|
+
else {
|
|
1887
|
+
this.analyticsSession = (0, analytics_1.touchSession)(this.analyticsSession, e.ts, uuid);
|
|
1888
|
+
this.analyticsQueue.push({ ...e, id: (_c = e.id) != null ? _c : uuid(), sessionId: this.analyticsSession.id });
|
|
1889
|
+
}
|
|
1574
1890
|
if (this.analyticsQueue.length > MAX_ANALYTICS_QUEUE)
|
|
1575
1891
|
this.analyticsQueue.splice(0, this.analyticsQueue.length - MAX_ANALYTICS_QUEUE);
|
|
1576
1892
|
void this.persistAnalyticsQueue();
|
|
@@ -1579,7 +1895,7 @@ var require_build = __commonJS({
|
|
|
1579
1895
|
}
|
|
1580
1896
|
/** The persisted queue first (older), then whatever was captured while it was being read. */
|
|
1581
1897
|
async restoreAnalytics() {
|
|
1582
|
-
var _a, _b, _c;
|
|
1898
|
+
var _a, _b, _c, _d, _e;
|
|
1583
1899
|
try {
|
|
1584
1900
|
const raw = await ((_a = this.opts.storage) == null ? void 0 : _a.getItem(ANALYTICS_QUEUE_KEY));
|
|
1585
1901
|
const stored = raw ? JSON.parse(raw) : null;
|
|
@@ -1588,7 +1904,61 @@ var require_build = __commonJS({
|
|
|
1588
1904
|
} catch {
|
|
1589
1905
|
}
|
|
1590
1906
|
try {
|
|
1591
|
-
|
|
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)));
|
|
1909
|
+
} catch {
|
|
1910
|
+
}
|
|
1911
|
+
if (this.resetBeforeInit) {
|
|
1912
|
+
this.resetBeforeInit = false;
|
|
1913
|
+
await this.persistSuperProps();
|
|
1914
|
+
await this.persistGroups();
|
|
1915
|
+
await this.forgetAnalyticsSession();
|
|
1916
|
+
return;
|
|
1917
|
+
}
|
|
1918
|
+
try {
|
|
1919
|
+
const raw = await ((_d = this.opts.storage) == null ? void 0 : _d.getItem(SUPER_PROPS_KEY2));
|
|
1920
|
+
const early = Object.keys(this.superProps).length > 0;
|
|
1921
|
+
if (raw)
|
|
1922
|
+
this.superProps = (0, analytics_1.cleanTraits)({ ...(0, analytics_1.cleanTraits)(JSON.parse(raw)), ...this.superProps });
|
|
1923
|
+
if (early)
|
|
1924
|
+
await this.persistSuperProps();
|
|
1925
|
+
} catch {
|
|
1926
|
+
}
|
|
1927
|
+
try {
|
|
1928
|
+
const raw = await ((_e = this.opts.storage) == null ? void 0 : _e.getItem(GROUPS_KEY2));
|
|
1929
|
+
const early = Object.keys(this.groups).length > 0;
|
|
1930
|
+
if (raw)
|
|
1931
|
+
this.groups = (0, analytics_1.cleanGroups)({ ...(0, analytics_1.cleanGroups)(JSON.parse(raw)), ...this.groups });
|
|
1932
|
+
if (early)
|
|
1933
|
+
await this.persistGroups();
|
|
1934
|
+
} catch {
|
|
1935
|
+
}
|
|
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
|
+
}
|
|
1947
|
+
async persistGroups() {
|
|
1948
|
+
var _a;
|
|
1949
|
+
if (!this.opts)
|
|
1950
|
+
return;
|
|
1951
|
+
try {
|
|
1952
|
+
await ((_a = this.opts.storage) == null ? void 0 : _a.setItem(GROUPS_KEY2, JSON.stringify(this.groups)));
|
|
1953
|
+
} catch {
|
|
1954
|
+
}
|
|
1955
|
+
}
|
|
1956
|
+
async persistSuperProps() {
|
|
1957
|
+
var _a;
|
|
1958
|
+
if (!this.opts)
|
|
1959
|
+
return;
|
|
1960
|
+
try {
|
|
1961
|
+
await ((_a = this.opts.storage) == null ? void 0 : _a.setItem(SUPER_PROPS_KEY2, JSON.stringify(this.superProps)));
|
|
1592
1962
|
} catch {
|
|
1593
1963
|
}
|
|
1594
1964
|
}
|
|
@@ -1880,18 +2250,18 @@ var require_build = __commonJS({
|
|
|
1880
2250
|
this.retryMs = BASE_RETRY_MS;
|
|
1881
2251
|
}
|
|
1882
2252
|
while (this.analyticsQueue.length) {
|
|
1883
|
-
const batch = this.analyticsQueue.
|
|
2253
|
+
const batch = (0, analytics_1.nextAnalyticsBatch)(this.analyticsQueue, this.opts.userId, MAX_ANALYTICS_BATCH);
|
|
1884
2254
|
const outcome = await this.send({
|
|
1885
2255
|
anonymousId: (_b = this.context.deviceId) != null ? _b : await this.getOrCreateDeviceId(),
|
|
1886
|
-
userId:
|
|
2256
|
+
userId: batch.userId,
|
|
1887
2257
|
platform: this.hostPlatform,
|
|
1888
|
-
events: batch
|
|
2258
|
+
events: batch.events
|
|
1889
2259
|
}, ANALYTICS_PATH);
|
|
1890
2260
|
if (outcome === "retry")
|
|
1891
2261
|
return this.backOff();
|
|
1892
2262
|
if (outcome === "refused")
|
|
1893
|
-
console.warn(`[traqio] the server refused ${batch.
|
|
1894
|
-
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);
|
|
1895
2265
|
await this.persistAnalyticsQueue();
|
|
1896
2266
|
this.retryMs = BASE_RETRY_MS;
|
|
1897
2267
|
}
|
|
@@ -2020,7 +2390,8 @@ var index_exports = {};
|
|
|
2020
2390
|
__export(index_exports, {
|
|
2021
2391
|
DEFAULT_ENDPOINT: () => DEFAULT_ENDPOINT,
|
|
2022
2392
|
Traqio: () => Traqio,
|
|
2023
|
-
TraqioWeb: () => TraqioWeb
|
|
2393
|
+
TraqioWeb: () => TraqioWeb,
|
|
2394
|
+
internalParam: () => internalParam
|
|
2024
2395
|
});
|
|
2025
2396
|
module.exports = __toCommonJS(index_exports);
|
|
2026
2397
|
var import_core = __toESM(require_build(), 1);
|
|
@@ -2080,7 +2451,8 @@ function deleteCookie(name, domain) {
|
|
|
2080
2451
|
|
|
2081
2452
|
// src/storage.ts
|
|
2082
2453
|
var DEVICE_ID_KEY = "traq_device_id";
|
|
2083
|
-
var
|
|
2454
|
+
var INTERNAL_KEY = "traq_internal";
|
|
2455
|
+
var MIRRORED = /* @__PURE__ */ new Set([DEVICE_ID_KEY, INTERNAL_KEY]);
|
|
2084
2456
|
var MIRROR_MAX_AGE_SECONDS = 395 * 24 * 60 * 60;
|
|
2085
2457
|
function local() {
|
|
2086
2458
|
try {
|
|
@@ -2374,13 +2746,252 @@ var TrackIdKeeper = class {
|
|
|
2374
2746
|
}
|
|
2375
2747
|
};
|
|
2376
2748
|
|
|
2749
|
+
// src/vitals.ts
|
|
2750
|
+
var DEFAULT_VITALS_SAMPLE_RATE = 0.5;
|
|
2751
|
+
function sampled(rate, random = Math.random) {
|
|
2752
|
+
const r = typeof rate === "number" && Number.isFinite(rate) && rate >= 0 && rate <= 1 ? rate : DEFAULT_VITALS_SAMPLE_RATE;
|
|
2753
|
+
if (r <= 0) return false;
|
|
2754
|
+
return r >= 1 || random() < r;
|
|
2755
|
+
}
|
|
2756
|
+
var CLS_GAP_MS = 1e3;
|
|
2757
|
+
var CLS_WINDOW_MS = 5e3;
|
|
2758
|
+
var INP_KEEP = 10;
|
|
2759
|
+
var VitalsCollector = class {
|
|
2760
|
+
constructor() {
|
|
2761
|
+
this.clsMax = 0;
|
|
2762
|
+
this.clsSeen = false;
|
|
2763
|
+
this.clsWindow = { value: 0, first: 0, last: 0 };
|
|
2764
|
+
/** The longest interactions, by interactionId: their longest entry. */
|
|
2765
|
+
this.longest = /* @__PURE__ */ new Map();
|
|
2766
|
+
this.interactions = /* @__PURE__ */ new Set();
|
|
2767
|
+
this.inpSeen = false;
|
|
2768
|
+
}
|
|
2769
|
+
/** A `largest-contentful-paint` entry: the latest one is the LCP. */
|
|
2770
|
+
addLcp(startTime) {
|
|
2771
|
+
if (Number.isFinite(startTime) && startTime >= 0) this.lcp = startTime;
|
|
2772
|
+
}
|
|
2773
|
+
/** The `first-contentful-paint` paint entry. */
|
|
2774
|
+
addFcp(startTime) {
|
|
2775
|
+
if (this.fcp === void 0 && Number.isFinite(startTime) && startTime >= 0) this.fcp = startTime;
|
|
2776
|
+
}
|
|
2777
|
+
/** The navigation entry's `responseStart` (minus `activationStart` for a prerendered page). */
|
|
2778
|
+
setTtfb(ms) {
|
|
2779
|
+
if (Number.isFinite(ms) && ms >= 0) this.ttfb = ms;
|
|
2780
|
+
}
|
|
2781
|
+
/**
|
|
2782
|
+
* A `layout-shift` entry. Shifts right after an input are expected and skipped; the
|
|
2783
|
+
* rest are grouped in session windows and CLS is the largest window's sum.
|
|
2784
|
+
*/
|
|
2785
|
+
addShift(value, startTime, hadRecentInput) {
|
|
2786
|
+
this.clsSeen = true;
|
|
2787
|
+
if (hadRecentInput || !Number.isFinite(value) || value < 0) return;
|
|
2788
|
+
const w = this.clsWindow;
|
|
2789
|
+
if (w.value > 0 && startTime - w.last < CLS_GAP_MS && startTime - w.first < CLS_WINDOW_MS) {
|
|
2790
|
+
w.value += value;
|
|
2791
|
+
w.last = startTime;
|
|
2792
|
+
} else {
|
|
2793
|
+
this.clsWindow = { value, first: startTime, last: startTime };
|
|
2794
|
+
}
|
|
2795
|
+
this.clsMax = Math.max(this.clsMax, this.clsWindow.value);
|
|
2796
|
+
}
|
|
2797
|
+
/** The browser supports layout shifts: a page that never moved has a CLS of 0, not none. */
|
|
2798
|
+
clsObserved() {
|
|
2799
|
+
this.clsSeen = true;
|
|
2800
|
+
}
|
|
2801
|
+
/** An `event` (or `first-input`) entry with an interactionId: one interaction's latency. */
|
|
2802
|
+
addInteraction(interactionId, duration) {
|
|
2803
|
+
if (!interactionId || !Number.isFinite(duration) || duration < 0) return;
|
|
2804
|
+
this.inpSeen = true;
|
|
2805
|
+
this.interactions.add(interactionId);
|
|
2806
|
+
const prev = this.longest.get(interactionId);
|
|
2807
|
+
if (prev !== void 0 && prev >= duration) return;
|
|
2808
|
+
this.longest.set(interactionId, duration);
|
|
2809
|
+
if (this.longest.size > INP_KEEP) {
|
|
2810
|
+
let minId = 0;
|
|
2811
|
+
let min = Infinity;
|
|
2812
|
+
for (const [id, d] of this.longest) {
|
|
2813
|
+
if (d < min) {
|
|
2814
|
+
min = d;
|
|
2815
|
+
minId = id;
|
|
2816
|
+
}
|
|
2817
|
+
}
|
|
2818
|
+
this.longest.delete(minId);
|
|
2819
|
+
}
|
|
2820
|
+
}
|
|
2821
|
+
/**
|
|
2822
|
+
* INP: the longest interaction, ignoring one per 50 — the 98th percentile for a busy
|
|
2823
|
+
* page, the worst one for an ordinary visit.
|
|
2824
|
+
*/
|
|
2825
|
+
inp() {
|
|
2826
|
+
if (!this.inpSeen || !this.longest.size) return void 0;
|
|
2827
|
+
const sorted = [...this.longest.values()].sort((a, b) => b - a);
|
|
2828
|
+
return sorted[Math.min(sorted.length - 1, Math.floor(this.interactions.size / 50))];
|
|
2829
|
+
}
|
|
2830
|
+
/** What was measured, rounded: ms for the timings, 4 decimals for CLS. Null when nothing was. */
|
|
2831
|
+
snapshot() {
|
|
2832
|
+
const out = {};
|
|
2833
|
+
if (this.lcp !== void 0) out.lcp = Math.round(this.lcp);
|
|
2834
|
+
const inp = this.inp();
|
|
2835
|
+
if (inp !== void 0) out.inp = Math.round(inp);
|
|
2836
|
+
if (this.clsSeen) out.cls = Math.round(this.clsMax * 1e4) / 1e4;
|
|
2837
|
+
if (this.fcp !== void 0) out.fcp = Math.round(this.fcp);
|
|
2838
|
+
if (this.ttfb !== void 0) out.ttfb = Math.round(this.ttfb);
|
|
2839
|
+
return Object.keys(out).length ? out : null;
|
|
2840
|
+
}
|
|
2841
|
+
};
|
|
2842
|
+
function observe(type, onEntries, extra = {}) {
|
|
2843
|
+
var _a;
|
|
2844
|
+
try {
|
|
2845
|
+
const PO = globalThis.PerformanceObserver;
|
|
2846
|
+
if (!PO || !((_a = PO.supportedEntryTypes) == null ? void 0 : _a.includes(type))) return false;
|
|
2847
|
+
const po = new PO((list) => onEntries(list.getEntries()));
|
|
2848
|
+
po.observe({ type, buffered: true, ...extra });
|
|
2849
|
+
return true;
|
|
2850
|
+
} catch {
|
|
2851
|
+
return false;
|
|
2852
|
+
}
|
|
2853
|
+
}
|
|
2854
|
+
function watchVitals(c) {
|
|
2855
|
+
var _a;
|
|
2856
|
+
const startedVisible = typeof document === "undefined" || document.visibilityState !== "hidden";
|
|
2857
|
+
let activation = 0;
|
|
2858
|
+
try {
|
|
2859
|
+
const nav = performance.getEntriesByType("navigation")[0];
|
|
2860
|
+
if (nav) {
|
|
2861
|
+
activation = Math.max(0, (_a = nav.activationStart) != null ? _a : 0);
|
|
2862
|
+
if (typeof nav.responseStart === "number" && nav.responseStart > 0) c.setTtfb(Math.max(0, nav.responseStart - activation));
|
|
2863
|
+
}
|
|
2864
|
+
} catch {
|
|
2865
|
+
}
|
|
2866
|
+
if (startedVisible) {
|
|
2867
|
+
let lcpFinal = false;
|
|
2868
|
+
const finalize = () => {
|
|
2869
|
+
lcpFinal = true;
|
|
2870
|
+
};
|
|
2871
|
+
for (const type of ["keydown", "click", "pointerdown"]) window.addEventListener(type, finalize, { once: true, capture: true });
|
|
2872
|
+
observe("largest-contentful-paint", (es) => {
|
|
2873
|
+
if (lcpFinal) return;
|
|
2874
|
+
for (const e of es) c.addLcp(Math.max(0, e.startTime - activation));
|
|
2875
|
+
});
|
|
2876
|
+
observe("paint", (es) => {
|
|
2877
|
+
for (const e of es) if (e.name === "first-contentful-paint") c.addFcp(Math.max(0, e.startTime - activation));
|
|
2878
|
+
});
|
|
2879
|
+
}
|
|
2880
|
+
if (observe("layout-shift", (es) => es.forEach((e) => {
|
|
2881
|
+
var _a2;
|
|
2882
|
+
return c.addShift((_a2 = e.value) != null ? _a2 : 0, e.startTime, !!e.hadRecentInput);
|
|
2883
|
+
}))) c.clsObserved();
|
|
2884
|
+
observe("event", (es) => es.forEach((e) => {
|
|
2885
|
+
var _a2;
|
|
2886
|
+
return c.addInteraction((_a2 = e.interactionId) != null ? _a2 : 0, e.duration);
|
|
2887
|
+
}), { durationThreshold: 40 });
|
|
2888
|
+
observe("first-input", (es) => es.forEach((e) => {
|
|
2889
|
+
var _a2;
|
|
2890
|
+
return c.addInteraction((_a2 = e.interactionId) != null ? _a2 : 0, e.duration);
|
|
2891
|
+
}));
|
|
2892
|
+
}
|
|
2893
|
+
|
|
2377
2894
|
// src/analytics.ts
|
|
2895
|
+
var isEntry = (e) => e.kind === "identify" || e.kind === "group" || e.kind === "alias";
|
|
2896
|
+
var GROUP_TYPE = /^[a-z][a-z0-9_]{0,29}$/;
|
|
2897
|
+
var MAX_GROUP_TYPES = 5;
|
|
2898
|
+
function cleanGroupType(v) {
|
|
2899
|
+
if (typeof v !== "string") return null;
|
|
2900
|
+
const t = v.trim();
|
|
2901
|
+
return GROUP_TYPE.test(t) ? t : null;
|
|
2902
|
+
}
|
|
2903
|
+
function cleanGroupKey(v) {
|
|
2904
|
+
if (typeof v === "number" && Number.isFinite(v)) return String(v);
|
|
2905
|
+
if (typeof v !== "string") return null;
|
|
2906
|
+
const t = v.trim();
|
|
2907
|
+
return t && t.length <= 256 ? t : null;
|
|
2908
|
+
}
|
|
2909
|
+
function cleanGroups(v) {
|
|
2910
|
+
const out = {};
|
|
2911
|
+
if (typeof v !== "object" || v === null || Array.isArray(v)) return out;
|
|
2912
|
+
for (const [t, k] of Object.entries(v)) {
|
|
2913
|
+
if (Object.keys(out).length >= MAX_GROUP_TYPES) break;
|
|
2914
|
+
const type = cleanGroupType(t);
|
|
2915
|
+
const key = cleanGroupKey(k);
|
|
2916
|
+
if (type && key) out[type] = key;
|
|
2917
|
+
}
|
|
2918
|
+
return out;
|
|
2919
|
+
}
|
|
2920
|
+
function withGroup(groups, type, key) {
|
|
2921
|
+
const t = cleanGroupType(type);
|
|
2922
|
+
const k = cleanGroupKey(key);
|
|
2923
|
+
if (!t || !k) return null;
|
|
2924
|
+
if (!(t in groups) && Object.keys(groups).length >= MAX_GROUP_TYPES) return null;
|
|
2925
|
+
return { groups: { ...groups, [t]: k }, type: t, key: k, changed: groups[t] !== k };
|
|
2926
|
+
}
|
|
2927
|
+
var TRAIT_KEY = /^[A-Za-z0-9_][A-Za-z0-9_.-]{0,63}$/;
|
|
2928
|
+
var MAX_TRAITS = 50;
|
|
2929
|
+
var MAX_PROPS_BYTES = 4096;
|
|
2930
|
+
function cleanTraits(v) {
|
|
2931
|
+
const out = {};
|
|
2932
|
+
if (typeof v !== "object" || v === null || Array.isArray(v)) return out;
|
|
2933
|
+
for (const [k, raw] of Object.entries(v)) {
|
|
2934
|
+
if (Object.keys(out).length >= MAX_TRAITS) break;
|
|
2935
|
+
if (!TRAIT_KEY.test(k)) continue;
|
|
2936
|
+
if (typeof raw === "string") {
|
|
2937
|
+
const t = raw.trim();
|
|
2938
|
+
if (t) out[k] = t.slice(0, 200);
|
|
2939
|
+
} else if (typeof raw === "number" && Number.isFinite(raw) || typeof raw === "boolean") {
|
|
2940
|
+
out[k] = raw;
|
|
2941
|
+
}
|
|
2942
|
+
}
|
|
2943
|
+
return out;
|
|
2944
|
+
}
|
|
2945
|
+
function withSuperProps(own, sup) {
|
|
2946
|
+
if (!Object.keys(sup).length) return own;
|
|
2947
|
+
const merged = { ...sup, ...own != null ? own : {} };
|
|
2948
|
+
try {
|
|
2949
|
+
return JSON.stringify(merged).length <= MAX_PROPS_BYTES ? merged : own;
|
|
2950
|
+
} catch {
|
|
2951
|
+
return own;
|
|
2952
|
+
}
|
|
2953
|
+
}
|
|
2378
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
|
+
}
|
|
2379
2989
|
var SESSION_IDLE_MS = 30 * 60 * 1e3;
|
|
2380
2990
|
var FLUSH_EVERY_MS = 5e3;
|
|
2381
2991
|
var FLUSH_AT = 20;
|
|
2382
2992
|
var MAX_QUEUE = 200;
|
|
2383
2993
|
var MAX_ERRORS_PER_PAGE = 10;
|
|
2994
|
+
var LINK_SCANNER_ERROR = /^Object Not Found Matching Id:\d+, MethodName:\w+, ParamCount:\d+$/;
|
|
2384
2995
|
var SCROLL_STEPS = [25, 50, 75, 100];
|
|
2385
2996
|
var UTM = [
|
|
2386
2997
|
["utm_source", "utmSource"],
|
|
@@ -2426,6 +3037,27 @@ function bare2(href) {
|
|
|
2426
3037
|
return void 0;
|
|
2427
3038
|
}
|
|
2428
3039
|
}
|
|
3040
|
+
var RAGE_WINDOW_MS = 1e3;
|
|
3041
|
+
var RAGE_RADIUS_PX = 30;
|
|
3042
|
+
var RAGE_THRESHOLD = 3;
|
|
3043
|
+
var RageClickDetector = class {
|
|
3044
|
+
constructor() {
|
|
3045
|
+
this.burst = null;
|
|
3046
|
+
}
|
|
3047
|
+
hit(x, y, t) {
|
|
3048
|
+
const b = this.burst;
|
|
3049
|
+
const near = b !== null && t - b.last <= RAGE_WINDOW_MS && Math.hypot(x - b.x, y - b.y) <= RAGE_RADIUS_PX;
|
|
3050
|
+
if (!near || !b.reported && t - b.first > RAGE_WINDOW_MS) {
|
|
3051
|
+
this.burst = { x, y, first: t, last: t, count: 1, reported: false };
|
|
3052
|
+
return false;
|
|
3053
|
+
}
|
|
3054
|
+
b.count++;
|
|
3055
|
+
b.last = t;
|
|
3056
|
+
if (b.reported || b.count < RAGE_THRESHOLD) return false;
|
|
3057
|
+
b.reported = true;
|
|
3058
|
+
return true;
|
|
3059
|
+
}
|
|
3060
|
+
};
|
|
2429
3061
|
var AnalyticsTracker = class {
|
|
2430
3062
|
constructor(deps) {
|
|
2431
3063
|
this.deps = deps;
|
|
@@ -2436,9 +3068,15 @@ var AnalyticsTracker = class {
|
|
|
2436
3068
|
this.scrollUrl = null;
|
|
2437
3069
|
this.errorsThisPage = 0;
|
|
2438
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;
|
|
3073
|
+
this.rage = new RageClickDetector();
|
|
3074
|
+
this.vitals = null;
|
|
3075
|
+
this.vitalsUrl = null;
|
|
2439
3076
|
}
|
|
2440
3077
|
/** Wires the listeners the options ask for and records the first page view. */
|
|
2441
3078
|
start() {
|
|
3079
|
+
var _a;
|
|
2442
3080
|
const o = this.deps.options;
|
|
2443
3081
|
this.timer = setInterval(() => this.flush(false), FLUSH_EVERY_MS);
|
|
2444
3082
|
document.addEventListener("visibilitychange", () => {
|
|
@@ -2450,17 +3088,68 @@ var AnalyticsTracker = class {
|
|
|
2450
3088
|
this.watchRoutes();
|
|
2451
3089
|
}
|
|
2452
3090
|
if (o.clicks !== false) document.addEventListener("click", (e) => this.onClick(e), true);
|
|
3091
|
+
if (o.rageClicks !== false) document.addEventListener("click", (e) => this.onRageClick(e), true);
|
|
2453
3092
|
if (o.scroll !== false) window.addEventListener("scroll", () => this.onScroll(), { passive: true });
|
|
2454
3093
|
if (o.errors !== false) {
|
|
2455
3094
|
window.addEventListener("error", (e) => this.onError(e));
|
|
2456
3095
|
window.addEventListener("unhandledrejection", (e) => this.onRejection(e));
|
|
2457
3096
|
}
|
|
3097
|
+
if (o.webVitals !== false) {
|
|
3098
|
+
const rate = typeof o.webVitals === "object" ? o.webVitals.sampleRate : DEFAULT_VITALS_SAMPLE_RATE;
|
|
3099
|
+
if (sampled(rate)) {
|
|
3100
|
+
this.vitals = new VitalsCollector();
|
|
3101
|
+
this.vitalsUrl = (_a = bare2(location.href)) != null ? _a : null;
|
|
3102
|
+
watchVitals(this.vitals);
|
|
3103
|
+
}
|
|
3104
|
+
}
|
|
2458
3105
|
}
|
|
2459
3106
|
/** A custom analytics event — never a conversion (that is `track()`). */
|
|
2460
3107
|
capture(name, props) {
|
|
2461
|
-
if (typeof name !== "string" || !name.trim() || name.startsWith("$")) return;
|
|
3108
|
+
if (typeof name !== "string" || !name.trim() || name.startsWith("$") && name.trim() !== "$exposure") return;
|
|
2462
3109
|
this.enqueue({ kind: "custom", name: name.trim(), props });
|
|
2463
3110
|
}
|
|
3111
|
+
/**
|
|
3112
|
+
* The user's properties (`identify(userId, traits, traitsOnce)`): one entry in the
|
|
3113
|
+
* batch, under its own user id. Not activity — it neither opens nor prolongs a session.
|
|
3114
|
+
*/
|
|
3115
|
+
identify(userId, set, setOnce) {
|
|
3116
|
+
if (this.stopped || !userId) return;
|
|
3117
|
+
const hasSet = Object.keys(set).length > 0;
|
|
3118
|
+
const hasOnce = Object.keys(setOnce).length > 0;
|
|
3119
|
+
if (!hasSet && !hasOnce) return;
|
|
3120
|
+
this.queue.push({ kind: "identify", ts: Date.now(), userId, ...hasSet ? { set } : {}, ...hasOnce ? { setOnce } : {} });
|
|
3121
|
+
if (this.queue.length > MAX_QUEUE) this.queue.shift();
|
|
3122
|
+
if (this.queue.length >= FLUSH_AT) this.flush(false);
|
|
3123
|
+
}
|
|
3124
|
+
/** An account's properties (`group(type, key, traits)`): one entry in the batch. Not activity either. */
|
|
3125
|
+
group(groupType, groupKey, set, setOnce) {
|
|
3126
|
+
if (this.stopped) return;
|
|
3127
|
+
const hasSet = Object.keys(set).length > 0;
|
|
3128
|
+
const hasOnce = Object.keys(setOnce).length > 0;
|
|
3129
|
+
this.queue.push({ kind: "group", ts: Date.now(), groupType, groupKey, ...hasSet ? { set } : {}, ...hasOnce ? { setOnce } : {} });
|
|
3130
|
+
if (this.queue.length > MAX_QUEUE) this.queue.shift();
|
|
3131
|
+
if (this.queue.length >= FLUSH_AT) this.flush(false);
|
|
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
|
+
}
|
|
2464
3153
|
/** Records a page view of the current URL. Called by itself on route changes; exposed for routers that need it. */
|
|
2465
3154
|
pageview() {
|
|
2466
3155
|
const url = bare2(location.href);
|
|
@@ -2476,6 +3165,7 @@ var AnalyticsTracker = class {
|
|
|
2476
3165
|
}
|
|
2477
3166
|
/** Sends what is queued. `leaving` = the page is going away: keepalive, so the request outlives it. */
|
|
2478
3167
|
flush(leaving) {
|
|
3168
|
+
var _a, _b;
|
|
2479
3169
|
if (!this.queue.length || this.stopped) return;
|
|
2480
3170
|
if (this.deps.isForgotten()) {
|
|
2481
3171
|
this.stop();
|
|
@@ -2483,13 +3173,17 @@ var AnalyticsTracker = class {
|
|
|
2483
3173
|
}
|
|
2484
3174
|
const anonymousId = this.deps.anonymousId();
|
|
2485
3175
|
if (!anonymousId) return;
|
|
2486
|
-
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;
|
|
2487
3179
|
const body = JSON.stringify({
|
|
2488
3180
|
sdkKey: this.deps.sdkKey,
|
|
2489
3181
|
anonymousId,
|
|
2490
|
-
userId
|
|
3182
|
+
userId,
|
|
2491
3183
|
debug: this.deps.isDebug() || void 0,
|
|
2492
|
-
|
|
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 })
|
|
2493
3187
|
});
|
|
2494
3188
|
try {
|
|
2495
3189
|
void fetch(`${this.deps.endpoint}/ingest/analytics`, {
|
|
@@ -2537,18 +3231,30 @@ var AnalyticsTracker = class {
|
|
|
2537
3231
|
return s;
|
|
2538
3232
|
}
|
|
2539
3233
|
enqueue(e) {
|
|
2540
|
-
var _a, _b;
|
|
3234
|
+
var _a, _b, _c, _d, _e, _f, _g, _h;
|
|
2541
3235
|
if (this.stopped) return;
|
|
2542
3236
|
const s = this.session();
|
|
2543
3237
|
const title = typeof document.title === "string" && document.title ? document.title.slice(0, 200) : void 0;
|
|
2544
|
-
|
|
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 : {});
|
|
3239
|
+
const g = (_f = (_e = (_d = this.deps).groups) == null ? void 0 : _e.call(_d)) != null ? _f : {};
|
|
3240
|
+
const groups = Object.keys(g).length ? { ...g } : void 0;
|
|
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 } : {} });
|
|
2545
3242
|
if (this.queue.length > MAX_QUEUE) this.queue.shift();
|
|
2546
3243
|
if (this.queue.length >= FLUSH_AT) this.flush(false);
|
|
2547
3244
|
}
|
|
2548
3245
|
leave() {
|
|
2549
3246
|
this.sendScroll();
|
|
3247
|
+
this.sendVitals();
|
|
2550
3248
|
this.flush(true);
|
|
2551
3249
|
}
|
|
3250
|
+
/** Once per load, at the first hide: LCP is final then, and CLS and INP have seen the visit. */
|
|
3251
|
+
sendVitals() {
|
|
3252
|
+
const c = this.vitals;
|
|
3253
|
+
if (!c || !this.vitalsUrl) return;
|
|
3254
|
+
this.vitals = null;
|
|
3255
|
+
const props = c.snapshot();
|
|
3256
|
+
if (props) this.enqueue({ kind: "vitals", url: this.vitalsUrl, props: { ...props } });
|
|
3257
|
+
}
|
|
2552
3258
|
/** Single-page apps change the URL through the history API: wrap it, and listen for back/forward. */
|
|
2553
3259
|
watchRoutes() {
|
|
2554
3260
|
if (typeof history === "undefined") return;
|
|
@@ -2596,9 +3302,27 @@ var AnalyticsTracker = class {
|
|
|
2596
3302
|
if (el.id) props.id = el.id.slice(0, 100);
|
|
2597
3303
|
this.enqueue({ kind: "click", props });
|
|
2598
3304
|
}
|
|
3305
|
+
/** Any element: frustration is most telling where nothing was clickable. */
|
|
3306
|
+
onRageClick(e) {
|
|
3307
|
+
var _a;
|
|
3308
|
+
const x = typeof e.clientX === "number" ? e.clientX : 0;
|
|
3309
|
+
const y = typeof e.clientY === "number" ? e.clientY : 0;
|
|
3310
|
+
if (!this.rage.hit(x, y, Date.now())) return;
|
|
3311
|
+
const target = e.target;
|
|
3312
|
+
if (!target || typeof target.closest !== "function" || target.closest("[data-traqio-ignore]")) return;
|
|
3313
|
+
const el = (_a = target.closest("a, button, [role='button'], input[type='submit'], input[type='button']")) != null ? _a : target;
|
|
3314
|
+
const text = (el.tagName === "INPUT" ? el.value : el.innerText || el.textContent || "").replace(/\s+/g, " ").trim().slice(0, 100);
|
|
3315
|
+
const props = { tag: el.tagName.toLowerCase(), count: RAGE_THRESHOLD };
|
|
3316
|
+
if (text) props.text = text;
|
|
3317
|
+
const href = el.tagName === "A" ? bare2(el.getAttribute("href")) : void 0;
|
|
3318
|
+
if (href) props.href = href;
|
|
3319
|
+
if (el.id) props.id = el.id.slice(0, 100);
|
|
3320
|
+
this.enqueue({ kind: "rageclick", props });
|
|
3321
|
+
}
|
|
2599
3322
|
onError(e) {
|
|
2600
3323
|
if (this.errorsThisPage >= MAX_ERRORS_PER_PAGE) return;
|
|
2601
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;
|
|
2602
3326
|
this.errorsThisPage++;
|
|
2603
3327
|
const stack = e.error instanceof Error && typeof e.error.stack === "string" ? e.error.stack.slice(0, 1500) : void 0;
|
|
2604
3328
|
this.enqueue({ kind: "error", props: { message, ...stack ? { stack } : {}, ...e.filename ? { source: bare2(e.filename) } : {}, ...e.lineno ? { line: e.lineno } : {} } });
|
|
@@ -2607,6 +3331,7 @@ var AnalyticsTracker = class {
|
|
|
2607
3331
|
if (this.errorsThisPage >= MAX_ERRORS_PER_PAGE) return;
|
|
2608
3332
|
const reason = e.reason;
|
|
2609
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;
|
|
2610
3335
|
this.errorsThisPage++;
|
|
2611
3336
|
const stack = reason instanceof Error && typeof reason.stack === "string" ? reason.stack.slice(0, 1500) : void 0;
|
|
2612
3337
|
this.enqueue({ kind: "error", props: { message, unhandledRejection: true, ...stack ? { stack } : {} } });
|
|
@@ -2615,6 +3340,8 @@ var AnalyticsTracker = class {
|
|
|
2615
3340
|
|
|
2616
3341
|
// src/index.ts
|
|
2617
3342
|
var DEFAULT_ENDPOINT = "https://api.traqio.app";
|
|
3343
|
+
var SUPER_PROPS_KEY = "traq_super";
|
|
3344
|
+
var GROUPS_KEY = "traq_groups";
|
|
2618
3345
|
var DEFAULT_TRACK_ID_DAYS = 30;
|
|
2619
3346
|
var DAY_MS = 24 * 60 * 60 * 1e3;
|
|
2620
3347
|
function inBrowser() {
|
|
@@ -2627,6 +3354,15 @@ function randomId() {
|
|
|
2627
3354
|
return null;
|
|
2628
3355
|
}
|
|
2629
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
|
+
}
|
|
2630
3366
|
var TraqioWeb = class {
|
|
2631
3367
|
constructor() {
|
|
2632
3368
|
this.core = new import_core.Client();
|
|
@@ -2640,6 +3376,18 @@ var TraqioWeb = class {
|
|
|
2640
3376
|
this.earlyCaptures = [];
|
|
2641
3377
|
/** `init()` has finished (or will never run here): early captures are no longer kept. */
|
|
2642
3378
|
this.ready = false;
|
|
3379
|
+
/** `register()`: properties added to every analytics event, kept in `traq_super`. */
|
|
3380
|
+
this.superProps = {};
|
|
3381
|
+
/** `identify()` calls with traits made before `init()` — replayed once the tracker exists. */
|
|
3382
|
+
this.earlyIdentifies = [];
|
|
3383
|
+
/** `group()`: the accounts every analytics event is stamped with, kept in `traq_groups`. */
|
|
3384
|
+
this.groups = {};
|
|
3385
|
+
/** Group entries made before `init()` — replayed once the tracker exists. */
|
|
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;
|
|
2643
3391
|
}
|
|
2644
3392
|
/**
|
|
2645
3393
|
* Call once, as early as possible on every page (or once in a single-page app).
|
|
@@ -2648,7 +3396,7 @@ var TraqioWeb = class {
|
|
|
2648
3396
|
* does nothing there.
|
|
2649
3397
|
*/
|
|
2650
3398
|
async init(opts) {
|
|
2651
|
-
var _a, _b, _c;
|
|
3399
|
+
var _a, _b, _c, _d;
|
|
2652
3400
|
if (this.started || !inBrowser()) return;
|
|
2653
3401
|
this.started = true;
|
|
2654
3402
|
this.waitForConsent = opts.waitForConsent === true;
|
|
@@ -2663,18 +3411,38 @@ var TraqioWeb = class {
|
|
|
2663
3411
|
this.trackIds.capture(location.href);
|
|
2664
3412
|
this.sources = new SourceKeeper(this.store, keepMs, cookieDomain, cookies);
|
|
2665
3413
|
this.sources.capture(location.href, document.referrer);
|
|
3414
|
+
const optOut = internalParam(location.href);
|
|
3415
|
+
if (optOut !== null) this.setInternal(optOut);
|
|
2666
3416
|
if (!this.store.getSync(DEVICE_ID_KEY)) {
|
|
2667
3417
|
const id = randomId();
|
|
2668
3418
|
if (id) this.store.setSync(DEVICE_ID_KEY, id);
|
|
2669
3419
|
}
|
|
2670
|
-
this.userId = opts.userId;
|
|
2671
|
-
|
|
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
|
+
}
|
|
3425
|
+
try {
|
|
3426
|
+
const raw = this.store.getSync(SUPER_PROPS_KEY);
|
|
3427
|
+
const early = Object.keys(this.superProps).length > 0;
|
|
3428
|
+
if (raw) this.superProps = cleanTraits({ ...cleanTraits(JSON.parse(raw)), ...this.superProps });
|
|
3429
|
+
if (early) this.store.setSync(SUPER_PROPS_KEY, JSON.stringify(this.superProps));
|
|
3430
|
+
} catch {
|
|
3431
|
+
}
|
|
3432
|
+
try {
|
|
3433
|
+
const raw = this.store.getSync(GROUPS_KEY);
|
|
3434
|
+
const early = Object.keys(this.groups).length > 0;
|
|
3435
|
+
if (raw) this.groups = cleanGroups({ ...cleanGroups(JSON.parse(raw)), ...this.groups });
|
|
3436
|
+
if (early) this.store.setSync(GROUPS_KEY, JSON.stringify(this.groups));
|
|
3437
|
+
} catch {
|
|
3438
|
+
}
|
|
3439
|
+
const endpoint = ((_d = opts.endpoint) != null ? _d : DEFAULT_ENDPOINT).replace(/\/+$/, "");
|
|
2672
3440
|
await this.core.init({
|
|
2673
3441
|
sdkKey: opts.sdkKey,
|
|
2674
3442
|
endpoint,
|
|
2675
3443
|
storage: this.store,
|
|
2676
3444
|
platform: "web",
|
|
2677
|
-
userId:
|
|
3445
|
+
userId: this.userId,
|
|
2678
3446
|
consent,
|
|
2679
3447
|
debug: opts.debug,
|
|
2680
3448
|
lifecycle: ({ background, foreground }) => {
|
|
@@ -2690,16 +3458,49 @@ var TraqioWeb = class {
|
|
|
2690
3458
|
sdkKey: opts.sdkKey,
|
|
2691
3459
|
anonymousId: () => store.getSync(DEVICE_ID_KEY),
|
|
2692
3460
|
userId: () => this.userId,
|
|
3461
|
+
superProps: () => this.superProps,
|
|
3462
|
+
groups: () => this.groups,
|
|
2693
3463
|
isDebug: () => this.core.isDebug,
|
|
2694
3464
|
isForgotten: () => this.core.forgotten,
|
|
3465
|
+
isInternal: () => this.isInternal,
|
|
2695
3466
|
options: opts.analytics === true ? {} : opts.analytics
|
|
2696
3467
|
});
|
|
2697
3468
|
this.analytics.start();
|
|
3469
|
+
for (const [id, set, once] of this.earlyIdentifies) this.analytics.identify(id, set, once);
|
|
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);
|
|
2698
3472
|
for (const [name, props] of this.earlyCaptures) this.analytics.capture(name, props);
|
|
2699
3473
|
}
|
|
2700
3474
|
this.earlyCaptures = [];
|
|
3475
|
+
this.earlyIdentifies = [];
|
|
3476
|
+
this.earlyGroups = [];
|
|
3477
|
+
this.earlyAliases = [];
|
|
2701
3478
|
this.ready = true;
|
|
2702
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
|
+
}
|
|
2703
3504
|
/** Whether this page runs in debug mode — see `WebInitOptions.debug`. Settled by `init()`. */
|
|
2704
3505
|
get isDebug() {
|
|
2705
3506
|
return this.core.isDebug;
|
|
@@ -2712,7 +3513,8 @@ var TraqioWeb = class {
|
|
|
2712
3513
|
* A product-analytics event — `search_performed`, `pricing_toggled`… — read in the
|
|
2713
3514
|
* console's Analytics section (events, funnels). NOT a conversion: it is never
|
|
2714
3515
|
* billed nor sent to an ad network. A conversion (sign-up, purchase) is `track()`.
|
|
2715
|
-
* Needs `analytics` at init; names starting with `$` are reserved
|
|
3516
|
+
* Needs `analytics` at init; names starting with `$` are reserved, but for an
|
|
3517
|
+
* experiment exposure: `capture("$exposure", { experiment, variant })`.
|
|
2716
3518
|
*/
|
|
2717
3519
|
capture(name, props) {
|
|
2718
3520
|
if (this.analytics) this.analytics.capture(name, props);
|
|
@@ -2731,6 +3533,159 @@ var TraqioWeb = class {
|
|
|
2731
3533
|
this.userId = id;
|
|
2732
3534
|
this.core.setUserId(id);
|
|
2733
3535
|
}
|
|
3536
|
+
/**
|
|
3537
|
+
* Who the user is, and what they are: sets the user id (like `setUserId`) and, with
|
|
3538
|
+
* traits, the user's PROPERTIES — `plan`, `role`, `company_size` — read in the
|
|
3539
|
+
* console as a breakdown, a report dimension and a cohort condition. Needs
|
|
3540
|
+
* `analytics` at init for the traits.
|
|
3541
|
+
*
|
|
3542
|
+
* Traqio.identify("u_123", { plan: "pro", seats: 12 }, { signup_source: "webinar" });
|
|
3543
|
+
*
|
|
3544
|
+
* `traits` overwrite what was set before; `traitsOnce` are written only where the
|
|
3545
|
+
* user has no value yet. Text, numbers, booleans — never an email or a name. At most 50 keys.
|
|
3546
|
+
*/
|
|
3547
|
+
identify(userId, traits, traitsOnce) {
|
|
3548
|
+
const id = typeof userId === "string" ? userId.trim() : "";
|
|
3549
|
+
if (!id) return;
|
|
3550
|
+
this.setUserId(id);
|
|
3551
|
+
const set = cleanTraits(traits);
|
|
3552
|
+
const once = cleanTraits(traitsOnce);
|
|
3553
|
+
if (!Object.keys(set).length && !Object.keys(once).length) return;
|
|
3554
|
+
if (this.analytics) this.analytics.identify(id, set, once);
|
|
3555
|
+
else if (!this.ready && this.earlyIdentifies.length < 10) this.earlyIdentifies.push([id, set, once]);
|
|
3556
|
+
}
|
|
3557
|
+
/**
|
|
3558
|
+
* Super-properties: added to EVERY analytics event from now on, on this page and the
|
|
3559
|
+
* next ones — an A/B arm, a pricing variant, a theme. An event's own property of the
|
|
3560
|
+
* same name wins. Kept with the visitor id (same consent rules). `unregister(key)` removes one.
|
|
3561
|
+
*
|
|
3562
|
+
* Traqio.register({ pricing_variant: "b" });
|
|
3563
|
+
*/
|
|
3564
|
+
register(props) {
|
|
3565
|
+
this.superProps = cleanTraits({ ...this.superProps, ...cleanTraits(props) });
|
|
3566
|
+
this.persistSuperProps();
|
|
3567
|
+
}
|
|
3568
|
+
/** Stop adding one super-property to the events. */
|
|
3569
|
+
unregister(key) {
|
|
3570
|
+
if (typeof key !== "string" || !(key in this.superProps)) return;
|
|
3571
|
+
const rest = { ...this.superProps };
|
|
3572
|
+
delete rest[key];
|
|
3573
|
+
this.superProps = rest;
|
|
3574
|
+
this.persistSuperProps();
|
|
3575
|
+
}
|
|
3576
|
+
/** The super-properties `register()` set — a copy. */
|
|
3577
|
+
getSuperProperties() {
|
|
3578
|
+
return { ...this.superProps };
|
|
3579
|
+
}
|
|
3580
|
+
/**
|
|
3581
|
+
* The B2B ACCOUNT the visitor works in (group analytics): every analytics event from
|
|
3582
|
+
* now on — on this page and the next ones — counts for it, so the console can count
|
|
3583
|
+
* accounts (unique accounts, funnels, retention, cohorts « by company ») and list them
|
|
3584
|
+
* with their traits. Call it after sign-in, and again on an account switch. Needs
|
|
3585
|
+
* `analytics` at init.
|
|
3586
|
+
*
|
|
3587
|
+
* Traqio.group("company", "acc_42", { plan: "enterprise", seats: 40 });
|
|
3588
|
+
*
|
|
3589
|
+
* `groupType` is lowercase (`company`, `workspace`; 5 types at most), `groupKey` your
|
|
3590
|
+
* own account id — never an email. Traits follow identify's rules.
|
|
3591
|
+
*/
|
|
3592
|
+
group(groupType, groupKey, traits, traitsOnce) {
|
|
3593
|
+
const next = withGroup(this.groups, groupType, groupKey);
|
|
3594
|
+
if (!next) return;
|
|
3595
|
+
this.groups = next.groups;
|
|
3596
|
+
this.persistGroups();
|
|
3597
|
+
const set = cleanTraits(traits);
|
|
3598
|
+
const once = cleanTraits(traitsOnce);
|
|
3599
|
+
if (!next.changed && !Object.keys(set).length && !Object.keys(once).length) return;
|
|
3600
|
+
if (this.analytics) this.analytics.group(next.type, next.key, set, once);
|
|
3601
|
+
else if (!this.ready && this.earlyGroups.length < 10) this.earlyGroups.push([next.type, next.key, set, once]);
|
|
3602
|
+
}
|
|
3603
|
+
/** Stop stamping the events with any account — call it on sign-out. */
|
|
3604
|
+
resetGroups() {
|
|
3605
|
+
this.groups = {};
|
|
3606
|
+
this.persistGroups();
|
|
3607
|
+
}
|
|
3608
|
+
/** The accounts `group()` set, by type — a copy. */
|
|
3609
|
+
getGroups() {
|
|
3610
|
+
return { ...this.groups };
|
|
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
|
+
}
|
|
3673
|
+
persistGroups() {
|
|
3674
|
+
try {
|
|
3675
|
+
if (!this.store) return;
|
|
3676
|
+
if (Object.keys(this.groups).length) this.store.setSync(GROUPS_KEY, JSON.stringify(this.groups));
|
|
3677
|
+
else this.store.removeSync(GROUPS_KEY);
|
|
3678
|
+
} catch {
|
|
3679
|
+
}
|
|
3680
|
+
}
|
|
3681
|
+
persistSuperProps() {
|
|
3682
|
+
try {
|
|
3683
|
+
if (!this.store) return;
|
|
3684
|
+
if (Object.keys(this.superProps).length) this.store.setSync(SUPER_PROPS_KEY, JSON.stringify(this.superProps));
|
|
3685
|
+
else this.store.removeSync(SUPER_PROPS_KEY);
|
|
3686
|
+
} catch {
|
|
3687
|
+
}
|
|
3688
|
+
}
|
|
2734
3689
|
/**
|
|
2735
3690
|
* The visitor's consent answer. Governs sharing with ad networks, and — with
|
|
2736
3691
|
* `waitForConsent` — whether anything may be stored: `true` writes out what the
|
|
@@ -2783,6 +3738,10 @@ var TraqioWeb = class {
|
|
|
2783
3738
|
(_a = this.trackIds) == null ? void 0 : _a.clear();
|
|
2784
3739
|
(_b = this.sources) == null ? void 0 : _b.clear();
|
|
2785
3740
|
(_c = this.analytics) == null ? void 0 : _c.stop();
|
|
3741
|
+
this.superProps = {};
|
|
3742
|
+
this.persistSuperProps();
|
|
3743
|
+
this.groups = {};
|
|
3744
|
+
this.persistGroups();
|
|
2786
3745
|
}
|
|
2787
3746
|
return ok;
|
|
2788
3747
|
}
|