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