@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
|
@@ -0,0 +1,168 @@
|
|
|
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
|
+
/** Application version */
|
|
62
|
+
version?: number;
|
|
63
|
+
};
|
|
64
|
+
/**
|
|
65
|
+
* Hook options for useConsent
|
|
66
|
+
*/
|
|
67
|
+
type UseConsentOptions = {
|
|
68
|
+
/** Current consent version (bump to re-prompt) */
|
|
69
|
+
consentVersion?: number;
|
|
70
|
+
};
|
|
71
|
+
/**
|
|
72
|
+
* Return type for useConsent hook
|
|
73
|
+
*/
|
|
74
|
+
type UseConsentReturn = {
|
|
75
|
+
/** Accept all tracking */
|
|
76
|
+
acceptAll: () => void;
|
|
77
|
+
/** Current consent state (null if not yet determined) */
|
|
78
|
+
consent: ConsentState | null;
|
|
79
|
+
/** Decline analytics tracking (functional always on) */
|
|
80
|
+
declineAnalytics: () => void;
|
|
81
|
+
/** Whether consent has been given (any response) */
|
|
82
|
+
hasResponded: boolean;
|
|
83
|
+
/** Whether currently loading from storage */
|
|
84
|
+
isLoading: boolean;
|
|
85
|
+
/** Reset consent (will re-show banner) */
|
|
86
|
+
resetConsent: () => void;
|
|
87
|
+
/** Update specific consent category */
|
|
88
|
+
updateConsent: (category: ConsentCategory, value: boolean) => void;
|
|
89
|
+
};
|
|
90
|
+
|
|
91
|
+
type AnalyticsState = {
|
|
92
|
+
app: string;
|
|
93
|
+
debug?: boolean;
|
|
94
|
+
traits?: Record<string, unknown>;
|
|
95
|
+
userId?: string;
|
|
96
|
+
version?: number;
|
|
97
|
+
};
|
|
98
|
+
/**
|
|
99
|
+
* Start the shared analytics instance and flush queued events. Blocks tracking,
|
|
100
|
+
* and drops the queue, when Do Not Track is on or stored consent declines it.
|
|
101
|
+
* @see https://oss.vllnt.com/packages/analytics
|
|
102
|
+
*/
|
|
103
|
+
declare function initAnalytics(config: AnalyticsConfig): void;
|
|
104
|
+
/**
|
|
105
|
+
* Track a custom event. Events with an empty or non-string name are ignored.
|
|
106
|
+
*/
|
|
107
|
+
declare function track(name: string, properties?: Record<string, unknown>): void;
|
|
108
|
+
/**
|
|
109
|
+
* Track a page view
|
|
110
|
+
*/
|
|
111
|
+
declare function trackPage(properties?: Record<string, unknown>): void;
|
|
112
|
+
/**
|
|
113
|
+
* Identify a user. An empty or non-string user id is ignored.
|
|
114
|
+
*/
|
|
115
|
+
declare function identify(userId: string, traits?: Record<string, unknown>): void;
|
|
116
|
+
/**
|
|
117
|
+
* Reset analytics state (on logout)
|
|
118
|
+
*/
|
|
119
|
+
declare function resetAnalytics(): void;
|
|
120
|
+
/**
|
|
121
|
+
* Enable analytics after consent is given
|
|
122
|
+
*/
|
|
123
|
+
declare function enableAnalytics(config: AnalyticsConfig): void;
|
|
124
|
+
/**
|
|
125
|
+
* Disable analytics after consent is revoked.
|
|
126
|
+
* NOTE: Does not clear in-memory userId/traits (GDPR Art. 17 limitation).
|
|
127
|
+
* Callers should trigger a page reload or call resetAnalytics() to purge PII.
|
|
128
|
+
*/
|
|
129
|
+
declare function disableAnalytics(): void;
|
|
130
|
+
/**
|
|
131
|
+
* Check if analytics is currently enabled
|
|
132
|
+
*/
|
|
133
|
+
declare function isAnalyticsEnabled(): boolean;
|
|
134
|
+
/**
|
|
135
|
+
* Get current analytics state (for advanced usage/debugging)
|
|
136
|
+
*/
|
|
137
|
+
declare function getAnalyticsState(): AnalyticsState | undefined;
|
|
138
|
+
/**
|
|
139
|
+
* Track tutorial start
|
|
140
|
+
*/
|
|
141
|
+
declare function trackTutorialStart(properties: Omit<TutorialEventProperties, "section_id">): void;
|
|
142
|
+
/**
|
|
143
|
+
* Track section view
|
|
144
|
+
*/
|
|
145
|
+
declare function trackSectionView(properties: TutorialEventProperties): void;
|
|
146
|
+
/**
|
|
147
|
+
* Track section completion
|
|
148
|
+
*/
|
|
149
|
+
declare function trackSectionComplete(properties: TutorialEventProperties): void;
|
|
150
|
+
/**
|
|
151
|
+
* Track tutorial completion
|
|
152
|
+
*/
|
|
153
|
+
declare function trackTutorialComplete(properties: Omit<TutorialEventProperties, "section_id">): void;
|
|
154
|
+
/**
|
|
155
|
+
* Track navigation between sections
|
|
156
|
+
*/
|
|
157
|
+
declare function trackTutorialNavigation(properties: TutorialEventProperties & {
|
|
158
|
+
navigation_type: "keyboard" | "next" | "prev" | "toc";
|
|
159
|
+
}): void;
|
|
160
|
+
/**
|
|
161
|
+
* Track time spent on a section
|
|
162
|
+
*/
|
|
163
|
+
declare function trackTimeSpent(properties: TutorialEventProperties & {
|
|
164
|
+
time_spent_seconds: number;
|
|
165
|
+
}): void;
|
|
166
|
+
|
|
167
|
+
export { 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 };
|
|
168
|
+
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,168 @@
|
|
|
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
|
+
/** Application version */
|
|
62
|
+
version?: number;
|
|
63
|
+
};
|
|
64
|
+
/**
|
|
65
|
+
* Hook options for useConsent
|
|
66
|
+
*/
|
|
67
|
+
type UseConsentOptions = {
|
|
68
|
+
/** Current consent version (bump to re-prompt) */
|
|
69
|
+
consentVersion?: number;
|
|
70
|
+
};
|
|
71
|
+
/**
|
|
72
|
+
* Return type for useConsent hook
|
|
73
|
+
*/
|
|
74
|
+
type UseConsentReturn = {
|
|
75
|
+
/** Accept all tracking */
|
|
76
|
+
acceptAll: () => void;
|
|
77
|
+
/** Current consent state (null if not yet determined) */
|
|
78
|
+
consent: ConsentState | null;
|
|
79
|
+
/** Decline analytics tracking (functional always on) */
|
|
80
|
+
declineAnalytics: () => void;
|
|
81
|
+
/** Whether consent has been given (any response) */
|
|
82
|
+
hasResponded: boolean;
|
|
83
|
+
/** Whether currently loading from storage */
|
|
84
|
+
isLoading: boolean;
|
|
85
|
+
/** Reset consent (will re-show banner) */
|
|
86
|
+
resetConsent: () => void;
|
|
87
|
+
/** Update specific consent category */
|
|
88
|
+
updateConsent: (category: ConsentCategory, value: boolean) => void;
|
|
89
|
+
};
|
|
90
|
+
|
|
91
|
+
type AnalyticsState = {
|
|
92
|
+
app: string;
|
|
93
|
+
debug?: boolean;
|
|
94
|
+
traits?: Record<string, unknown>;
|
|
95
|
+
userId?: string;
|
|
96
|
+
version?: number;
|
|
97
|
+
};
|
|
98
|
+
/**
|
|
99
|
+
* Start the shared analytics instance and flush queued events. Blocks tracking,
|
|
100
|
+
* and drops the queue, when Do Not Track is on or stored consent declines it.
|
|
101
|
+
* @see https://oss.vllnt.com/packages/analytics
|
|
102
|
+
*/
|
|
103
|
+
declare function initAnalytics(config: AnalyticsConfig): void;
|
|
104
|
+
/**
|
|
105
|
+
* Track a custom event. Events with an empty or non-string name are ignored.
|
|
106
|
+
*/
|
|
107
|
+
declare function track(name: string, properties?: Record<string, unknown>): void;
|
|
108
|
+
/**
|
|
109
|
+
* Track a page view
|
|
110
|
+
*/
|
|
111
|
+
declare function trackPage(properties?: Record<string, unknown>): void;
|
|
112
|
+
/**
|
|
113
|
+
* Identify a user. An empty or non-string user id is ignored.
|
|
114
|
+
*/
|
|
115
|
+
declare function identify(userId: string, traits?: Record<string, unknown>): void;
|
|
116
|
+
/**
|
|
117
|
+
* Reset analytics state (on logout)
|
|
118
|
+
*/
|
|
119
|
+
declare function resetAnalytics(): void;
|
|
120
|
+
/**
|
|
121
|
+
* Enable analytics after consent is given
|
|
122
|
+
*/
|
|
123
|
+
declare function enableAnalytics(config: AnalyticsConfig): void;
|
|
124
|
+
/**
|
|
125
|
+
* Disable analytics after consent is revoked.
|
|
126
|
+
* NOTE: Does not clear in-memory userId/traits (GDPR Art. 17 limitation).
|
|
127
|
+
* Callers should trigger a page reload or call resetAnalytics() to purge PII.
|
|
128
|
+
*/
|
|
129
|
+
declare function disableAnalytics(): void;
|
|
130
|
+
/**
|
|
131
|
+
* Check if analytics is currently enabled
|
|
132
|
+
*/
|
|
133
|
+
declare function isAnalyticsEnabled(): boolean;
|
|
134
|
+
/**
|
|
135
|
+
* Get current analytics state (for advanced usage/debugging)
|
|
136
|
+
*/
|
|
137
|
+
declare function getAnalyticsState(): AnalyticsState | undefined;
|
|
138
|
+
/**
|
|
139
|
+
* Track tutorial start
|
|
140
|
+
*/
|
|
141
|
+
declare function trackTutorialStart(properties: Omit<TutorialEventProperties, "section_id">): void;
|
|
142
|
+
/**
|
|
143
|
+
* Track section view
|
|
144
|
+
*/
|
|
145
|
+
declare function trackSectionView(properties: TutorialEventProperties): void;
|
|
146
|
+
/**
|
|
147
|
+
* Track section completion
|
|
148
|
+
*/
|
|
149
|
+
declare function trackSectionComplete(properties: TutorialEventProperties): void;
|
|
150
|
+
/**
|
|
151
|
+
* Track tutorial completion
|
|
152
|
+
*/
|
|
153
|
+
declare function trackTutorialComplete(properties: Omit<TutorialEventProperties, "section_id">): void;
|
|
154
|
+
/**
|
|
155
|
+
* Track navigation between sections
|
|
156
|
+
*/
|
|
157
|
+
declare function trackTutorialNavigation(properties: TutorialEventProperties & {
|
|
158
|
+
navigation_type: "keyboard" | "next" | "prev" | "toc";
|
|
159
|
+
}): void;
|
|
160
|
+
/**
|
|
161
|
+
* Track time spent on a section
|
|
162
|
+
*/
|
|
163
|
+
declare function trackTimeSpent(properties: TutorialEventProperties & {
|
|
164
|
+
time_spent_seconds: number;
|
|
165
|
+
}): void;
|
|
166
|
+
|
|
167
|
+
export { 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 };
|
|
168
|
+
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,259 @@
|
|
|
1
|
+
// 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 {
|
|
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 {
|
|
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 {
|
|
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 eventQueue = [];
|
|
99
|
+
var isInitialized = false;
|
|
100
|
+
var isBlocked = false;
|
|
101
|
+
function block() {
|
|
102
|
+
isBlocked = true;
|
|
103
|
+
eventQueue = [];
|
|
104
|
+
}
|
|
105
|
+
function debugLog(type, data) {
|
|
106
|
+
if (analyticsState?.debug) {
|
|
107
|
+
console.log(`[analytics:${type}]`, data);
|
|
108
|
+
}
|
|
109
|
+
}
|
|
110
|
+
function sendEvent(name, properties) {
|
|
111
|
+
debugLog("track", { name, properties });
|
|
112
|
+
}
|
|
113
|
+
function initAnalytics(config) {
|
|
114
|
+
if (analyticsState) return;
|
|
115
|
+
if (isDoNotTrackEnabled()) {
|
|
116
|
+
block();
|
|
117
|
+
debugLog("init", "Do Not Track enabled, analytics blocked");
|
|
118
|
+
return;
|
|
119
|
+
}
|
|
120
|
+
const consent = loadConsent();
|
|
121
|
+
if (consent && !consent.analytics) {
|
|
122
|
+
block();
|
|
123
|
+
debugLog("init", "Analytics consent declined");
|
|
124
|
+
return;
|
|
125
|
+
}
|
|
126
|
+
analyticsState = {
|
|
127
|
+
app: config.app,
|
|
128
|
+
debug: config.debug,
|
|
129
|
+
version: config.version
|
|
130
|
+
};
|
|
131
|
+
isInitialized = true;
|
|
132
|
+
debugLog("init", { app: config.app, version: config.version });
|
|
133
|
+
processQueue();
|
|
134
|
+
}
|
|
135
|
+
function processQueue() {
|
|
136
|
+
if (!analyticsState || isBlocked) return;
|
|
137
|
+
const queue = [...eventQueue];
|
|
138
|
+
eventQueue = [];
|
|
139
|
+
for (const event of queue) {
|
|
140
|
+
if (event.name === "__page__") {
|
|
141
|
+
debugLog("page", event.properties);
|
|
142
|
+
} else {
|
|
143
|
+
sendEvent(event.name, event.properties);
|
|
144
|
+
}
|
|
145
|
+
}
|
|
146
|
+
}
|
|
147
|
+
function track(name, properties) {
|
|
148
|
+
if (typeof window === "undefined") return;
|
|
149
|
+
if (typeof name !== "string" || name === "") return;
|
|
150
|
+
if (isBlocked) return;
|
|
151
|
+
const event = {
|
|
152
|
+
name,
|
|
153
|
+
properties,
|
|
154
|
+
timestamp: (/* @__PURE__ */ new Date()).toISOString()
|
|
155
|
+
};
|
|
156
|
+
if (!isInitialized) {
|
|
157
|
+
eventQueue.push(event);
|
|
158
|
+
return;
|
|
159
|
+
}
|
|
160
|
+
sendEvent(name, properties);
|
|
161
|
+
}
|
|
162
|
+
function trackPage(properties) {
|
|
163
|
+
if (typeof window === "undefined") return;
|
|
164
|
+
if (isBlocked) return;
|
|
165
|
+
if (!isInitialized) {
|
|
166
|
+
eventQueue.push({
|
|
167
|
+
name: "__page__",
|
|
168
|
+
properties,
|
|
169
|
+
timestamp: (/* @__PURE__ */ new Date()).toISOString()
|
|
170
|
+
});
|
|
171
|
+
return;
|
|
172
|
+
}
|
|
173
|
+
debugLog("page", properties);
|
|
174
|
+
}
|
|
175
|
+
function identify(userId, traits) {
|
|
176
|
+
if (typeof window === "undefined") return;
|
|
177
|
+
if (typeof userId !== "string" || userId === "") return;
|
|
178
|
+
if (isBlocked) return;
|
|
179
|
+
if (!isInitialized || !analyticsState) {
|
|
180
|
+
return;
|
|
181
|
+
}
|
|
182
|
+
analyticsState.userId = userId;
|
|
183
|
+
analyticsState.traits = traits;
|
|
184
|
+
debugLog("identify", { traits, userId });
|
|
185
|
+
}
|
|
186
|
+
function resetAnalytics() {
|
|
187
|
+
if (typeof window === "undefined") return;
|
|
188
|
+
if (analyticsState) {
|
|
189
|
+
analyticsState.userId = void 0;
|
|
190
|
+
analyticsState.traits = void 0;
|
|
191
|
+
}
|
|
192
|
+
debugLog("reset", void 0);
|
|
193
|
+
}
|
|
194
|
+
function enableAnalytics(config) {
|
|
195
|
+
if (isBlocked && !isDoNotTrackEnabled()) {
|
|
196
|
+
isBlocked = false;
|
|
197
|
+
analyticsState = void 0;
|
|
198
|
+
initAnalytics(config);
|
|
199
|
+
}
|
|
200
|
+
}
|
|
201
|
+
function disableAnalytics() {
|
|
202
|
+
block();
|
|
203
|
+
}
|
|
204
|
+
function isAnalyticsEnabled() {
|
|
205
|
+
return isInitialized && !isBlocked;
|
|
206
|
+
}
|
|
207
|
+
function getAnalyticsState() {
|
|
208
|
+
return analyticsState;
|
|
209
|
+
}
|
|
210
|
+
function trackTutorialStart(properties) {
|
|
211
|
+
track("tutorial_start", properties);
|
|
212
|
+
}
|
|
213
|
+
function trackSectionView(properties) {
|
|
214
|
+
track("tutorial_section_view", properties);
|
|
215
|
+
}
|
|
216
|
+
function trackSectionComplete(properties) {
|
|
217
|
+
track("tutorial_section_complete", properties);
|
|
218
|
+
}
|
|
219
|
+
function trackTutorialComplete(properties) {
|
|
220
|
+
track("tutorial_complete", properties);
|
|
221
|
+
}
|
|
222
|
+
function trackTutorialNavigation(properties) {
|
|
223
|
+
track("tutorial_navigation", properties);
|
|
224
|
+
}
|
|
225
|
+
function trackTimeSpent(properties) {
|
|
226
|
+
track("tutorial_time_spent", properties);
|
|
227
|
+
}
|
|
228
|
+
|
|
229
|
+
export {
|
|
230
|
+
CONSENT_VERSION,
|
|
231
|
+
CONSENT_STORAGE_KEY,
|
|
232
|
+
CONSENT_COOKIE_NAME,
|
|
233
|
+
isValidConsentState,
|
|
234
|
+
isStorageAvailable,
|
|
235
|
+
loadConsent,
|
|
236
|
+
saveConsent,
|
|
237
|
+
clearConsent,
|
|
238
|
+
createAcceptAllConsent,
|
|
239
|
+
createDeclineAnalyticsConsent,
|
|
240
|
+
updateConsentCategory,
|
|
241
|
+
needsRePrompt,
|
|
242
|
+
isDoNotTrackEnabled,
|
|
243
|
+
getConsentFromCookie,
|
|
244
|
+
initAnalytics,
|
|
245
|
+
track,
|
|
246
|
+
trackPage,
|
|
247
|
+
identify,
|
|
248
|
+
resetAnalytics,
|
|
249
|
+
enableAnalytics,
|
|
250
|
+
disableAnalytics,
|
|
251
|
+
isAnalyticsEnabled,
|
|
252
|
+
getAnalyticsState,
|
|
253
|
+
trackTutorialStart,
|
|
254
|
+
trackSectionView,
|
|
255
|
+
trackSectionComplete,
|
|
256
|
+
trackTutorialComplete,
|
|
257
|
+
trackTutorialNavigation,
|
|
258
|
+
trackTimeSpent
|
|
259
|
+
};
|