@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/README.md
CHANGED
|
@@ -103,13 +103,13 @@ hidden or the page is left.
|
|
|
103
103
|
|
|
104
104
|
## Product analytics
|
|
105
105
|
|
|
106
|
-
Page views, traffic sources, clicks, scroll depth, front-end errors and your own
|
|
106
|
+
Page views, traffic sources, clicks, scroll depth, front-end errors, Core Web Vitals and your own
|
|
107
107
|
analytics events — read in the console's **Analytics** section (and through the API /
|
|
108
108
|
MCP). Off by default:
|
|
109
109
|
|
|
110
110
|
```ts
|
|
111
111
|
Traqio.init({ sdkKey: "YOUR_WEB_KEY", analytics: true });
|
|
112
|
-
// or pick: analytics: { pageviews: true, clicks: true, scroll: true, errors: false }
|
|
112
|
+
// or pick: analytics: { pageviews: true, clicks: true, rageClicks: true, scroll: true, errors: false, webVitals: { sampleRate: 0.2 } }
|
|
113
113
|
|
|
114
114
|
Traqio.capture("search_performed", { results: 12 }); // an analytics event, for events and funnels
|
|
115
115
|
```
|
|
@@ -120,12 +120,72 @@ Traqio.capture("search_performed", { results: 12 }); // an analytics event, for
|
|
|
120
120
|
read once per session (30 minutes of inactivity end one).
|
|
121
121
|
- **Clicks** record a link's or a button's visible text and link, never what was typed.
|
|
122
122
|
Add `data-traqio-ignore` to an element to leave it out.
|
|
123
|
+
- **Rage clicks** — 3 or more clicks within a second on the same spot (30 px), on ANY
|
|
124
|
+
element: a dead button, a picture that looks like a link. One `$rageclick` per burst,
|
|
125
|
+
with the element's text and tag, never more. `rageClicks: false` turns them off.
|
|
126
|
+
- **Web vitals** — LCP, INP, CLS, FCP and TTFB of a SAMPLE of page loads, measured with
|
|
127
|
+
the browser's own Performance APIs (no extra dependency) and sent once per measured
|
|
128
|
+
load as `$web_vitals`, when the page is first hidden. The console's **Performance** tab
|
|
129
|
+
reads them at p75 per page, device and day against Google's thresholds. `sampleRate`
|
|
130
|
+
defaults to `0.5` (half the loads); `webVitals: { sampleRate: 1 }` measures every load,
|
|
131
|
+
`webVitals: false` none.
|
|
123
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.
|
|
139
|
+
- **User properties** — `Traqio.identify(user.id, { plan: "pro", seats: 12 }, { signup_source: "webinar" })`
|
|
140
|
+
sets the user id and the user's properties: the second object overwrites, the third is
|
|
141
|
+
written only where the user has no value yet. Read them as a breakdown (`user:plan`), a
|
|
142
|
+
report dimension (`user__plan`) and a cohort condition. Text, numbers, booleans — never
|
|
143
|
+
an email or a name; at most 50 keys.
|
|
144
|
+
- **Super-properties** — `Traqio.register({ pricing_variant: "b" })` adds properties to
|
|
145
|
+
every analytics event from now on (and on the next pages); an event's own property of the
|
|
146
|
+
same name wins. `Traqio.unregister("pricing_variant")` removes one.
|
|
124
147
|
|
|
125
148
|
**`capture()` is not `track()`.** An analytics event is never billed, never counted as a
|
|
126
149
|
conversion and never sent to an ad network — it has its own route and its own table.
|
|
127
150
|
A sign-up or a purchase stays `track()` (or your backend's server SDK).
|
|
128
151
|
|
|
152
|
+
## Groups (B2B accounts)
|
|
153
|
+
|
|
154
|
+
If you sell to teams, tell Traqio which account the visitor works in, right after
|
|
155
|
+
sign-in. Every analytics event that follows — on this page and the next ones — carries
|
|
156
|
+
it (`groups: { company: "acc_42" }`), so the console's Analytics can count accounts
|
|
157
|
+
instead of people: funnels and retention by account, unique accounts, and the list of
|
|
158
|
+
your accounts with their properties. Needs `analytics` at init.
|
|
159
|
+
|
|
160
|
+
```ts
|
|
161
|
+
Traqio.group("company", account.id, { plan: "enterprise", seats: 40 }); // traits optional
|
|
162
|
+
Traqio.getGroups(); // { company: "acc_42" }
|
|
163
|
+
Traqio.resetGroups(); // an account switch; on sign-out, reset() clears them too
|
|
164
|
+
```
|
|
165
|
+
|
|
166
|
+
The type is lowercase (`company`, `workspace`, `team`: a letter, then letters, digits or
|
|
167
|
+
`_`, 30 characters at most), 5 types at most; the key is your own account id, 1–256
|
|
168
|
+
characters — never an email. Traits follow the identify rules (a 4th argument fills only
|
|
169
|
+
what is unset). The same key again with no traits sends nothing new, so calling it on
|
|
170
|
+
every page is free. `forgetMe()` forgets the groups too; an invalid call is ignored.
|
|
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
|
+
|
|
129
189
|
## Frameworks
|
|
130
190
|
|
|
131
191
|
- **React / Next.js** — call `init()` in a `useEffect` of a client component mounted
|
|
@@ -162,7 +222,7 @@ networks.
|
|
|
162
222
|
| `trackIdDays` | `30` | How long the trackID and the visit's source are kept |
|
|
163
223
|
| `cookies` | `true` | `false` = localStorage only, no sharing across subdomains |
|
|
164
224
|
| `debug` | detected | Debug traffic, kept out of every report (see below) |
|
|
165
|
-
| `analytics` | `false` | Product analytics: `true`, or `{ pageviews, clicks, scroll, errors }` |
|
|
225
|
+
| `analytics` | `false` | Product analytics: `true`, or `{ pageviews, clicks, rageClicks, scroll, errors, webVitals }` (`webVitals: { sampleRate }`, default 0.5) |
|
|
166
226
|
|
|
167
227
|
**Debug traffic.** Omitted, `debug` is on for a page served from `localhost`, `127.0.0.1`, `*.localhost`, `*.local` or `file:`, and off everywhere else; `true` forces it (a staging site), `false` counts a local page. `traqio.isDebug` tells which mode is on. Debug traffic shows in the console's **Live events › Debug** view, but it is never counted in a report, attributed, sent to an ad network or a webhook, or billed.
|
|
168
228
|
|
package/build/analytics.d.ts
CHANGED
|
@@ -1,14 +1,56 @@
|
|
|
1
1
|
import type { KeyValueStore } from "@traqio/core";
|
|
2
|
+
import { type WebVitalsOptions } from "./vitals";
|
|
2
3
|
export interface AnalyticsOptions {
|
|
3
4
|
/** A page view on load and on every client-side route change (history API). Default true. */
|
|
4
5
|
pageviews?: boolean;
|
|
5
6
|
/** Clicks on links and buttons: their text and link. Default true. */
|
|
6
7
|
clicks?: boolean;
|
|
8
|
+
/**
|
|
9
|
+
* Rage clicks: 3 or more clicks within a second on the same spot, on ANY element —
|
|
10
|
+
* a dead button, a picture that looks like a link. One `$rageclick` per burst, with
|
|
11
|
+
* the element's text and tag. Default true.
|
|
12
|
+
*/
|
|
13
|
+
rageClicks?: boolean;
|
|
7
14
|
/** The deepest of 25/50/75/100 % each page view reached, sent once per page view. Default true. */
|
|
8
15
|
scroll?: boolean;
|
|
9
16
|
/** Uncaught errors and unhandled promise rejections. Default true. */
|
|
10
17
|
errors?: boolean;
|
|
18
|
+
/**
|
|
19
|
+
* Core Web Vitals — LCP, INP, CLS, FCP, TTFB — of a SAMPLE of page loads: one
|
|
20
|
+
* `$web_vitals` per measured load, sent when the page is first hidden. Default on, at
|
|
21
|
+
* `sampleRate` 0.5; `{ sampleRate: 0.1 }` on a very busy site, `false` to turn it off.
|
|
22
|
+
*/
|
|
23
|
+
webVitals?: boolean | WebVitalsOptions;
|
|
11
24
|
}
|
|
25
|
+
/** The accounts a visitor belongs to, by group type: `{ company: "acme" }`. */
|
|
26
|
+
export type Groups = Record<string, string>;
|
|
27
|
+
/** Group types held at once (the server's cap) — a new one past it is ignored. */
|
|
28
|
+
export declare const MAX_GROUP_TYPES = 5;
|
|
29
|
+
/** A group type as the server takes it (`company`, `workspace`), or null. */
|
|
30
|
+
export declare function cleanGroupType(v: unknown): string | null;
|
|
31
|
+
/** An account key: trimmed text (or a number) of 1–256 characters, or null. */
|
|
32
|
+
export declare function cleanGroupKey(v: unknown): string | null;
|
|
33
|
+
/** Stored groups re-read through the same rules: valid pairs only, at most 5. */
|
|
34
|
+
export declare function cleanGroups(v: unknown): Groups;
|
|
35
|
+
/** The groups after `group(type, key)`, or null to ignore the call (bad type/key, a 6th new type); `changed` = a new key. */
|
|
36
|
+
export declare function withGroup(groups: Groups, type: unknown, key: unknown): {
|
|
37
|
+
groups: Groups;
|
|
38
|
+
type: string;
|
|
39
|
+
key: string;
|
|
40
|
+
changed: boolean;
|
|
41
|
+
} | null;
|
|
42
|
+
/** A user property or super-property value: a label to segment by. */
|
|
43
|
+
export type TraitValue = string | number | boolean;
|
|
44
|
+
export type Traits = Record<string, TraitValue>;
|
|
45
|
+
/** Traits one identify call carries at most, and super-properties kept at most (the server's cap). */
|
|
46
|
+
export declare const MAX_TRAITS = 50;
|
|
47
|
+
/**
|
|
48
|
+
* Traits as the server keeps them: valid keys, values that are text (cut at 200), a
|
|
49
|
+
* finite number or a boolean — a nested object, null or a function never travels.
|
|
50
|
+
*/
|
|
51
|
+
export declare function cleanTraits(v: unknown): Traits;
|
|
52
|
+
/** An event's properties over the super-properties (the event's own win); past the server's cap, its own alone. */
|
|
53
|
+
export declare function withSuperProps(own: Record<string, unknown> | undefined, sup: Traits): Record<string, unknown> | undefined;
|
|
12
54
|
/** Where the SESSION landed from — copied on each of its events (the server stores it per row). */
|
|
13
55
|
export interface SessionSource {
|
|
14
56
|
utmSource?: string;
|
|
@@ -25,20 +67,57 @@ export interface AnalyticsDeps {
|
|
|
25
67
|
store: KeyValueStore & {
|
|
26
68
|
getSync(key: string): string | null;
|
|
27
69
|
setSync(key: string, value: string): void;
|
|
70
|
+
removeSync(key: string): void;
|
|
28
71
|
};
|
|
29
72
|
endpoint: string;
|
|
30
73
|
sdkKey: string;
|
|
31
74
|
anonymousId: () => string | null;
|
|
32
75
|
userId: () => string | undefined;
|
|
76
|
+
/** `group()`'s accounts, read at each event. */
|
|
77
|
+
groups?: () => Groups;
|
|
78
|
+
/** `register()`'s super-properties, read at each event. */
|
|
79
|
+
superProps?: () => Traits;
|
|
33
80
|
isDebug: () => boolean;
|
|
34
81
|
isForgotten: () => boolean;
|
|
82
|
+
/** The browser opted itself out as the team's own (`?traqio_internal=1`): batches say so. */
|
|
83
|
+
isInternal?: () => boolean;
|
|
35
84
|
options: AnalyticsOptions;
|
|
36
85
|
}
|
|
37
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
|
+
};
|
|
38
103
|
/** 30 minutes without activity end a session — the convention PostHog and GA share. */
|
|
39
104
|
export declare const SESSION_IDLE_MS: number;
|
|
40
105
|
/** The landing's source: the URL's utm_* and click ids, and the referrer when it is another site. */
|
|
41
106
|
export declare function landingSource(href: string, referrer: string): SessionSource | undefined;
|
|
107
|
+
/** Clicks within this long of the burst's first one, and this close to it, are one burst. */
|
|
108
|
+
export declare const RAGE_WINDOW_MS = 1000;
|
|
109
|
+
export declare const RAGE_RADIUS_PX = 30;
|
|
110
|
+
export declare const RAGE_THRESHOLD = 3;
|
|
111
|
+
/**
|
|
112
|
+
* Rage-click detection, pure: feed it every click's position and time, it answers
|
|
113
|
+
* `true` ONCE per burst — on the click that makes the third within a second and 30 px
|
|
114
|
+
* of the burst's first. The burst then has to go quiet for a second before another
|
|
115
|
+
* can be reported, so ten furious clicks are one rage click, not three.
|
|
116
|
+
*/
|
|
117
|
+
export declare class RageClickDetector {
|
|
118
|
+
private burst;
|
|
119
|
+
hit(x: number, y: number, t: number): boolean;
|
|
120
|
+
}
|
|
42
121
|
export declare class AnalyticsTracker {
|
|
43
122
|
private readonly deps;
|
|
44
123
|
private queue;
|
|
@@ -48,11 +127,30 @@ export declare class AnalyticsTracker {
|
|
|
48
127
|
private scrollUrl;
|
|
49
128
|
private errorsThisPage;
|
|
50
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;
|
|
132
|
+
private readonly rage;
|
|
133
|
+
private vitals;
|
|
134
|
+
private vitalsUrl;
|
|
51
135
|
constructor(deps: AnalyticsDeps);
|
|
52
136
|
/** Wires the listeners the options ask for and records the first page view. */
|
|
53
137
|
start(): void;
|
|
54
138
|
/** A custom analytics event — never a conversion (that is `track()`). */
|
|
55
139
|
capture(name: string, props?: Record<string, unknown>): void;
|
|
140
|
+
/**
|
|
141
|
+
* The user's properties (`identify(userId, traits, traitsOnce)`): one entry in the
|
|
142
|
+
* batch, under its own user id. Not activity — it neither opens nor prolongs a session.
|
|
143
|
+
*/
|
|
144
|
+
identify(userId: string, set: Traits, setOnce: Traits): void;
|
|
145
|
+
/** An account's properties (`group(type, key, traits)`): one entry in the batch. Not activity either. */
|
|
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;
|
|
56
154
|
/** Records a page view of the current URL. Called by itself on route changes; exposed for routers that need it. */
|
|
57
155
|
pageview(): void;
|
|
58
156
|
/** Sends what is queued. `leaving` = the page is going away: keepalive, so the request outlives it. */
|
|
@@ -63,11 +161,15 @@ export declare class AnalyticsTracker {
|
|
|
63
161
|
private session;
|
|
64
162
|
private enqueue;
|
|
65
163
|
private leave;
|
|
164
|
+
/** Once per load, at the first hide: LCP is final then, and CLS and INP have seen the visit. */
|
|
165
|
+
private sendVitals;
|
|
66
166
|
/** Single-page apps change the URL through the history API: wrap it, and listen for back/forward. */
|
|
67
167
|
private watchRoutes;
|
|
68
168
|
private onScroll;
|
|
69
169
|
private sendScroll;
|
|
70
170
|
private onClick;
|
|
171
|
+
/** Any element: frustration is most telling where nothing was clickable. */
|
|
172
|
+
private onRageClick;
|
|
71
173
|
private onError;
|
|
72
174
|
private onRejection;
|
|
73
175
|
}
|