@vllnt/analytics 0.2.0-canary.13db137 → 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 +243 -137
- package/dist/{analytics-DdAxcZwX.d.cts → analytics-BxUQktfB.d.cts} +3 -1
- package/dist/{analytics-DdAxcZwX.d.ts → analytics-BxUQktfB.d.ts} +3 -1
- package/dist/{chunk-6RHY2BTB.js → chunk-5COCGKHD.js} +7 -4
- package/dist/{chunk-MVOBIOKZ.cjs → chunk-ACWD5NRI.cjs} +7 -4
- package/dist/index.cjs +2 -2
- package/dist/index.d.cts +2 -2
- package/dist/index.d.ts +2 -2
- package/dist/index.js +1 -1
- package/dist/react.cjs +45 -36
- package/dist/react.d.cts +1 -1
- package/dist/react.d.ts +1 -1
- package/dist/react.js +19 -10
- package/docs/API.md +490 -0
- package/llms.txt +74 -0
- package/package.json +36 -26
- package/src/analytics.ts +289 -0
- package/src/consent.ts +179 -0
- package/src/index.ts +48 -0
- package/src/react.tsx +231 -0
- package/src/types.ts +102 -0
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,78 +21,159 @@ Lightweight, privacy-first analytics library with consent management and React i
|
|
|
17
21
|
pnpm add @vllnt/analytics
|
|
18
22
|
```
|
|
19
23
|
|
|
20
|
-
|
|
24
|
+
## Quick start
|
|
25
|
+
|
|
26
|
+
```tsx
|
|
27
|
+
import { track } from "@vllnt/analytics";
|
|
28
|
+
import { AnalyticsProvider, useAnalytics } from "@vllnt/analytics/react";
|
|
29
|
+
|
|
30
|
+
const config = { app: "my-app" };
|
|
31
|
+
|
|
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
|
+
}
|
|
36
|
+
|
|
37
|
+
export const App = () => (
|
|
38
|
+
<AnalyticsProvider config={config}><Banner /><button onClick={() => track("cta_click")}>Buy</button></AnalyticsProvider>
|
|
39
|
+
);
|
|
40
|
+
```
|
|
41
|
+
|
|
42
|
+
## API
|
|
43
|
+
|
|
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.
|
|
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;
|
|
105
|
+
return (
|
|
106
|
+
<div>
|
|
107
|
+
<button onClick={acceptAll}>Accept</button>
|
|
108
|
+
<button onClick={declineAnalytics}>Decline</button>
|
|
109
|
+
</div>
|
|
110
|
+
);
|
|
111
|
+
}
|
|
112
|
+
```
|
|
113
|
+
|
|
114
|
+
### Read consent on the server
|
|
115
|
+
|
|
116
|
+
```ts
|
|
117
|
+
import { getConsentFromCookie } from "@vllnt/analytics";
|
|
21
118
|
|
|
22
|
-
|
|
119
|
+
export function GET(request: Request): Response {
|
|
120
|
+
const allowed = getConsentFromCookie(request.headers.get("cookie") ?? "");
|
|
121
|
+
return Response.json({ allowed });
|
|
122
|
+
}
|
|
123
|
+
```
|
|
23
124
|
|
|
24
|
-
|
|
125
|
+
### Ask every visitor again after a policy change
|
|
25
126
|
|
|
26
|
-
|
|
127
|
+
Raise `consentVersion`. Stored choices with a lower version are cleared once; new choices are saved
|
|
128
|
+
with the new version.
|
|
27
129
|
|
|
28
130
|
```tsx
|
|
131
|
+
import type { ReactNode } from "react";
|
|
29
132
|
import { AnalyticsProvider } from "@vllnt/analytics/react";
|
|
30
133
|
|
|
31
|
-
|
|
134
|
+
const config = { app: "my-app" };
|
|
135
|
+
|
|
136
|
+
export function Root({ children }: { children: ReactNode }) {
|
|
32
137
|
return (
|
|
33
|
-
<AnalyticsProvider config={{
|
|
138
|
+
<AnalyticsProvider config={config} consentVersion={2}>
|
|
34
139
|
{children}
|
|
35
140
|
</AnalyticsProvider>
|
|
36
141
|
);
|
|
37
142
|
}
|
|
38
143
|
```
|
|
39
144
|
|
|
40
|
-
###
|
|
145
|
+
### Let the visitor change their choice later
|
|
41
146
|
|
|
42
147
|
```tsx
|
|
43
|
-
import {
|
|
44
|
-
|
|
45
|
-
function CookieBanner() {
|
|
46
|
-
const { hasResponded, isLoading, acceptAll, declineAnalytics } = useConsent();
|
|
47
|
-
|
|
48
|
-
if (isLoading || hasResponded) return null;
|
|
148
|
+
import { useAnalytics } from "@vllnt/analytics/react";
|
|
49
149
|
|
|
150
|
+
export function PrivacySettings() {
|
|
151
|
+
const { consent, updateConsent, resetConsent } = useAnalytics();
|
|
50
152
|
return (
|
|
51
153
|
<div>
|
|
52
|
-
<
|
|
53
|
-
|
|
54
|
-
|
|
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>
|
|
55
163
|
</div>
|
|
56
164
|
);
|
|
57
165
|
}
|
|
58
166
|
```
|
|
59
167
|
|
|
60
|
-
### Track
|
|
168
|
+
### Track page views, users and tutorials
|
|
61
169
|
|
|
62
|
-
```
|
|
63
|
-
import {
|
|
64
|
-
|
|
65
|
-
// Generic event
|
|
66
|
-
track("button_click", { id: "cta-hero" });
|
|
170
|
+
```ts
|
|
171
|
+
import { identify, resetAnalytics, trackPage, trackSectionView } from "@vllnt/analytics";
|
|
67
172
|
|
|
68
|
-
// Page view
|
|
69
173
|
trackPage({ path: "/about" });
|
|
70
|
-
```
|
|
71
|
-
|
|
72
|
-
### Tutorial Tracking
|
|
73
|
-
|
|
74
|
-
Typed helpers for tutorial/content flows:
|
|
75
|
-
|
|
76
|
-
```tsx
|
|
77
|
-
import {
|
|
78
|
-
trackTutorialStart,
|
|
79
|
-
trackSectionView,
|
|
80
|
-
trackSectionComplete,
|
|
81
|
-
trackTutorialComplete,
|
|
82
|
-
trackTutorialNavigation,
|
|
83
|
-
trackTimeSpent,
|
|
84
|
-
} from "@vllnt/analytics";
|
|
85
174
|
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
tutorial_title: "Getting Started",
|
|
89
|
-
locale: "en",
|
|
90
|
-
total_sections: 5,
|
|
91
|
-
});
|
|
175
|
+
identify("user-123", { plan: "pro" });
|
|
176
|
+
resetAnalytics();
|
|
92
177
|
|
|
93
178
|
trackSectionView({
|
|
94
179
|
tutorial_id: "getting-started",
|
|
@@ -100,108 +185,129 @@ trackSectionView({
|
|
|
100
185
|
});
|
|
101
186
|
```
|
|
102
187
|
|
|
103
|
-
###
|
|
104
|
-
|
|
105
|
-
```tsx
|
|
106
|
-
import { identify, resetAnalytics } from "@vllnt/analytics";
|
|
107
|
-
|
|
108
|
-
// After login
|
|
109
|
-
identify("user-123", { plan: "pro" });
|
|
110
|
-
|
|
111
|
-
// After logout
|
|
112
|
-
resetAnalytics();
|
|
113
|
-
```
|
|
114
|
-
|
|
115
|
-
### Consent Utilities (non-React)
|
|
188
|
+
### Use it without React
|
|
116
189
|
|
|
117
190
|
```ts
|
|
118
191
|
import {
|
|
119
|
-
loadConsent,
|
|
120
|
-
saveConsent,
|
|
121
|
-
clearConsent,
|
|
122
192
|
createAcceptAllConsent,
|
|
123
193
|
createDeclineAnalyticsConsent,
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
194
|
+
disableAnalytics,
|
|
195
|
+
enableAnalytics,
|
|
196
|
+
initAnalytics,
|
|
197
|
+
loadConsent,
|
|
198
|
+
saveConsent,
|
|
127
199
|
} from "@vllnt/analytics";
|
|
128
200
|
|
|
129
|
-
|
|
130
|
-
|
|
201
|
+
const config = { app: "my-app" };
|
|
202
|
+
|
|
203
|
+
// A stored accept starts tracking; a stored decline blocks it and drops queued events.
|
|
204
|
+
if (loadConsent()) initAnalytics(config);
|
|
131
205
|
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
206
|
+
export function onAccept(): void {
|
|
207
|
+
saveConsent(createAcceptAllConsent());
|
|
208
|
+
enableAnalytics(config);
|
|
209
|
+
initAnalytics(config);
|
|
210
|
+
}
|
|
211
|
+
|
|
212
|
+
export function onDecline(): void {
|
|
213
|
+
saveConsent(createDeclineAnalyticsConsent());
|
|
214
|
+
disableAnalytics();
|
|
136
215
|
}
|
|
137
216
|
```
|
|
138
217
|
|
|
139
|
-
##
|
|
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
|
|
263
|
+
```
|
|
140
264
|
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
| Function | Description |
|
|
144
|
-
| --------------------------- | ---------------------------------------------- |
|
|
145
|
-
| `initAnalytics(config)` | Initialize analytics singleton |
|
|
146
|
-
| `track(name, properties?)` | Track a custom event (empty names are ignored) |
|
|
147
|
-
| `trackPage(properties?)` | Track a page view |
|
|
148
|
-
| `identify(userId, traits?)` | Identify a user (empty ids are ignored) |
|
|
149
|
-
| `resetAnalytics()` | Clear user identity (logout) |
|
|
150
|
-
| `enableAnalytics(config)` | Re-enable after consent granted |
|
|
151
|
-
| `disableAnalytics()` | Disable after consent revoked |
|
|
152
|
-
| `isAnalyticsEnabled()` | Check if tracking is active |
|
|
153
|
-
|
|
154
|
-
### Consent
|
|
155
|
-
|
|
156
|
-
| Function | Description |
|
|
157
|
-
| ----------------------------------------------- | ---------------------------------------- |
|
|
158
|
-
| `loadConsent()` | Load consent from localStorage |
|
|
159
|
-
| `saveConsent(state)` | Persist consent to localStorage + cookie |
|
|
160
|
-
| `clearConsent()` | Remove consent (triggers re-prompt) |
|
|
161
|
-
| `createAcceptAllConsent()` | Create accept-all consent state |
|
|
162
|
-
| `createDeclineAnalyticsConsent()` | Create analytics-declined state |
|
|
163
|
-
| `updateConsentCategory(state, category, value)` | Update a single category |
|
|
164
|
-
| `needsRePrompt(consent, version)` | Check if version requires re-prompt |
|
|
165
|
-
| `getConsentFromCookie(cookieString)` | Parse consent from cookie (SSR) |
|
|
166
|
-
| `isDoNotTrackEnabled()` | Check browser DNT setting |
|
|
167
|
-
|
|
168
|
-
### React (`@vllnt/analytics/react`)
|
|
169
|
-
|
|
170
|
-
| Export | Description |
|
|
171
|
-
| ---------------------- | ------------------------------------------------- |
|
|
172
|
-
| `AnalyticsProvider` | Context provider — wires consent + analytics init |
|
|
173
|
-
| `useAnalytics()` | Access full analytics context |
|
|
174
|
-
| `useConsent(options?)` | Standalone consent hook for banner UIs |
|
|
175
|
-
|
|
176
|
-
## How It Works
|
|
265
|
+
## Compatibility
|
|
177
266
|
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
│ ├─ Version outdated? ──▶ Clear & re-prompt
|
|
185
|
-
│ ├─ Analytics declined? ──▶ Block tracking
|
|
186
|
-
│ └─ Analytics accepted ──▶ Initialize & flush queue
|
|
187
|
-
│
|
|
188
|
-
└─ No consent ──▶ Queue events until user responds
|
|
189
|
-
```
|
|
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.
|
|
190
273
|
|
|
191
|
-
|
|
274
|
+
## Testing
|
|
192
275
|
|
|
193
|
-
|
|
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.
|
|
194
278
|
|
|
195
|
-
|
|
196
|
-
pnpm
|
|
197
|
-
|
|
198
|
-
|
|
199
|
-
|
|
200
|
-
|
|
201
|
-
|
|
202
|
-
|
|
203
|
-
|
|
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)
|
|
204
306
|
|
|
205
307
|
## License
|
|
206
308
|
|
|
207
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)
|
|
@@ -96,7 +96,9 @@ type AnalyticsState = {
|
|
|
96
96
|
version?: number;
|
|
97
97
|
};
|
|
98
98
|
/**
|
|
99
|
-
*
|
|
99
|
+
* Start the shared analytics instance and flush queued events. Blocks tracking,
|
|
100
|
+
* and drops the queue, when Do Not Track is on or stored consent declines it.
|
|
101
|
+
* @see https://oss.vllnt.com/packages/analytics
|
|
100
102
|
*/
|
|
101
103
|
declare function initAnalytics(config: AnalyticsConfig): void;
|
|
102
104
|
/**
|
|
@@ -96,7 +96,9 @@ type AnalyticsState = {
|
|
|
96
96
|
version?: number;
|
|
97
97
|
};
|
|
98
98
|
/**
|
|
99
|
-
*
|
|
99
|
+
* Start the shared analytics instance and flush queued events. Blocks tracking,
|
|
100
|
+
* and drops the queue, when Do Not Track is on or stored consent declines it.
|
|
101
|
+
* @see https://oss.vllnt.com/packages/analytics
|
|
100
102
|
*/
|
|
101
103
|
declare function initAnalytics(config: AnalyticsConfig): void;
|
|
102
104
|
/**
|
|
@@ -98,6 +98,10 @@ var analyticsState;
|
|
|
98
98
|
var eventQueue = [];
|
|
99
99
|
var isInitialized = false;
|
|
100
100
|
var isBlocked = false;
|
|
101
|
+
function block() {
|
|
102
|
+
isBlocked = true;
|
|
103
|
+
eventQueue = [];
|
|
104
|
+
}
|
|
101
105
|
function debugLog(type, data) {
|
|
102
106
|
if (analyticsState?.debug) {
|
|
103
107
|
console.log(`[analytics:${type}]`, data);
|
|
@@ -109,13 +113,13 @@ function sendEvent(name, properties) {
|
|
|
109
113
|
function initAnalytics(config) {
|
|
110
114
|
if (analyticsState) return;
|
|
111
115
|
if (isDoNotTrackEnabled()) {
|
|
112
|
-
|
|
116
|
+
block();
|
|
113
117
|
debugLog("init", "Do Not Track enabled, analytics blocked");
|
|
114
118
|
return;
|
|
115
119
|
}
|
|
116
120
|
const consent = loadConsent();
|
|
117
121
|
if (consent && !consent.analytics) {
|
|
118
|
-
|
|
122
|
+
block();
|
|
119
123
|
debugLog("init", "Analytics consent declined");
|
|
120
124
|
return;
|
|
121
125
|
}
|
|
@@ -195,8 +199,7 @@ function enableAnalytics(config) {
|
|
|
195
199
|
}
|
|
196
200
|
}
|
|
197
201
|
function disableAnalytics() {
|
|
198
|
-
|
|
199
|
-
eventQueue.length = 0;
|
|
202
|
+
block();
|
|
200
203
|
}
|
|
201
204
|
function isAnalyticsEnabled() {
|
|
202
205
|
return isInitialized && !isBlocked;
|
|
@@ -98,6 +98,10 @@ var analyticsState;
|
|
|
98
98
|
var eventQueue = [];
|
|
99
99
|
var isInitialized = false;
|
|
100
100
|
var isBlocked = false;
|
|
101
|
+
function block() {
|
|
102
|
+
isBlocked = true;
|
|
103
|
+
eventQueue = [];
|
|
104
|
+
}
|
|
101
105
|
function debugLog(type, data) {
|
|
102
106
|
if (_optionalChain([analyticsState, 'optionalAccess', _ => _.debug])) {
|
|
103
107
|
console.log(`[analytics:${type}]`, data);
|
|
@@ -109,13 +113,13 @@ function sendEvent(name, properties) {
|
|
|
109
113
|
function initAnalytics(config) {
|
|
110
114
|
if (analyticsState) return;
|
|
111
115
|
if (isDoNotTrackEnabled()) {
|
|
112
|
-
|
|
116
|
+
block();
|
|
113
117
|
debugLog("init", "Do Not Track enabled, analytics blocked");
|
|
114
118
|
return;
|
|
115
119
|
}
|
|
116
120
|
const consent = loadConsent();
|
|
117
121
|
if (consent && !consent.analytics) {
|
|
118
|
-
|
|
122
|
+
block();
|
|
119
123
|
debugLog("init", "Analytics consent declined");
|
|
120
124
|
return;
|
|
121
125
|
}
|
|
@@ -195,8 +199,7 @@ function enableAnalytics(config) {
|
|
|
195
199
|
}
|
|
196
200
|
}
|
|
197
201
|
function disableAnalytics() {
|
|
198
|
-
|
|
199
|
-
eventQueue.length = 0;
|
|
202
|
+
block();
|
|
200
203
|
}
|
|
201
204
|
function isAnalyticsEnabled() {
|
|
202
205
|
return isInitialized && !isBlocked;
|
package/dist/index.cjs
CHANGED
|
@@ -28,7 +28,7 @@
|
|
|
28
28
|
|
|
29
29
|
|
|
30
30
|
|
|
31
|
-
var
|
|
31
|
+
var _chunkACWD5NRIcjs = require('./chunk-ACWD5NRI.cjs');
|
|
32
32
|
|
|
33
33
|
|
|
34
34
|
|
|
@@ -59,4 +59,4 @@ var _chunkMVOBIOKZcjs = require('./chunk-MVOBIOKZ.cjs');
|
|
|
59
59
|
|
|
60
60
|
|
|
61
61
|
|
|
62
|
-
exports.CONSENT_COOKIE_NAME =
|
|
62
|
+
exports.CONSENT_COOKIE_NAME = _chunkACWD5NRIcjs.CONSENT_COOKIE_NAME; exports.CONSENT_STORAGE_KEY = _chunkACWD5NRIcjs.CONSENT_STORAGE_KEY; exports.CONSENT_VERSION = _chunkACWD5NRIcjs.CONSENT_VERSION; exports.clearConsent = _chunkACWD5NRIcjs.clearConsent; exports.createAcceptAllConsent = _chunkACWD5NRIcjs.createAcceptAllConsent; exports.createDeclineAnalyticsConsent = _chunkACWD5NRIcjs.createDeclineAnalyticsConsent; exports.disableAnalytics = _chunkACWD5NRIcjs.disableAnalytics; exports.enableAnalytics = _chunkACWD5NRIcjs.enableAnalytics; exports.getAnalyticsState = _chunkACWD5NRIcjs.getAnalyticsState; exports.getConsentFromCookie = _chunkACWD5NRIcjs.getConsentFromCookie; exports.identify = _chunkACWD5NRIcjs.identify; exports.initAnalytics = _chunkACWD5NRIcjs.initAnalytics; exports.isAnalyticsEnabled = _chunkACWD5NRIcjs.isAnalyticsEnabled; exports.isDoNotTrackEnabled = _chunkACWD5NRIcjs.isDoNotTrackEnabled; exports.isStorageAvailable = _chunkACWD5NRIcjs.isStorageAvailable; exports.isValidConsentState = _chunkACWD5NRIcjs.isValidConsentState; exports.loadConsent = _chunkACWD5NRIcjs.loadConsent; exports.needsRePrompt = _chunkACWD5NRIcjs.needsRePrompt; exports.resetAnalytics = _chunkACWD5NRIcjs.resetAnalytics; exports.saveConsent = _chunkACWD5NRIcjs.saveConsent; exports.track = _chunkACWD5NRIcjs.track; exports.trackPage = _chunkACWD5NRIcjs.trackPage; exports.trackSectionComplete = _chunkACWD5NRIcjs.trackSectionComplete; exports.trackSectionView = _chunkACWD5NRIcjs.trackSectionView; exports.trackTimeSpent = _chunkACWD5NRIcjs.trackTimeSpent; exports.trackTutorialComplete = _chunkACWD5NRIcjs.trackTutorialComplete; exports.trackTutorialNavigation = _chunkACWD5NRIcjs.trackTutorialNavigation; exports.trackTutorialStart = _chunkACWD5NRIcjs.trackTutorialStart; exports.updateConsentCategory = _chunkACWD5NRIcjs.updateConsentCategory;
|
package/dist/index.d.cts
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
|
-
import { C as ConsentState, a as ConsentCategory } from './analytics-
|
|
2
|
-
export { A as AnalyticsConfig, b as AnalyticsEvent, T as TutorialEventName, c as TutorialEventProperties, U as UseConsentOptions, d as UseConsentReturn, e as disableAnalytics, f as enableAnalytics, g as getAnalyticsState, i as identify, h as initAnalytics, j as isAnalyticsEnabled, r as resetAnalytics, t as track, k as trackPage, l as trackSectionComplete, m as trackSectionView, n as trackTimeSpent, o as trackTutorialComplete, p as trackTutorialNavigation, q as trackTutorialStart } from './analytics-
|
|
1
|
+
import { C as ConsentState, a as ConsentCategory } from './analytics-BxUQktfB.cjs';
|
|
2
|
+
export { A as AnalyticsConfig, b as AnalyticsEvent, T as TutorialEventName, c as TutorialEventProperties, U as UseConsentOptions, d as UseConsentReturn, e as disableAnalytics, f as enableAnalytics, g as getAnalyticsState, i as identify, h as initAnalytics, j as isAnalyticsEnabled, r as resetAnalytics, t as track, k as trackPage, l as trackSectionComplete, m as trackSectionView, n as trackTimeSpent, o as trackTutorialComplete, p as trackTutorialNavigation, q as trackTutorialStart } from './analytics-BxUQktfB.cjs';
|
|
3
3
|
|
|
4
4
|
/** Current consent schema version - bump to re-prompt users */
|
|
5
5
|
declare const CONSENT_VERSION = 1;
|
package/dist/index.d.ts
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
|
-
import { C as ConsentState, a as ConsentCategory } from './analytics-
|
|
2
|
-
export { A as AnalyticsConfig, b as AnalyticsEvent, T as TutorialEventName, c as TutorialEventProperties, U as UseConsentOptions, d as UseConsentReturn, e as disableAnalytics, f as enableAnalytics, g as getAnalyticsState, i as identify, h as initAnalytics, j as isAnalyticsEnabled, r as resetAnalytics, t as track, k as trackPage, l as trackSectionComplete, m as trackSectionView, n as trackTimeSpent, o as trackTutorialComplete, p as trackTutorialNavigation, q as trackTutorialStart } from './analytics-
|
|
1
|
+
import { C as ConsentState, a as ConsentCategory } from './analytics-BxUQktfB.js';
|
|
2
|
+
export { A as AnalyticsConfig, b as AnalyticsEvent, T as TutorialEventName, c as TutorialEventProperties, U as UseConsentOptions, d as UseConsentReturn, e as disableAnalytics, f as enableAnalytics, g as getAnalyticsState, i as identify, h as initAnalytics, j as isAnalyticsEnabled, r as resetAnalytics, t as track, k as trackPage, l as trackSectionComplete, m as trackSectionView, n as trackTimeSpent, o as trackTutorialComplete, p as trackTutorialNavigation, q as trackTutorialStart } from './analytics-BxUQktfB.js';
|
|
3
3
|
|
|
4
4
|
/** Current consent schema version - bump to re-prompt users */
|
|
5
5
|
declare const CONSENT_VERSION = 1;
|