@vllnt/analytics 0.1.1 → 0.2.0-canary.e4bb20a

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 CHANGED
@@ -19,31 +19,33 @@ pnpm add @vllnt/analytics
19
19
 
20
20
  Peer dependency: `react ^18.0.0 || ^19.0.0`
21
21
 
22
+ Ships ESM and CommonJS builds with type declarations; no bundler transpilation of `node_modules` is needed.
23
+
22
24
  ## Usage
23
25
 
24
26
  ### React Provider
25
27
 
26
28
  ```tsx
27
- import { AnalyticsProvider } from '@vllnt/analytics/react'
29
+ import { AnalyticsProvider } from "@vllnt/analytics/react";
28
30
 
29
31
  function App() {
30
32
  return (
31
- <AnalyticsProvider config={{ app: 'my-app', version: 1, debug: true }}>
33
+ <AnalyticsProvider config={{ app: "my-app", version: 1, debug: true }}>
32
34
  {children}
33
35
  </AnalyticsProvider>
34
- )
36
+ );
35
37
  }
36
38
  ```
37
39
 
38
40
  ### Consent Banner
39
41
 
40
42
  ```tsx
41
- import { useConsent } from '@vllnt/analytics/react'
43
+ import { useConsent } from "@vllnt/analytics/react";
42
44
 
43
45
  function CookieBanner() {
44
- const { hasResponded, isLoading, acceptAll, declineAnalytics } = useConsent()
46
+ const { hasResponded, isLoading, acceptAll, declineAnalytics } = useConsent();
45
47
 
46
- if (isLoading || hasResponded) return null
48
+ if (isLoading || hasResponded) return null;
47
49
 
48
50
  return (
49
51
  <div>
@@ -51,20 +53,20 @@ function CookieBanner() {
51
53
  <button onClick={acceptAll}>Accept</button>
52
54
  <button onClick={declineAnalytics}>Decline</button>
53
55
  </div>
54
- )
56
+ );
55
57
  }
56
58
  ```
57
59
 
58
60
  ### Track Events
59
61
 
60
62
  ```tsx
61
- import { track, trackPage } from '@vllnt/analytics'
63
+ import { track, trackPage } from "@vllnt/analytics";
62
64
 
63
65
  // Generic event
64
- track('button_click', { id: 'cta-hero' })
66
+ track("button_click", { id: "cta-hero" });
65
67
 
66
68
  // Page view
67
- trackPage({ path: '/about' })
69
+ trackPage({ path: "/about" });
68
70
  ```
69
71
 
70
72
  ### Tutorial Tracking
@@ -79,35 +81,35 @@ import {
79
81
  trackTutorialComplete,
80
82
  trackTutorialNavigation,
81
83
  trackTimeSpent,
82
- } from '@vllnt/analytics'
84
+ } from "@vllnt/analytics";
83
85
 
84
86
  trackTutorialStart({
85
- tutorial_id: 'getting-started',
86
- tutorial_title: 'Getting Started',
87
- locale: 'en',
87
+ tutorial_id: "getting-started",
88
+ tutorial_title: "Getting Started",
89
+ locale: "en",
88
90
  total_sections: 5,
89
- })
91
+ });
90
92
 
91
93
  trackSectionView({
92
- tutorial_id: 'getting-started',
93
- tutorial_title: 'Getting Started',
94
- locale: 'en',
95
- section_id: 'intro',
94
+ tutorial_id: "getting-started",
95
+ tutorial_title: "Getting Started",
96
+ locale: "en",
97
+ section_id: "intro",
96
98
  section_index: 0,
97
99
  total_sections: 5,
98
- })
100
+ });
99
101
  ```
100
102
 
101
103
  ### Identify Users
102
104
 
103
105
  ```tsx
104
- import { identify, resetAnalytics } from '@vllnt/analytics'
106
+ import { identify, resetAnalytics } from "@vllnt/analytics";
105
107
 
106
108
  // After login
107
- identify('user-123', { plan: 'pro' })
109
+ identify("user-123", { plan: "pro" });
108
110
 
109
111
  // After logout
110
- resetAnalytics()
112
+ resetAnalytics();
111
113
  ```
112
114
 
113
115
  ### Consent Utilities (non-React)
@@ -122,15 +124,15 @@ import {
122
124
  getConsentFromCookie,
123
125
  isDoNotTrackEnabled,
124
126
  needsRePrompt,
125
- } from '@vllnt/analytics'
127
+ } from "@vllnt/analytics";
126
128
 
127
129
  // SSR: read consent from cookie header
128
- const analyticsAllowed = getConsentFromCookie(request.headers.cookie)
130
+ const analyticsAllowed = getConsentFromCookie(request.headers.cookie);
129
131
 
130
132
  // Check if re-prompt needed after version bump
131
- const consent = loadConsent()
133
+ const consent = loadConsent();
132
134
  if (consent && needsRePrompt(consent, 2)) {
133
- clearConsent()
135
+ clearConsent();
134
136
  }
135
137
  ```
