@vllnt/analytics 0.2.0-canary.8064ae0 → 0.2.0-canary.8bb6613
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/README.md +13 -47
- package/package.json +3 -11
- package/CHANGELOG.md +0 -48
- package/docs/API.md +0 -384
- package/llms.txt +0 -56
- package/src/analytics.ts +0 -282
- package/src/consent.ts +0 -179
- package/src/index.ts +0 -48
- package/src/react.tsx +0 -221
- package/src/types.ts +0 -102
package/src/analytics.ts
DELETED
|
@@ -1,282 +0,0 @@
|
|
|
1
|
-
// Lightweight custom analytics implementation
|
|
2
|
-
// Replaces external 'analytics' package with minimal implementation
|
|
3
|
-
|
|
4
|
-
import { isDoNotTrackEnabled, loadConsent } from "./consent";
|
|
5
|
-
import type {
|
|
6
|
-
AnalyticsConfig,
|
|
7
|
-
AnalyticsEvent,
|
|
8
|
-
TutorialEventName,
|
|
9
|
-
TutorialEventProperties,
|
|
10
|
-
} from "./types";
|
|
11
|
-
|
|
12
|
-
// Analytics state (singleton)
|
|
13
|
-
type AnalyticsState = {
|
|
14
|
-
app: string;
|
|
15
|
-
debug?: boolean;
|
|
16
|
-
traits?: Record<string, unknown>;
|
|
17
|
-
userId?: string;
|
|
18
|
-
version?: number;
|
|
19
|
-
};
|
|
20
|
-
|
|
21
|
-
let analyticsState: AnalyticsState | undefined;
|
|
22
|
-
|
|
23
|
-
// Queue for events before initialization
|
|
24
|
-
let eventQueue: AnalyticsEvent[] = [];
|
|
25
|
-
|
|
26
|
-
// Track initialization state
|
|
27
|
-
let isInitialized = false;
|
|
28
|
-
let isBlocked = false;
|
|
29
|
-
|
|
30
|
-
/**
|
|
31
|
-
* Reset analytics state (for testing only)
|
|
32
|
-
* @internal
|
|
33
|
-
*/
|
|
34
|
-
export function _resetAnalyticsState(): void {
|
|
35
|
-
analyticsState = undefined;
|
|
36
|
-
eventQueue = [];
|
|
37
|
-
isInitialized = false;
|
|
38
|
-
isBlocked = false;
|
|
39
|
-
}
|
|
40
|
-
|
|
41
|
-
/**
|
|
42
|
-
* Log event in debug mode
|
|
43
|
-
*/
|
|
44
|
-
function debugLog(type: string, data: unknown): void {
|
|
45
|
-
if (analyticsState?.debug) {
|
|
46
|
-
// eslint-disable-next-line no-console -- Debug logging is intentional
|
|
47
|
-
console.log(`[analytics:${type}]`, data);
|
|
48
|
-
}
|
|
49
|
-
}
|
|
50
|
-
|
|
51
|
-
/**
|
|
52
|
-
* Central tracking point. The library has no transport: events are only
|
|
53
|
-
* logged in debug mode and are not sent anywhere.
|
|
54
|
-
*/
|
|
55
|
-
function sendEvent(name: string, properties?: Record<string, unknown>): void {
|
|
56
|
-
debugLog("track", { name, properties });
|
|
57
|
-
}
|
|
58
|
-
|
|
59
|
-
/**
|
|
60
|
-
* Initialize analytics with configuration
|
|
61
|
-
*/
|
|
62
|
-
export function initAnalytics(config: AnalyticsConfig): void {
|
|
63
|
-
if (analyticsState) return;
|
|
64
|
-
|
|
65
|
-
// Check Do Not Track
|
|
66
|
-
if (isDoNotTrackEnabled()) {
|
|
67
|
-
isBlocked = true;
|
|
68
|
-
debugLog("init", "Do Not Track enabled, analytics blocked");
|
|
69
|
-
return;
|
|
70
|
-
}
|
|
71
|
-
|
|
72
|
-
// Check consent
|
|
73
|
-
const consent = loadConsent();
|
|
74
|
-
if (consent && !consent.analytics) {
|
|
75
|
-
isBlocked = true;
|
|
76
|
-
debugLog("init", "Analytics consent declined");
|
|
77
|
-
return;
|
|
78
|
-
}
|
|
79
|
-
|
|
80
|
-
analyticsState = {
|
|
81
|
-
app: config.app,
|
|
82
|
-
debug: config.debug,
|
|
83
|
-
version: config.version,
|
|
84
|
-
};
|
|
85
|
-
|
|
86
|
-
isInitialized = true;
|
|
87
|
-
|
|
88
|
-
debugLog("init", { app: config.app, version: config.version });
|
|
89
|
-
|
|
90
|
-
// Process queued events
|
|
91
|
-
processQueue();
|
|
92
|
-
}
|
|
93
|
-
|
|
94
|
-
/**
|
|
95
|
-
* Process queued events after initialization
|
|
96
|
-
*/
|
|
97
|
-
function processQueue(): void {
|
|
98
|
-
if (!analyticsState || isBlocked) return;
|
|
99
|
-
|
|
100
|
-
// Process and clear the queue
|
|
101
|
-
const queue = [...eventQueue];
|
|
102
|
-
eventQueue = [];
|
|
103
|
-
|
|
104
|
-
for (const event of queue) {
|
|
105
|
-
if (event.name === "__page__") {
|
|
106
|
-
debugLog("page", event.properties);
|
|
107
|
-
} else {
|
|
108
|
-
sendEvent(event.name, event.properties);
|
|
109
|
-
}
|
|
110
|
-
}
|
|
111
|
-
}
|
|
112
|
-
|
|
113
|
-
/**
|
|
114
|
-
* Track a custom event. Events with an empty or non-string name are ignored.
|
|
115
|
-
*/
|
|
116
|
-
export function track(
|
|
117
|
-
name: string,
|
|
118
|
-
properties?: Record<string, unknown>,
|
|
119
|
-
): void {
|
|
120
|
-
if (typeof window === "undefined") return;
|
|
121
|
-
if (typeof name !== "string" || name === "") return;
|
|
122
|
-
|
|
123
|
-
if (isBlocked) return;
|
|
124
|
-
|
|
125
|
-
const event: AnalyticsEvent = {
|
|
126
|
-
name,
|
|
127
|
-
properties,
|
|
128
|
-
timestamp: new Date().toISOString(),
|
|
129
|
-
};
|
|
130
|
-
|
|
131
|
-
if (!isInitialized) {
|
|
132
|
-
eventQueue.push(event);
|
|
133
|
-
return;
|
|
134
|
-
}
|
|
135
|
-
|
|
136
|
-
sendEvent(name, properties);
|
|
137
|
-
}
|
|
138
|
-
|
|
139
|
-
/**
|
|
140
|
-
* Track a page view
|
|
141
|
-
*/
|
|
142
|
-
export function trackPage(properties?: Record<string, unknown>): void {
|
|
143
|
-
if (typeof window === "undefined") return;
|
|
144
|
-
if (isBlocked) return;
|
|
145
|
-
|
|
146
|
-
if (!isInitialized) {
|
|
147
|
-
eventQueue.push({
|
|
148
|
-
name: "__page__",
|
|
149
|
-
properties,
|
|
150
|
-
timestamp: new Date().toISOString(),
|
|
151
|
-
});
|
|
152
|
-
return;
|
|
153
|
-
}
|
|
154
|
-
|
|
155
|
-
debugLog("page", properties);
|
|
156
|
-
}
|
|
157
|
-
|
|
158
|
-
/**
|
|
159
|
-
* Identify a user. An empty or non-string user id is ignored.
|
|
160
|
-
*/
|
|
161
|
-
export function identify(
|
|
162
|
-
userId: string,
|
|
163
|
-
traits?: Record<string, unknown>,
|
|
164
|
-
): void {
|
|
165
|
-
if (typeof window === "undefined") return;
|
|
166
|
-
if (typeof userId !== "string" || userId === "") return;
|
|
167
|
-
if (isBlocked) return;
|
|
168
|
-
|
|
169
|
-
if (!isInitialized || !analyticsState) {
|
|
170
|
-
return;
|
|
171
|
-
}
|
|
172
|
-
|
|
173
|
-
analyticsState.userId = userId;
|
|
174
|
-
analyticsState.traits = traits;
|
|
175
|
-
|
|
176
|
-
debugLog("identify", { traits, userId });
|
|
177
|
-
}
|
|
178
|
-
|
|
179
|
-
/**
|
|
180
|
-
* Reset analytics state (on logout)
|
|
181
|
-
*/
|
|
182
|
-
export function resetAnalytics(): void {
|
|
183
|
-
if (typeof window === "undefined") return;
|
|
184
|
-
|
|
185
|
-
if (analyticsState) {
|
|
186
|
-
analyticsState.userId = undefined;
|
|
187
|
-
analyticsState.traits = undefined;
|
|
188
|
-
}
|
|
189
|
-
|
|
190
|
-
debugLog("reset", undefined);
|
|
191
|
-
}
|
|
192
|
-
|
|
193
|
-
/**
|
|
194
|
-
* Enable analytics after consent is given
|
|
195
|
-
*/
|
|
196
|
-
export function enableAnalytics(config: AnalyticsConfig): void {
|
|
197
|
-
if (isBlocked && !isDoNotTrackEnabled()) {
|
|
198
|
-
isBlocked = false;
|
|
199
|
-
analyticsState = undefined;
|
|
200
|
-
initAnalytics(config);
|
|
201
|
-
}
|
|
202
|
-
}
|
|
203
|
-
|
|
204
|
-
/**
|
|
205
|
-
* Disable analytics after consent is revoked.
|
|
206
|
-
* NOTE: Does not clear in-memory userId/traits (GDPR Art. 17 limitation).
|
|
207
|
-
* Callers should trigger a page reload or call resetAnalytics() to purge PII.
|
|
208
|
-
*/
|
|
209
|
-
export function disableAnalytics(): void {
|
|
210
|
-
isBlocked = true;
|
|
211
|
-
eventQueue.length = 0; // Clear queue
|
|
212
|
-
}
|
|
213
|
-
|
|
214
|
-
/**
|
|
215
|
-
* Check if analytics is currently enabled
|
|
216
|
-
*/
|
|
217
|
-
export function isAnalyticsEnabled(): boolean {
|
|
218
|
-
return isInitialized && !isBlocked;
|
|
219
|
-
}
|
|
220
|
-
|
|
221
|
-
/**
|
|
222
|
-
* Get current analytics state (for advanced usage/debugging)
|
|
223
|
-
*/
|
|
224
|
-
export function getAnalyticsState(): AnalyticsState | undefined {
|
|
225
|
-
return analyticsState;
|
|
226
|
-
}
|
|
227
|
-
|
|
228
|
-
// Tutorial-specific tracking helpers
|
|
229
|
-
|
|
230
|
-
/**
|
|
231
|
-
* Track tutorial start
|
|
232
|
-
*/
|
|
233
|
-
export function trackTutorialStart(
|
|
234
|
-
properties: Omit<TutorialEventProperties, "section_id">,
|
|
235
|
-
): void {
|
|
236
|
-
track("tutorial_start" satisfies TutorialEventName, properties);
|
|
237
|
-
}
|
|
238
|
-
|
|
239
|
-
/**
|
|
240
|
-
* Track section view
|
|
241
|
-
*/
|
|
242
|
-
export function trackSectionView(properties: TutorialEventProperties): void {
|
|
243
|
-
track("tutorial_section_view" satisfies TutorialEventName, properties);
|
|
244
|
-
}
|
|
245
|
-
|
|
246
|
-
/**
|
|
247
|
-
* Track section completion
|
|
248
|
-
*/
|
|
249
|
-
export function trackSectionComplete(
|
|
250
|
-
properties: TutorialEventProperties,
|
|
251
|
-
): void {
|
|
252
|
-
track("tutorial_section_complete" satisfies TutorialEventName, properties);
|
|
253
|
-
}
|
|
254
|
-
|
|
255
|
-
/**
|
|
256
|
-
* Track tutorial completion
|
|
257
|
-
*/
|
|
258
|
-
export function trackTutorialComplete(
|
|
259
|
-
properties: Omit<TutorialEventProperties, "section_id">,
|
|
260
|
-
): void {
|
|
261
|
-
track("tutorial_complete" satisfies TutorialEventName, properties);
|
|
262
|
-
}
|
|
263
|
-
|
|
264
|
-
/**
|
|
265
|
-
* Track navigation between sections
|
|
266
|
-
*/
|
|
267
|
-
export function trackTutorialNavigation(
|
|
268
|
-
properties: TutorialEventProperties & {
|
|
269
|
-
navigation_type: "keyboard" | "next" | "prev" | "toc";
|
|
270
|
-
},
|
|
271
|
-
): void {
|
|
272
|
-
track("tutorial_navigation" satisfies TutorialEventName, properties);
|
|
273
|
-
}
|
|
274
|
-
|
|
275
|
-
/**
|
|
276
|
-
* Track time spent on a section
|
|
277
|
-
*/
|
|
278
|
-
export function trackTimeSpent(
|
|
279
|
-
properties: TutorialEventProperties & { time_spent_seconds: number },
|
|
280
|
-
): void {
|
|
281
|
-
track("tutorial_time_spent" satisfies TutorialEventName, properties);
|
|
282
|
-
}
|
package/src/consent.ts
DELETED
|
@@ -1,179 +0,0 @@
|
|
|
1
|
-
import type { ConsentCategory, ConsentState } from "./types";
|
|
2
|
-
|
|
3
|
-
/** Current consent schema version - bump to re-prompt users */
|
|
4
|
-
export const CONSENT_VERSION = 1;
|
|
5
|
-
|
|
6
|
-
/** localStorage key for consent state */
|
|
7
|
-
export const CONSENT_STORAGE_KEY = "vllnt:analytics-consent";
|
|
8
|
-
|
|
9
|
-
/** Cookie name for SSR consent detection */
|
|
10
|
-
export const CONSENT_COOKIE_NAME = "vllnt-consent";
|
|
11
|
-
|
|
12
|
-
/**
|
|
13
|
-
* Type guard to validate consent state from storage
|
|
14
|
-
*/
|
|
15
|
-
export function isValidConsentState(value: unknown): value is ConsentState {
|
|
16
|
-
if (typeof value !== "object" || value === null) return false;
|
|
17
|
-
|
|
18
|
-
const state = value as Record<string, unknown>;
|
|
19
|
-
|
|
20
|
-
return (
|
|
21
|
-
typeof state.analytics === "boolean" &&
|
|
22
|
-
typeof state.functional === "boolean" &&
|
|
23
|
-
typeof state.timestamp === "string" &&
|
|
24
|
-
typeof state.version === "number"
|
|
25
|
-
);
|
|
26
|
-
}
|
|
27
|
-
|
|
28
|
-
/**
|
|
29
|
-
* Check if localStorage is available
|
|
30
|
-
*/
|
|
31
|
-
export function isStorageAvailable(): boolean {
|
|
32
|
-
if (typeof window === "undefined") return false;
|
|
33
|
-
|
|
34
|
-
try {
|
|
35
|
-
const testKey = "__storage_test__";
|
|
36
|
-
localStorage.setItem(testKey, testKey);
|
|
37
|
-
localStorage.removeItem(testKey);
|
|
38
|
-
return true;
|
|
39
|
-
} catch {
|
|
40
|
-
return false;
|
|
41
|
-
}
|
|
42
|
-
}
|
|
43
|
-
|
|
44
|
-
/**
|
|
45
|
-
* Load consent state from localStorage
|
|
46
|
-
*/
|
|
47
|
-
export function loadConsent(): ConsentState | null {
|
|
48
|
-
if (!isStorageAvailable()) return null;
|
|
49
|
-
|
|
50
|
-
try {
|
|
51
|
-
const raw = localStorage.getItem(CONSENT_STORAGE_KEY);
|
|
52
|
-
if (!raw) return null;
|
|
53
|
-
|
|
54
|
-
const parsed: unknown = JSON.parse(raw);
|
|
55
|
-
if (!isValidConsentState(parsed)) return null;
|
|
56
|
-
|
|
57
|
-
return parsed;
|
|
58
|
-
} catch {
|
|
59
|
-
return null;
|
|
60
|
-
}
|
|
61
|
-
}
|
|
62
|
-
|
|
63
|
-
/**
|
|
64
|
-
* Save consent state to localStorage and cookie
|
|
65
|
-
*/
|
|
66
|
-
export function saveConsent(state: ConsentState): void {
|
|
67
|
-
if (!isStorageAvailable()) return;
|
|
68
|
-
|
|
69
|
-
try {
|
|
70
|
-
localStorage.setItem(CONSENT_STORAGE_KEY, JSON.stringify(state));
|
|
71
|
-
|
|
72
|
-
// Also set a cookie for SSR detection (1 year expiry)
|
|
73
|
-
const expires = new Date();
|
|
74
|
-
expires.setFullYear(expires.getFullYear() + 1);
|
|
75
|
-
|
|
76
|
-
// Cookie value: "1" for analytics allowed, "0" for declined
|
|
77
|
-
const cookieValue = state.analytics ? "1" : "0";
|
|
78
|
-
document.cookie = `${CONSENT_COOKIE_NAME}=${cookieValue}; expires=${expires.toUTCString()}; path=/; SameSite=Lax`;
|
|
79
|
-
} catch (error) {
|
|
80
|
-
console.warn("Failed to save consent:", error);
|
|
81
|
-
}
|
|
82
|
-
}
|
|
83
|
-
|
|
84
|
-
/**
|
|
85
|
-
* Clear consent state (for re-prompting)
|
|
86
|
-
*/
|
|
87
|
-
export function clearConsent(): void {
|
|
88
|
-
if (!isStorageAvailable()) return;
|
|
89
|
-
|
|
90
|
-
try {
|
|
91
|
-
localStorage.removeItem(CONSENT_STORAGE_KEY);
|
|
92
|
-
|
|
93
|
-
// Clear cookie
|
|
94
|
-
document.cookie = `${CONSENT_COOKIE_NAME}=; expires=Thu, 01 Jan 1970 00:00:00 UTC; path=/; SameSite=Lax`;
|
|
95
|
-
} catch {
|
|
96
|
-
// Ignore errors during cleanup
|
|
97
|
-
}
|
|
98
|
-
}
|
|
99
|
-
|
|
100
|
-
/**
|
|
101
|
-
* Create a new consent state with all categories accepted
|
|
102
|
-
*/
|
|
103
|
-
export function createAcceptAllConsent(): ConsentState {
|
|
104
|
-
return {
|
|
105
|
-
analytics: true,
|
|
106
|
-
functional: true,
|
|
107
|
-
timestamp: new Date().toISOString(),
|
|
108
|
-
version: CONSENT_VERSION,
|
|
109
|
-
};
|
|
110
|
-
}
|
|
111
|
-
|
|
112
|
-
/**
|
|
113
|
-
* Create a consent state with analytics declined
|
|
114
|
-
*/
|
|
115
|
-
export function createDeclineAnalyticsConsent(): ConsentState {
|
|
116
|
-
return {
|
|
117
|
-
analytics: false,
|
|
118
|
-
functional: true, // Functional always enabled
|
|
119
|
-
timestamp: new Date().toISOString(),
|
|
120
|
-
version: CONSENT_VERSION,
|
|
121
|
-
};
|
|
122
|
-
}
|
|
123
|
-
|
|
124
|
-
/**
|
|
125
|
-
* Update a specific consent category
|
|
126
|
-
*/
|
|
127
|
-
export function updateConsentCategory(
|
|
128
|
-
current: ConsentState,
|
|
129
|
-
category: ConsentCategory,
|
|
130
|
-
value: boolean,
|
|
131
|
-
): ConsentState {
|
|
132
|
-
// Functional cookies cannot be disabled
|
|
133
|
-
if (category === "functional" && !value) {
|
|
134
|
-
return current;
|
|
135
|
-
}
|
|
136
|
-
|
|
137
|
-
return {
|
|
138
|
-
...current,
|
|
139
|
-
[category]: value,
|
|
140
|
-
timestamp: new Date().toISOString(),
|
|
141
|
-
};
|
|
142
|
-
}
|
|
143
|
-
|
|
144
|
-
/**
|
|
145
|
-
* Check if consent needs to be re-prompted (version mismatch)
|
|
146
|
-
*/
|
|
147
|
-
export function needsRePrompt(
|
|
148
|
-
consent: ConsentState,
|
|
149
|
-
currentVersion: number,
|
|
150
|
-
): boolean {
|
|
151
|
-
return consent.version < currentVersion;
|
|
152
|
-
}
|
|
153
|
-
|
|
154
|
-
/**
|
|
155
|
-
* Check Do Not Track browser setting
|
|
156
|
-
*/
|
|
157
|
-
export function isDoNotTrackEnabled(): boolean {
|
|
158
|
-
if (typeof window === "undefined") return false;
|
|
159
|
-
|
|
160
|
-
const dnt =
|
|
161
|
-
navigator.doNotTrack ||
|
|
162
|
-
(window as unknown as { doNotTrack?: string }).doNotTrack ||
|
|
163
|
-
(navigator as unknown as { msDoNotTrack?: string }).msDoNotTrack;
|
|
164
|
-
|
|
165
|
-
return dnt === "1" || dnt === "yes";
|
|
166
|
-
}
|
|
167
|
-
|
|
168
|
-
/**
|
|
169
|
-
* Get consent from cookie (for SSR)
|
|
170
|
-
*/
|
|
171
|
-
export function getConsentFromCookie(cookieString: string): boolean | null {
|
|
172
|
-
// Anchor to the start of the string or a cookie separator so that
|
|
173
|
-
// look-alike names such as "evil-vllnt-consent" are not matched.
|
|
174
|
-
const match = new RegExp(`(?:^|;\\s*)${CONSENT_COOKIE_NAME}=([^;]+)`).exec(
|
|
175
|
-
cookieString,
|
|
176
|
-
);
|
|
177
|
-
if (!match) return null;
|
|
178
|
-
return match[1] === "1";
|
|
179
|
-
}
|
package/src/index.ts
DELETED
|
@@ -1,48 +0,0 @@
|
|
|
1
|
-
// Core analytics
|
|
2
|
-
export {
|
|
3
|
-
disableAnalytics,
|
|
4
|
-
enableAnalytics,
|
|
5
|
-
getAnalyticsState,
|
|
6
|
-
identify,
|
|
7
|
-
initAnalytics,
|
|
8
|
-
isAnalyticsEnabled,
|
|
9
|
-
resetAnalytics,
|
|
10
|
-
track,
|
|
11
|
-
trackPage,
|
|
12
|
-
trackSectionComplete,
|
|
13
|
-
trackSectionView,
|
|
14
|
-
trackTimeSpent,
|
|
15
|
-
trackTutorialComplete,
|
|
16
|
-
trackTutorialNavigation,
|
|
17
|
-
trackTutorialStart,
|
|
18
|
-
} from "./analytics";
|
|
19
|
-
|
|
20
|
-
// Consent management
|
|
21
|
-
export {
|
|
22
|
-
clearConsent,
|
|
23
|
-
CONSENT_COOKIE_NAME,
|
|
24
|
-
CONSENT_STORAGE_KEY,
|
|
25
|
-
CONSENT_VERSION,
|
|
26
|
-
createAcceptAllConsent,
|
|
27
|
-
createDeclineAnalyticsConsent,
|
|
28
|
-
getConsentFromCookie,
|
|
29
|
-
isDoNotTrackEnabled,
|
|
30
|
-
isStorageAvailable,
|
|
31
|
-
isValidConsentState,
|
|
32
|
-
loadConsent,
|
|
33
|
-
needsRePrompt,
|
|
34
|
-
saveConsent,
|
|
35
|
-
updateConsentCategory,
|
|
36
|
-
} from "./consent";
|
|
37
|
-
|
|
38
|
-
// Types
|
|
39
|
-
export type {
|
|
40
|
-
AnalyticsConfig,
|
|
41
|
-
AnalyticsEvent,
|
|
42
|
-
ConsentCategory,
|
|
43
|
-
ConsentState,
|
|
44
|
-
TutorialEventName,
|
|
45
|
-
TutorialEventProperties,
|
|
46
|
-
UseConsentOptions,
|
|
47
|
-
UseConsentReturn,
|
|
48
|
-
} from "./types";
|
package/src/react.tsx
DELETED
|
@@ -1,221 +0,0 @@
|
|
|
1
|
-
"use client";
|
|
2
|
-
|
|
3
|
-
import {
|
|
4
|
-
createContext,
|
|
5
|
-
type ReactNode,
|
|
6
|
-
useCallback,
|
|
7
|
-
useContext,
|
|
8
|
-
useEffect,
|
|
9
|
-
useMemo,
|
|
10
|
-
useState,
|
|
11
|
-
} from "react";
|
|
12
|
-
|
|
13
|
-
import { disableAnalytics, enableAnalytics, initAnalytics } from "./analytics";
|
|
14
|
-
import {
|
|
15
|
-
clearConsent,
|
|
16
|
-
CONSENT_VERSION,
|
|
17
|
-
createAcceptAllConsent,
|
|
18
|
-
createDeclineAnalyticsConsent,
|
|
19
|
-
loadConsent,
|
|
20
|
-
needsRePrompt,
|
|
21
|
-
saveConsent,
|
|
22
|
-
updateConsentCategory,
|
|
23
|
-
} from "./consent";
|
|
24
|
-
import type {
|
|
25
|
-
AnalyticsConfig,
|
|
26
|
-
ConsentCategory,
|
|
27
|
-
ConsentState,
|
|
28
|
-
UseConsentOptions,
|
|
29
|
-
UseConsentReturn,
|
|
30
|
-
} from "./types";
|
|
31
|
-
|
|
32
|
-
// Context for analytics configuration
|
|
33
|
-
type AnalyticsContextValue = {
|
|
34
|
-
acceptAll: () => void;
|
|
35
|
-
config: AnalyticsConfig;
|
|
36
|
-
consent: ConsentState | null;
|
|
37
|
-
declineAnalytics: () => void;
|
|
38
|
-
hasResponded: boolean;
|
|
39
|
-
isLoading: boolean;
|
|
40
|
-
resetConsent: () => void;
|
|
41
|
-
updateConsent: (category: ConsentCategory, value: boolean) => void;
|
|
42
|
-
};
|
|
43
|
-
|
|
44
|
-
const AnalyticsContext = createContext<AnalyticsContextValue | null>(null);
|
|
45
|
-
|
|
46
|
-
/**
|
|
47
|
-
* Analytics provider component
|
|
48
|
-
*/
|
|
49
|
-
export function AnalyticsProvider({
|
|
50
|
-
children,
|
|
51
|
-
config,
|
|
52
|
-
consentVersion = CONSENT_VERSION,
|
|
53
|
-
}: {
|
|
54
|
-
children: ReactNode;
|
|
55
|
-
config: AnalyticsConfig;
|
|
56
|
-
consentVersion?: number;
|
|
57
|
-
}) {
|
|
58
|
-
const [consent, setConsent] = useState<ConsentState | null>(() => {
|
|
59
|
-
const stored = loadConsent();
|
|
60
|
-
if (!stored) return null;
|
|
61
|
-
if (needsRePrompt(stored, consentVersion)) {
|
|
62
|
-
clearConsent();
|
|
63
|
-
return null;
|
|
64
|
-
}
|
|
65
|
-
return stored;
|
|
66
|
-
});
|
|
67
|
-
|
|
68
|
-
const [hasResponded, setHasResponded] = useState(() => consent !== null);
|
|
69
|
-
|
|
70
|
-
// Initialize analytics if stored consent allows
|
|
71
|
-
useEffect(() => {
|
|
72
|
-
if (consent?.analytics) {
|
|
73
|
-
initAnalytics(config);
|
|
74
|
-
}
|
|
75
|
-
}, [consent, config]);
|
|
76
|
-
|
|
77
|
-
const acceptAll = useCallback(() => {
|
|
78
|
-
const newConsent = createAcceptAllConsent();
|
|
79
|
-
saveConsent(newConsent);
|
|
80
|
-
setConsent(newConsent);
|
|
81
|
-
setHasResponded(true);
|
|
82
|
-
enableAnalytics(config);
|
|
83
|
-
}, [config]);
|
|
84
|
-
|
|
85
|
-
const declineAnalytics = useCallback(() => {
|
|
86
|
-
const newConsent = createDeclineAnalyticsConsent();
|
|
87
|
-
saveConsent(newConsent);
|
|
88
|
-
setConsent(newConsent);
|
|
89
|
-
setHasResponded(true);
|
|
90
|
-
disableAnalytics();
|
|
91
|
-
}, []);
|
|
92
|
-
|
|
93
|
-
const updateConsentHandler = useCallback(
|
|
94
|
-
(category: ConsentCategory, value: boolean) => {
|
|
95
|
-
if (!consent) return;
|
|
96
|
-
|
|
97
|
-
const newConsent = updateConsentCategory(consent, category, value);
|
|
98
|
-
saveConsent(newConsent);
|
|
99
|
-
setConsent(newConsent);
|
|
100
|
-
|
|
101
|
-
// Update analytics state
|
|
102
|
-
if (category === "analytics") {
|
|
103
|
-
if (value) {
|
|
104
|
-
enableAnalytics(config);
|
|
105
|
-
} else {
|
|
106
|
-
disableAnalytics();
|
|
107
|
-
}
|
|
108
|
-
}
|
|
109
|
-
},
|
|
110
|
-
[consent, config],
|
|
111
|
-
);
|
|
112
|
-
|
|
113
|
-
const resetConsentHandler = useCallback(() => {
|
|
114
|
-
clearConsent();
|
|
115
|
-
setConsent(null);
|
|
116
|
-
setHasResponded(false);
|
|
117
|
-
disableAnalytics();
|
|
118
|
-
}, []);
|
|
119
|
-
|
|
120
|
-
const value = useMemo<AnalyticsContextValue>(
|
|
121
|
-
() => ({
|
|
122
|
-
acceptAll,
|
|
123
|
-
config,
|
|
124
|
-
consent,
|
|
125
|
-
declineAnalytics,
|
|
126
|
-
hasResponded,
|
|
127
|
-
isLoading: false,
|
|
128
|
-
resetConsent: resetConsentHandler,
|
|
129
|
-
updateConsent: updateConsentHandler,
|
|
130
|
-
}),
|
|
131
|
-
[
|
|
132
|
-
config,
|
|
133
|
-
consent,
|
|
134
|
-
hasResponded,
|
|
135
|
-
acceptAll,
|
|
136
|
-
declineAnalytics,
|
|
137
|
-
updateConsentHandler,
|
|
138
|
-
resetConsentHandler,
|
|
139
|
-
],
|
|
140
|
-
);
|
|
141
|
-
|
|
142
|
-
return (
|
|
143
|
-
<AnalyticsContext.Provider value={value}>
|
|
144
|
-
{children}
|
|
145
|
-
</AnalyticsContext.Provider>
|
|
146
|
-
);
|
|
147
|
-
}
|
|
148
|
-
|
|
149
|
-
/**
|
|
150
|
-
* Hook to access analytics context
|
|
151
|
-
*/
|
|
152
|
-
export function useAnalytics() {
|
|
153
|
-
const context = useContext(AnalyticsContext);
|
|
154
|
-
if (!context) {
|
|
155
|
-
throw new Error("useAnalytics must be used within an AnalyticsProvider");
|
|
156
|
-
}
|
|
157
|
-
return context;
|
|
158
|
-
}
|
|
159
|
-
|
|
160
|
-
/**
|
|
161
|
-
* Hook to manage consent state
|
|
162
|
-
*/
|
|
163
|
-
export function useConsent(options: UseConsentOptions = {}): UseConsentReturn {
|
|
164
|
-
const { consentVersion = CONSENT_VERSION } = options;
|
|
165
|
-
|
|
166
|
-
const [consent, setConsent] = useState<ConsentState | null>(() => {
|
|
167
|
-
const stored = loadConsent();
|
|
168
|
-
if (!stored) return null;
|
|
169
|
-
if (needsRePrompt(stored, consentVersion)) {
|
|
170
|
-
clearConsent();
|
|
171
|
-
return null;
|
|
172
|
-
}
|
|
173
|
-
return stored;
|
|
174
|
-
});
|
|
175
|
-
|
|
176
|
-
const [hasResponded, setHasResponded] = useState(() => consent !== null);
|
|
177
|
-
|
|
178
|
-
const acceptAll = useCallback(() => {
|
|
179
|
-
const newConsent = createAcceptAllConsent();
|
|
180
|
-
saveConsent(newConsent);
|
|
181
|
-
setConsent(newConsent);
|
|
182
|
-
setHasResponded(true);
|
|
183
|
-
}, []);
|
|
184
|
-
|
|
185
|
-
const declineAnalytics = useCallback(() => {
|
|
186
|
-
const newConsent = createDeclineAnalyticsConsent();
|
|
187
|
-
saveConsent(newConsent);
|
|
188
|
-
setConsent(newConsent);
|
|
189
|
-
setHasResponded(true);
|
|
190
|
-
}, []);
|
|
191
|
-
|
|
192
|
-
const updateConsentHandler = useCallback(
|
|
193
|
-
(category: ConsentCategory, value: boolean) => {
|
|
194
|
-
if (!consent) return;
|
|
195
|
-
|
|
196
|
-
const newConsent = updateConsentCategory(consent, category, value);
|
|
197
|
-
saveConsent(newConsent);
|
|
198
|
-
setConsent(newConsent);
|
|
199
|
-
},
|
|
200
|
-
[consent],
|
|
201
|
-
);
|
|
202
|
-
|
|
203
|
-
const resetConsentHandler = useCallback(() => {
|
|
204
|
-
clearConsent();
|
|
205
|
-
setConsent(null);
|
|
206
|
-
setHasResponded(false);
|
|
207
|
-
}, []);
|
|
208
|
-
|
|
209
|
-
return {
|
|
210
|
-
acceptAll,
|
|
211
|
-
consent,
|
|
212
|
-
declineAnalytics,
|
|
213
|
-
hasResponded,
|
|
214
|
-
isLoading: false,
|
|
215
|
-
resetConsent: resetConsentHandler,
|
|
216
|
-
updateConsent: updateConsentHandler,
|
|
217
|
-
};
|
|
218
|
-
}
|
|
219
|
-
|
|
220
|
-
// Re-export core analytics functions for convenience
|
|
221
|
-
export { initAnalytics } from "./analytics";
|