@traqio/web 0.4.1 → 0.5.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 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,49 @@ 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
+ - **User properties** — `Traqio.identify(user.id, { plan: "pro", seats: 12 }, { signup_source: "webinar" })`
134
+ sets the user id and the user's properties: the second object overwrites, the third is
135
+ written only where the user has no value yet. Read them as a breakdown (`user:plan`), a
136
+ report dimension (`user__plan`) and a cohort condition. Text, numbers, booleans — never
137
+ an email or a name; at most 50 keys.
138
+ - **Super-properties** — `Traqio.register({ pricing_variant: "b" })` adds properties to
139
+ every analytics event from now on (and on the next pages); an event's own property of the
140
+ same name wins. `Traqio.unregister("pricing_variant")` removes one.
124
141
 
125
142
  **`capture()` is not `track()`.** An analytics event is never billed, never counted as a
126
143
  conversion and never sent to an ad network — it has its own route and its own table.
127
144
  A sign-up or a purchase stays `track()` (or your backend's server SDK).
128
145
 
146
+ ## Groups (B2B accounts)
147
+
148
+ If you sell to teams, tell Traqio which account the visitor works in, right after
149
+ sign-in. Every analytics event that follows — on this page and the next ones — carries
150
+ it (`groups: { company: "acc_42" }`), so the console's Analytics can count accounts
151
+ instead of people: funnels and retention by account, unique accounts, and the list of
152
+ your accounts with their properties. Needs `analytics` at init.
153
+
154
+ ```ts
155
+ Traqio.group("company", account.id, { plan: "enterprise", seats: 40 }); // traits optional
156
+ Traqio.getGroups(); // { company: "acc_42" }
157
+ Traqio.resetGroups(); // on sign-out
158
+ ```
159
+
160
+ The type is lowercase (`company`, `workspace`, `team`: a letter, then letters, digits or
161
+ `_`, 30 characters at most), 5 types at most; the key is your own account id, 1–256
162
+ characters — never an email. Traits follow the identify rules (a 4th argument fills only
163
+ what is unset). The same key again with no traits sends nothing new, so calling it on
164
+ every page is free. `forgetMe()` forgets the groups too; an invalid call is ignored.
165
+
129
166
  ## Frameworks
130
167
 
131
168
  - **React / Next.js** — call `init()` in a `useEffect` of a client component mounted
@@ -162,7 +199,7 @@ networks.
162
199
  | `trackIdDays` | `30` | How long the trackID and the visit's source are kept |
163
200
  | `cookies` | `true` | `false` = localStorage only, no sharing across subdomains |
164
201
  | `debug` | detected | Debug traffic, kept out of every report (see below) |
165
- | `analytics` | `false` | Product analytics: `true`, or `{ pageviews, clicks, scroll, errors }` |
202
+ | `analytics` | `false` | Product analytics: `true`, or `{ pageviews, clicks, rageClicks, scroll, errors, webVitals }` (`webVitals: { sampleRate }`, default 0.5) |
166
203
 
167
204
  **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
205
 
@@ -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;
@@ -30,6 +72,10 @@ export interface AnalyticsDeps {
30
72
  sdkKey: string;
31
73
  anonymousId: () => string | null;
32
74
  userId: () => string | undefined;
75
+ /** `group()`'s accounts, read at each event. */
76
+ groups?: () => Groups;
77
+ /** `register()`'s super-properties, read at each event. */
78
+ superProps?: () => Traits;
33
79
  isDebug: () => boolean;
34
80
  isForgotten: () => boolean;
35
81
  options: AnalyticsOptions;
@@ -39,6 +85,20 @@ export declare const SESSION_KEY = "traq_session";
39
85
  export declare const SESSION_IDLE_MS: number;
40
86
  /** The landing's source: the URL's utm_* and click ids, and the referrer when it is another site. */
41
87
  export declare function landingSource(href: string, referrer: string): SessionSource | undefined;
88
+ /** Clicks within this long of the burst's first one, and this close to it, are one burst. */
89
+ export declare const RAGE_WINDOW_MS = 1000;
90
+ export declare const RAGE_RADIUS_PX = 30;
91
+ export declare const RAGE_THRESHOLD = 3;
92
+ /**
93
+ * Rage-click detection, pure: feed it every click's position and time, it answers
94
+ * `true` ONCE per burst — on the click that makes the third within a second and 30 px
95
+ * of the burst's first. The burst then has to go quiet for a second before another
96
+ * can be reported, so ten furious clicks are one rage click, not three.
97
+ */
98
+ export declare class RageClickDetector {
99
+ private burst;
100
+ hit(x: number, y: number, t: number): boolean;
101
+ }
42
102
  export declare class AnalyticsTracker {
43
103
  private readonly deps;
44
104
  private queue;
@@ -48,11 +108,21 @@ export declare class AnalyticsTracker {
48
108
  private scrollUrl;
49
109
  private errorsThisPage;
50
110
  private stopped;
111
+ private readonly rage;
112
+ private vitals;
113
+ private vitalsUrl;
51
114
  constructor(deps: AnalyticsDeps);
52
115
  /** Wires the listeners the options ask for and records the first page view. */
53
116
  start(): void;
54
117
  /** A custom analytics event — never a conversion (that is `track()`). */
55
118
  capture(name: string, props?: Record<string, unknown>): void;
119
+ /**
120
+ * The user's properties (`identify(userId, traits, traitsOnce)`): one entry in the
121
+ * batch, under its own user id. Not activity — it neither opens nor prolongs a session.
122
+ */
123
+ identify(userId: string, set: Traits, setOnce: Traits): void;
124
+ /** An account's properties (`group(type, key, traits)`): one entry in the batch. Not activity either. */
125
+ group(groupType: string, groupKey: string, set: Traits, setOnce: Traits): void;
56
126
  /** Records a page view of the current URL. Called by itself on route changes; exposed for routers that need it. */
57
127
  pageview(): void;
58
128
  /** Sends what is queued. `leaving` = the page is going away: keepalive, so the request outlives it. */
@@ -63,11 +133,15 @@ export declare class AnalyticsTracker {
63
133
  private session;
64
134
  private enqueue;
65
135
  private leave;
136
+ /** Once per load, at the first hide: LCP is final then, and CLS and INP have seen the visit. */
137
+ private sendVitals;
66
138
  /** Single-page apps change the URL through the history API: wrap it, and listen for back/forward. */
67
139
  private watchRoutes;
68
140
  private onScroll;
69
141
  private sendScroll;
70
142
  private onClick;
143
+ /** Any element: frustration is most telling where nothing was clickable. */
144
+ private onRageClick;
71
145
  private onError;
72
146
  private onRejection;
73
147
  }