@vllnt/analytics 0.2.0-canary.13db137 → 0.2.0-canary.2103207

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/docs/API.md ADDED
@@ -0,0 +1,524 @@
1
+ # @vllnt/analytics API
2
+
3
+ Reference for every public export of this package version, written from `src/index.ts` and
4
+ `src/react.tsx`. Those sources ship in this package under `src/`. Errors and their fixes are in the
5
+ README: [Errors and fixes](../README.md#errors-and-fixes).
6
+
7
+ The library has no transport. Events are queued and gated by consent, then dropped; with
8
+ `debug: true` they are written to `console.log`. Send them to your own backend yourself.
9
+
10
+ ## Entry points
11
+
12
+ Only these subpaths are exported (`package.json` `exports`). Each has ESM, CommonJS and type
13
+ declarations in `dist/`.
14
+
15
+ | Import | Contents |
16
+ | ------------------------------- | ------------------------------------------------------------------ |
17
+ | `@vllnt/analytics` | Tracking functions, consent functions and constants, types |
18
+ | `@vllnt/analytics/react` | `AnalyticsProvider`, `useAnalytics`, `useConsent`, `initAnalytics` |
19
+ | `@vllnt/analytics/package.json` | Package metadata |
20
+
21
+ Types are exported from `@vllnt/analytics` only. Within one module format, both entry points share
22
+ one analytics state, so `initAnalytics` from `./react` and `track` from `.` act on the same instance.
23
+ The ESM and CommonJS builds each have their own copy, so mixing `import` and `require` in one
24
+ process gives two instances.
25
+
26
+ Peer dependency: `react` `^18.0.0 || ^19.0.0` (used by `./react` only). TypeScript users need
27
+ `@types/react` 18.2.6 or later: the `./react` declarations use `React.JSX.Element`.
28
+
29
+ All browser APIs are guarded, so importing on the server is safe. On the server `track`,
30
+ `trackPage`, `identify` and `resetAnalytics` return early, `loadConsent` returns `null`, and
31
+ `isStorageAvailable` and `isDoNotTrackEnabled` return `false`. `getConsentFromCookie` only parses
32
+ the string it is given, so it is the consent reader to use on the server. `initAnalytics`,
33
+ `enableAnalytics` and `disableAnalytics` have no server guard: `initAnalytics` on the server
34
+ starts the process-wide instance, so call it only in the browser.
35
+
36
+ ## Tracking (`@vllnt/analytics`)
37
+
38
+ ### `initAnalytics`
39
+
40
+ Signature: `initAnalytics(config: AnalyticsConfig): void`
41
+
42
+ Starts the shared analytics instance and flushes queued events. Later calls do nothing while an
43
+ instance exists. It does not start, and blocks tracking, when Do Not Track is on (`"1"` or
44
+ `"yes"`) or stored consent has `analytics: false`; already queued events are then dropped, so
45
+ they are never flushed later. With no stored consent it starts, so call it only after the user
46
+ accepts if you need opt-in (`AnalyticsProvider` does this for you). Once tracking is blocked (by a
47
+ blocked init or by `disableAnalytics`), calling `initAnalytics` again does not unblock it; only
48
+ `enableAnalytics` does. It applies `config.maxQueuedEvents` first, so only the first
49
+ `maxQueuedEvents` queued events are kept.
50
+
51
+ ```ts
52
+ import { initAnalytics } from "@vllnt/analytics";
53
+
54
+ initAnalytics({ app: "my-app", version: 1, debug: true });
55
+ ```
56
+
57
+ ### `track`
58
+
59
+ Signature: `track(name: string, properties?: Record<string, unknown>): void`
60
+
61
+ Records a custom event. Before `initAnalytics` the event is queued; while blocked it is dropped.
62
+ An empty or non-string `name` is ignored. The queue keeps the first `maxQueuedEvents` calls to
63
+ `track` and `trackPage` together (default `DEFAULT_MAX_QUEUED_EVENTS`, 100) and drops later
64
+ calls. Events after init are not limited.
65
+
66
+ ```ts
67
+ import { track } from "@vllnt/analytics";
68
+
69
+ track("button_click", { id: "cta-hero" });
70
+ ```
71
+
72
+ ### `trackPage`
73
+
74
+ Signature: `trackPage(properties?: Record<string, unknown>): void`
75
+
76
+ Records a page view, queued before init like `track`. Page views and custom events share one
77
+ queue limit.
78
+
79
+ ```ts
80
+ import { trackPage } from "@vllnt/analytics";
81
+
82
+ trackPage({ path: "/about" });
83
+ ```
84
+
85
+ ### `identify`
86
+
87
+ Signature: `identify(userId: string, traits?: Record<string, unknown>): void`
88
+
89
+ Stores a user id and traits on the started instance. Ignored before init, while blocked, or
90
+ when `userId` is empty or not a string. Not queued.
91
+
92
+ ```ts
93
+ import { identify } from "@vllnt/analytics";
94
+
95
+ identify("user-123", { plan: "pro" });
96
+ ```
97
+
98
+ ### `resetAnalytics`
99
+
100
+ Signature: `resetAnalytics(): void`
101
+
102
+ Clears the stored user id and traits (for logout). The instance stays started.
103
+
104
+ ```ts
105
+ import { resetAnalytics } from "@vllnt/analytics";
106
+
107
+ resetAnalytics();
108
+ ```
109
+
110
+ ### `enableAnalytics`
111
+
112
+ Signature: `enableAnalytics(config: AnalyticsConfig): void`
113
+
114
+ Unblocks, discards the current instance and runs `initAnalytics(config)` again. Does nothing
115
+ unless tracking is currently blocked, and does nothing while Do Not Track is on. Because it
116
+ re-reads stored consent, save the accepted consent first: a stored `analytics: false` blocks it
117
+ again. The new instance has no `userId` or `traits`.
118
+
119
+ ```ts
120
+ import { createAcceptAllConsent, enableAnalytics, saveConsent } from "@vllnt/analytics";
121
+
122
+ saveConsent(createAcceptAllConsent());
123
+ enableAnalytics({ app: "my-app" });
124
+ ```
125
+
126
+ ### `disableAnalytics`
127
+
128
+ Signature: `disableAnalytics(): void`
129
+
130
+ Blocks tracking and empties the queue after consent is revoked. It keeps the in-memory user id
131
+ and traits; call `resetAnalytics()` or reload the page to clear them.
132
+
133
+ ```ts
134
+ import { disableAnalytics, resetAnalytics } from "@vllnt/analytics";
135
+
136
+ disableAnalytics();
137
+ resetAnalytics();
138
+ ```
139
+
140
+ ### `isAnalyticsEnabled`
141
+
142
+ Signature: `isAnalyticsEnabled(): boolean`
143
+
144
+ `true` when the instance has started and is not blocked.
145
+
146
+ ```ts
147
+ import { isAnalyticsEnabled, track } from "@vllnt/analytics";
148
+
149
+ if (isAnalyticsEnabled()) track("export_clicked");
150
+ ```
151
+
152
+ ### `getAnalyticsState`
153
+
154
+ Signature: `getAnalyticsState(): { app: string; debug?: boolean; version?: number; userId?: string; traits?: Record<string, unknown> } | undefined`
155
+
156
+ Returns the current instance state for debugging, or `undefined` before init. The state type is
157
+ not exported.
158
+
159
+ ```ts
160
+ import { getAnalyticsState } from "@vllnt/analytics";
161
+
162
+ console.log(getAnalyticsState()?.userId);
163
+ ```
164
+
165
+ ### `DEFAULT_MAX_QUEUED_EVENTS`
166
+
167
+ Value: `100`
168
+
169
+ The default for `AnalyticsConfig.maxQueuedEvents`: how many `track` and `trackPage` calls are
170
+ queued before init. A `maxQueuedEvents` that is not a non-negative safe integer (for example
171
+ `-1`, `1.5` or `NaN`) means this default. `0` queues nothing. `AnalyticsProvider` applies
172
+ `config.maxQueuedEvents` on mount, before `initAnalytics` runs. Without the provider, the default
173
+ applies until `initAnalytics(config)` runs.
174
+
175
+ ```ts
176
+ import { DEFAULT_MAX_QUEUED_EVENTS, initAnalytics } from "@vllnt/analytics";
177
+
178
+ initAnalytics({ app: "my-app", maxQueuedEvents: DEFAULT_MAX_QUEUED_EVENTS * 2 });
179
+ ```
180
+
181
+ ### Tutorial helpers
182
+
183
+ Typed wrappers around `track` with fixed event names (`TutorialEventName`). Every helper
184
+ needs `tutorial_id`, `tutorial_title` and `locale`.
185
+
186
+ | Function | Event name | Extra required fields |
187
+ | ------------------------------------- | --------------------------- | ------------------------------- |
188
+ | `trackTutorialStart(properties)` | `tutorial_start` | none (`section_id` not allowed) |
189
+ | `trackSectionView(properties)` | `tutorial_section_view` | none |
190
+ | `trackSectionComplete(properties)` | `tutorial_section_complete` | none |
191
+ | `trackTutorialComplete(properties)` | `tutorial_complete` | none (`section_id` not allowed) |
192
+ | `trackTutorialNavigation(properties)` | `tutorial_navigation` | `navigation_type` |
193
+ | `trackTimeSpent(properties)` | `tutorial_time_spent` | `time_spent_seconds` |
194
+
195
+ ```ts
196
+ import { trackSectionView, trackTutorialNavigation } from "@vllnt/analytics";
197
+
198
+ const base = { tutorial_id: "intro", tutorial_title: "Intro", locale: "en" };
199
+ trackSectionView({ ...base, section_id: "setup", section_index: 0, total_sections: 5 });
200
+ trackTutorialNavigation({ ...base, section_id: "setup", navigation_type: "next" });
201
+ ```
202
+
203
+ ## Consent (`@vllnt/analytics`)
204
+
205
+ Consent is stored as JSON in `localStorage` under `CONSENT_STORAGE_KEY` and mirrored to the
206
+ `CONSENT_COOKIE_NAME` cookie (`1` allowed, `0` declined; one-year expiry, `path=/`,
207
+ `SameSite=Lax`) so the server can read it.
208
+
209
+ ### Constants
210
+
211
+ | Name | Value |
212
+ | --------------------- | --------------------------- |
213
+ | `CONSENT_VERSION` | `1` |
214
+ | `CONSENT_STORAGE_KEY` | `"vllnt:analytics-consent"` |
215
+ | `CONSENT_COOKIE_NAME` | `"vllnt-consent"` |
216
+
217
+ ### `loadConsent`
218
+
219
+ Signature: `loadConsent(): ConsentState | null`
220
+
221
+ Reads and validates stored consent. `null` when none is stored, the value is invalid, there is no
222
+ `window`, or reading `localStorage` throws. It reads without a write probe, so a stored choice is
223
+ found even when the storage quota is full.
224
+
225
+ ```ts
226
+ import { loadConsent } from "@vllnt/analytics";
227
+
228
+ const consent = loadConsent();
229
+ console.log(consent?.analytics);
230
+ ```
231
+
232
+ ### `saveConsent`
233
+
234
+ Signature: `saveConsent(state: ConsentState): void`
235
+
236
+ Writes consent to `localStorage` and the cookie. Does nothing, without a warning, when
237
+ `isStorageAvailable()` is `false`, which includes a completely full quota. Logs
238
+ `Failed to save consent:` with `console.warn` when that probe succeeds but writing the consent
239
+ value or the cookie throws.
240
+
241
+ ```ts
242
+ import { createAcceptAllConsent, saveConsent } from "@vllnt/analytics";
243
+
244
+ saveConsent(createAcceptAllConsent());
245
+ ```
246
+
247
+ ### `clearConsent`
248
+
249
+ Signature: `clearConsent(): void`
250
+
251
+ Removes stored consent and expires the cookie.
252
+
253
+ ```ts
254
+ import { clearConsent } from "@vllnt/analytics";
255
+
256
+ clearConsent();
257
+ ```
258
+
259
+ ### `createAcceptAllConsent`
260
+
261
+ Signature: `createAcceptAllConsent(): ConsentState`
262
+
263
+ New state with `analytics: true`, `functional: true`, the current time and `CONSENT_VERSION`. To
264
+ record another version, override it: `{ ...createAcceptAllConsent(), version: 2 }`.
265
+
266
+ ```ts
267
+ import { createAcceptAllConsent } from "@vllnt/analytics";
268
+
269
+ const accepted = createAcceptAllConsent();
270
+ console.log(accepted.version);
271
+ ```
272
+
273
+ ### `createDeclineAnalyticsConsent`
274
+
275
+ Signature: `createDeclineAnalyticsConsent(): ConsentState`
276
+
277
+ New state with `analytics: false`, `functional: true`, the current time and `CONSENT_VERSION`.
278
+
279
+ ```ts
280
+ import { createDeclineAnalyticsConsent } from "@vllnt/analytics";
281
+
282
+ const declined = createDeclineAnalyticsConsent();
283
+ console.log(declined.analytics);
284
+ ```
285
+
286
+ ### `updateConsentCategory`
287
+
288
+ Signature: `updateConsentCategory(current: ConsentState, category: ConsentCategory, value: boolean): ConsentState`
289
+
290
+ Returns a copy with one category changed and a new timestamp. Setting `functional` to `false`
291
+ returns `current` unchanged.
292
+
293
+ ```ts
294
+ import { loadConsent, saveConsent, updateConsentCategory } from "@vllnt/analytics";
295
+
296
+ const stored = loadConsent();
297
+ if (stored) saveConsent(updateConsentCategory(stored, "analytics", false));
298
+ ```
299
+
300
+ ### `needsRePrompt`
301
+
302
+ Signature: `needsRePrompt(consent: ConsentState, currentVersion: number): boolean`
303
+
304
+ `true` when `consent.version < currentVersion`.
305
+
306
+ ```ts
307
+ import { clearConsent, loadConsent, needsRePrompt } from "@vllnt/analytics";
308
+
309
+ const consent = loadConsent();
310
+ if (consent && needsRePrompt(consent, 2)) clearConsent();
311
+ ```
312
+
313
+ ### `getConsentFromCookie`
314
+
315
+ Signature: `getConsentFromCookie(cookieString: string): boolean | null`
316
+
317
+ Parses a `Cookie` header: `true` for `1`, `false` for any other value, `null` when the cookie is
318
+ absent or empty (`vllnt-consent=`). The name must start the string or follow `;`, so
319
+ `evil-vllnt-consent=1` does not match.
320
+
321
+ ```ts
322
+ import { getConsentFromCookie } from "@vllnt/analytics";
323
+
324
+ export function isAllowed(request: Request): boolean | null {
325
+ return getConsentFromCookie(request.headers.get("cookie") ?? "");
326
+ }
327
+ ```
328
+
329
+ ### `isDoNotTrackEnabled`
330
+
331
+ Signature: `isDoNotTrackEnabled(): boolean`
332
+
333
+ Reads `navigator.doNotTrack`, then `window.doNotTrack`, then `navigator.msDoNotTrack`, and
334
+ returns `true` when the first one that is set equals `"1"` or `"yes"`. `false` on the server.
335
+
336
+ ```ts
337
+ import { isDoNotTrackEnabled } from "@vllnt/analytics";
338
+
339
+ const showBanner = !isDoNotTrackEnabled();
340
+ console.log(showBanner);
341
+ ```
342
+
343
+ ### `isStorageAvailable`
344
+
345
+ Signature: `isStorageAvailable(): boolean`
346
+
347
+ `true` when a test key can be written to and removed from `localStorage`. `false` on the server
348
+ or when that probe throws, for example when storage is blocked or the quota is full. `loadConsent`
349
+ does not depend on it, so a stored choice can still be read when it is `false`.
350
+
351
+ ```ts
352
+ import { isStorageAvailable } from "@vllnt/analytics";
353
+
354
+ if (!isStorageAvailable()) console.warn("consent will not persist");
355
+ ```
356
+
357
+ ### `isValidConsentState`
358
+
359
+ Signature: `isValidConsentState(value: unknown): value is ConsentState`
360
+
361
+ Type guard: checks the four fields and their types.
362
+
363
+ ```ts
364
+ import { isValidConsentState, saveConsent } from "@vllnt/analytics";
365
+
366
+ const parsed: unknown = JSON.parse('{"analytics":true,"functional":true,"timestamp":"2026-01-01T00:00:00.000Z","version":1}');
367
+ if (isValidConsentState(parsed)) saveConsent(parsed);
368
+ ```
369
+
370
+ ## React (`@vllnt/analytics/react`)
371
+
372
+ Both builds of this entry (`dist/react.js` and `dist/react.cjs`) start with `"use client"`; the
373
+ core entry does not.
374
+
375
+ Both `AnalyticsProvider` and `useConsent` read stored consent during the first render. On the
376
+ server that read returns `null`, so `consent` is `null` and `hasResponded` is `false` there, while
377
+ the first browser render may already see stored consent. If you render a banner from
378
+ `hasResponded`, render it only after the component has mounted to avoid a hydration mismatch.
379
+
380
+ ### `AnalyticsProvider`
381
+
382
+ Signature: `AnalyticsProvider(props: { children: ReactNode; config: AnalyticsConfig; consentVersion?: number }): React.JSX.Element`
383
+
384
+ `consentVersion` defaults to `CONSENT_VERSION`. The provider reads stored consent, clears it when
385
+ `needsRePrompt(stored, consentVersion)`, calls `initAnalytics(config)` while consent has
386
+ `analytics: true`, and calls `disableAnalytics()` while it has `analytics: false`, so after a
387
+ stored decline `track()` calls are dropped, not queued. `acceptAll` and `declineAnalytics` save
388
+ consent with `consentVersion`; `updateConsent` copies the current consent, so it keeps the stored
389
+ version. The actions call `enableAnalytics` or `disableAnalytics`. Before the user responds (no
390
+ stored consent), events are queued in memory and flushed if the user accepts; the provider applies
391
+ `config.maxQueuedEvents` during its first render, so the queue keeps the first `maxQueuedEvents`
392
+ events (default 100), including events from child effects on mount. After
393
+ `resetConsent()`, tracking is blocked and events are dropped until the user responds again. Pass a stable
394
+ `config` (for example a module constant), because the init effect depends on it.
395
+
396
+ ```tsx
397
+ import type { ReactNode } from "react";
398
+ import { AnalyticsProvider } from "@vllnt/analytics/react";
399
+
400
+ const config = { app: "my-app", version: 1 };
401
+
402
+ export function Root({ children }: { children: ReactNode }) {
403
+ return <AnalyticsProvider config={config}>{children}</AnalyticsProvider>;
404
+ }
405
+ ```
406
+
407
+ ### `useAnalytics`
408
+
409
+ Signature: `useAnalytics(): UseConsentReturn & { config: AnalyticsConfig }`
410
+
411
+ Returns the provider context; throws `useAnalytics must be used within an AnalyticsProvider`
412
+ outside one. Fields: `config`, `consent`, `hasResponded`, `isLoading` (always `false`),
413
+ `acceptAll()`, `declineAnalytics()`, `updateConsent(category, value)`, `resetConsent()`.
414
+ `updateConsent` does nothing while `consent` is `null` (before the user has responded).
415
+
416
+ ```tsx
417
+ import { useEffect, useState } from "react";
418
+ import { useAnalytics } from "@vllnt/analytics/react";
419
+
420
+ export function CookieBanner() {
421
+ const { hasResponded, acceptAll, declineAnalytics } = useAnalytics();
422
+ const [mounted, setMounted] = useState(false);
423
+ useEffect(() => setMounted(true), []);
424
+ if (!mounted || hasResponded) return null;
425
+ return (
426
+ <div>
427
+ <button onClick={acceptAll}>Accept</button>
428
+ <button onClick={declineAnalytics}>Decline</button>
429
+ </div>
430
+ );
431
+ }
432
+ ```
433
+
434
+ ### `useConsent`
435
+
436
+ Signature: `useConsent(options?: UseConsentOptions): UseConsentReturn`
437
+
438
+ Standalone consent state with the same fields as `useAnalytics()` except `config`. It reads and
439
+ writes stored consent only: it does not start or stop analytics and does not share state with
440
+ `AnalyticsProvider`. Inside a provider, use `useAnalytics()` for a banner so accepting starts
441
+ tracking.
442
+
443
+ ```tsx
444
+ import { useConsent } from "@vllnt/analytics/react";
445
+
446
+ export function ConsentStatus() {
447
+ const { consent, updateConsent } = useConsent({ consentVersion: 1 });
448
+ return (
449
+ <button onClick={() => updateConsent("analytics", !consent?.analytics)}>
450
+ Analytics: {consent?.analytics ? "on" : "off"}
451
+ </button>
452
+ );
453
+ }
454
+ ```
455
+
456
+ ### `initAnalytics` (re-export)
457
+
458
+ Re-export of the core [`initAnalytics`](#initanalytics), for apps that only import from `./react`.
459
+
460
+ ### Consent versions
461
+
462
+ In `AnalyticsProvider` and `useConsent`, `acceptAll` and `declineAnalytics` save the choice with
463
+ `consentVersion`. `updateConsent` copies the current consent, so it keeps the stored version. Raising
464
+ `consentVersion` asks every visitor once more; the new choice is then kept on later visits.
465
+
466
+ ## Types
467
+
468
+ Exported from `@vllnt/analytics` as types only.
469
+
470
+ ```ts
471
+ type AnalyticsConfig = {
472
+ app: string;
473
+ debug?: boolean;
474
+ maxQueuedEvents?: number; // events queued before init; default 100, 0 queues nothing
475
+ version?: number;
476
+ };
477
+
478
+ type AnalyticsEvent = {
479
+ name: string;
480
+ properties?: Record<string, unknown>;
481
+ timestamp?: string;
482
+ };
483
+
484
+ type ConsentCategory = "analytics" | "functional";
485
+
486
+ type ConsentState = {
487
+ analytics: boolean;
488
+ functional: boolean;
489
+ timestamp: string; // ISO time of the choice
490
+ version: number;
491
+ };
492
+
493
+ type TutorialEventName =
494
+ | "tutorial_complete"
495
+ | "tutorial_navigation"
496
+ | "tutorial_section_complete"
497
+ | "tutorial_section_view"
498
+ | "tutorial_start"
499
+ | "tutorial_time_spent";
500
+
501
+ type TutorialEventProperties = {
502
+ tutorial_id: string;
503
+ tutorial_title: string;
504
+ locale: string;
505
+ completion_percent?: number; // 0-100
506
+ navigation_type?: "keyboard" | "next" | "prev" | "toc";
507
+ section_id?: string;
508
+ section_index?: number; // 0-based
509
+ time_spent_seconds?: number;
510
+ total_sections?: number;
511
+ };
512
+
513
+ type UseConsentOptions = { consentVersion?: number };
514
+
515
+ type UseConsentReturn = {
516
+ acceptAll: () => void;
517
+ consent: ConsentState | null;
518
+ declineAnalytics: () => void;
519
+ hasResponded: boolean;
520
+ isLoading: boolean;
521
+ resetConsent: () => void;
522
+ updateConsent: (category: ConsentCategory, value: boolean) => void;
523
+ };
524
+ ```
package/llms.txt ADDED
@@ -0,0 +1,77 @@
1
+ # @vllnt/analytics
2
+
3
+ > Cookie consent state and consent-gated event tracking for React and TypeScript, with Do Not Track and an SSR consent cookie.
4
+
5
+ Facts for AI tools. The library has no transport: it queues and gates events, and only
6
+ `console.log`s them when `debug: true`. Events never leave the page.
7
+
8
+ ## For coding agents
9
+
10
+ The docs are bundled in the package at `node_modules/@vllnt/analytics/docs/`, with this file,
11
+ `CHANGELOG.md` and the TypeScript sources in `src/`. They match the installed version. Read them
12
+ before writing code.
13
+
14
+ ## When to use
15
+
16
+ - Use it when a React app needs cookie-banner state stored in `localStorage` and a cookie.
17
+ - Use it when tracking calls must wait for consent and stop on Do Not Track or a decline.
18
+ - Use it when server code must read the visitor's choice from the `Cookie` header.
19
+ - Do not use it when events must reach a backend: it sends nothing; use your analytics vendor's SDK.
20
+ - Do not use it for a server-side consent record: use `@vllnt/convex-consent`.
21
+
22
+ ## Package
23
+
24
+ - npm: `@vllnt/analytics` (MIT), repo: https://github.com/vllnt/analytics
25
+ - Unreleased builds from `main` use the `canary` dist-tag
26
+ - Ships ESM, CommonJS and type declarations from `dist` (built with tsup); both builds of
27
+ `./react` start with `"use client"`
28
+ - Entry points: `@vllnt/analytics` (core + consent + types), `@vllnt/analytics/react` (`AnalyticsProvider`, `useAnalytics`, `useConsent`, `initAnalytics`)
29
+ - Peer dependency: React `^18.0.0 || ^19.0.0`
30
+ - Install: `pnpm add @vllnt/analytics`
31
+
32
+ ## Behaviour
33
+
34
+ - Events tracked before `initAnalytics()` are queued and flushed on the first unblocked init;
35
+ a blocked init drops them. The queue keeps the first `maxQueuedEvents` `track()` and
36
+ `trackPage()` calls (default `DEFAULT_MAX_QUEUED_EVENTS`, 100; `0` queues nothing) and drops
37
+ later ones. `AnalyticsProvider` applies the option on mount; without it, the default applies
38
+ until `initAnalytics()` runs.
39
+ - `initAnalytics()` blocks tracking when Do Not Track is on (`"1"` or `"yes"`) or stored consent
40
+ has `analytics: false`. With no stored consent it starts tracking. `AnalyticsProvider` calls it
41
+ while consent has `analytics: true` and calls `disableAnalytics()` after a stored decline, so a
42
+ declined visitor's events are dropped. Before the user responds, events are queued in memory,
43
+ except after `resetConsent()`, which blocks and drops events until the user responds again.
44
+ - Once blocked, only `enableAnalytics(config)` unblocks; it re-reads stored consent.
45
+ - Consent is stored in `localStorage` (`vllnt:analytics-consent`) and a `vllnt-consent` cookie
46
+ (`1` or `0`) for SSR; `getConsentFromCookie(cookieString)` reads it with an anchored match.
47
+ - Consent is versioned; a stored version below the hook's `consentVersion` (default
48
+ `CONSENT_VERSION`) re-prompts once, and the hooks save the new choice with that version.
49
+ - `track()` ignores empty or non-string names; `identify()` ignores empty or non-string ids.
50
+ - Browser APIs are guarded, so server-side import is safe. `initAnalytics()` has no server
51
+ guard and starts the process-wide instance there, so call it only in the browser.
52
+ - The only thrown error is `useAnalytics must be used within an AnalyticsProvider`; there are no
53
+ error codes.
54
+
55
+ ## Docs
56
+
57
+ - [API reference](./docs/API.md): every public export, signatures, examples, entry points
58
+ - [Errors and fixes](./README.md#errors-and-fixes): every message the package throws or logs
59
+ - [Limits and gotchas](./README.md#limits-and-gotchas): defaults and calls that do nothing
60
+ - [README](./README.md): usage, common tasks, compatibility
61
+ - [Changelog](./CHANGELOG.md): release history
62
+ - [Core exports](./src/index.ts) and [React exports](./src/react.tsx): source of the public API
63
+
64
+ ## Optional
65
+
66
+ - [Contributing](https://github.com/vllnt/analytics/blob/main/CONTRIBUTING.md): setup, checks, release flow
67
+ - [Security policy](https://github.com/vllnt/analytics/blob/main/SECURITY.md): private vulnerability reporting
68
+ - [AGENTS.md](https://github.com/vllnt/analytics/blob/main/AGENTS.md): instructions for contributing agents
69
+
70
+ ## Ecosystem
71
+
72
+ - [Package page](https://oss.vllnt.com/packages/analytics)
73
+ - [Release feed](https://oss.vllnt.com/packages/analytics/rss.xml)
74
+ - [All @vllnt packages](https://oss.vllnt.com/llms.txt)
75
+ - [Search vllnt packages and docs](https://index.vllnt.com)
76
+ - [vllnt](https://vllnt.com/llms.txt)
77
+ - [Author](https://bntvllnt.com)