136
138
 
@@ -138,38 +140,38 @@ if (consent && needsRePrompt(consent, 2)) {
138
140
 
139
141
  ### Core
140
142
 
141
- | Function | Description |
142
- |---|---|
143
- | `initAnalytics(config)` | Initialize analytics singleton |
144
- | `track(name, properties?)` | Track a custom event |
145
- | `trackPage(properties?)` | Track a page view |
146
- | `identify(userId, traits?)` | Identify a user |
147
- | `resetAnalytics()` | Clear user identity (logout) |
148
- | `enableAnalytics(config)` | Re-enable after consent granted |
149
- | `disableAnalytics()` | Disable after consent revoked |
150
- | `isAnalyticsEnabled()` | Check if tracking is active |
143
+ | Function | Description |
144
+ | --------------------------- | ---------------------------------------------- |
145
+ | `initAnalytics(config)` | Initialize analytics singleton |
146
+ | `track(name, properties?)` | Track a custom event (empty names are ignored) |
147
+ | `trackPage(properties?)` | Track a page view |
148
+ | `identify(userId, traits?)` | Identify a user (empty ids are ignored) |
149
+ | `resetAnalytics()` | Clear user identity (logout) |
150
+ | `enableAnalytics(config)` | Re-enable after consent granted |
151
+ | `disableAnalytics()` | Disable after consent revoked |
152
+ | `isAnalyticsEnabled()` | Check if tracking is active |
151
153
 
152
154
  ### Consent
153
155
 
154
- | Function | Description |
155
- |---|---|
156
- | `loadConsent()` | Load consent from localStorage |
157
- | `saveConsent(state)` | Persist consent to localStorage + cookie |
158
- | `clearConsent()` | Remove consent (triggers re-prompt) |
159
- | `createAcceptAllConsent()` | Create accept-all consent state |
160
- | `createDeclineAnalyticsConsent()` | Create analytics-declined state |
161
- | `updateConsentCategory(state, category, value)` | Update a single category |
162
- | `needsRePrompt(consent, version)` | Check if version requires re-prompt |
163
- | `getConsentFromCookie(cookieString)` | Parse consent from cookie (SSR) |
164
- | `isDoNotTrackEnabled()` | Check browser DNT setting |
156
+ | Function | Description |
157
+ | ----------------------------------------------- | ---------------------------------------- |
158
+ | `loadConsent()` | Load consent from localStorage |
159
+ | `saveConsent(state)` | Persist consent to localStorage + cookie |
160
+ | `clearConsent()` | Remove consent (triggers re-prompt) |
161
+ | `createAcceptAllConsent()` | Create accept-all consent state |
162
+ | `createDeclineAnalyticsConsent()` | Create analytics-declined state |
163
+ | `updateConsentCategory(state, category, value)` | Update a single category |
164
+ | `needsRePrompt(consent, version)` | Check if version requires re-prompt |
165
+ | `getConsentFromCookie(cookieString)` | Parse consent from cookie (SSR) |
166
+ | `isDoNotTrackEnabled()` | Check browser DNT setting |
165
167
 
166
168
  ### React (`@vllnt/analytics/react`)
167
169
 
168
- | Export | Description |
169
- |---|---|
170
- | `AnalyticsProvider` | Context provider — wires consent + analytics init |
171
- | `useAnalytics()` | Access full analytics context |
172
- | `useConsent(options?)` | Standalone consent hook for banner UIs |
170
+ | Export | Description |
171
+ | ---------------------- | ------------------------------------------------- |
172
+ | `AnalyticsProvider` | Context provider — wires consent + analytics init |
173
+ | `useAnalytics()` | Access full analytics context |
174
+ | `useConsent(options?)` | Standalone consent hook for banner UIs |
173
175
 
174
176
  ## How It Works
175
177
 
@@ -196,6 +198,8 @@ pnpm test:once # Run tests once
196
198
  pnpm test:coverage # Run with coverage
197
199
  pnpm lint # Lint
198
200
  pnpm lint:fix # Lint + autofix
201
+ pnpm typecheck # Type-check
202
+ pnpm build # Build dist (ESM, CJS, types)
199
203
  ```
200
204
 
201
205
  ## License
@@ -0,0 +1,166 @@
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
+ * Initialize analytics with configuration
100
+ */
101
+ declare function initAnalytics(config: AnalyticsConfig): void;
102
+ /**
103
+ * Track a custom event. Events with an empty or non-string name are ignored.
104
+ */
105
+ declare function track(name: string, properties?: Record<string, unknown>): void;
106
+ /**
107
+ * Track a page view
108
+ */
109
+ declare function trackPage(properties?: Record<string, unknown>): void;
110
+ /**
111
+ * Identify a user. An empty or non-string user id is ignored.
112
+ */
113
+ declare function identify(userId: string, traits?: Record<string, unknown>): void;
114
+ /**
115
+ * Reset analytics state (on logout)
116
+ */
117
+ declare function resetAnalytics(): void;
118
+ /**
119
+ * Enable analytics after consent is given
120
+ */
121
+ declare function enableAnalytics(config: AnalyticsConfig): void;
122
+ /**
123
+ * Disable analytics after consent is revoked.
124
+ * NOTE: Does not clear in-memory userId/traits (GDPR Art. 17 limitation).
125
+ * Callers should trigger a page reload or call resetAnalytics() to purge PII.
126
+ */
127
+ declare function disableAnalytics(): void;
128
+ /**
129
+ * Check if analytics is currently enabled
130
+ */
131
+ declare function isAnalyticsEnabled(): boolean;
132
+ /**
133
+ * Get current analytics state (for advanced usage/debugging)
134
+ */
135
+ declare function getAnalyticsState(): AnalyticsState | undefined;
136
+ /**
137
+ * Track tutorial start
138
+ */
139
+ declare function trackTutorialStart(properties: Omit<TutorialEventProperties, "section_id">): void;
140
+ /**
141
+ * Track section view
142
+ */
143
+ declare function trackSectionView(properties: TutorialEventProperties): void;
144
+ /**
145
+ * Track section completion
146
+ */
147
+ declare function trackSectionComplete(properties: TutorialEventProperties): void;
148
+ /**
149
+ * Track tutorial completion
150
+ */
151
+ declare function trackTutorialComplete(properties: Omit<TutorialEventProperties, "section_id">): void;
152
+ /**
153
+ * Track navigation between sections
154
+ */
155
+ declare function trackTutorialNavigation(properties: TutorialEventProperties & {
156
+ navigation_type: "keyboard" | "next" | "prev" | "toc";
157
+ }): void;
158
+ /**
159
+ * Track time spent on a section
160
+ */
161
+ declare function trackTimeSpent(properties: TutorialEventProperties & {
162
+ time_spent_seconds: number;
163
+ }): void;
164
+
165
+ 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 };
166
+ 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,166 @@
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
+ * Initialize analytics with configuration
100
+ */
101
+ declare function initAnalytics(config: AnalyticsConfig): void;
102
+ /**
103
+ * Track a custom event. Events with an empty or non-string name are ignored.
104
+ */
105
+ declare function track(name: string, properties?: Record<string, unknown>): void;
106
+ /**
107
+ * Track a page view
108
+ */
109
+ declare function trackPage(properties?: Record<string, unknown>): void;
110
+ /**
111
+ * Identify a user. An empty or non-string user id is ignored.
112
+ */
113
+ declare function identify(userId: string, traits?: Record<string, unknown>): void;
114
+ /**
115
+ * Reset analytics state (on logout)
116
+ */
117
+ declare function resetAnalytics(): void;
118
+ /**
119
+ * Enable analytics after consent is given
120
+ */
121
+ declare function enableAnalytics(config: AnalyticsConfig): void;
122
+ /**
123
+ * Disable analytics after consent is revoked.
124
+ * NOTE: Does not clear in-memory userId/traits (GDPR Art. 17 limitation).
125
+ * Callers should trigger a page reload or call resetAnalytics() to purge PII.
126
+ */
127
+ declare function disableAnalytics(): void;
128
+ /**
129
+ * Check if analytics is currently enabled
130
+ */
131
+ declare function isAnalyticsEnabled(): boolean;
132
+ /**
133
+ * Get current analytics state (for advanced usage/debugging)
134
+ */
135
+ declare function getAnalyticsState(): AnalyticsState | undefined;
136
+ /**
137
+ * Track tutorial start
138
+ */
139
+ declare function trackTutorialStart(properties: Omit<TutorialEventProperties, "section_id">): void;
140
+ /**
141
+ * Track section view
142
+ */
143
+ declare function trackSectionView(properties: TutorialEventProperties): void;
144
+ /**
145
+ * Track section completion
146
+ */
147
+ declare function trackSectionComplete(properties: TutorialEventProperties): void;
148
+ /**
149
+ * Track tutorial completion
150
+ */
151
+ declare function trackTutorialComplete(properties: Omit<TutorialEventProperties, "section_id">): void;
152
+ /**
153
+ * Track navigation between sections
154
+ */
155
+ declare function trackTutorialNavigation(properties: TutorialEventProperties & {
156
+ navigation_type: "keyboard" | "next" | "prev" | "toc";
157
+ }): void;
158
+ /**
159
+ * Track time spent on a section
160
+ */
161
+ declare function trackTimeSpent(properties: TutorialEventProperties & {
162
+ time_spent_seconds: number;
163
+ }): void;
164
+
165
+ 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 };
166
+ 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 };