@vllnt/analytics 0.2.0-canary.13db137 → 0.2.0-canary.8064ae0
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/CHANGELOG.md +48 -0
- package/README.md +47 -13
- package/docs/API.md +384 -0
- package/llms.txt +56 -0
- package/package.json +15 -6
- package/src/analytics.ts +282 -0
- package/src/consent.ts +179 -0
- package/src/index.ts +48 -0
- package/src/react.tsx +221 -0
- package/src/types.ts +102 -0
package/CHANGELOG.md
ADDED
|
@@ -0,0 +1,48 @@
|
|
|
1
|
+
# Changelog
|
|
2
|
+
|
|
3
|
+
All notable changes to this project are documented in this file.
|
|
4
|
+
|
|
5
|
+
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.1.0/), and this project
|
|
6
|
+
adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
|
|
7
|
+
|
|
8
|
+
## [0.2.0] - Unreleased
|
|
9
|
+
|
|
10
|
+
### Added
|
|
11
|
+
|
|
12
|
+
- The package ships version-matched docs for coding agents and other readers: `docs/API.md`
|
|
13
|
+
(every public export with its signature and an example), `llms.txt`, `CHANGELOG.md` and the
|
|
14
|
+
TypeScript sources in `src/` (tests excluded). `llms.txt` links to these bundled files with
|
|
15
|
+
relative paths.
|
|
16
|
+
|
|
17
|
+
### Changed
|
|
18
|
+
|
|
19
|
+
- The package entry points now resolve to compiled output in `dist` (ESM, CommonJS and type
|
|
20
|
+
declarations) instead of raw TypeScript sources, so it works without transpiling
|
|
21
|
+
`node_modules`. The `.` and `./react` export subpaths and the `react` peer range are
|
|
22
|
+
unchanged. `./package.json` is now exported too.
|
|
23
|
+
- `track()` ignores events whose name is empty or not a string, and `identify()` ignores an empty
|
|
24
|
+
or non-string user id, instead of recording them.
|
|
25
|
+
|
|
26
|
+
### Fixed
|
|
27
|
+
|
|
28
|
+
- `getConsentFromCookie()` no longer matches look-alike cookie names that merely end with
|
|
29
|
+
`vllnt-consent` (for example `evil-vllnt-consent=1`).
|
|
30
|
+
|
|
31
|
+
### Security
|
|
32
|
+
|
|
33
|
+
- Releases are published from CI with npm trusted publishing and provenance. `@latest` is published
|
|
34
|
+
only from a `vX.Y.Z` tag on `main` that matches `package.json` and this changelog.
|
|
35
|
+
|
|
36
|
+
## [0.1.1] - 2026-03-10
|
|
37
|
+
|
|
38
|
+
### Added
|
|
39
|
+
|
|
40
|
+
- CI and publish workflows.
|
|
41
|
+
|
|
42
|
+
## [0.1.0] - 2026-03-05
|
|
43
|
+
|
|
44
|
+
### Added
|
|
45
|
+
|
|
46
|
+
- Initial release: event tracking with a pre-initialization queue, two-category consent management
|
|
47
|
+
(localStorage and SSR cookie), versioned consent re-prompts, Do Not Track support, tutorial
|
|
48
|
+
tracking helpers, and React integration (`AnalyticsProvider`, `useAnalytics`, `useConsent`).
|
package/README.md
CHANGED
|
@@ -21,16 +21,27 @@ Peer dependency: `react ^18.0.0 || ^19.0.0`
|
|
|
21
21
|
|
|
22
22
|
Ships ESM and CommonJS builds with type declarations; no bundler transpilation of `node_modules` is needed.
|
|
23
23
|
|
|
24
|
+
## For coding agents
|
|
25
|
+
|
|
26
|
+
The docs are bundled in the package at `node_modules/@vllnt/analytics/docs/` ([`docs/API.md`](docs/API.md)), together with
|
|
27
|
+
[`llms.txt`](llms.txt), `CHANGELOG.md` and the TypeScript sources in `src/`. They match the installed
|
|
28
|
+
version. Read them before writing code.
|
|
29
|
+
|
|
24
30
|
## Usage
|
|
25
31
|
|
|
26
32
|
### React Provider
|
|
27
33
|
|
|
28
34
|
```tsx
|
|
35
|
+
import type { ReactNode } from "react";
|
|
29
36
|
import { AnalyticsProvider } from "@vllnt/analytics/react";
|
|
30
37
|
|
|
31
|
-
|
|
38
|
+
// Keep config stable: an inline object re-runs the init effect and re-renders every consumer.
|
|
39
|
+
// Config changes are ignored while analytics is running (initAnalytics returns once started).
|
|
40
|
+
const config = { app: "my-app", version: 1, debug: true };
|
|
41
|
+
|
|
42
|
+
function App({ children }: { children: ReactNode }) {
|
|
32
43
|
return (
|
|
33
|
-
<AnalyticsProvider config={
|
|
44
|
+
<AnalyticsProvider config={config}>
|
|
34
45
|
{children}
|
|
35
46
|
</AnalyticsProvider>
|
|
36
47
|
);
|
|
@@ -40,12 +51,17 @@ function App() {
|
|
|
40
51
|
### Consent Banner
|
|
41
52
|
|
|
42
53
|
```tsx
|
|
43
|
-
import {
|
|
54
|
+
import { useEffect, useState } from "react";
|
|
55
|
+
import { useAnalytics } from "@vllnt/analytics/react";
|
|
44
56
|
|
|
57
|
+
// Render inside <AnalyticsProvider> so accepting starts tracking.
|
|
45
58
|
function CookieBanner() {
|
|
46
|
-
const { hasResponded,
|
|
59
|
+
const { hasResponded, acceptAll, declineAnalytics } = useAnalytics();
|
|
60
|
+
// Stored consent is read in the browser only; wait for mount to avoid a hydration mismatch.
|
|
61
|
+
const [mounted, setMounted] = useState(false);
|
|
62
|
+
useEffect(() => setMounted(true), []);
|
|
47
63
|
|
|
48
|
-
if (
|
|
64
|
+
if (!mounted || hasResponded) return null;
|
|
49
65
|
|
|
50
66
|
return (
|
|
51
67
|
<div>
|
|
@@ -57,6 +73,9 @@ function CookieBanner() {
|
|
|
57
73
|
}
|
|
58
74
|
```
|
|
59
75
|
|
|
76
|
+
`useConsent()` exposes the same consent fields without a provider, but it only reads and writes
|
|
77
|
+
stored consent: it does not start or stop analytics.
|
|
78
|
+
|
|
60
79
|
### Track Events
|
|
61
80
|
|
|
62
81
|
```tsx
|
|
@@ -127,7 +146,7 @@ import {
|
|
|
127
146
|
} from "@vllnt/analytics";
|
|
128
147
|
|
|
129
148
|
// SSR: read consent from cookie header
|
|
130
|
-
const analyticsAllowed = getConsentFromCookie(request.headers.cookie);
|
|
149
|
+
const analyticsAllowed = getConsentFromCookie(request.headers.get("cookie") ?? "");
|
|
131
150
|
|
|
132
151
|
// Check if re-prompt needed after version bump
|
|
133
152
|
const consent = loadConsent();
|
|
@@ -138,6 +157,8 @@ if (consent && needsRePrompt(consent, 2)) {
|
|
|
138
157
|
|
|
139
158
|
## API
|
|
140
159
|
|
|
160
|
+
Full reference with signatures and examples: [`docs/API.md`](docs/API.md).
|
|
161
|
+
|
|
141
162
|
### Core
|
|
142
163
|
|
|
143
164
|
| Function | Description |
|
|
@@ -176,16 +197,16 @@ if (consent && needsRePrompt(consent, 2)) {
|
|
|
176
197
|
## How It Works
|
|
177
198
|
|
|
178
199
|
```
|
|
179
|
-
|
|
200
|
+
AnalyticsProvider mounts
|
|
180
201
|
│
|
|
181
|
-
├─
|
|
202
|
+
├─ Stored consent outdated? ──▶ Clear it & re-prompt
|
|
182
203
|
│
|
|
183
|
-
├─
|
|
184
|
-
│ ├─
|
|
185
|
-
│
|
|
186
|
-
│ └─ Analytics accepted ──▶ Initialize & flush queue
|
|
204
|
+
├─ Analytics accepted ──▶ initAnalytics()
|
|
205
|
+
│ ├─ DNT enabled? ──▶ Block tracking
|
|
206
|
+
│ └─ Otherwise ──▶ Start & flush queue
|
|
187
207
|
│
|
|
188
|
-
└─
|
|
208
|
+
└─ Declined or no consent ──▶ No init; track() calls stay queued in memory
|
|
209
|
+
(declineAnalytics() blocks and empties the queue)
|
|
189
210
|
```
|
|
190
211
|
|
|
191
212
|
Events fired before initialization are queued and flushed once consent is granted and analytics initializes.
|
|
@@ -202,6 +223,19 @@ pnpm typecheck # Type-check
|
|
|
202
223
|
pnpm build # Build dist (ESM, CJS, types)
|
|
203
224
|
```
|
|
204
225
|
|
|
226
|
+
## Related packages
|
|
227
|
+
|
|
228
|
+
- [`@vllnt/convex-analytics`](https://www.npmjs.com/package/@vllnt/convex-analytics): a Convex
|
|
229
|
+
component that stores events and serves rollups, funnels and retention. Use it as the backend if
|
|
230
|
+
your app runs on Convex; this package does not send events to it for you.
|
|
231
|
+
- [`@vllnt/convex-consent`](https://www.npmjs.com/package/@vllnt/convex-consent): an append-only
|
|
232
|
+
consent ledger stored in Convex. Use it when you need a server-side record of consent per
|
|
233
|
+
subject and purpose; this package only keeps the visitor's choice in the browser.
|
|
234
|
+
|
|
205
235
|
## License
|
|
206
236
|
|
|
207
237
|
[MIT](LICENSE)
|
|
238
|
+
|
|
239
|
+
---
|
|
240
|
+
|
|
241
|
+
Part of vllnt oss: https://oss.vllnt.com
|
package/docs/API.md
ADDED
|
@@ -0,0 +1,384 @@
|
|
|
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/`.
|
|
5
|
+
|
|
6
|
+
The library has no transport. Events are queued and gated by consent, then dropped; with
|
|
7
|
+
`debug: true` they are written to `console.log`. Send them to your own backend yourself.
|
|
8
|
+
|
|
9
|
+
## Entry points
|
|
10
|
+
|
|
11
|
+
Only these subpaths are exported (`package.json` `exports`). Each has ESM, CommonJS and type
|
|
12
|
+
declarations in `dist/`.
|
|
13
|
+
|
|
14
|
+
| Import | Contents |
|
|
15
|
+
| ---------------------------- | ------------------------------------------------------------ |
|
|
16
|
+
| `@vllnt/analytics` | Tracking functions, consent functions and constants, types |
|
|
17
|
+
| `@vllnt/analytics/react` | `AnalyticsProvider`, `useAnalytics`, `useConsent`, `initAnalytics` |
|
|
18
|
+
| `@vllnt/analytics/package.json` | Package metadata |
|
|
19
|
+
|
|
20
|
+
Types are exported from `@vllnt/analytics` only. Both entry points share one analytics state, so
|
|
21
|
+
`initAnalytics` from `./react` and `track` from `.` act on the same instance.
|
|
22
|
+
|
|
23
|
+
Peer dependency: `react` `^18.0.0 || ^19.0.0` (used by `./react` only).
|
|
24
|
+
|
|
25
|
+
All browser APIs are guarded, so importing on the server is safe. On the server `track`,
|
|
26
|
+
`trackPage`, `identify` and `resetAnalytics` return early, `loadConsent` returns `null`, and
|
|
27
|
+
`isStorageAvailable` and `isDoNotTrackEnabled` return `false`. `getConsentFromCookie` only parses
|
|
28
|
+
the string it is given, so it is the consent reader to use on the server. `initAnalytics`,
|
|
29
|
+
`enableAnalytics` and `disableAnalytics` have no server guard: `initAnalytics` on the server
|
|
30
|
+
starts the process-wide instance, so call it only in the browser.
|
|
31
|
+
|
|
32
|
+
## Tracking (`@vllnt/analytics`)
|
|
33
|
+
|
|
34
|
+
### `initAnalytics(config: AnalyticsConfig): void`
|
|
35
|
+
|
|
36
|
+
Starts the shared analytics instance and flushes queued events. Later calls do nothing while an
|
|
37
|
+
instance exists. It does not start, and blocks tracking, when Do Not Track is on (`"1"` or
|
|
38
|
+
`"yes"`) or stored consent has `analytics: false`; already queued events are kept, not flushed.
|
|
39
|
+
With no stored consent it starts, so call it only after the user accepts if you need opt-in
|
|
40
|
+
(`AnalyticsProvider` does this for you). Once tracking is blocked (by a blocked init or by
|
|
41
|
+
`disableAnalytics`), calling `initAnalytics` again does not unblock it; only `enableAnalytics`
|
|
42
|
+
does.
|
|
43
|
+
|
|
44
|
+
```ts
|
|
45
|
+
import { initAnalytics } from "@vllnt/analytics";
|
|
46
|
+
|
|
47
|
+
initAnalytics({ app: "my-app", version: 1, debug: true });
|
|
48
|
+
```
|
|
49
|
+
|
|
50
|
+
### `track(name: string, properties?: Record<string, unknown>): void`
|
|
51
|
+
|
|
52
|
+
Records a custom event. Before `initAnalytics` the event is queued; while blocked it is dropped.
|
|
53
|
+
An empty or non-string `name` is ignored.
|
|
54
|
+
|
|
55
|
+
```ts
|
|
56
|
+
track("button_click", { id: "cta-hero" });
|
|
57
|
+
```
|
|
58
|
+
|
|
59
|
+
### `trackPage(properties?: Record<string, unknown>): void`
|
|
60
|
+
|
|
61
|
+
Records a page view, queued before init like `track`.
|
|
62
|
+
|
|
63
|
+
```ts
|
|
64
|
+
trackPage({ path: "/about" });
|
|
65
|
+
```
|
|
66
|
+
|
|
67
|
+
### `identify(userId: string, traits?: Record<string, unknown>): void`
|
|
68
|
+
|
|
69
|
+
Stores a user id and traits on the started instance. Ignored before init, while blocked, or
|
|
70
|
+
when `userId` is empty or not a string. Not queued.
|
|
71
|
+
|
|
72
|
+
```ts
|
|
73
|
+
identify("user-123", { plan: "pro" });
|
|
74
|
+
```
|
|
75
|
+
|
|
76
|
+
### `resetAnalytics(): void`
|
|
77
|
+
|
|
78
|
+
Clears the stored user id and traits (for logout). The instance stays started.
|
|
79
|
+
|
|
80
|
+
```ts
|
|
81
|
+
resetAnalytics();
|
|
82
|
+
```
|
|
83
|
+
|
|
84
|
+
### `enableAnalytics(config: AnalyticsConfig): void`
|
|
85
|
+
|
|
86
|
+
Unblocks, discards the current instance and runs `initAnalytics(config)` again. Does nothing
|
|
87
|
+
unless tracking is currently blocked, and does nothing while Do Not Track is on. Because it
|
|
88
|
+
re-reads stored consent, save the accepted consent first: a stored `analytics: false` blocks it
|
|
89
|
+
again. The new instance has no `userId` or `traits`.
|
|
90
|
+
|
|
91
|
+
```ts
|
|
92
|
+
saveConsent(createAcceptAllConsent());
|
|
93
|
+
enableAnalytics({ app: "my-app" });
|
|
94
|
+
```
|
|
95
|
+
|
|
96
|
+
### `disableAnalytics(): void`
|
|
97
|
+
|
|
98
|
+
Blocks tracking and empties the queue after consent is revoked. It keeps the in-memory user id
|
|
99
|
+
and traits; call `resetAnalytics()` or reload the page to clear them.
|
|
100
|
+
|
|
101
|
+
```ts
|
|
102
|
+
disableAnalytics();
|
|
103
|
+
resetAnalytics();
|
|
104
|
+
```
|
|
105
|
+
|
|
106
|
+
### `isAnalyticsEnabled(): boolean`
|
|
107
|
+
|
|
108
|
+
`true` when the instance has started and is not blocked.
|
|
109
|
+
|
|
110
|
+
```ts
|
|
111
|
+
if (isAnalyticsEnabled()) track("export_clicked");
|
|
112
|
+
```
|
|
113
|
+
|
|
114
|
+
### `getAnalyticsState()`
|
|
115
|
+
|
|
116
|
+
Returns the current instance state for debugging, or `undefined` before init. Shape:
|
|
117
|
+
`{ app: string; debug?: boolean; version?: number; userId?: string; traits?: Record<string, unknown> }`.
|
|
118
|
+
The state type is not exported.
|
|
119
|
+
|
|
120
|
+
```ts
|
|
121
|
+
console.log(getAnalyticsState()?.userId);
|
|
122
|
+
```
|
|
123
|
+
|
|
124
|
+
### Tutorial helpers
|
|
125
|
+
|
|
126
|
+
Typed wrappers around `track` that send fixed event names (`TutorialEventName`). Every helper
|
|
127
|
+
needs `tutorial_id`, `tutorial_title` and `locale`.
|
|
128
|
+
|
|
129
|
+
| Function | Event name | Extra required fields |
|
|
130
|
+
| ------------------------------------- | --------------------------- | ---------------------------------- |
|
|
131
|
+
| `trackTutorialStart(properties)` | `tutorial_start` | none (`section_id` not allowed) |
|
|
132
|
+
| `trackSectionView(properties)` | `tutorial_section_view` | none |
|
|
133
|
+
| `trackSectionComplete(properties)` | `tutorial_section_complete` | none |
|
|
134
|
+
| `trackTutorialComplete(properties)` | `tutorial_complete` | none (`section_id` not allowed) |
|
|
135
|
+
| `trackTutorialNavigation(properties)` | `tutorial_navigation` | `navigation_type` |
|
|
136
|
+
| `trackTimeSpent(properties)` | `tutorial_time_spent` | `time_spent_seconds` |
|
|
137
|
+
|
|
138
|
+
```ts
|
|
139
|
+
import { trackSectionView, trackTutorialNavigation } from "@vllnt/analytics";
|
|
140
|
+
|
|
141
|
+
const base = { tutorial_id: "intro", tutorial_title: "Intro", locale: "en" };
|
|
142
|
+
trackSectionView({ ...base, section_id: "setup", section_index: 0, total_sections: 5 });
|
|
143
|
+
trackTutorialNavigation({ ...base, section_id: "setup", navigation_type: "next" });
|
|
144
|
+
```
|
|
145
|
+
|
|
146
|
+
## Consent (`@vllnt/analytics`)
|
|
147
|
+
|
|
148
|
+
Consent is stored as JSON in `localStorage` under `CONSENT_STORAGE_KEY` and mirrored to the
|
|
149
|
+
`CONSENT_COOKIE_NAME` cookie (`1` allowed, `0` declined; one-year expiry, `path=/`,
|
|
150
|
+
`SameSite=Lax`) so the server can read it.
|
|
151
|
+
|
|
152
|
+
### Constants
|
|
153
|
+
|
|
154
|
+
| Name | Value |
|
|
155
|
+
| --------------------- | ---------------------------- |
|
|
156
|
+
| `CONSENT_VERSION` | `1` |
|
|
157
|
+
| `CONSENT_STORAGE_KEY` | `"vllnt:analytics-consent"` |
|
|
158
|
+
| `CONSENT_COOKIE_NAME` | `"vllnt-consent"` |
|
|
159
|
+
|
|
160
|
+
### `loadConsent(): ConsentState | null`
|
|
161
|
+
|
|
162
|
+
Reads and validates stored consent. `null` when none, invalid, or storage is unavailable.
|
|
163
|
+
|
|
164
|
+
```ts
|
|
165
|
+
const consent = loadConsent();
|
|
166
|
+
```
|
|
167
|
+
|
|
168
|
+
### `saveConsent(state: ConsentState): void`
|
|
169
|
+
|
|
170
|
+
Writes consent to `localStorage` and the cookie. Does nothing when storage is unavailable.
|
|
171
|
+
|
|
172
|
+
```ts
|
|
173
|
+
saveConsent(createAcceptAllConsent());
|
|
174
|
+
```
|
|
175
|
+
|
|
176
|
+
### `clearConsent(): void`
|
|
177
|
+
|
|
178
|
+
Removes stored consent and expires the cookie.
|
|
179
|
+
|
|
180
|
+
```ts
|
|
181
|
+
clearConsent();
|
|
182
|
+
```
|
|
183
|
+
|
|
184
|
+
### `createAcceptAllConsent(): ConsentState`
|
|
185
|
+
|
|
186
|
+
New state with `analytics: true`, `functional: true`, the current time and `CONSENT_VERSION`.
|
|
187
|
+
|
|
188
|
+
```ts
|
|
189
|
+
const accepted = createAcceptAllConsent();
|
|
190
|
+
```
|
|
191
|
+
|
|
192
|
+
### `createDeclineAnalyticsConsent(): ConsentState`
|
|
193
|
+
|
|
194
|
+
New state with `analytics: false`, `functional: true`, the current time and `CONSENT_VERSION`.
|
|
195
|
+
|
|
196
|
+
```ts
|
|
197
|
+
const declined = createDeclineAnalyticsConsent();
|
|
198
|
+
```
|
|
199
|
+
|
|
200
|
+
### `updateConsentCategory(current: ConsentState, category: ConsentCategory, value: boolean): ConsentState`
|
|
201
|
+
|
|
202
|
+
Returns a copy with one category changed and a new timestamp. Setting `functional` to `false`
|
|
203
|
+
returns `current` unchanged.
|
|
204
|
+
|
|
205
|
+
```ts
|
|
206
|
+
const stored = loadConsent();
|
|
207
|
+
if (stored) saveConsent(updateConsentCategory(stored, "analytics", false));
|
|
208
|
+
```
|
|
209
|
+
|
|
210
|
+
### `needsRePrompt(consent: ConsentState, currentVersion: number): boolean`
|
|
211
|
+
|
|
212
|
+
`true` when `consent.version < currentVersion`.
|
|
213
|
+
|
|
214
|
+
```ts
|
|
215
|
+
if (consent && needsRePrompt(consent, 2)) clearConsent();
|
|
216
|
+
```
|
|
217
|
+
|
|
218
|
+
### `getConsentFromCookie(cookieString: string): boolean | null`
|
|
219
|
+
|
|
220
|
+
Parses a `Cookie` header: `true` for `1`, `false` for any other value, `null` when the cookie is
|
|
221
|
+
absent. The name must start the string or follow `;`, so `evil-vllnt-consent=1` does not match.
|
|
222
|
+
|
|
223
|
+
```ts
|
|
224
|
+
const allowed = getConsentFromCookie(request.headers.get("cookie") ?? "");
|
|
225
|
+
```
|
|
226
|
+
|
|
227
|
+
### `isDoNotTrackEnabled(): boolean`
|
|
228
|
+
|
|
229
|
+
Reads `navigator.doNotTrack`, then `window.doNotTrack`, then `navigator.msDoNotTrack`, and
|
|
230
|
+
returns `true` when the first one that is set equals `"1"` or `"yes"`. `false` on the server.
|
|
231
|
+
|
|
232
|
+
```ts
|
|
233
|
+
if (isDoNotTrackEnabled()) hideBanner();
|
|
234
|
+
```
|
|
235
|
+
|
|
236
|
+
### `isStorageAvailable(): boolean`
|
|
237
|
+
|
|
238
|
+
`true` when `localStorage` can be written. `false` on the server or when storage throws.
|
|
239
|
+
|
|
240
|
+
```ts
|
|
241
|
+
if (!isStorageAvailable()) console.warn("consent will not persist");
|
|
242
|
+
```
|
|
243
|
+
|
|
244
|
+
### `isValidConsentState(value: unknown): value is ConsentState`
|
|
245
|
+
|
|
246
|
+
Type guard: checks the four fields and their types.
|
|
247
|
+
|
|
248
|
+
```ts
|
|
249
|
+
const parsed: unknown = JSON.parse(raw);
|
|
250
|
+
if (isValidConsentState(parsed)) saveConsent(parsed);
|
|
251
|
+
```
|
|
252
|
+
|
|
253
|
+
## React (`@vllnt/analytics/react`)
|
|
254
|
+
|
|
255
|
+
The ESM build (`dist/react.js`) starts with `"use client"`.
|
|
256
|
+
|
|
257
|
+
Both `AnalyticsProvider` and `useConsent` read stored consent during the first render. On the
|
|
258
|
+
server that read returns `null`, so `consent` is `null` and `hasResponded` is `false` there, while
|
|
259
|
+
the first browser render may already see stored consent. If you render a banner from
|
|
260
|
+
`hasResponded`, render it only after the component has mounted to avoid a hydration mismatch.
|
|
261
|
+
|
|
262
|
+
### `AnalyticsProvider`
|
|
263
|
+
|
|
264
|
+
```ts
|
|
265
|
+
function AnalyticsProvider(props: {
|
|
266
|
+
children: ReactNode;
|
|
267
|
+
config: AnalyticsConfig;
|
|
268
|
+
consentVersion?: number; // default CONSENT_VERSION
|
|
269
|
+
}): React.JSX.Element;
|
|
270
|
+
```
|
|
271
|
+
|
|
272
|
+
Reads stored consent, clears it when `needsRePrompt(stored, consentVersion)`, and calls
|
|
273
|
+
`initAnalytics(config)` while consent has `analytics: true`. Its context actions save consent and
|
|
274
|
+
call `enableAnalytics` or `disableAnalytics`. Pass a stable `config` (for example a module
|
|
275
|
+
constant), because the init effect depends on it.
|
|
276
|
+
|
|
277
|
+
With stored `analytics: false`, the provider does not call `initAnalytics`, so tracking is not
|
|
278
|
+
blocked after a reload: `track()` calls are queued in memory and are flushed if the user accepts
|
|
279
|
+
later. Call `disableAnalytics()` yourself if events from a declined visitor must be dropped.
|
|
280
|
+
|
|
281
|
+
```tsx
|
|
282
|
+
const config = { app: "my-app", version: 1 };
|
|
283
|
+
|
|
284
|
+
export function Root({ children }: { children: React.ReactNode }) {
|
|
285
|
+
return <AnalyticsProvider config={config}>{children}</AnalyticsProvider>;
|
|
286
|
+
}
|
|
287
|
+
```
|
|
288
|
+
|
|
289
|
+
### `useAnalytics()`
|
|
290
|
+
|
|
291
|
+
Returns the provider context; throws `useAnalytics must be used within an AnalyticsProvider`
|
|
292
|
+
outside one. Fields: `config`, `consent`, `hasResponded`, `isLoading` (always `false`),
|
|
293
|
+
`acceptAll()`, `declineAnalytics()`, `updateConsent(category, value)`, `resetConsent()`.
|
|
294
|
+
`updateConsent` does nothing while `consent` is `null` (before the user has responded).
|
|
295
|
+
|
|
296
|
+
```tsx
|
|
297
|
+
function CookieBanner() {
|
|
298
|
+
const { hasResponded, acceptAll, declineAnalytics } = useAnalytics();
|
|
299
|
+
const [mounted, setMounted] = useState(false);
|
|
300
|
+
useEffect(() => setMounted(true), []);
|
|
301
|
+
if (!mounted || hasResponded) return null;
|
|
302
|
+
return (
|
|
303
|
+
<div>
|
|
304
|
+
<button onClick={acceptAll}>Accept</button>
|
|
305
|
+
<button onClick={declineAnalytics}>Decline</button>
|
|
306
|
+
</div>
|
|
307
|
+
);
|
|
308
|
+
}
|
|
309
|
+
```
|
|
310
|
+
|
|
311
|
+
### `useConsent(options?: UseConsentOptions): UseConsentReturn`
|
|
312
|
+
|
|
313
|
+
Standalone consent state with the same fields as `useAnalytics()` except `config`. It reads and
|
|
314
|
+
writes stored consent only: it does not start or stop analytics and does not share state with
|
|
315
|
+
`AnalyticsProvider`. Inside a provider, use `useAnalytics()` for a banner so accepting starts
|
|
316
|
+
tracking.
|
|
317
|
+
|
|
318
|
+
```tsx
|
|
319
|
+
const { consent, updateConsent } = useConsent({ consentVersion: 1 });
|
|
320
|
+
```
|
|
321
|
+
|
|
322
|
+
### `initAnalytics`
|
|
323
|
+
|
|
324
|
+
Re-export of the core `initAnalytics`, for apps that only import from `./react`.
|
|
325
|
+
|
|
326
|
+
### Consent version caveat
|
|
327
|
+
|
|
328
|
+
`createAcceptAllConsent()` and `createDeclineAnalyticsConsent()` always write
|
|
329
|
+
`CONSENT_VERSION` (`1`), and the hooks use them. With `consentVersion` above `1`, the stored
|
|
330
|
+
consent is older than the requested version on every mount, so it is cleared and the user is
|
|
331
|
+
asked again each visit.
|
|
332
|
+
|
|
333
|
+
## Types (`@vllnt/analytics`)
|
|
334
|
+
|
|
335
|
+
```ts
|
|
336
|
+
type AnalyticsConfig = { app: string; debug?: boolean; version?: number };
|
|
337
|
+
|
|
338
|
+
type AnalyticsEvent = {
|
|
339
|
+
name: string;
|
|
340
|
+
properties?: Record<string, unknown>;
|
|
341
|
+
timestamp?: string;
|
|
342
|
+
};
|
|
343
|
+
|
|
344
|
+
type ConsentCategory = "analytics" | "functional";
|
|
345
|
+
|
|
346
|
+
type ConsentState = {
|
|
347
|
+
analytics: boolean;
|
|
348
|
+
functional: boolean;
|
|
349
|
+
timestamp: string; // ISO time of the choice
|
|
350
|
+
version: number;
|
|
351
|
+
};
|
|
352
|
+
|
|
353
|
+
type TutorialEventName =
|
|
354
|
+
| "tutorial_complete"
|
|
355
|
+
| "tutorial_navigation"
|
|
356
|
+
| "tutorial_section_complete"
|
|
357
|
+
| "tutorial_section_view"
|
|
358
|
+
| "tutorial_start"
|
|
359
|
+
| "tutorial_time_spent";
|
|
360
|
+
|
|
361
|
+
type TutorialEventProperties = {
|
|
362
|
+
tutorial_id: string;
|
|
363
|
+
tutorial_title: string;
|
|
364
|
+
locale: string;
|
|
365
|
+
completion_percent?: number; // 0-100
|
|
366
|
+
navigation_type?: "keyboard" | "next" | "prev" | "toc";
|
|
367
|
+
section_id?: string;
|
|
368
|
+
section_index?: number; // 0-based
|
|
369
|
+
time_spent_seconds?: number;
|
|
370
|
+
total_sections?: number;
|
|
371
|
+
};
|
|
372
|
+
|
|
373
|
+
type UseConsentOptions = { consentVersion?: number };
|
|
374
|
+
|
|
375
|
+
type UseConsentReturn = {
|
|
376
|
+
acceptAll: () => void;
|
|
377
|
+
consent: ConsentState | null;
|
|
378
|
+
declineAnalytics: () => void;
|
|
379
|
+
hasResponded: boolean;
|
|
380
|
+
isLoading: boolean;
|
|
381
|
+
resetConsent: () => void;
|
|
382
|
+
updateConsent: (category: ConsentCategory, value: boolean) => void;
|
|
383
|
+
};
|
|
384
|
+
```
|
package/llms.txt
ADDED
|
@@ -0,0 +1,56 @@
|
|
|
1
|
+
# @vllnt/analytics
|
|
2
|
+
|
|
3
|
+
> Lightweight, privacy-first analytics with consent management and React integration.
|
|
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`. Bring your own backend.
|
|
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
|
+
## Package
|
|
15
|
+
|
|
16
|
+
- npm: `@vllnt/analytics` (MIT), repo: https://github.com/vllnt/analytics
|
|
17
|
+
- Unreleased builds from `main` use the `canary` dist-tag
|
|
18
|
+
- Ships ESM, CommonJS and type declarations from `dist` (built with tsup)
|
|
19
|
+
- Entry points: `@vllnt/analytics` (core + consent + types), `@vllnt/analytics/react` (`AnalyticsProvider`, `useAnalytics`, `useConsent`, `initAnalytics`)
|
|
20
|
+
- Peer dependency: React `^18.0.0 || ^19.0.0`
|
|
21
|
+
- Install: `pnpm add @vllnt/analytics`
|
|
22
|
+
|
|
23
|
+
## Behaviour
|
|
24
|
+
|
|
25
|
+
- Events tracked before `initAnalytics()` are queued and flushed on the first unblocked init;
|
|
26
|
+
a blocked init keeps them queued.
|
|
27
|
+
- `initAnalytics()` blocks tracking when Do Not Track is on (`"1"` or `"yes"`) or stored consent
|
|
28
|
+
has `analytics: false`. With no stored consent it starts tracking. `AnalyticsProvider` calls it
|
|
29
|
+
only while consent has `analytics: true`; after a stored decline it does not call it, so events
|
|
30
|
+
stay queued in memory until the user accepts or `disableAnalytics()` is called.
|
|
31
|
+
- Once blocked, only `enableAnalytics(config)` unblocks; it re-reads stored consent.
|
|
32
|
+
- Consent is stored in `localStorage` (`vllnt:analytics-consent`) and a `vllnt-consent` cookie
|
|
33
|
+
(`1` or `0`) for SSR; `getConsentFromCookie(cookieString)` reads it with an anchored match.
|
|
34
|
+
- Consent is versioned (`CONSENT_VERSION`); a lower stored version re-prompts. The hooks always
|
|
35
|
+
save version `CONSENT_VERSION` (`1`), so a `consentVersion` above `1` re-prompts on every visit
|
|
36
|
+
(see `docs/API.md`).
|
|
37
|
+
- `track()` ignores empty or non-string names; `identify()` ignores empty or non-string ids.
|
|
38
|
+
- Browser APIs are guarded, so server-side import is safe. `initAnalytics()` has no server
|
|
39
|
+
guard and starts the process-wide instance there, so call it only in the browser.
|
|
40
|
+
|
|
41
|
+
## Docs
|
|
42
|
+
|
|
43
|
+
- [API reference](./docs/API.md): every public export, signatures, examples, entry points
|
|
44
|
+
- [README](./README.md): usage and API summary
|
|
45
|
+
- [Changelog](./CHANGELOG.md): release history
|
|
46
|
+
- [Core exports](./src/index.ts) and [React exports](./src/react.tsx): source of the public API
|
|
47
|
+
|
|
48
|
+
## Optional
|
|
49
|
+
|
|
50
|
+
- [Contributing](https://github.com/vllnt/analytics/blob/main/CONTRIBUTING.md): setup, checks, release flow
|
|
51
|
+
- [Security policy](https://github.com/vllnt/analytics/blob/main/SECURITY.md): private vulnerability reporting
|
|
52
|
+
- [AGENTS.md](https://github.com/vllnt/analytics/blob/main/AGENTS.md): instructions for contributing agents
|
|
53
|
+
|
|
54
|
+
## Ecosystem
|
|
55
|
+
|
|
56
|
+
- [vllnt oss](https://oss.vllnt.com/llms.txt): index of all vllnt open-source packages
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@vllnt/analytics",
|
|
3
|
-
"version": "0.2.0-canary.
|
|
3
|
+
"version": "0.2.0-canary.8064ae0",
|
|
4
4
|
"description": "Lightweight, privacy-first analytics with consent management and React integration",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"packageManager": "pnpm@10.34.8",
|
|
@@ -9,17 +9,25 @@
|
|
|
9
9
|
"type": "git",
|
|
10
10
|
"url": "git+https://github.com/vllnt/analytics.git"
|
|
11
11
|
},
|
|
12
|
+
"homepage": "https://oss.vllnt.com/packages/analytics",
|
|
12
13
|
"keywords": [
|
|
13
14
|
"analytics",
|
|
14
15
|
"consent",
|
|
15
16
|
"privacy",
|
|
16
17
|
"gdpr",
|
|
17
18
|
"react",
|
|
18
|
-
"do-not-track"
|
|
19
|
+
"do-not-track",
|
|
20
|
+
"cookie-consent",
|
|
21
|
+
"ssr"
|
|
19
22
|
],
|
|
20
23
|
"sideEffects": false,
|
|
21
24
|
"files": [
|
|
22
|
-
"dist"
|
|
25
|
+
"dist",
|
|
26
|
+
"src",
|
|
27
|
+
"!src/__tests__",
|
|
28
|
+
"docs/API.md",
|
|
29
|
+
"CHANGELOG.md",
|
|
30
|
+
"llms.txt"
|
|
23
31
|
],
|
|
24
32
|
"main": "./dist/index.cjs",
|
|
25
33
|
"module": "./dist/index.js",
|
|
@@ -66,15 +74,15 @@
|
|
|
66
74
|
"@testing-library/react": "^16.3.3",
|
|
67
75
|
"@types/node": "^24.19.2",
|
|
68
76
|
"@types/react": "^19.3.0",
|
|
69
|
-
"@vitest/coverage-v8": "^5.0
|
|
70
|
-
"@vllnt/eslint-config": "^2.
|
|
77
|
+
"@vitest/coverage-v8": "^5.1.0",
|
|
78
|
+
"@vllnt/eslint-config": "^2.1.0",
|
|
71
79
|
"@vllnt/typescript": "^2.0.0",
|
|
72
80
|
"eslint": "^9.39.5",
|
|
73
81
|
"jsdom": "^30.1.3",
|
|
74
82
|
"react": "^19.3.1",
|
|
75
83
|
"tsup": "^9.0.0",
|
|
76
84
|
"typescript": "^6.0.3",
|
|
77
|
-
"vitest": "^5.0
|
|
85
|
+
"vitest": "^5.1.0"
|
|
78
86
|
},
|
|
79
87
|
"pnpm": {
|
|
80
88
|
"minimumReleaseAge": 10080,
|
|
@@ -82,6 +90,7 @@
|
|
|
82
90
|
"@vllnt/*"
|
|
83
91
|
],
|
|
84
92
|
"overrides": {
|
|
93
|
+
"minimatch@>=7.0.0 <11.0.1": "11.0.5",
|
|
85
94
|
"deepmerge-ts@<8.0.0": "8.0.0",
|
|
86
95
|
"handlebars@<4.7.10": "4.7.10"
|
|
87
96
|
},
|