@vllnt/analytics 0.1.1-canary.c0d1582 → 0.2.0-canary.0cf6be9
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +68 -0
- package/README.md +257 -143
- package/dist/analytics-BGm5JlCM.d.cts +184 -0
- package/dist/analytics-BGm5JlCM.d.ts +184 -0
- package/dist/chunk-JB5OMGDZ.cjs +271 -0
- package/dist/chunk-UVPDJSPZ.js +271 -0
- package/dist/index.cjs +64 -0
- package/dist/index.d.cts +55 -0
- package/dist/index.d.ts +55 -0
- package/dist/index.js +64 -0
- package/dist/react.cjs +177 -0
- package/dist/react.d.cts +33 -0
- package/dist/react.d.ts +33 -0
- package/dist/react.js +177 -0
- package/docs/API.md +517 -0
- package/llms.txt +77 -0
- package/package.json +69 -33
- package/src/analytics.ts +57 -22
- package/src/consent.ts +5 -1
- package/src/index.ts +1 -0
- package/src/react.tsx +29 -11
- package/src/types.ts +8 -0
|
@@ -0,0 +1,184 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Analytics consent state stored in localStorage
|
|
3
|
+
*/
|
|
4
|
+
type ConsentState = {
|
|
5
|
+
/** Whether analytics tracking is allowed */
|
|
6
|
+
analytics: boolean;
|
|
7
|
+
/** Whether functional cookies are allowed (always true for site operation) */
|
|
8
|
+
functional: boolean;
|
|
9
|
+
/** ISO timestamp when consent was given */
|
|
10
|
+
timestamp: string;
|
|
11
|
+
/** Version of consent prompt (bump to re-prompt users) */
|
|
12
|
+
version: number;
|
|
13
|
+
};
|
|
14
|
+
/**
|
|
15
|
+
* Consent categories that can be toggled
|
|
16
|
+
*/
|
|
17
|
+
type ConsentCategory = "analytics" | "functional";
|
|
18
|
+
/**
|
|
19
|
+
* Tutorial-specific event names
|
|
20
|
+
*/
|
|
21
|
+
type TutorialEventName = "tutorial_complete" | "tutorial_navigation" | "tutorial_section_complete" | "tutorial_section_view" | "tutorial_start" | "tutorial_time_spent";
|
|
22
|
+
/**
|
|
23
|
+
* Tutorial event properties
|
|
24
|
+
*/
|
|
25
|
+
type TutorialEventProperties = {
|
|
26
|
+
/** Completion percentage (0-100) */
|
|
27
|
+
completion_percent?: number;
|
|
28
|
+
/** User's locale */
|
|
29
|
+
locale: string;
|
|
30
|
+
/** Navigation method used */
|
|
31
|
+
navigation_type?: "keyboard" | "next" | "prev" | "toc";
|
|
32
|
+
/** Section identifier */
|
|
33
|
+
section_id?: string;
|
|
34
|
+
/** Section position (0-indexed) */
|
|
35
|
+
section_index?: number;
|
|
36
|
+
/** Time spent in seconds */
|
|
37
|
+
time_spent_seconds?: number;
|
|
38
|
+
/** Total number of sections */
|
|
39
|
+
total_sections?: number;
|
|
40
|
+
/** Unique tutorial identifier (contentId) */
|
|
41
|
+
tutorial_id: string;
|
|
42
|
+
/** Human-readable tutorial title */
|
|
43
|
+
tutorial_title: string;
|
|
44
|
+
};
|
|
45
|
+
/**
|
|
46
|
+
* Generic analytics event
|
|
47
|
+
*/
|
|
48
|
+
type AnalyticsEvent = {
|
|
49
|
+
name: string;
|
|
50
|
+
properties?: Record<string, unknown>;
|
|
51
|
+
timestamp?: string;
|
|
52
|
+
};
|
|
53
|
+
/**
|
|
54
|
+
* Analytics instance configuration
|
|
55
|
+
*/
|
|
56
|
+
type AnalyticsConfig = {
|
|
57
|
+
/** Application name for analytics */
|
|
58
|
+
app: string;
|
|
59
|
+
/** Enable debug mode */
|
|
60
|
+
debug?: boolean;
|
|
61
|
+
/**
|
|
62
|
+
* How many `track` and `trackPage` calls are queued before `initAnalytics`
|
|
63
|
+
* runs; later calls are dropped. `0` queues nothing. Defaults to
|
|
64
|
+
* `DEFAULT_MAX_QUEUED_EVENTS` (100); any value that is not a non-negative
|
|
65
|
+
* safe integer means the default. `AnalyticsProvider` applies it on mount;
|
|
66
|
+
* without the provider, the default applies until `initAnalytics` runs.
|
|
67
|
+
*/
|
|
68
|
+
maxQueuedEvents?: number;
|
|
69
|
+
/** Application version */
|
|
70
|
+
version?: number;
|
|
71
|
+
};
|
|
72
|
+
/**
|
|
73
|
+
* Hook options for useConsent
|
|
74
|
+
*/
|
|
75
|
+
type UseConsentOptions = {
|
|
76
|
+
/** Current consent version (bump to re-prompt) */
|
|
77
|
+
consentVersion?: number;
|
|
78
|
+
};
|
|
79
|
+
/**
|
|
80
|
+
* Return type for useConsent hook
|
|
81
|
+
*/
|
|
82
|
+
type UseConsentReturn = {
|
|
83
|
+
/** Accept all tracking */
|
|
84
|
+
acceptAll: () => void;
|
|
85
|
+
/** Current consent state (null if not yet determined) */
|
|
86
|
+
consent: ConsentState | null;
|
|
87
|
+
/** Decline analytics tracking (functional always on) */
|
|
88
|
+
declineAnalytics: () => void;
|
|
89
|
+
/** Whether consent has been given (any response) */
|
|
90
|
+
hasResponded: boolean;
|
|
91
|
+
/** Whether currently loading from storage */
|
|
92
|
+
isLoading: boolean;
|
|
93
|
+
/** Reset consent (will re-show banner) */
|
|
94
|
+
resetConsent: () => void;
|
|
95
|
+
/** Update specific consent category */
|
|
96
|
+
updateConsent: (category: ConsentCategory, value: boolean) => void;
|
|
97
|
+
};
|
|
98
|
+
|
|
99
|
+
type AnalyticsState = {
|
|
100
|
+
app: string;
|
|
101
|
+
debug?: boolean;
|
|
102
|
+
traits?: Record<string, unknown>;
|
|
103
|
+
userId?: string;
|
|
104
|
+
version?: number;
|
|
105
|
+
};
|
|
106
|
+
/**
|
|
107
|
+
* Default `maxQueuedEvents`: how many `track` and `trackPage` calls are kept
|
|
108
|
+
* in memory before `initAnalytics` runs.
|
|
109
|
+
*/
|
|
110
|
+
declare const DEFAULT_MAX_QUEUED_EVENTS = 100;
|
|
111
|
+
/**
|
|
112
|
+
* Start the shared analytics instance and flush queued events. Blocks tracking,
|
|
113
|
+
* and drops the queue, when Do Not Track is on or stored consent declines it.
|
|
114
|
+
* @see https://oss.vllnt.com/packages/analytics
|
|
115
|
+
*/
|
|
116
|
+
declare function initAnalytics(config: AnalyticsConfig): void;
|
|
117
|
+
/**
|
|
118
|
+
* Track a custom event. Events with an empty or non-string name are ignored.
|
|
119
|
+
* Before init, only the first `maxQueuedEvents` calls (shared with
|
|
120
|
+
* `trackPage`) are queued.
|
|
121
|
+
*/
|
|
122
|
+
declare function track(name: string, properties?: Record<string, unknown>): void;
|
|
123
|
+
/**
|
|
124
|
+
* Track a page view. Before init, it shares the `maxQueuedEvents` limit with
|
|
125
|
+
* `track`.
|
|
126
|
+
*/
|
|
127
|
+
declare function trackPage(properties?: Record<string, unknown>): void;
|
|
128
|
+
/**
|
|
129
|
+
* Identify a user. An empty or non-string user id is ignored.
|
|
130
|
+
*/
|
|
131
|
+
declare function identify(userId: string, traits?: Record<string, unknown>): void;
|
|
132
|
+
/**
|
|
133
|
+
* Reset analytics state (on logout)
|
|
134
|
+
*/
|
|
135
|
+
declare function resetAnalytics(): void;
|
|
136
|
+
/**
|
|
137
|
+
* Enable analytics after consent is given
|
|
138
|
+
*/
|
|
139
|
+
declare function enableAnalytics(config: AnalyticsConfig): void;
|
|
140
|
+
/**
|
|
141
|
+
* Disable analytics after consent is revoked.
|
|
142
|
+
* NOTE: Does not clear in-memory userId/traits (GDPR Art. 17 limitation).
|
|
143
|
+
* Callers should trigger a page reload or call resetAnalytics() to purge PII.
|
|
144
|
+
*/
|
|
145
|
+
declare function disableAnalytics(): void;
|
|
146
|
+
/**
|
|
147
|
+
* Check if analytics is currently enabled
|
|
148
|
+
*/
|
|
149
|
+
declare function isAnalyticsEnabled(): boolean;
|
|
150
|
+
/**
|
|
151
|
+
* Get current analytics state (for advanced usage/debugging)
|
|
152
|
+
*/
|
|
153
|
+
declare function getAnalyticsState(): AnalyticsState | undefined;
|
|
154
|
+
/**
|
|
155
|
+
* Track tutorial start
|
|
156
|
+
*/
|
|
157
|
+
declare function trackTutorialStart(properties: Omit<TutorialEventProperties, "section_id">): void;
|
|
158
|
+
/**
|
|
159
|
+
* Track section view
|
|
160
|
+
*/
|
|
161
|
+
declare function trackSectionView(properties: TutorialEventProperties): void;
|
|
162
|
+
/**
|
|
163
|
+
* Track section completion
|
|
164
|
+
*/
|
|
165
|
+
declare function trackSectionComplete(properties: TutorialEventProperties): void;
|
|
166
|
+
/**
|
|
167
|
+
* Track tutorial completion
|
|
168
|
+
*/
|
|
169
|
+
declare function trackTutorialComplete(properties: Omit<TutorialEventProperties, "section_id">): void;
|
|
170
|
+
/**
|
|
171
|
+
* Track navigation between sections
|
|
172
|
+
*/
|
|
173
|
+
declare function trackTutorialNavigation(properties: TutorialEventProperties & {
|
|
174
|
+
navigation_type: "keyboard" | "next" | "prev" | "toc";
|
|
175
|
+
}): void;
|
|
176
|
+
/**
|
|
177
|
+
* Track time spent on a section
|
|
178
|
+
*/
|
|
179
|
+
declare function trackTimeSpent(properties: TutorialEventProperties & {
|
|
180
|
+
time_spent_seconds: number;
|
|
181
|
+
}): void;
|
|
182
|
+
|
|
183
|
+
export { DEFAULT_MAX_QUEUED_EVENTS as D, disableAnalytics as e, enableAnalytics as f, getAnalyticsState as g, initAnalytics as h, identify as i, isAnalyticsEnabled as j, trackPage as k, trackSectionComplete as l, trackSectionView as m, trackTimeSpent as n, trackTutorialComplete as o, trackTutorialNavigation as p, trackTutorialStart as q, resetAnalytics as r, track as t };
|
|
184
|
+
export type { AnalyticsConfig as A, ConsentState as C, TutorialEventName as T, UseConsentOptions as U, ConsentCategory as a, AnalyticsEvent as b, TutorialEventProperties as c, UseConsentReturn as d };
|
|
@@ -0,0 +1,184 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Analytics consent state stored in localStorage
|
|
3
|
+
*/
|
|
4
|
+
type ConsentState = {
|
|
5
|
+
/** Whether analytics tracking is allowed */
|
|
6
|
+
analytics: boolean;
|
|
7
|
+
/** Whether functional cookies are allowed (always true for site operation) */
|
|
8
|
+
functional: boolean;
|
|
9
|
+
/** ISO timestamp when consent was given */
|
|
10
|
+
timestamp: string;
|
|
11
|
+
/** Version of consent prompt (bump to re-prompt users) */
|
|
12
|
+
version: number;
|
|
13
|
+
};
|
|
14
|
+
/**
|
|
15
|
+
* Consent categories that can be toggled
|
|
16
|
+
*/
|
|
17
|
+
type ConsentCategory = "analytics" | "functional";
|
|
18
|
+
/**
|
|
19
|
+
* Tutorial-specific event names
|
|
20
|
+
*/
|
|
21
|
+
type TutorialEventName = "tutorial_complete" | "tutorial_navigation" | "tutorial_section_complete" | "tutorial_section_view" | "tutorial_start" | "tutorial_time_spent";
|
|
22
|
+
/**
|
|
23
|
+
* Tutorial event properties
|
|
24
|
+
*/
|
|
25
|
+
type TutorialEventProperties = {
|
|
26
|
+
/** Completion percentage (0-100) */
|
|
27
|
+
completion_percent?: number;
|
|
28
|
+
/** User's locale */
|
|
29
|
+
locale: string;
|
|
30
|
+
/** Navigation method used */
|
|
31
|
+
navigation_type?: "keyboard" | "next" | "prev" | "toc";
|
|
32
|
+
/** Section identifier */
|
|
33
|
+
section_id?: string;
|
|
34
|
+
/** Section position (0-indexed) */
|
|
35
|
+
section_index?: number;
|
|
36
|
+
/** Time spent in seconds */
|
|
37
|
+
time_spent_seconds?: number;
|
|
38
|
+
/** Total number of sections */
|
|
39
|
+
total_sections?: number;
|
|
40
|
+
/** Unique tutorial identifier (contentId) */
|
|
41
|
+
tutorial_id: string;
|
|
42
|
+
/** Human-readable tutorial title */
|
|
43
|
+
tutorial_title: string;
|
|
44
|
+
};
|
|
45
|
+
/**
|
|
46
|
+
* Generic analytics event
|
|
47
|
+
*/
|
|
48
|
+
type AnalyticsEvent = {
|
|
49
|
+
name: string;
|
|
50
|
+
properties?: Record<string, unknown>;
|
|
51
|
+
timestamp?: string;
|
|
52
|
+
};
|
|
53
|
+
/**
|
|
54
|
+
* Analytics instance configuration
|
|
55
|
+
*/
|
|
56
|
+
type AnalyticsConfig = {
|
|
57
|
+
/** Application name for analytics */
|
|
58
|
+
app: string;
|
|
59
|
+
/** Enable debug mode */
|
|
60
|
+
debug?: boolean;
|
|
61
|
+
/**
|
|
62
|
+
* How many `track` and `trackPage` calls are queued before `initAnalytics`
|
|
63
|
+
* runs; later calls are dropped. `0` queues nothing. Defaults to
|
|
64
|
+
* `DEFAULT_MAX_QUEUED_EVENTS` (100); any value that is not a non-negative
|
|
65
|
+
* safe integer means the default. `AnalyticsProvider` applies it on mount;
|
|
66
|
+
* without the provider, the default applies until `initAnalytics` runs.
|
|
67
|
+
*/
|
|
68
|
+
maxQueuedEvents?: number;
|
|
69
|
+
/** Application version */
|
|
70
|
+
version?: number;
|
|
71
|
+
};
|
|
72
|
+
/**
|
|
73
|
+
* Hook options for useConsent
|
|
74
|
+
*/
|
|
75
|
+
type UseConsentOptions = {
|
|
76
|
+
/** Current consent version (bump to re-prompt) */
|
|
77
|
+
consentVersion?: number;
|
|
78
|
+
};
|
|
79
|
+
/**
|
|
80
|
+
* Return type for useConsent hook
|
|
81
|
+
*/
|
|
82
|
+
type UseConsentReturn = {
|
|
83
|
+
/** Accept all tracking */
|
|
84
|
+
acceptAll: () => void;
|
|
85
|
+
/** Current consent state (null if not yet determined) */
|
|
86
|
+
consent: ConsentState | null;
|
|
87
|
+
/** Decline analytics tracking (functional always on) */
|
|
88
|
+
declineAnalytics: () => void;
|
|
89
|
+
/** Whether consent has been given (any response) */
|
|
90
|
+
hasResponded: boolean;
|
|
91
|
+
/** Whether currently loading from storage */
|
|
92
|
+
isLoading: boolean;
|
|
93
|
+
/** Reset consent (will re-show banner) */
|
|
94
|
+
resetConsent: () => void;
|
|
95
|
+
/** Update specific consent category */
|
|
96
|
+
updateConsent: (category: ConsentCategory, value: boolean) => void;
|
|
97
|
+
};
|
|
98
|
+
|
|
99
|
+
type AnalyticsState = {
|
|
100
|
+
app: string;
|
|
101
|
+
debug?: boolean;
|
|
102
|
+
traits?: Record<string, unknown>;
|
|
103
|
+
userId?: string;
|
|
104
|
+
version?: number;
|
|
105
|
+
};
|
|
106
|
+
/**
|
|
107
|
+
* Default `maxQueuedEvents`: how many `track` and `trackPage` calls are kept
|
|
108
|
+
* in memory before `initAnalytics` runs.
|
|
109
|
+
*/
|
|
110
|
+
declare const DEFAULT_MAX_QUEUED_EVENTS = 100;
|
|
111
|
+
/**
|
|
112
|
+
* Start the shared analytics instance and flush queued events. Blocks tracking,
|
|
113
|
+
* and drops the queue, when Do Not Track is on or stored consent declines it.
|
|
114
|
+
* @see https://oss.vllnt.com/packages/analytics
|
|
115
|
+
*/
|
|
116
|
+
declare function initAnalytics(config: AnalyticsConfig): void;
|
|
117
|
+
/**
|
|
118
|
+
* Track a custom event. Events with an empty or non-string name are ignored.
|
|
119
|
+
* Before init, only the first `maxQueuedEvents` calls (shared with
|
|
120
|
+
* `trackPage`) are queued.
|
|
121
|
+
*/
|
|
122
|
+
declare function track(name: string, properties?: Record<string, unknown>): void;
|
|
123
|
+
/**
|
|
124
|
+
* Track a page view. Before init, it shares the `maxQueuedEvents` limit with
|
|
125
|
+
* `track`.
|
|
126
|
+
*/
|
|
127
|
+
declare function trackPage(properties?: Record<string, unknown>): void;
|
|
128
|
+
/**
|
|
129
|
+
* Identify a user. An empty or non-string user id is ignored.
|
|
130
|
+
*/
|
|
131
|
+
declare function identify(userId: string, traits?: Record<string, unknown>): void;
|
|
132
|
+
/**
|
|
133
|
+
* Reset analytics state (on logout)
|
|
134
|
+
*/
|
|
135
|
+
declare function resetAnalytics(): void;
|
|
136
|
+
/**
|
|
137
|
+
* Enable analytics after consent is given
|
|
138
|
+
*/
|
|
139
|
+
declare function enableAnalytics(config: AnalyticsConfig): void;
|
|
140
|
+
/**
|
|
141
|
+
* Disable analytics after consent is revoked.
|
|
142
|
+
* NOTE: Does not clear in-memory userId/traits (GDPR Art. 17 limitation).
|
|
143
|
+
* Callers should trigger a page reload or call resetAnalytics() to purge PII.
|
|
144
|
+
*/
|
|
145
|
+
declare function disableAnalytics(): void;
|
|
146
|
+
/**
|
|
147
|
+
* Check if analytics is currently enabled
|
|
148
|
+
*/
|
|
149
|
+
declare function isAnalyticsEnabled(): boolean;
|
|
150
|
+
/**
|
|
151
|
+
* Get current analytics state (for advanced usage/debugging)
|
|
152
|
+
*/
|
|
153
|
+
declare function getAnalyticsState(): AnalyticsState | undefined;
|
|
154
|
+
/**
|
|
155
|
+
* Track tutorial start
|
|
156
|
+
*/
|
|
157
|
+
declare function trackTutorialStart(properties: Omit<TutorialEventProperties, "section_id">): void;
|
|
158
|
+
/**
|
|
159
|
+
* Track section view
|
|
160
|
+
*/
|
|
161
|
+
declare function trackSectionView(properties: TutorialEventProperties): void;
|
|
162
|
+
/**
|
|
163
|
+
* Track section completion
|
|
164
|
+
*/
|
|
165
|
+
declare function trackSectionComplete(properties: TutorialEventProperties): void;
|
|
166
|
+
/**
|
|
167
|
+
* Track tutorial completion
|
|
168
|
+
*/
|
|
169
|
+
declare function trackTutorialComplete(properties: Omit<TutorialEventProperties, "section_id">): void;
|
|
170
|
+
/**
|
|
171
|
+
* Track navigation between sections
|
|
172
|
+
*/
|
|
173
|
+
declare function trackTutorialNavigation(properties: TutorialEventProperties & {
|
|
174
|
+
navigation_type: "keyboard" | "next" | "prev" | "toc";
|
|
175
|
+
}): void;
|
|
176
|
+
/**
|
|
177
|
+
* Track time spent on a section
|
|
178
|
+
*/
|
|
179
|
+
declare function trackTimeSpent(properties: TutorialEventProperties & {
|
|
180
|
+
time_spent_seconds: number;
|
|
181
|
+
}): void;
|
|
182
|
+
|
|
183
|
+
export { DEFAULT_MAX_QUEUED_EVENTS as D, disableAnalytics as e, enableAnalytics as f, getAnalyticsState as g, initAnalytics as h, identify as i, isAnalyticsEnabled as j, trackPage as k, trackSectionComplete as l, trackSectionView as m, trackTimeSpent as n, trackTutorialComplete as o, trackTutorialNavigation as p, trackTutorialStart as q, resetAnalytics as r, track as t };
|
|
184
|
+
export type { AnalyticsConfig as A, ConsentState as C, TutorialEventName as T, UseConsentOptions as U, ConsentCategory as a, AnalyticsEvent as b, TutorialEventProperties as c, UseConsentReturn as d };
|
|
@@ -0,0 +1,271 @@
|
|
|
1
|
+
"use strict";Object.defineProperty(exports, "__esModule", {value: true}); function _optionalChain(ops) { let lastAccessLHS = undefined; let value = ops[0]; let i = 1; while (i < ops.length) { const op = ops[i]; const fn = ops[i + 1]; i += 2; if ((op === 'optionalAccess' || op === 'optionalCall') && value == null) { return undefined; } if (op === 'access' || op === 'optionalAccess') { lastAccessLHS = value; value = fn(value); } else if (op === 'call' || op === 'optionalCall') { value = fn((...args) => value.call(lastAccessLHS, ...args)); lastAccessLHS = undefined; } } return value; }// src/consent.ts
|
|
2
|
+
var CONSENT_VERSION = 1;
|
|
3
|
+
var CONSENT_STORAGE_KEY = "vllnt:analytics-consent";
|
|
4
|
+
var CONSENT_COOKIE_NAME = "vllnt-consent";
|
|
5
|
+
function isValidConsentState(value) {
|
|
6
|
+
if (typeof value !== "object" || value === null) return false;
|
|
7
|
+
const state = value;
|
|
8
|
+
return typeof state.analytics === "boolean" && typeof state.functional === "boolean" && typeof state.timestamp === "string" && typeof state.version === "number";
|
|
9
|
+
}
|
|
10
|
+
function isStorageAvailable() {
|
|
11
|
+
if (typeof window === "undefined") return false;
|
|
12
|
+
try {
|
|
13
|
+
const testKey = "__storage_test__";
|
|
14
|
+
localStorage.setItem(testKey, testKey);
|
|
15
|
+
localStorage.removeItem(testKey);
|
|
16
|
+
return true;
|
|
17
|
+
} catch (e) {
|
|
18
|
+
return false;
|
|
19
|
+
}
|
|
20
|
+
}
|
|
21
|
+
function loadConsent() {
|
|
22
|
+
if (!isStorageAvailable()) return null;
|
|
23
|
+
try {
|
|
24
|
+
const raw = localStorage.getItem(CONSENT_STORAGE_KEY);
|
|
25
|
+
if (!raw) return null;
|
|
26
|
+
const parsed = JSON.parse(raw);
|
|
27
|
+
if (!isValidConsentState(parsed)) return null;
|
|
28
|
+
return parsed;
|
|
29
|
+
} catch (e2) {
|
|
30
|
+
return null;
|
|
31
|
+
}
|
|
32
|
+
}
|
|
33
|
+
function saveConsent(state) {
|
|
34
|
+
if (!isStorageAvailable()) return;
|
|
35
|
+
try {
|
|
36
|
+
localStorage.setItem(CONSENT_STORAGE_KEY, JSON.stringify(state));
|
|
37
|
+
const expires = /* @__PURE__ */ new Date();
|
|
38
|
+
expires.setFullYear(expires.getFullYear() + 1);
|
|
39
|
+
const cookieValue = state.analytics ? "1" : "0";
|
|
40
|
+
document.cookie = `${CONSENT_COOKIE_NAME}=${cookieValue}; expires=${expires.toUTCString()}; path=/; SameSite=Lax`;
|
|
41
|
+
} catch (error) {
|
|
42
|
+
console.warn("Failed to save consent:", error);
|
|
43
|
+
}
|
|
44
|
+
}
|
|
45
|
+
function clearConsent() {
|
|
46
|
+
if (!isStorageAvailable()) return;
|
|
47
|
+
try {
|
|
48
|
+
localStorage.removeItem(CONSENT_STORAGE_KEY);
|
|
49
|
+
document.cookie = `${CONSENT_COOKIE_NAME}=; expires=Thu, 01 Jan 1970 00:00:00 UTC; path=/; SameSite=Lax`;
|
|
50
|
+
} catch (e3) {
|
|
51
|
+
}
|
|
52
|
+
}
|
|
53
|
+
function createAcceptAllConsent() {
|
|
54
|
+
return {
|
|
55
|
+
analytics: true,
|
|
56
|
+
functional: true,
|
|
57
|
+
timestamp: (/* @__PURE__ */ new Date()).toISOString(),
|
|
58
|
+
version: CONSENT_VERSION
|
|
59
|
+
};
|
|
60
|
+
}
|
|
61
|
+
function createDeclineAnalyticsConsent() {
|
|
62
|
+
return {
|
|
63
|
+
analytics: false,
|
|
64
|
+
functional: true,
|
|
65
|
+
// Functional always enabled
|
|
66
|
+
timestamp: (/* @__PURE__ */ new Date()).toISOString(),
|
|
67
|
+
version: CONSENT_VERSION
|
|
68
|
+
};
|
|
69
|
+
}
|
|
70
|
+
function updateConsentCategory(current, category, value) {
|
|
71
|
+
if (category === "functional" && !value) {
|
|
72
|
+
return current;
|
|
73
|
+
}
|
|
74
|
+
return {
|
|
75
|
+
...current,
|
|
76
|
+
[category]: value,
|
|
77
|
+
timestamp: (/* @__PURE__ */ new Date()).toISOString()
|
|
78
|
+
};
|
|
79
|
+
}
|
|
80
|
+
function needsRePrompt(consent, currentVersion) {
|
|
81
|
+
return consent.version < currentVersion;
|
|
82
|
+
}
|
|
83
|
+
function isDoNotTrackEnabled() {
|
|
84
|
+
if (typeof window === "undefined") return false;
|
|
85
|
+
const dnt = navigator.doNotTrack || window.doNotTrack || navigator.msDoNotTrack;
|
|
86
|
+
return dnt === "1" || dnt === "yes";
|
|
87
|
+
}
|
|
88
|
+
function getConsentFromCookie(cookieString) {
|
|
89
|
+
const match = new RegExp(`(?:^|;\\s*)${CONSENT_COOKIE_NAME}=([^;]+)`).exec(
|
|
90
|
+
cookieString
|
|
91
|
+
);
|
|
92
|
+
if (!match) return null;
|
|
93
|
+
return match[1] === "1";
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
// src/analytics.ts
|
|
97
|
+
var analyticsState;
|
|
98
|
+
var DEFAULT_MAX_QUEUED_EVENTS = 100;
|
|
99
|
+
var eventQueue = [];
|
|
100
|
+
var maxQueuedEvents = DEFAULT_MAX_QUEUED_EVENTS;
|
|
101
|
+
var isInitialized = false;
|
|
102
|
+
var isBlocked = false;
|
|
103
|
+
function setMaxQueuedEvents(limit) {
|
|
104
|
+
maxQueuedEvents = typeof limit === "number" && Number.isSafeInteger(limit) && limit >= 0 ? limit : DEFAULT_MAX_QUEUED_EVENTS;
|
|
105
|
+
eventQueue = eventQueue.slice(0, maxQueuedEvents);
|
|
106
|
+
}
|
|
107
|
+
function enqueue(event) {
|
|
108
|
+
if (eventQueue.length < maxQueuedEvents) eventQueue.push(event);
|
|
109
|
+
}
|
|
110
|
+
function block() {
|
|
111
|
+
isBlocked = true;
|
|
112
|
+
eventQueue = [];
|
|
113
|
+
}
|
|
114
|
+
function debugLog(type, data) {
|
|
115
|
+
if (_optionalChain([analyticsState, 'optionalAccess', _ => _.debug])) {
|
|
116
|
+
console.log(`[analytics:${type}]`, data);
|
|
117
|
+
}
|
|
118
|
+
}
|
|
119
|
+
function sendEvent(name, properties) {
|
|
120
|
+
debugLog("track", { name, properties });
|
|
121
|
+
}
|
|
122
|
+
function initAnalytics(config) {
|
|
123
|
+
if (analyticsState) return;
|
|
124
|
+
setMaxQueuedEvents(config.maxQueuedEvents);
|
|
125
|
+
if (isDoNotTrackEnabled()) {
|
|
126
|
+
block();
|
|
127
|
+
debugLog("init", "Do Not Track enabled, analytics blocked");
|
|
128
|
+
return;
|
|
129
|
+
}
|
|
130
|
+
const consent = loadConsent();
|
|
131
|
+
if (consent && !consent.analytics) {
|
|
132
|
+
block();
|
|
133
|
+
debugLog("init", "Analytics consent declined");
|
|
134
|
+
return;
|
|
135
|
+
}
|
|
136
|
+
analyticsState = {
|
|
137
|
+
app: config.app,
|
|
138
|
+
debug: config.debug,
|
|
139
|
+
version: config.version
|
|
140
|
+
};
|
|
141
|
+
isInitialized = true;
|
|
142
|
+
debugLog("init", { app: config.app, version: config.version });
|
|
143
|
+
processQueue();
|
|
144
|
+
}
|
|
145
|
+
function processQueue() {
|
|
146
|
+
if (!analyticsState || isBlocked) return;
|
|
147
|
+
const queue = [...eventQueue];
|
|
148
|
+
eventQueue = [];
|
|
149
|
+
for (const event of queue) {
|
|
150
|
+
if (event.name === "__page__") {
|
|
151
|
+
debugLog("page", event.properties);
|
|
152
|
+
} else {
|
|
153
|
+
sendEvent(event.name, event.properties);
|
|
154
|
+
}
|
|
155
|
+
}
|
|
156
|
+
}
|
|
157
|
+
function track(name, properties) {
|
|
158
|
+
if (typeof window === "undefined") return;
|
|
159
|
+
if (typeof name !== "string" || name === "") return;
|
|
160
|
+
if (isBlocked) return;
|
|
161
|
+
const event = {
|
|
162
|
+
name,
|
|
163
|
+
properties,
|
|
164
|
+
timestamp: (/* @__PURE__ */ new Date()).toISOString()
|
|
165
|
+
};
|
|
166
|
+
if (!isInitialized) {
|
|
167
|
+
enqueue(event);
|
|
168
|
+
return;
|
|
169
|
+
}
|
|
170
|
+
sendEvent(name, properties);
|
|
171
|
+
}
|
|
172
|
+
function trackPage(properties) {
|
|
173
|
+
if (typeof window === "undefined") return;
|
|
174
|
+
if (isBlocked) return;
|
|
175
|
+
if (!isInitialized) {
|
|
176
|
+
enqueue({
|
|
177
|
+
name: "__page__",
|
|
178
|
+
properties,
|
|
179
|
+
timestamp: (/* @__PURE__ */ new Date()).toISOString()
|
|
180
|
+
});
|
|
181
|
+
return;
|
|
182
|
+
}
|
|
183
|
+
debugLog("page", properties);
|
|
184
|
+
}
|
|
185
|
+
function identify(userId, traits) {
|
|
186
|
+
if (typeof window === "undefined") return;
|
|
187
|
+
if (typeof userId !== "string" || userId === "") return;
|
|
188
|
+
if (isBlocked) return;
|
|
189
|
+
if (!isInitialized || !analyticsState) {
|
|
190
|
+
return;
|
|
191
|
+
}
|
|
192
|
+
analyticsState.userId = userId;
|
|
193
|
+
analyticsState.traits = traits;
|
|
194
|
+
debugLog("identify", { traits, userId });
|
|
195
|
+
}
|
|
196
|
+
function resetAnalytics() {
|
|
197
|
+
if (typeof window === "undefined") return;
|
|
198
|
+
if (analyticsState) {
|
|
199
|
+
analyticsState.userId = void 0;
|
|
200
|
+
analyticsState.traits = void 0;
|
|
201
|
+
}
|
|
202
|
+
debugLog("reset", void 0);
|
|
203
|
+
}
|
|
204
|
+
function enableAnalytics(config) {
|
|
205
|
+
if (isBlocked && !isDoNotTrackEnabled()) {
|
|
206
|
+
isBlocked = false;
|
|
207
|
+
analyticsState = void 0;
|
|
208
|
+
initAnalytics(config);
|
|
209
|
+
}
|
|
210
|
+
}
|
|
211
|
+
function disableAnalytics() {
|
|
212
|
+
block();
|
|
213
|
+
}
|
|
214
|
+
function isAnalyticsEnabled() {
|
|
215
|
+
return isInitialized && !isBlocked;
|
|
216
|
+
}
|
|
217
|
+
function getAnalyticsState() {
|
|
218
|
+
return analyticsState;
|
|
219
|
+
}
|
|
220
|
+
function trackTutorialStart(properties) {
|
|
221
|
+
track("tutorial_start", properties);
|
|
222
|
+
}
|
|
223
|
+
function trackSectionView(properties) {
|
|
224
|
+
track("tutorial_section_view", properties);
|
|
225
|
+
}
|
|
226
|
+
function trackSectionComplete(properties) {
|
|
227
|
+
track("tutorial_section_complete", properties);
|
|
228
|
+
}
|
|
229
|
+
function trackTutorialComplete(properties) {
|
|
230
|
+
track("tutorial_complete", properties);
|
|
231
|
+
}
|
|
232
|
+
function trackTutorialNavigation(properties) {
|
|
233
|
+
track("tutorial_navigation", properties);
|
|
234
|
+
}
|
|
235
|
+
function trackTimeSpent(properties) {
|
|
236
|
+
track("tutorial_time_spent", properties);
|
|
237
|
+
}
|
|
238
|
+
|
|
239
|
+
|
|
240
|
+
|
|
241
|
+
|
|
242
|
+
|
|
243
|
+
|
|
244
|
+
|
|
245
|
+
|
|
246
|
+
|
|
247
|
+
|
|
248
|
+
|
|
249
|
+
|
|
250
|
+
|
|
251
|
+
|
|
252
|
+
|
|
253
|
+
|
|
254
|
+
|
|
255
|
+
|
|
256
|
+
|
|
257
|
+
|
|
258
|
+
|
|
259
|
+
|
|
260
|
+
|
|
261
|
+
|
|
262
|
+
|
|
263
|
+
|
|
264
|
+
|
|
265
|
+
|
|
266
|
+
|
|
267
|
+
|
|
268
|
+
|
|
269
|
+
|
|
270
|
+
|
|
271
|
+
exports.CONSENT_VERSION = CONSENT_VERSION; exports.CONSENT_STORAGE_KEY = CONSENT_STORAGE_KEY; exports.CONSENT_COOKIE_NAME = CONSENT_COOKIE_NAME; exports.isValidConsentState = isValidConsentState; exports.isStorageAvailable = isStorageAvailable; exports.loadConsent = loadConsent; exports.saveConsent = saveConsent; exports.clearConsent = clearConsent; exports.createAcceptAllConsent = createAcceptAllConsent; exports.createDeclineAnalyticsConsent = createDeclineAnalyticsConsent; exports.updateConsentCategory = updateConsentCategory; exports.needsRePrompt = needsRePrompt; exports.isDoNotTrackEnabled = isDoNotTrackEnabled; exports.getConsentFromCookie = getConsentFromCookie; exports.DEFAULT_MAX_QUEUED_EVENTS = DEFAULT_MAX_QUEUED_EVENTS; exports.setMaxQueuedEvents = setMaxQueuedEvents; exports.initAnalytics = initAnalytics; exports.track = track; exports.trackPage = trackPage; exports.identify = identify; exports.resetAnalytics = resetAnalytics; exports.enableAnalytics = enableAnalytics; exports.disableAnalytics = disableAnalytics; exports.isAnalyticsEnabled = isAnalyticsEnabled; exports.getAnalyticsState = getAnalyticsState; exports.trackTutorialStart = trackTutorialStart; exports.trackSectionView = trackSectionView; exports.trackSectionComplete = trackSectionComplete; exports.trackTutorialComplete = trackTutorialComplete; exports.trackTutorialNavigation = trackTutorialNavigation; exports.trackTimeSpent = trackTimeSpent;
|