@vllnt/analytics 0.2.0-canary.8064ae0 → 0.2.0-canary.9bbc838
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 +18 -9
- package/README.md +5 -3
- 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.js +1 -1
- package/dist/react.cjs +45 -36
- package/dist/react.js +19 -10
- package/docs/API.md +16 -16
- package/llms.txt +7 -7
- package/package.json +7 -21
- package/src/analytics.ts +12 -4
- package/src/react.tsx +20 -10
package/CHANGELOG.md
CHANGED
|
@@ -5,33 +5,42 @@ All notable changes to this project are documented in this file.
|
|
|
5
5
|
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.1.0/), and this project
|
|
6
6
|
adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
|
|
7
7
|
|
|
8
|
-
## [0.2.0] -
|
|
8
|
+
## [0.2.0] - 2026-11-27
|
|
9
9
|
|
|
10
10
|
### Added
|
|
11
11
|
|
|
12
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
|
|
14
|
-
|
|
15
|
-
relative paths.
|
|
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.
|
|
16
15
|
|
|
17
16
|
### Changed
|
|
18
17
|
|
|
19
18
|
- The package entry points now resolve to compiled output in `dist` (ESM, CommonJS and type
|
|
20
19
|
declarations) instead of raw TypeScript sources, so it works without transpiling
|
|
21
20
|
`node_modules`. The `.` and `./react` export subpaths and the `react` peer range are
|
|
22
|
-
unchanged. `./package.json` is now exported too.
|
|
23
|
-
|
|
24
|
-
|
|
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 sent 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 sent by a
|
|
30
|
+
later successful `enableAnalytics()` (after the user accepted, or Do Not Track was turned off).
|
|
25
31
|
|
|
26
32
|
### Fixed
|
|
27
33
|
|
|
28
34
|
- `getConsentFromCookie()` no longer matches look-alike cookie names that merely end with
|
|
29
35
|
`vllnt-consent` (for example `evil-vllnt-consent=1`).
|
|
36
|
+
- `AnalyticsProvider` and `useConsent` save each choice with their `consentVersion`. Before, they
|
|
37
|
+
always saved version `1`, so a `consentVersion` above `1` asked the user again on every visit.
|
|
30
38
|
|
|
31
39
|
### Security
|
|
32
40
|
|
|
33
|
-
-
|
|
34
|
-
|
|
41
|
+
- `@latest` is now published only from a `vX.Y.Z` tag on `main` that matches `package.json` and
|
|
42
|
+
this changelog. Before, a manual workflow run bumped the version in CI. Publishing still uses
|
|
43
|
+
npm trusted publishing (OIDC) with provenance, as in 0.1.1.
|
|
35
44
|
|
|
36
45
|
## [0.1.1] - 2026-03-10
|
|
37
46
|
|
package/README.md
CHANGED
|
@@ -205,11 +205,13 @@ AnalyticsProvider mounts
|
|
|
205
205
|
│ ├─ DNT enabled? ──▶ Block tracking
|
|
206
206
|
│ └─ Otherwise ──▶ Start & flush queue
|
|
207
207
|
│
|
|
208
|
-
|
|
209
|
-
|
|
208
|
+
├─ Analytics declined ──▶ disableAnalytics(); track() calls are dropped
|
|
209
|
+
│
|
|
210
|
+
└─ No consent yet ──▶ No init; track() calls are queued in memory
|
|
210
211
|
```
|
|
211
212
|
|
|
212
|
-
Events fired before
|
|
213
|
+
Events fired before the user responds are queued and flushed once consent is granted and
|
|
214
|
+
analytics initializes. A decline, or an init blocked by Do Not Track, drops them.
|
|
213
215
|
|
|
214
216
|
## Scripts
|
|
215
217
|
|
|
@@ -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.js
CHANGED
package/dist/react.cjs
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
"use strict";Object.defineProperty(exports, "__esModule", {value: true});
|
|
1
|
+
"use client";"use strict";Object.defineProperty(exports, "__esModule", {value: true});
|
|
2
2
|
|
|
3
3
|
|
|
4
4
|
|
|
@@ -11,7 +11,7 @@
|
|
|
11
11
|
|
|
12
12
|
|
|
13
13
|
|
|
14
|
-
var
|
|
14
|
+
var _chunkACWD5NRIcjs = require('./chunk-ACWD5NRI.cjs');
|
|
15
15
|
|
|
16
16
|
// src/react.tsx
|
|
17
17
|
|
|
@@ -27,58 +27,64 @@ var AnalyticsContext = _react.createContext.call(void 0, null);
|
|
|
27
27
|
function AnalyticsProvider({
|
|
28
28
|
children,
|
|
29
29
|
config,
|
|
30
|
-
consentVersion =
|
|
30
|
+
consentVersion = _chunkACWD5NRIcjs.CONSENT_VERSION
|
|
31
31
|
}) {
|
|
32
32
|
const [consent, setConsent] = _react.useState.call(void 0, () => {
|
|
33
|
-
const stored =
|
|
33
|
+
const stored = _chunkACWD5NRIcjs.loadConsent.call(void 0, );
|
|
34
34
|
if (!stored) return null;
|
|
35
|
-
if (
|
|
36
|
-
|
|
35
|
+
if (_chunkACWD5NRIcjs.needsRePrompt.call(void 0, stored, consentVersion)) {
|
|
36
|
+
_chunkACWD5NRIcjs.clearConsent.call(void 0, );
|
|
37
37
|
return null;
|
|
38
38
|
}
|
|
39
39
|
return stored;
|
|
40
40
|
});
|
|
41
41
|
const [hasResponded, setHasResponded] = _react.useState.call(void 0, () => consent !== null);
|
|
42
42
|
_react.useEffect.call(void 0, () => {
|
|
43
|
-
if (
|
|
44
|
-
|
|
43
|
+
if (!consent) return;
|
|
44
|
+
if (consent.analytics) {
|
|
45
|
+
_chunkACWD5NRIcjs.initAnalytics.call(void 0, config);
|
|
46
|
+
} else {
|
|
47
|
+
_chunkACWD5NRIcjs.disableAnalytics.call(void 0, );
|
|
45
48
|
}
|
|
46
49
|
}, [consent, config]);
|
|
47
50
|
const acceptAll = _react.useCallback.call(void 0, () => {
|
|
48
|
-
const newConsent =
|
|
49
|
-
|
|
51
|
+
const newConsent = { ..._chunkACWD5NRIcjs.createAcceptAllConsent.call(void 0, ), version: consentVersion };
|
|
52
|
+
_chunkACWD5NRIcjs.saveConsent.call(void 0, newConsent);
|
|
50
53
|
setConsent(newConsent);
|
|
51
54
|
setHasResponded(true);
|
|
52
|
-
|
|
53
|
-
}, [config]);
|
|
55
|
+
_chunkACWD5NRIcjs.enableAnalytics.call(void 0, config);
|
|
56
|
+
}, [config, consentVersion]);
|
|
54
57
|
const declineAnalytics = _react.useCallback.call(void 0, () => {
|
|
55
|
-
const newConsent =
|
|
56
|
-
|
|
58
|
+
const newConsent = {
|
|
59
|
+
..._chunkACWD5NRIcjs.createDeclineAnalyticsConsent.call(void 0, ),
|
|
60
|
+
version: consentVersion
|
|
61
|
+
};
|
|
62
|
+
_chunkACWD5NRIcjs.saveConsent.call(void 0, newConsent);
|
|
57
63
|
setConsent(newConsent);
|
|
58
64
|
setHasResponded(true);
|
|
59
|
-
|
|
60
|
-
}, []);
|
|
65
|
+
_chunkACWD5NRIcjs.disableAnalytics.call(void 0, );
|
|
66
|
+
}, [consentVersion]);
|
|
61
67
|
const updateConsentHandler = _react.useCallback.call(void 0,
|
|
62
68
|
(category, value2) => {
|
|
63
69
|
if (!consent) return;
|
|
64
|
-
const newConsent =
|
|
65
|
-
|
|
70
|
+
const newConsent = _chunkACWD5NRIcjs.updateConsentCategory.call(void 0, consent, category, value2);
|
|
71
|
+
_chunkACWD5NRIcjs.saveConsent.call(void 0, newConsent);
|
|
66
72
|
setConsent(newConsent);
|
|
67
73
|
if (category === "analytics") {
|
|
68
74
|
if (value2) {
|
|
69
|
-
|
|
75
|
+
_chunkACWD5NRIcjs.enableAnalytics.call(void 0, config);
|
|
70
76
|
} else {
|
|
71
|
-
|
|
77
|
+
_chunkACWD5NRIcjs.disableAnalytics.call(void 0, );
|
|
72
78
|
}
|
|
73
79
|
}
|
|
74
80
|
},
|
|
75
81
|
[consent, config]
|
|
76
82
|
);
|
|
77
83
|
const resetConsentHandler = _react.useCallback.call(void 0, () => {
|
|
78
|
-
|
|
84
|
+
_chunkACWD5NRIcjs.clearConsent.call(void 0, );
|
|
79
85
|
setConsent(null);
|
|
80
86
|
setHasResponded(false);
|
|
81
|
-
|
|
87
|
+
_chunkACWD5NRIcjs.disableAnalytics.call(void 0, );
|
|
82
88
|
}, []);
|
|
83
89
|
const value = _react.useMemo.call(void 0,
|
|
84
90
|
() => ({
|
|
@@ -111,40 +117,43 @@ function useAnalytics() {
|
|
|
111
117
|
return context;
|
|
112
118
|
}
|
|
113
119
|
function useConsent(options = {}) {
|
|
114
|
-
const { consentVersion =
|
|
120
|
+
const { consentVersion = _chunkACWD5NRIcjs.CONSENT_VERSION } = options;
|
|
115
121
|
const [consent, setConsent] = _react.useState.call(void 0, () => {
|
|
116
|
-
const stored =
|
|
122
|
+
const stored = _chunkACWD5NRIcjs.loadConsent.call(void 0, );
|
|
117
123
|
if (!stored) return null;
|
|
118
|
-
if (
|
|
119
|
-
|
|
124
|
+
if (_chunkACWD5NRIcjs.needsRePrompt.call(void 0, stored, consentVersion)) {
|
|
125
|
+
_chunkACWD5NRIcjs.clearConsent.call(void 0, );
|
|
120
126
|
return null;
|
|
121
127
|
}
|
|
122
128
|
return stored;
|
|
123
129
|
});
|
|
124
130
|
const [hasResponded, setHasResponded] = _react.useState.call(void 0, () => consent !== null);
|
|
125
131
|
const acceptAll = _react.useCallback.call(void 0, () => {
|
|
126
|
-
const newConsent =
|
|
127
|
-
|
|
132
|
+
const newConsent = { ..._chunkACWD5NRIcjs.createAcceptAllConsent.call(void 0, ), version: consentVersion };
|
|
133
|
+
_chunkACWD5NRIcjs.saveConsent.call(void 0, newConsent);
|
|
128
134
|
setConsent(newConsent);
|
|
129
135
|
setHasResponded(true);
|
|
130
|
-
}, []);
|
|
136
|
+
}, [consentVersion]);
|
|
131
137
|
const declineAnalytics = _react.useCallback.call(void 0, () => {
|
|
132
|
-
const newConsent =
|
|
133
|
-
|
|
138
|
+
const newConsent = {
|
|
139
|
+
..._chunkACWD5NRIcjs.createDeclineAnalyticsConsent.call(void 0, ),
|
|
140
|
+
version: consentVersion
|
|
141
|
+
};
|
|
142
|
+
_chunkACWD5NRIcjs.saveConsent.call(void 0, newConsent);
|
|
134
143
|
setConsent(newConsent);
|
|
135
144
|
setHasResponded(true);
|
|
136
|
-
}, []);
|
|
145
|
+
}, [consentVersion]);
|
|
137
146
|
const updateConsentHandler = _react.useCallback.call(void 0,
|
|
138
147
|
(category, value) => {
|
|
139
148
|
if (!consent) return;
|
|
140
|
-
const newConsent =
|
|
141
|
-
|
|
149
|
+
const newConsent = _chunkACWD5NRIcjs.updateConsentCategory.call(void 0, consent, category, value);
|
|
150
|
+
_chunkACWD5NRIcjs.saveConsent.call(void 0, newConsent);
|
|
142
151
|
setConsent(newConsent);
|
|
143
152
|
},
|
|
144
153
|
[consent]
|
|
145
154
|
);
|
|
146
155
|
const resetConsentHandler = _react.useCallback.call(void 0, () => {
|
|
147
|
-
|
|
156
|
+
_chunkACWD5NRIcjs.clearConsent.call(void 0, );
|
|
148
157
|
setConsent(null);
|
|
149
158
|
setHasResponded(false);
|
|
150
159
|
}, []);
|
|
@@ -163,4 +172,4 @@ function useConsent(options = {}) {
|
|
|
163
172
|
|
|
164
173
|
|
|
165
174
|
|
|
166
|
-
exports.AnalyticsProvider = AnalyticsProvider; exports.initAnalytics =
|
|
175
|
+
exports.AnalyticsProvider = AnalyticsProvider; exports.initAnalytics = _chunkACWD5NRIcjs.initAnalytics; exports.useAnalytics = useAnalytics; exports.useConsent = useConsent;
|
package/dist/react.js
CHANGED
|
@@ -11,7 +11,7 @@ import {
|
|
|
11
11
|
needsRePrompt,
|
|
12
12
|
saveConsent,
|
|
13
13
|
updateConsentCategory
|
|
14
|
-
} from "./chunk-
|
|
14
|
+
} from "./chunk-5COCGKHD.js";
|
|
15
15
|
|
|
16
16
|
// src/react.tsx
|
|
17
17
|
import {
|
|
@@ -40,24 +40,30 @@ function AnalyticsProvider({
|
|
|
40
40
|
});
|
|
41
41
|
const [hasResponded, setHasResponded] = useState(() => consent !== null);
|
|
42
42
|
useEffect(() => {
|
|
43
|
-
if (consent
|
|
43
|
+
if (!consent) return;
|
|
44
|
+
if (consent.analytics) {
|
|
44
45
|
initAnalytics(config);
|
|
46
|
+
} else {
|
|
47
|
+
disableAnalytics();
|
|
45
48
|
}
|
|
46
49
|
}, [consent, config]);
|
|
47
50
|
const acceptAll = useCallback(() => {
|
|
48
|
-
const newConsent = createAcceptAllConsent();
|
|
51
|
+
const newConsent = { ...createAcceptAllConsent(), version: consentVersion };
|
|
49
52
|
saveConsent(newConsent);
|
|
50
53
|
setConsent(newConsent);
|
|
51
54
|
setHasResponded(true);
|
|
52
55
|
enableAnalytics(config);
|
|
53
|
-
}, [config]);
|
|
56
|
+
}, [config, consentVersion]);
|
|
54
57
|
const declineAnalytics = useCallback(() => {
|
|
55
|
-
const newConsent =
|
|
58
|
+
const newConsent = {
|
|
59
|
+
...createDeclineAnalyticsConsent(),
|
|
60
|
+
version: consentVersion
|
|
61
|
+
};
|
|
56
62
|
saveConsent(newConsent);
|
|
57
63
|
setConsent(newConsent);
|
|
58
64
|
setHasResponded(true);
|
|
59
65
|
disableAnalytics();
|
|
60
|
-
}, []);
|
|
66
|
+
}, [consentVersion]);
|
|
61
67
|
const updateConsentHandler = useCallback(
|
|
62
68
|
(category, value2) => {
|
|
63
69
|
if (!consent) return;
|
|
@@ -123,17 +129,20 @@ function useConsent(options = {}) {
|
|
|
123
129
|
});
|
|
124
130
|
const [hasResponded, setHasResponded] = useState(() => consent !== null);
|
|
125
131
|
const acceptAll = useCallback(() => {
|
|
126
|
-
const newConsent = createAcceptAllConsent();
|
|
132
|
+
const newConsent = { ...createAcceptAllConsent(), version: consentVersion };
|
|
127
133
|
saveConsent(newConsent);
|
|
128
134
|
setConsent(newConsent);
|
|
129
135
|
setHasResponded(true);
|
|
130
|
-
}, []);
|
|
136
|
+
}, [consentVersion]);
|
|
131
137
|
const declineAnalytics = useCallback(() => {
|
|
132
|
-
const newConsent =
|
|
138
|
+
const newConsent = {
|
|
139
|
+
...createDeclineAnalyticsConsent(),
|
|
140
|
+
version: consentVersion
|
|
141
|
+
};
|
|
133
142
|
saveConsent(newConsent);
|
|
134
143
|
setConsent(newConsent);
|
|
135
144
|
setHasResponded(true);
|
|
136
|
-
}, []);
|
|
145
|
+
}, [consentVersion]);
|
|
137
146
|
const updateConsentHandler = useCallback(
|
|
138
147
|
(category, value) => {
|
|
139
148
|
if (!consent) return;
|
package/docs/API.md
CHANGED
|
@@ -35,7 +35,8 @@ starts the process-wide instance, so call it only in the browser.
|
|
|
35
35
|
|
|
36
36
|
Starts the shared analytics instance and flushes queued events. Later calls do nothing while an
|
|
37
37
|
instance exists. It does not start, and blocks tracking, when Do Not Track is on (`"1"` or
|
|
38
|
-
`"yes"`) or stored consent has `analytics: false`; already queued events are
|
|
38
|
+
`"yes"`) or stored consent has `analytics: false`; already queued events are then dropped, so
|
|
39
|
+
they are never sent later.
|
|
39
40
|
With no stored consent it starts, so call it only after the user accepts if you need opt-in
|
|
40
41
|
(`AnalyticsProvider` does this for you). Once tracking is blocked (by a blocked init or by
|
|
41
42
|
`disableAnalytics`), calling `initAnalytics` again does not unblock it; only `enableAnalytics`
|
|
@@ -183,7 +184,8 @@ clearConsent();
|
|
|
183
184
|
|
|
184
185
|
### `createAcceptAllConsent(): ConsentState`
|
|
185
186
|
|
|
186
|
-
New state with `analytics: true`, `functional: true`, the current time and `CONSENT_VERSION`.
|
|
187
|
+
New state with `analytics: true`, `functional: true`, the current time and `CONSENT_VERSION`. To
|
|
188
|
+
record another version, override it: `{ ...createAcceptAllConsent(), version: 2 }`.
|
|
187
189
|
|
|
188
190
|
```ts
|
|
189
191
|
const accepted = createAcceptAllConsent();
|
|
@@ -252,7 +254,8 @@ if (isValidConsentState(parsed)) saveConsent(parsed);
|
|
|
252
254
|
|
|
253
255
|
## React (`@vllnt/analytics/react`)
|
|
254
256
|
|
|
255
|
-
|
|
257
|
+
Both builds of this entry (`dist/react.js` and `dist/react.cjs`) start with `"use client"`; the
|
|
258
|
+
core entry does not.
|
|
256
259
|
|
|
257
260
|
Both `AnalyticsProvider` and `useConsent` read stored consent during the first render. On the
|
|
258
261
|
server that read returns `null`, so `consent` is `null` and `hasResponded` is `false` there, while
|
|
@@ -269,14 +272,13 @@ function AnalyticsProvider(props: {
|
|
|
269
272
|
}): React.JSX.Element;
|
|
270
273
|
```
|
|
271
274
|
|
|
272
|
-
Reads stored consent, clears it when `needsRePrompt(stored, consentVersion)`,
|
|
273
|
-
`initAnalytics(config)` while consent has `analytics: true
|
|
274
|
-
|
|
275
|
-
|
|
276
|
-
|
|
277
|
-
|
|
278
|
-
|
|
279
|
-
later. Call `disableAnalytics()` yourself if events from a declined visitor must be dropped.
|
|
275
|
+
Reads stored consent, clears it when `needsRePrompt(stored, consentVersion)`, calls
|
|
276
|
+
`initAnalytics(config)` while consent has `analytics: true`, and calls `disableAnalytics()` while
|
|
277
|
+
it has `analytics: false`, so after a stored decline `track()` calls are dropped, not queued. Its
|
|
278
|
+
context actions save consent with `consentVersion` and call `enableAnalytics` or
|
|
279
|
+
`disableAnalytics`. Before the user responds (no stored consent), events are queued in memory and
|
|
280
|
+
flushed if the user accepts. Pass a stable `config` (for example a module constant), because the
|
|
281
|
+
init effect depends on it.
|
|
280
282
|
|
|
281
283
|
```tsx
|
|
282
284
|
const config = { app: "my-app", version: 1 };
|
|
@@ -323,12 +325,10 @@ const { consent, updateConsent } = useConsent({ consentVersion: 1 });
|
|
|
323
325
|
|
|
324
326
|
Re-export of the core `initAnalytics`, for apps that only import from `./react`.
|
|
325
327
|
|
|
326
|
-
### Consent
|
|
328
|
+
### Consent versions
|
|
327
329
|
|
|
328
|
-
`
|
|
329
|
-
`
|
|
330
|
-
consent is older than the requested version on every mount, so it is cleared and the user is
|
|
331
|
-
asked again each visit.
|
|
330
|
+
`AnalyticsProvider` and `useConsent` save each choice with their `consentVersion`. Raising
|
|
331
|
+
`consentVersion` asks every visitor once more; the new choice is then kept on later visits.
|
|
332
332
|
|
|
333
333
|
## Types (`@vllnt/analytics`)
|
|
334
334
|
|
package/llms.txt
CHANGED
|
@@ -15,7 +15,8 @@ before writing code.
|
|
|
15
15
|
|
|
16
16
|
- npm: `@vllnt/analytics` (MIT), repo: https://github.com/vllnt/analytics
|
|
17
17
|
- Unreleased builds from `main` use the `canary` dist-tag
|
|
18
|
-
- Ships ESM, CommonJS and type declarations from `dist` (built with tsup)
|
|
18
|
+
- Ships ESM, CommonJS and type declarations from `dist` (built with tsup); both builds of
|
|
19
|
+
`./react` start with `"use client"`
|
|
19
20
|
- Entry points: `@vllnt/analytics` (core + consent + types), `@vllnt/analytics/react` (`AnalyticsProvider`, `useAnalytics`, `useConsent`, `initAnalytics`)
|
|
20
21
|
- Peer dependency: React `^18.0.0 || ^19.0.0`
|
|
21
22
|
- Install: `pnpm add @vllnt/analytics`
|
|
@@ -23,17 +24,16 @@ before writing code.
|
|
|
23
24
|
## Behaviour
|
|
24
25
|
|
|
25
26
|
- Events tracked before `initAnalytics()` are queued and flushed on the first unblocked init;
|
|
26
|
-
a blocked init
|
|
27
|
+
a blocked init drops them.
|
|
27
28
|
- `initAnalytics()` blocks tracking when Do Not Track is on (`"1"` or `"yes"`) or stored consent
|
|
28
29
|
has `analytics: false`. With no stored consent it starts tracking. `AnalyticsProvider` calls it
|
|
29
|
-
|
|
30
|
-
|
|
30
|
+
while consent has `analytics: true` and calls `disableAnalytics()` after a stored decline, so a
|
|
31
|
+
declined visitor's events are dropped. Before the user responds, events are queued in memory.
|
|
31
32
|
- Once blocked, only `enableAnalytics(config)` unblocks; it re-reads stored consent.
|
|
32
33
|
- Consent is stored in `localStorage` (`vllnt:analytics-consent`) and a `vllnt-consent` cookie
|
|
33
34
|
(`1` or `0`) for SSR; `getConsentFromCookie(cookieString)` reads it with an anchored match.
|
|
34
|
-
- Consent is versioned
|
|
35
|
-
|
|
36
|
-
(see `docs/API.md`).
|
|
35
|
+
- Consent is versioned; a stored version below the hook's `consentVersion` (default
|
|
36
|
+
`CONSENT_VERSION`) re-prompts once, and the hooks save the new choice with that version.
|
|
37
37
|
- `track()` ignores empty or non-string names; `identify()` ignores empty or non-string ids.
|
|
38
38
|
- Browser APIs are guarded, so server-side import is safe. `initAnalytics()` has no server
|
|
39
39
|
guard and starts the process-wide instance there, so call it only in the browser.
|
package/package.json
CHANGED
|
@@ -1,9 +1,9 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@vllnt/analytics",
|
|
3
|
-
"version": "0.2.0-canary.
|
|
3
|
+
"version": "0.2.0-canary.9bbc838",
|
|
4
4
|
"description": "Lightweight, privacy-first analytics with consent management and React integration",
|
|
5
5
|
"type": "module",
|
|
6
|
-
"packageManager": "pnpm@
|
|
6
|
+
"packageManager": "pnpm@12.22.0",
|
|
7
7
|
"license": "MIT",
|
|
8
8
|
"repository": {
|
|
9
9
|
"type": "git",
|
|
@@ -70,32 +70,18 @@
|
|
|
70
70
|
"react": "^18.0.0 || ^19.0.0"
|
|
71
71
|
},
|
|
72
72
|
"devDependencies": {
|
|
73
|
-
"@testing-library/jest-dom": "^
|
|
73
|
+
"@testing-library/jest-dom": "^8.0.0",
|
|
74
74
|
"@testing-library/react": "^16.3.3",
|
|
75
75
|
"@types/node": "^24.19.2",
|
|
76
76
|
"@types/react": "^19.3.0",
|
|
77
|
-
"@vitest/coverage-v8": "^5.1.
|
|
77
|
+
"@vitest/coverage-v8": "^5.1.1",
|
|
78
78
|
"@vllnt/eslint-config": "^2.1.0",
|
|
79
|
-
"@vllnt/typescript": "^
|
|
79
|
+
"@vllnt/typescript": "^3.0.0",
|
|
80
80
|
"eslint": "^9.39.5",
|
|
81
|
-
"jsdom": "^
|
|
81
|
+
"jsdom": "^31.0.1",
|
|
82
82
|
"react": "^19.3.1",
|
|
83
83
|
"tsup": "^9.0.0",
|
|
84
84
|
"typescript": "^6.0.3",
|
|
85
|
-
"vitest": "^5.1.
|
|
86
|
-
},
|
|
87
|
-
"pnpm": {
|
|
88
|
-
"minimumReleaseAge": 10080,
|
|
89
|
-
"minimumReleaseAgeExclude": [
|
|
90
|
-
"@vllnt/*"
|
|
91
|
-
],
|
|
92
|
-
"overrides": {
|
|
93
|
-
"minimatch@>=7.0.0 <11.0.1": "11.0.5",
|
|
94
|
-
"deepmerge-ts@<8.0.0": "8.0.0",
|
|
95
|
-
"handlebars@<4.7.10": "4.7.10"
|
|
96
|
-
},
|
|
97
|
-
"ignoredBuiltDependencies": [
|
|
98
|
-
"esbuild"
|
|
99
|
-
]
|
|
85
|
+
"vitest": "^5.1.1"
|
|
100
86
|
}
|
|
101
87
|
}
|
package/src/analytics.ts
CHANGED
|
@@ -38,6 +38,15 @@ export function _resetAnalyticsState(): void {
|
|
|
38
38
|
isBlocked = false;
|
|
39
39
|
}
|
|
40
40
|
|
|
41
|
+
/**
|
|
42
|
+
* Block tracking and drop queued events, so nothing recorded before the
|
|
43
|
+
* decision is sent later.
|
|
44
|
+
*/
|
|
45
|
+
function block(): void {
|
|
46
|
+
isBlocked = true;
|
|
47
|
+
eventQueue = [];
|
|
48
|
+
}
|
|
49
|
+
|
|
41
50
|
/**
|
|
42
51
|
* Log event in debug mode
|
|
43
52
|
*/
|
|
@@ -64,7 +73,7 @@ export function initAnalytics(config: AnalyticsConfig): void {
|
|
|
64
73
|
|
|
65
74
|
// Check Do Not Track
|
|
66
75
|
if (isDoNotTrackEnabled()) {
|
|
67
|
-
|
|
76
|
+
block();
|
|
68
77
|
debugLog("init", "Do Not Track enabled, analytics blocked");
|
|
69
78
|
return;
|
|
70
79
|
}
|
|
@@ -72,7 +81,7 @@ export function initAnalytics(config: AnalyticsConfig): void {
|
|
|
72
81
|
// Check consent
|
|
73
82
|
const consent = loadConsent();
|
|
74
83
|
if (consent && !consent.analytics) {
|
|
75
|
-
|
|
84
|
+
block();
|
|
76
85
|
debugLog("init", "Analytics consent declined");
|
|
77
86
|
return;
|
|
78
87
|
}
|
|
@@ -207,8 +216,7 @@ export function enableAnalytics(config: AnalyticsConfig): void {
|
|
|
207
216
|
* Callers should trigger a page reload or call resetAnalytics() to purge PII.
|
|
208
217
|
*/
|
|
209
218
|
export function disableAnalytics(): void {
|
|
210
|
-
|
|
211
|
-
eventQueue.length = 0; // Clear queue
|
|
219
|
+
block();
|
|
212
220
|
}
|
|
213
221
|
|
|
214
222
|
/**
|
package/src/react.tsx
CHANGED
|
@@ -67,28 +67,35 @@ export function AnalyticsProvider({
|
|
|
67
67
|
|
|
68
68
|
const [hasResponded, setHasResponded] = useState(() => consent !== null);
|
|
69
69
|
|
|
70
|
-
//
|
|
70
|
+
// Start analytics when consent allows it; block it (and drop queued
|
|
71
|
+
// events) when the stored choice is a decline.
|
|
71
72
|
useEffect(() => {
|
|
72
|
-
if (consent
|
|
73
|
+
if (!consent) return;
|
|
74
|
+
if (consent.analytics) {
|
|
73
75
|
initAnalytics(config);
|
|
76
|
+
} else {
|
|
77
|
+
disableAnalytics();
|
|
74
78
|
}
|
|
75
79
|
}, [consent, config]);
|
|
76
80
|
|
|
77
81
|
const acceptAll = useCallback(() => {
|
|
78
|
-
const newConsent = createAcceptAllConsent();
|
|
82
|
+
const newConsent = { ...createAcceptAllConsent(), version: consentVersion };
|
|
79
83
|
saveConsent(newConsent);
|
|
80
84
|
setConsent(newConsent);
|
|
81
85
|
setHasResponded(true);
|
|
82
86
|
enableAnalytics(config);
|
|
83
|
-
}, [config]);
|
|
87
|
+
}, [config, consentVersion]);
|
|
84
88
|
|
|
85
89
|
const declineAnalytics = useCallback(() => {
|
|
86
|
-
const newConsent =
|
|
90
|
+
const newConsent = {
|
|
91
|
+
...createDeclineAnalyticsConsent(),
|
|
92
|
+
version: consentVersion,
|
|
93
|
+
};
|
|
87
94
|
saveConsent(newConsent);
|
|
88
95
|
setConsent(newConsent);
|
|
89
96
|
setHasResponded(true);
|
|
90
97
|
disableAnalytics();
|
|
91
|
-
}, []);
|
|
98
|
+
}, [consentVersion]);
|
|
92
99
|
|
|
93
100
|
const updateConsentHandler = useCallback(
|
|
94
101
|
(category: ConsentCategory, value: boolean) => {
|
|
@@ -176,18 +183,21 @@ export function useConsent(options: UseConsentOptions = {}): UseConsentReturn {
|
|
|
176
183
|
const [hasResponded, setHasResponded] = useState(() => consent !== null);
|
|
177
184
|
|
|
178
185
|
const acceptAll = useCallback(() => {
|
|
179
|
-
const newConsent = createAcceptAllConsent();
|
|
186
|
+
const newConsent = { ...createAcceptAllConsent(), version: consentVersion };
|
|
180
187
|
saveConsent(newConsent);
|
|
181
188
|
setConsent(newConsent);
|
|
182
189
|
setHasResponded(true);
|
|
183
|
-
}, []);
|
|
190
|
+
}, [consentVersion]);
|
|
184
191
|
|
|
185
192
|
const declineAnalytics = useCallback(() => {
|
|
186
|
-
const newConsent =
|
|
193
|
+
const newConsent = {
|
|
194
|
+
...createDeclineAnalyticsConsent(),
|
|
195
|
+
version: consentVersion,
|
|
196
|
+
};
|
|
187
197
|
saveConsent(newConsent);
|
|
188
198
|
setConsent(newConsent);
|
|
189
199
|
setHasResponded(true);
|
|
190
|
-
}, []);
|
|
200
|
+
}, [consentVersion]);
|
|
191
201
|
|
|
192
202
|
const updateConsentHandler = useCallback(
|
|
193
203
|
(category: ConsentCategory, value: boolean) => {
|