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