@vllnt/analytics 0.1.1 → 0.2.0-canary.6a57f79
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 +58 -0
- package/README.md +253 -143
- package/dist/analytics-BxUQktfB.d.cts +168 -0
- package/dist/analytics-BxUQktfB.d.ts +168 -0
- package/dist/chunk-5COCGKHD.js +259 -0
- package/dist/chunk-ACWD5NRI.cjs +259 -0
- package/dist/index.cjs +62 -0
- package/dist/index.d.cts +55 -0
- package/dist/index.d.ts +55 -0
- package/dist/index.js +62 -0
- package/dist/react.cjs +175 -0
- package/dist/react.d.cts +33 -0
- package/dist/react.d.ts +33 -0
- package/dist/react.js +175 -0
- package/docs/API.md +490 -0
- package/llms.txt +74 -0
- package/package.json +74 -38
- package/src/analytics.ts +27 -43
- package/src/consent.ts +5 -1
- package/src/react.tsx +20 -10
package/CHANGELOG.md
ADDED
|
@@ -0,0 +1,58 @@
|
|
|
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] - 2026-11-27
|
|
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` and `CHANGELOG.md`.
|
|
14
|
+
`llms.txt` links to these files and to the TypeScript sources in `src/`, which still ship.
|
|
15
|
+
|
|
16
|
+
### Changed
|
|
17
|
+
|
|
18
|
+
- The package entry points now resolve to compiled output in `dist` (ESM, CommonJS and type
|
|
19
|
+
declarations) instead of raw TypeScript sources, so it works without transpiling
|
|
20
|
+
`node_modules`. The `.` and `./react` export subpaths and the `react` peer range are
|
|
21
|
+
unchanged. `./package.json` is now exported too. Both builds of `./react` start with
|
|
22
|
+
`"use client"`.
|
|
23
|
+
- **Breaking:** `track()` ignores events whose name is empty or not a string, and `identify()`
|
|
24
|
+
ignores an empty or non-string user id, instead of recording them.
|
|
25
|
+
- **Breaking:** after a stored decline, `AnalyticsProvider` now blocks tracking with
|
|
26
|
+
`disableAnalytics()`, so `track()` calls are dropped. Before, they were queued in memory without
|
|
27
|
+
limit and flushed if the user accepted later.
|
|
28
|
+
- **Breaking:** when `initAnalytics()` is blocked by Do Not Track or a stored decline, it drops the
|
|
29
|
+
events queued before it, as `disableAnalytics()` does. Before, they were kept and flushed by a
|
|
30
|
+
later successful `enableAnalytics()` (after the user accepted, or Do Not Track was turned off).
|
|
31
|
+
|
|
32
|
+
### Fixed
|
|
33
|
+
|
|
34
|
+
- `getConsentFromCookie()` no longer matches look-alike cookie names that merely end with
|
|
35
|
+
`vllnt-consent` (for example `evil-vllnt-consent=1`).
|
|
36
|
+
- In `AnalyticsProvider` and `useConsent`, `acceptAll` and `declineAnalytics` save the choice with
|
|
37
|
+
their `consentVersion`; `updateConsent` keeps the stored version. Before, they always saved
|
|
38
|
+
version `1`, so a `consentVersion` above `1` asked the user again on every visit.
|
|
39
|
+
|
|
40
|
+
### Security
|
|
41
|
+
|
|
42
|
+
- `@latest` is now published only from a `vX.Y.Z` tag on `main` that matches `package.json` and
|
|
43
|
+
this changelog. Before, a manual workflow run bumped the version in CI. Publishing still uses
|
|
44
|
+
npm trusted publishing (OIDC) with provenance, as in 0.1.1.
|
|
45
|
+
|
|
46
|
+
## [0.1.1] - 2026-03-10
|
|
47
|
+
|
|
48
|
+
### Added
|
|
49
|
+
|
|
50
|
+
- CI and publish workflows.
|
|
51
|
+
|
|
52
|
+
## [0.1.0] - 2026-03-05
|
|
53
|
+
|
|
54
|
+
### Added
|
|
55
|
+
|
|
56
|
+
- Initial release: event tracking with a pre-initialization queue, two-category consent management
|
|
57
|
+
(localStorage and SSR cookie), versioned consent re-prompts, Do Not Track support, tutorial
|
|
58
|
+
tracking helpers, and React integration (`AnalyticsProvider`, `useAnalytics`, `useConsent`).
|
package/README.md
CHANGED
|
@@ -1,15 +1,19 @@
|
|
|
1
1
|
# @vllnt/analytics
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
Cookie consent state and consent-gated event tracking for React and TypeScript, with Do Not Track and an SSR consent cookie.
|
|
4
4
|
|
|
5
|
-
##
|
|
5
|
+
## For coding agents
|
|
6
6
|
|
|
7
|
-
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
-
|
|
7
|
+
Version-matched docs ship in the package: [`docs/API.md`](docs/API.md), [`llms.txt`](llms.txt),
|
|
8
|
+
`CHANGELOG.md` and the TypeScript sources in `src/`. Read them before writing code.
|
|
9
|
+
|
|
10
|
+
## When to use
|
|
11
|
+
|
|
12
|
+
- Use it when a React app needs cookie-banner state stored in `localStorage` and a cookie.
|
|
13
|
+
- Use it when tracking calls must wait for consent and stop on Do Not Track or a decline.
|
|
14
|
+
- Use it when server code must read the visitor's choice from the `Cookie` header.
|
|
15
|
+
- Do not use it when events must reach a backend: it sends nothing; use your analytics vendor's SDK.
|
|
16
|
+
- Do not use it for a server-side consent record: use [`@vllnt/convex-consent`](https://oss.vllnt.com/packages/convex-consent).
|
|
13
17
|
|
|
14
18
|
## Install
|
|
15
19
|
|
|
@@ -17,187 +21,293 @@ Lightweight, privacy-first analytics library with consent management and React i
|
|
|
17
21
|
pnpm add @vllnt/analytics
|
|
18
22
|
```
|
|
19
23
|
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
## Usage
|
|
23
|
-
|
|
24
|
-
### React Provider
|
|
24
|
+
## Quick start
|
|
25
25
|
|
|
26
26
|
```tsx
|
|
27
|
-
import {
|
|
27
|
+
import { track } from "@vllnt/analytics";
|
|
28
|
+
import { AnalyticsProvider, useAnalytics } from "@vllnt/analytics/react";
|
|
28
29
|
|
|
29
|
-
|
|
30
|
-
return (
|
|
31
|
-
<AnalyticsProvider config={{ app: 'my-app', version: 1, debug: true }}>
|
|
32
|
-
{children}
|
|
33
|
-
</AnalyticsProvider>
|
|
34
|
-
)
|
|
35
|
-
}
|
|
36
|
-
```
|
|
30
|
+
const config = { app: "my-app" };
|
|
37
31
|
|
|
38
|
-
|
|
32
|
+
function Banner() {
|
|
33
|
+
const { hasResponded, acceptAll, declineAnalytics } = useAnalytics();
|
|
34
|
+
return hasResponded ? null : <p><button onClick={acceptAll}>Accept</button><button onClick={declineAnalytics}>Decline</button></p>;
|
|
35
|
+
}
|
|
39
36
|
|
|
40
|
-
|
|
41
|
-
|
|
37
|
+
export const App = () => (
|
|
38
|
+
<AnalyticsProvider config={config}><Banner /><button onClick={() => track("cta_click")}>Buy</button></AnalyticsProvider>
|
|
39
|
+
);
|
|
40
|
+
```
|
|
42
41
|
|
|
43
|
-
|
|
44
|
-
const { hasResponded, isLoading, acceptAll, declineAnalytics } = useConsent()
|
|
42
|
+
## API
|
|
45
43
|
|
|
46
|
-
|
|
44
|
+
Every public export. Full signatures and examples: [`docs/API.md`](docs/API.md).
|
|
45
|
+
|
|
46
|
+
| Export | Entry | What it does |
|
|
47
|
+
| --- | --- | --- |
|
|
48
|
+
| [`initAnalytics`](docs/API.md#initanalytics) | `.`, `./react` | Starts the shared instance and flushes queued events, unless blocked. |
|
|
49
|
+
| [`track`](docs/API.md#track) | `.` | Records a custom event; queued before init, dropped while blocked. |
|
|
50
|
+
| [`trackPage`](docs/API.md#trackpage) | `.` | Records a page view, queued before init like `track`. |
|
|
51
|
+
| [`identify`](docs/API.md#identify) | `.` | Stores a user id and traits on the started instance. |
|
|
52
|
+
| [`resetAnalytics`](docs/API.md#resetanalytics) | `.` | Clears the user id and traits (logout). |
|
|
53
|
+
| [`enableAnalytics`](docs/API.md#enableanalytics) | `.` | Unblocks after consent is granted and starts again. |
|
|
54
|
+
| [`disableAnalytics`](docs/API.md#disableanalytics) | `.` | Blocks tracking and empties the queue. |
|
|
55
|
+
| [`isAnalyticsEnabled`](docs/API.md#isanalyticsenabled) | `.` | `true` when started and not blocked. |
|
|
56
|
+
| [`getAnalyticsState`](docs/API.md#getanalyticsstate) | `.` | Current instance state, for debugging. |
|
|
57
|
+
| [`trackTutorialStart`](docs/API.md#tutorial-helpers) | `.` | Tracks a `tutorial_start` event. |
|
|
58
|
+
| [`trackSectionView`](docs/API.md#tutorial-helpers) | `.` | Tracks a `tutorial_section_view` event. |
|
|
59
|
+
| [`trackSectionComplete`](docs/API.md#tutorial-helpers) | `.` | Tracks a `tutorial_section_complete` event. |
|
|
60
|
+
| [`trackTutorialComplete`](docs/API.md#tutorial-helpers) | `.` | Tracks a `tutorial_complete` event. |
|
|
61
|
+
| [`trackTutorialNavigation`](docs/API.md#tutorial-helpers) | `.` | Tracks a `tutorial_navigation` event. |
|
|
62
|
+
| [`trackTimeSpent`](docs/API.md#tutorial-helpers) | `.` | Tracks a `tutorial_time_spent` event. |
|
|
63
|
+
| [`CONSENT_VERSION`](docs/API.md#constants) | `.` | Default consent version, `1`. |
|
|
64
|
+
| [`CONSENT_STORAGE_KEY`](docs/API.md#constants) | `.` | `localStorage` key, `"vllnt:analytics-consent"`. |
|
|
65
|
+
| [`CONSENT_COOKIE_NAME`](docs/API.md#constants) | `.` | Cookie name, `"vllnt-consent"`. |
|
|
66
|
+
| [`loadConsent`](docs/API.md#loadconsent) | `.` | Reads and validates stored consent, or `null`. |
|
|
67
|
+
| [`saveConsent`](docs/API.md#saveconsent) | `.` | Writes consent to `localStorage` and the cookie. |
|
|
68
|
+
| [`clearConsent`](docs/API.md#clearconsent) | `.` | Removes stored consent and expires the cookie. |
|
|
69
|
+
| [`createAcceptAllConsent`](docs/API.md#createacceptallconsent) | `.` | New consent state with everything accepted. |
|
|
70
|
+
| [`createDeclineAnalyticsConsent`](docs/API.md#createdeclineanalyticsconsent) | `.` | New consent state with analytics declined. |
|
|
71
|
+
| [`updateConsentCategory`](docs/API.md#updateconsentcategory) | `.` | Copy of a consent state with one category changed. |
|
|
72
|
+
| [`needsRePrompt`](docs/API.md#needsreprompt) | `.` | `true` when stored consent is older than a version. |
|
|
73
|
+
| [`getConsentFromCookie`](docs/API.md#getconsentfromcookie) | `.` | Parses the consent cookie from a `Cookie` header. |
|
|
74
|
+
| [`isDoNotTrackEnabled`](docs/API.md#isdonottrackenabled) | `.` | `true` when the browser sends Do Not Track. |
|
|
75
|
+
| [`isStorageAvailable`](docs/API.md#isstorageavailable) | `.` | `true` when `localStorage` can be written. |
|
|
76
|
+
| [`isValidConsentState`](docs/API.md#isvalidconsentstate) | `.` | Type guard for a stored consent object. |
|
|
77
|
+
| [`AnalyticsProvider`](docs/API.md#analyticsprovider) | `./react` | Context provider that wires consent to tracking. |
|
|
78
|
+
| [`useAnalytics`](docs/API.md#useanalytics) | `./react` | Provider context: consent state and actions. |
|
|
79
|
+
| [`useConsent`](docs/API.md#useconsent) | `./react` | Standalone consent state; does not start tracking. |
|
|
80
|
+
| [`AnalyticsConfig`](docs/API.md#types) | `.` (type) | `{ app; debug?; version? }`. |
|
|
81
|
+
| [`AnalyticsEvent`](docs/API.md#types) | `.` (type) | A queued event: name, properties, timestamp. |
|
|
82
|
+
| [`ConsentCategory`](docs/API.md#types) | `.` (type) | `"analytics" \| "functional"`. |
|
|
83
|
+
| [`ConsentState`](docs/API.md#types) | `.` (type) | Stored consent: categories, timestamp, version. |
|
|
84
|
+
| [`TutorialEventName`](docs/API.md#types) | `.` (type) | Event names used by the tutorial helpers. |
|
|
85
|
+
| [`TutorialEventProperties`](docs/API.md#types) | `.` (type) | Properties for the tutorial helpers. |
|
|
86
|
+
| [`UseConsentOptions`](docs/API.md#types) | `.` (type) | `{ consentVersion? }`. |
|
|
87
|
+
| [`UseConsentReturn`](docs/API.md#types) | `.` (type) | Return value of `useConsent`. |
|
|
88
|
+
|
|
89
|
+
## Common tasks
|
|
90
|
+
|
|
91
|
+
### Show the banner only after mount
|
|
92
|
+
|
|
93
|
+
Stored consent is read in the browser only, so on the server `hasResponded` is `false`. Render the
|
|
94
|
+
banner after mount to avoid a hydration mismatch.
|
|
47
95
|
|
|
96
|
+
```tsx
|
|
97
|
+
import { useEffect, useState } from "react";
|
|
98
|
+
import { useAnalytics } from "@vllnt/analytics/react";
|
|
99
|
+
|
|
100
|
+
export function CookieBanner() {
|
|
101
|
+
const { hasResponded, acceptAll, declineAnalytics } = useAnalytics();
|
|
102
|
+
const [mounted, setMounted] = useState(false);
|
|
103
|
+
useEffect(() => setMounted(true), []);
|
|
104
|
+
if (!mounted || hasResponded) return null;
|
|
48
105
|
return (
|
|
49
106
|
<div>
|
|
50
|
-
<p>We use cookies for analytics.</p>
|
|
51
107
|
<button onClick={acceptAll}>Accept</button>
|
|
52
108
|
<button onClick={declineAnalytics}>Decline</button>
|
|
53
109
|
</div>
|
|
54
|
-
)
|
|
110
|
+
);
|
|
55
111
|
}
|
|
56
112
|
```
|
|
57
113
|
|
|
58
|
-
###
|
|
59
|
-
|
|
60
|
-
```tsx
|
|
61
|
-
import { track, trackPage } from '@vllnt/analytics'
|
|
114
|
+
### Read consent on the server
|
|
62
115
|
|
|
63
|
-
|
|
64
|
-
|
|
116
|
+
```ts
|
|
117
|
+
import { getConsentFromCookie } from "@vllnt/analytics";
|
|
65
118
|
|
|
66
|
-
|
|
67
|
-
|
|
119
|
+
export function GET(request: Request): Response {
|
|
120
|
+
const allowed = getConsentFromCookie(request.headers.get("cookie") ?? "");
|
|
121
|
+
return Response.json({ allowed });
|
|
122
|
+
}
|
|
68
123
|
```
|
|
69
124
|
|
|
70
|
-
###
|
|
125
|
+
### Ask every visitor again after a policy change
|
|
71
126
|
|
|
72
|
-
|
|
127
|
+
Raise `consentVersion`. Stored choices with a lower version are cleared once; new choices are saved
|
|
128
|
+
with the new version.
|
|
73
129
|
|
|
74
130
|
```tsx
|
|
75
|
-
import {
|
|
76
|
-
|
|
77
|
-
trackSectionView,
|
|
78
|
-
trackSectionComplete,
|
|
79
|
-
trackTutorialComplete,
|
|
80
|
-
trackTutorialNavigation,
|
|
81
|
-
trackTimeSpent,
|
|
82
|
-
} from '@vllnt/analytics'
|
|
83
|
-
|
|
84
|
-
trackTutorialStart({
|
|
85
|
-
tutorial_id: 'getting-started',
|
|
86
|
-
tutorial_title: 'Getting Started',
|
|
87
|
-
locale: 'en',
|
|
88
|
-
total_sections: 5,
|
|
89
|
-
})
|
|
131
|
+
import type { ReactNode } from "react";
|
|
132
|
+
import { AnalyticsProvider } from "@vllnt/analytics/react";
|
|
90
133
|
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
134
|
+
const config = { app: "my-app" };
|
|
135
|
+
|
|
136
|
+
export function Root({ children }: { children: ReactNode }) {
|
|
137
|
+
return (
|
|
138
|
+
<AnalyticsProvider config={config} consentVersion={2}>
|
|
139
|
+
{children}
|
|
140
|
+
</AnalyticsProvider>
|
|
141
|
+
);
|
|
142
|
+
}
|
|
99
143
|
```
|
|
100
144
|
|
|
101
|
-
###
|
|
145
|
+
### Let the visitor change their choice later
|
|
102
146
|
|
|
103
147
|
```tsx
|
|
104
|
-
import {
|
|
148
|
+
import { useAnalytics } from "@vllnt/analytics/react";
|
|
149
|
+
|
|
150
|
+
export function PrivacySettings() {
|
|
151
|
+
const { consent, updateConsent, resetConsent } = useAnalytics();
|
|
152
|
+
return (
|
|
153
|
+
<div>
|
|
154
|
+
<label>
|
|
155
|
+
<input
|
|
156
|
+
type="checkbox"
|
|
157
|
+
checked={consent?.analytics ?? false}
|
|
158
|
+
onChange={(event) => updateConsent("analytics", event.target.checked)}
|
|
159
|
+
/>
|
|
160
|
+
Analytics
|
|
161
|
+
</label>
|
|
162
|
+
<button onClick={resetConsent}>Ask me again</button>
|
|
163
|
+
</div>
|
|
164
|
+
);
|
|
165
|
+
}
|
|
166
|
+
```
|
|
167
|
+
|
|
168
|
+
### Track page views, users and tutorials
|
|
169
|
+
|
|
170
|
+
```ts
|
|
171
|
+
import { identify, resetAnalytics, trackPage, trackSectionView } from "@vllnt/analytics";
|
|
172
|
+
|
|
173
|
+
trackPage({ path: "/about" });
|
|
105
174
|
|
|
106
|
-
|
|
107
|
-
|
|
175
|
+
identify("user-123", { plan: "pro" });
|
|
176
|
+
resetAnalytics();
|
|
108
177
|
|
|
109
|
-
|
|
110
|
-
|
|
178
|
+
trackSectionView({
|
|
179
|
+
tutorial_id: "getting-started",
|
|
180
|
+
tutorial_title: "Getting Started",
|
|
181
|
+
locale: "en",
|
|
182
|
+
section_id: "intro",
|
|
183
|
+
section_index: 0,
|
|
184
|
+
total_sections: 5,
|
|
185
|
+
});
|
|
111
186
|
```
|
|
112
187
|
|
|
113
|
-
###
|
|
188
|
+
### Use it without React
|
|
114
189
|
|
|
115
190
|
```ts
|
|
116
191
|
import {
|
|
117
|
-
loadConsent,
|
|
118
|
-
saveConsent,
|
|
119
|
-
clearConsent,
|
|
120
192
|
createAcceptAllConsent,
|
|
121
193
|
createDeclineAnalyticsConsent,
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
const analyticsAllowed = getConsentFromCookie(request.headers.cookie)
|
|
129
|
-
|
|
130
|
-
// Check if re-prompt needed after version bump
|
|
131
|
-
const consent = loadConsent()
|
|
132
|
-
if (consent && needsRePrompt(consent, 2)) {
|
|
133
|
-
clearConsent()
|
|
134
|
-
}
|
|
135
|
-
```
|
|
194
|
+
disableAnalytics,
|
|
195
|
+
enableAnalytics,
|
|
196
|
+
initAnalytics,
|
|
197
|
+
loadConsent,
|
|
198
|
+
saveConsent,
|
|
199
|
+
} from "@vllnt/analytics";
|
|
136
200
|
|
|
137
|
-
|
|
201
|
+
const config = { app: "my-app" };
|
|
138
202
|
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
| `resetAnalytics()` | Clear user identity (logout) |
|
|
148
|
-
| `enableAnalytics(config)` | Re-enable after consent granted |
|
|
149
|
-
| `disableAnalytics()` | Disable after consent revoked |
|
|
150
|
-
| `isAnalyticsEnabled()` | Check if tracking is active |
|
|
151
|
-
|
|
152
|
-
### Consent
|
|
153
|
-
|
|
154
|
-
| Function | Description |
|
|
155
|
-
|---|---|
|
|
156
|
-
| `loadConsent()` | Load consent from localStorage |
|
|
157
|
-
| `saveConsent(state)` | Persist consent to localStorage + cookie |
|
|
158
|
-
| `clearConsent()` | Remove consent (triggers re-prompt) |
|
|
159
|
-
| `createAcceptAllConsent()` | Create accept-all consent state |
|
|
160
|
-
| `createDeclineAnalyticsConsent()` | Create analytics-declined state |
|
|
161
|
-
| `updateConsentCategory(state, category, value)` | Update a single category |
|
|
162
|
-
| `needsRePrompt(consent, version)` | Check if version requires re-prompt |
|
|
163
|
-
| `getConsentFromCookie(cookieString)` | Parse consent from cookie (SSR) |
|
|
164
|
-
| `isDoNotTrackEnabled()` | Check browser DNT setting |
|
|
165
|
-
|
|
166
|
-
### React (`@vllnt/analytics/react`)
|
|
167
|
-
|
|
168
|
-
| Export | Description |
|
|
169
|
-
|---|---|
|
|
170
|
-
| `AnalyticsProvider` | Context provider — wires consent + analytics init |
|
|
171
|
-
| `useAnalytics()` | Access full analytics context |
|
|
172
|
-
| `useConsent(options?)` | Standalone consent hook for banner UIs |
|
|
173
|
-
|
|
174
|
-
## How It Works
|
|
203
|
+
// A stored accept starts tracking; a stored decline blocks it and drops queued events.
|
|
204
|
+
if (loadConsent()) initAnalytics(config);
|
|
205
|
+
|
|
206
|
+
export function onAccept(): void {
|
|
207
|
+
saveConsent(createAcceptAllConsent());
|
|
208
|
+
enableAnalytics(config);
|
|
209
|
+
initAnalytics(config);
|
|
210
|
+
}
|
|
175
211
|
|
|
212
|
+
export function onDecline(): void {
|
|
213
|
+
saveConsent(createDeclineAnalyticsConsent());
|
|
214
|
+
disableAnalytics();
|
|
215
|
+
}
|
|
176
216
|
```
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
|
|
217
|
+
|
|
218
|
+
## Errors and fixes
|
|
219
|
+
|
|
220
|
+
The package has no error codes. These are every message it throws or logs as a warning.
|
|
221
|
+
|
|
222
|
+
| Message | Kind | Cause | Fix |
|
|
223
|
+
| --- | --- | --- | --- |
|
|
224
|
+
| `useAnalytics must be used within an AnalyticsProvider` | thrown by `useAnalytics()` | The hook ran outside `<AnalyticsProvider>`. | Render the component inside `<AnalyticsProvider>`, or use `useConsent()`. |
|
|
225
|
+
| `Failed to save consent:` | `console.warn` from `saveConsent()` | Writing `localStorage` or the cookie threw, for example a full quota. | The new choice applies on this page but is not stored, so later visits load the earlier stored choice, if any. A failed accept over a stored decline stays blocked on this page too, because `initAnalytics` re-reads the stored decline. Free storage, or call `clearConsent()` after a failed save. |
|
|
226
|
+
|
|
227
|
+
## Limits and gotchas
|
|
228
|
+
|
|
229
|
+
- No transport and no event callback: events never leave the page. With `debug: true` they are
|
|
230
|
+
logged with `console.log` as `[analytics:track]`, `[analytics:page]` and similar.
|
|
231
|
+
- Before the visitor responds, `track()` and `trackPage()` are queued in memory with no size
|
|
232
|
+
limit. The first unblocked `initAnalytics()` flushes them; a decline or a Do Not Track block drops
|
|
233
|
+
them. After `resetConsent()`, tracking is blocked and events are dropped until the visitor
|
|
234
|
+
responds again.
|
|
235
|
+
- `identify()` before init or while blocked does nothing; it is not queued.
|
|
236
|
+
- `track()` with an empty or non-string name and `identify()` with an empty or non-string id do
|
|
237
|
+
nothing.
|
|
238
|
+
- `initAnalytics()` runs once: later calls, and later `config` values, are ignored while an
|
|
239
|
+
instance exists. Pass a stable `config` (a module constant) to `AnalyticsProvider`.
|
|
240
|
+
- Once blocked, only `enableAnalytics()` unblocks. It re-reads stored consent, so save the accepted
|
|
241
|
+
consent first, and it does nothing while Do Not Track (`"1"` or `"yes"`) is on.
|
|
242
|
+
- `disableAnalytics()` keeps the user id and traits; call `resetAnalytics()` to clear them.
|
|
243
|
+
- On the server, `track`, `trackPage`, `identify` and `resetAnalytics` do nothing and `loadConsent`
|
|
244
|
+
returns `null`. `initAnalytics` has no server guard: call it only in the browser.
|
|
245
|
+
- When `localStorage` is unavailable, `saveConsent` and `clearConsent` do nothing, so the choice
|
|
246
|
+
lasts for the current page only.
|
|
247
|
+
- `updateConsent("functional", false)` is ignored: functional consent is always on.
|
|
248
|
+
`updateConsent` does nothing while `consent` is `null`.
|
|
249
|
+
- `useConsent()` reads and writes stored consent only; it does not start or stop tracking and does
|
|
250
|
+
not share state with `AnalyticsProvider`.
|
|
251
|
+
- `isLoading` is always `false`.
|
|
252
|
+
- The cookie is `vllnt-consent=1` or `vllnt-consent=0`, with a one-year expiry, `path=/` and
|
|
253
|
+
`SameSite=Lax`, and without `Secure` or `HttpOnly`.
|
|
254
|
+
|
|
255
|
+
```text
|
|
256
|
+
AnalyticsProvider mounts
|
|
257
|
+
├─ Stored consent older than consentVersion ──▶ cleared, banner shown again
|
|
258
|
+
├─ Analytics accepted ──▶ initAnalytics()
|
|
259
|
+
│ ├─ Do Not Track on ──▶ blocked, queue dropped
|
|
260
|
+
│ └─ otherwise ──▶ started, queue flushed
|
|
261
|
+
├─ Analytics declined ──▶ disableAnalytics(); track() calls dropped
|
|
262
|
+
└─ No consent yet ──▶ no init; track() calls queued in memory
|
|
187
263
|
```
|
|
188
264
|
|
|
189
|
-
|
|
265
|
+
## Compatibility
|
|
190
266
|
|
|
191
|
-
|
|
267
|
+
- React: peer range `^18.0.0 || ^19.0.0`, used by `./react` only.
|
|
268
|
+
- Module formats: ESM and CommonJS, with type declarations for both, from `dist/`.
|
|
269
|
+
- `./react` starts with `"use client"` in both builds; the core entry does not, so server code can
|
|
270
|
+
import it.
|
|
271
|
+
- Runtimes: browsers for tracking and consent storage. Server runtimes can import every entry point;
|
|
272
|
+
there, use `getConsentFromCookie` to read consent.
|
|
192
273
|
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
|
|
196
|
-
|
|
197
|
-
|
|
198
|
-
pnpm lint:
|
|
199
|
-
|
|
274
|
+
## Testing
|
|
275
|
+
|
|
276
|
+
The analytics state is a module-level singleton with no public reset. In your tests, clear
|
|
277
|
+
`localStorage` between cases and load the module fresh when a case needs a new instance.
|
|
278
|
+
|
|
279
|
+
This repository's own checks: `pnpm lint`, `pnpm typecheck`, `pnpm test:coverage` (Vitest with
|
|
280
|
+
jsdom, 100% coverage gate), `pnpm build` and `pnpm test:package` (installs the packed tarball in a
|
|
281
|
+
clean project and imports and type-checks every entry point).
|
|
282
|
+
|
|
283
|
+
## Security
|
|
284
|
+
|
|
285
|
+
Report vulnerabilities privately through
|
|
286
|
+
[GitHub Security Advisories](https://github.com/vllnt/analytics/security/advisories/new). Releases
|
|
287
|
+
are published from CI with npm trusted publishing and provenance.
|
|
288
|
+
|
|
289
|
+
## Related packages
|
|
290
|
+
|
|
291
|
+
None: no other @vllnt package is used together with this one in code.
|
|
292
|
+
|
|
293
|
+
## Contributing
|
|
294
|
+
|
|
295
|
+
See [CONTRIBUTING.md](https://github.com/vllnt/analytics/blob/main/CONTRIBUTING.md). Setup:
|
|
296
|
+
`pnpm install`, then `pnpm lint && pnpm typecheck && pnpm test:coverage && pnpm build && pnpm test:package`.
|
|
297
|
+
|
|
298
|
+
## Links
|
|
299
|
+
|
|
300
|
+
- Docs and changelog: [oss.vllnt.com/packages/analytics](https://oss.vllnt.com/packages/analytics) · [release feed](https://oss.vllnt.com/packages/analytics/rss.xml)
|
|
301
|
+
- All @vllnt packages: [oss.vllnt.com](https://oss.vllnt.com) · [search packages and docs on index.vllnt.com](https://index.vllnt.com)
|
|
302
|
+
- Source and issues: [GitHub](https://github.com/vllnt/analytics) · [issues](https://github.com/vllnt/analytics/issues)
|
|
303
|
+
- Questions and updates: [Discord](https://bntvllnt.com/discord) · [X @bntvllnt](https://x.com/bntvllnt)
|
|
304
|
+
- Company and author: [vllnt.com](https://vllnt.com) · [bntvllnt.com](https://bntvllnt.com) · [GitHub @bntvllnt](https://github.com/bntvllnt) · [LinkedIn](https://linkedin.com/in/bntvllnt)
|
|
305
|
+
- Sponsor: [GitHub Sponsors](https://github.com/sponsors/bntvllnt)
|
|
200
306
|
|
|
201
307
|
## License
|
|
202
308
|
|
|
203
309
|
[MIT](LICENSE)
|
|
310
|
+
|
|
311
|
+
---
|
|
312
|
+
|
|
313
|
+
Part of [vllnt open source](https://oss.vllnt.com) · [Package page](https://oss.vllnt.com/packages/analytics) · [vllnt.com](https://vllnt.com) · Built by [bntvllnt](https://bntvllnt.com)
|