@vllnt/analytics 0.1.1 → 0.2.0-canary.7824f2c
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 +48 -0
- package/README.md +99 -61
- package/dist/analytics-DdAxcZwX.d.cts +166 -0
- package/dist/analytics-DdAxcZwX.d.ts +166 -0
- package/dist/chunk-6RHY2BTB.js +256 -0
- package/dist/chunk-MVOBIOKZ.cjs +256 -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 +166 -0
- package/dist/react.d.cts +33 -0
- package/dist/react.d.ts +33 -0
- package/dist/react.js +166 -0
- package/docs/API.md +384 -0
- package/llms.txt +56 -0
- package/package.json +56 -35
- package/src/analytics.ts +12 -35
- package/src/consent.ts +5 -1
package/CHANGELOG.md
ADDED
|
@@ -0,0 +1,48 @@
|
|
|
1
|
+
# Changelog
|
|
2
|
+
|
|
3
|
+
All notable changes to this project are documented in this file.
|
|
4
|
+
|
|
5
|
+
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.1.0/), and this project
|
|
6
|
+
adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
|
|
7
|
+
|
|
8
|
+
## [0.2.0] - Unreleased
|
|
9
|
+
|
|
10
|
+
### Added
|
|
11
|
+
|
|
12
|
+
- The package ships version-matched docs for coding agents and other readers: `docs/API.md`
|
|
13
|
+
(every public export with its signature and an example), `llms.txt`, `CHANGELOG.md` and the
|
|
14
|
+
TypeScript sources in `src/` (tests excluded). `llms.txt` links to these bundled files with
|
|
15
|
+
relative paths.
|
|
16
|
+
|
|
17
|
+
### Changed
|
|
18
|
+
|
|
19
|
+
- The package entry points now resolve to compiled output in `dist` (ESM, CommonJS and type
|
|
20
|
+
declarations) instead of raw TypeScript sources, so it works without transpiling
|
|
21
|
+
`node_modules`. The `.` and `./react` export subpaths and the `react` peer range are
|
|
22
|
+
unchanged. `./package.json` is now exported too.
|
|
23
|
+
- `track()` ignores events whose name is empty or not a string, and `identify()` ignores an empty
|
|
24
|
+
or non-string user id, instead of recording them.
|
|
25
|
+
|
|
26
|
+
### Fixed
|
|
27
|
+
|
|
28
|
+
- `getConsentFromCookie()` no longer matches look-alike cookie names that merely end with
|
|
29
|
+
`vllnt-consent` (for example `evil-vllnt-consent=1`).
|
|
30
|
+
|
|
31
|
+
### Security
|
|
32
|
+
|
|
33
|
+
- Releases are published from CI with npm trusted publishing and provenance. `@latest` is published
|
|
34
|
+
only from a `vX.Y.Z` tag on `main` that matches `package.json` and this changelog.
|
|
35
|
+
|
|
36
|
+
## [0.1.1] - 2026-03-10
|
|
37
|
+
|
|
38
|
+
### Added
|
|
39
|
+
|
|
40
|
+
- CI and publish workflows.
|
|
41
|
+
|
|
42
|
+
## [0.1.0] - 2026-03-05
|
|
43
|
+
|
|
44
|
+
### Added
|
|
45
|
+
|
|
46
|
+
- Initial release: event tracking with a pre-initialization queue, two-category consent management
|
|
47
|
+
(localStorage and SSR cookie), versioned consent re-prompts, Do Not Track support, tutorial
|
|
48
|
+
tracking helpers, and React integration (`AnalyticsProvider`, `useAnalytics`, `useConsent`).
|
package/README.md
CHANGED
|
@@ -19,31 +19,49 @@ 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
|
+
|
|
24
|
+
## For coding agents
|
|
25
|
+
|
|
26
|
+
The docs are bundled in the package at `node_modules/@vllnt/analytics/docs/` ([`docs/API.md`](docs/API.md)), together with
|
|
27
|
+
[`llms.txt`](llms.txt), `CHANGELOG.md` and the TypeScript sources in `src/`. They match the installed
|
|
28
|
+
version. Read them before writing code.
|
|
29
|
+
|
|
22
30
|
## Usage
|
|
23
31
|
|
|
24
32
|
### React Provider
|
|
25
33
|
|
|
26
34
|
```tsx
|
|
27
|
-
import {
|
|
35
|
+
import type { ReactNode } from "react";
|
|
36
|
+
import { AnalyticsProvider } from "@vllnt/analytics/react";
|
|
28
37
|
|
|
29
|
-
|
|
38
|
+
// Keep config stable: an inline object re-runs the init effect and re-renders every consumer.
|
|
39
|
+
// Config changes are ignored while analytics is running (initAnalytics returns once started).
|
|
40
|
+
const config = { app: "my-app", version: 1, debug: true };
|
|
41
|
+
|
|
42
|
+
function App({ children }: { children: ReactNode }) {
|
|
30
43
|
return (
|
|
31
|
-
<AnalyticsProvider config={
|
|
44
|
+
<AnalyticsProvider config={config}>
|
|
32
45
|
{children}
|
|
33
46
|
</AnalyticsProvider>
|
|
34
|
-
)
|
|
47
|
+
);
|
|
35
48
|
}
|
|
36
49
|
```
|
|
37
50
|
|
|
38
51
|
### Consent Banner
|
|
39
52
|
|
|
40
53
|
```tsx
|
|
41
|
-
import {
|
|
54
|
+
import { useEffect, useState } from "react";
|
|
55
|
+
import { useAnalytics } from "@vllnt/analytics/react";
|
|
42
56
|
|
|
57
|
+
// Render inside <AnalyticsProvider> so accepting starts tracking.
|
|
43
58
|
function CookieBanner() {
|
|
44
|
-
const { hasResponded,
|
|
59
|
+
const { hasResponded, acceptAll, declineAnalytics } = useAnalytics();
|
|
60
|
+
// Stored consent is read in the browser only; wait for mount to avoid a hydration mismatch.
|
|
61
|
+
const [mounted, setMounted] = useState(false);
|
|
62
|
+
useEffect(() => setMounted(true), []);
|
|
45
63
|
|
|
46
|
-
if (
|
|
64
|
+
if (!mounted || hasResponded) return null;
|
|
47
65
|
|
|
48
66
|
return (
|
|
49
67
|
<div>
|
|
@@ -51,20 +69,23 @@ function CookieBanner() {
|
|
|
51
69
|
<button onClick={acceptAll}>Accept</button>
|
|
52
70
|
<button onClick={declineAnalytics}>Decline</button>
|
|
53
71
|
</div>
|
|
54
|
-
)
|
|
72
|
+
);
|
|
55
73
|
}
|
|
56
74
|
```
|
|
57
75
|
|
|
76
|
+
`useConsent()` exposes the same consent fields without a provider, but it only reads and writes
|
|
77
|
+
stored consent: it does not start or stop analytics.
|
|
78
|
+
|
|
58
79
|
### Track Events
|
|
59
80
|
|
|
60
81
|
```tsx
|
|
61
|
-
import { track, trackPage } from
|
|
82
|
+
import { track, trackPage } from "@vllnt/analytics";
|
|
62
83
|
|
|
63
84
|
// Generic event
|
|
64
|
-
track(
|
|
85
|
+
track("button_click", { id: "cta-hero" });
|
|
65
86
|
|
|
66
87
|
// Page view
|
|
67
|
-
trackPage({ path:
|
|
88
|
+
trackPage({ path: "/about" });
|
|
68
89
|
```
|
|
69
90
|
|
|
70
91
|
### Tutorial Tracking
|
|
@@ -79,35 +100,35 @@ import {
|
|
|
79
100
|
trackTutorialComplete,
|
|
80
101
|
trackTutorialNavigation,
|
|
81
102
|
trackTimeSpent,
|
|
82
|
-
} from
|
|
103
|
+
} from "@vllnt/analytics";
|
|
83
104
|
|
|
84
105
|
trackTutorialStart({
|
|
85
|
-
tutorial_id:
|
|
86
|
-
tutorial_title:
|
|
87
|
-
locale:
|
|
106
|
+
tutorial_id: "getting-started",
|
|
107
|
+
tutorial_title: "Getting Started",
|
|
108
|
+
locale: "en",
|
|
88
109
|
total_sections: 5,
|
|
89
|
-
})
|
|
110
|
+
});
|
|
90
111
|
|
|
91
112
|
trackSectionView({
|
|
92
|
-
tutorial_id:
|
|
93
|
-
tutorial_title:
|
|
94
|
-
locale:
|
|
95
|
-
section_id:
|
|
113
|
+
tutorial_id: "getting-started",
|
|
114
|
+
tutorial_title: "Getting Started",
|
|
115
|
+
locale: "en",
|
|
116
|
+
section_id: "intro",
|
|
96
117
|
section_index: 0,
|
|
97
118
|
total_sections: 5,
|
|
98
|
-
})
|
|
119
|
+
});
|
|
99
120
|
```
|
|
100
121
|
|
|
101
122
|
### Identify Users
|
|
102
123
|
|
|
103
124
|
```tsx
|
|
104
|
-
import { identify, resetAnalytics } from
|
|
125
|
+
import { identify, resetAnalytics } from "@vllnt/analytics";
|
|
105
126
|
|
|
106
127
|
// After login
|
|
107
|
-
identify(
|
|
128
|
+
identify("user-123", { plan: "pro" });
|
|
108
129
|
|
|
109
130
|
// After logout
|
|
110
|
-
resetAnalytics()
|
|
131
|
+
resetAnalytics();
|
|
111
132
|
```
|
|
112
133
|
|
|
113
134
|
### Consent Utilities (non-React)
|
|
@@ -122,68 +143,70 @@ import {
|
|
|
122
143
|
getConsentFromCookie,
|
|
123
144
|
isDoNotTrackEnabled,
|
|
124
145
|
needsRePrompt,
|
|
125
|
-
} from
|
|
146
|
+
} from "@vllnt/analytics";
|
|
126
147
|
|
|
127
148
|
// SSR: read consent from cookie header
|
|
128
|
-
const analyticsAllowed = getConsentFromCookie(request.headers.cookie)
|
|
149
|
+
const analyticsAllowed = getConsentFromCookie(request.headers.get("cookie") ?? "");
|
|
129
150
|
|
|
130
151
|
// Check if re-prompt needed after version bump
|
|
131
|
-
const consent = loadConsent()
|
|
152
|
+
const consent = loadConsent();
|
|
132
153
|
if (consent && needsRePrompt(consent, 2)) {
|
|
133
|
-
clearConsent()
|
|
154
|
+
clearConsent();
|
|
134
155
|
}
|
|
135
156
|
```
|
|
136
157
|
|
|
137
158
|
## API
|
|
138
159
|
|
|
160
|
+
Full reference with signatures and examples: [`docs/API.md`](docs/API.md).
|
|
161
|
+
|
|
139
162
|
### Core
|
|
140
163
|
|
|
141
|
-
| Function
|
|
142
|
-
|
|
143
|
-
| `initAnalytics(config)`
|
|
144
|
-
| `track(name, properties?)`
|
|
145
|
-
| `trackPage(properties?)`
|
|
146
|
-
| `identify(userId, traits?)` | Identify a user |
|
|
147
|
-
| `resetAnalytics()`
|
|
148
|
-
| `enableAnalytics(config)`
|
|
149
|
-
| `disableAnalytics()`
|
|
150
|
-
| `isAnalyticsEnabled()`
|
|
164
|
+
| Function | Description |
|
|
165
|
+
| --------------------------- | ---------------------------------------------- |
|
|
166
|
+
| `initAnalytics(config)` | Initialize analytics singleton |
|
|
167
|
+
| `track(name, properties?)` | Track a custom event (empty names are ignored) |
|
|
168
|
+
| `trackPage(properties?)` | Track a page view |
|
|
169
|
+
| `identify(userId, traits?)` | Identify a user (empty ids are ignored) |
|
|
170
|
+
| `resetAnalytics()` | Clear user identity (logout) |
|
|
171
|
+
| `enableAnalytics(config)` | Re-enable after consent granted |
|
|
172
|
+
| `disableAnalytics()` | Disable after consent revoked |
|
|
173
|
+
| `isAnalyticsEnabled()` | Check if tracking is active |
|
|
151
174
|
|
|
152
175
|
### Consent
|
|
153
176
|
|
|
154
|
-
| Function
|
|
155
|
-
|
|
156
|
-
| `loadConsent()`
|
|
157
|
-
| `saveConsent(state)`
|
|
158
|
-
| `clearConsent()`
|
|
159
|
-
| `createAcceptAllConsent()`
|
|
160
|
-
| `createDeclineAnalyticsConsent()`
|
|
161
|
-
| `updateConsentCategory(state, category, value)` | Update a single category
|
|
162
|
-
| `needsRePrompt(consent, version)`
|
|
163
|
-
| `getConsentFromCookie(cookieString)`
|
|
164
|
-
| `isDoNotTrackEnabled()`
|
|
177
|
+
| Function | Description |
|
|
178
|
+
| ----------------------------------------------- | ---------------------------------------- |
|
|
179
|
+
| `loadConsent()` | Load consent from localStorage |
|
|
180
|
+
| `saveConsent(state)` | Persist consent to localStorage + cookie |
|
|
181
|
+
| `clearConsent()` | Remove consent (triggers re-prompt) |
|
|
182
|
+
| `createAcceptAllConsent()` | Create accept-all consent state |
|
|
183
|
+
| `createDeclineAnalyticsConsent()` | Create analytics-declined state |
|
|
184
|
+
| `updateConsentCategory(state, category, value)` | Update a single category |
|
|
185
|
+
| `needsRePrompt(consent, version)` | Check if version requires re-prompt |
|
|
186
|
+
| `getConsentFromCookie(cookieString)` | Parse consent from cookie (SSR) |
|
|
187
|
+
| `isDoNotTrackEnabled()` | Check browser DNT setting |
|
|
165
188
|
|
|
166
189
|
### React (`@vllnt/analytics/react`)
|
|
167
190
|
|
|
168
|
-
| Export
|
|
169
|
-
|
|
170
|
-
| `AnalyticsProvider`
|
|
171
|
-
| `useAnalytics()`
|
|
172
|
-
| `useConsent(options?)` | Standalone consent hook for banner UIs
|
|
191
|
+
| Export | Description |
|
|
192
|
+
| ---------------------- | ------------------------------------------------- |
|
|
193
|
+
| `AnalyticsProvider` | Context provider — wires consent + analytics init |
|
|
194
|
+
| `useAnalytics()` | Access full analytics context |
|
|
195
|
+
| `useConsent(options?)` | Standalone consent hook for banner UIs |
|
|
173
196
|
|
|
174
197
|
## How It Works
|
|
175
198
|
|
|
176
199
|
```
|
|
177
|
-
|
|
200
|
+
AnalyticsProvider mounts
|
|
178
201
|
│
|
|
179
|
-
├─
|
|
202
|
+
├─ Stored consent outdated? ──▶ Clear it & re-prompt
|
|
180
203
|
│
|
|
181
|
-
├─
|
|
182
|
-
│ ├─
|
|
183
|
-
│
|
|
184
|
-
│ └─ Analytics accepted ──▶ Initialize & flush queue
|
|
204
|
+
├─ Analytics accepted ──▶ initAnalytics()
|
|
205
|
+
│ ├─ DNT enabled? ──▶ Block tracking
|
|
206
|
+
│ └─ Otherwise ──▶ Start & flush queue
|
|
185
207
|
│
|
|
186
|
-
└─
|
|
208
|
+
└─ Declined or no consent ──▶ No init; track() calls stay queued in memory
|
|
209
|
+
(declineAnalytics() blocks and empties the queue)
|
|
187
210
|
```
|
|
188
211
|
|
|
189
212
|
Events fired before initialization are queued and flushed once consent is granted and analytics initializes.
|
|
@@ -196,8 +219,23 @@ pnpm test:once # Run tests once
|
|
|
196
219
|
pnpm test:coverage # Run with coverage
|
|
197
220
|
pnpm lint # Lint
|
|
198
221
|
pnpm lint:fix # Lint + autofix
|
|
222
|
+
pnpm typecheck # Type-check
|
|
223
|
+
pnpm build # Build dist (ESM, CJS, types)
|
|
199
224
|
```
|
|
200
225
|
|
|
226
|
+
## Related packages
|
|
227
|
+
|
|
228
|
+
- [`@vllnt/convex-analytics`](https://www.npmjs.com/package/@vllnt/convex-analytics): a Convex
|
|
229
|
+
component that stores events and serves rollups, funnels and retention. Use it as the backend if
|
|
230
|
+
your app runs on Convex; this package does not send events to it for you.
|
|
231
|
+
- [`@vllnt/convex-consent`](https://www.npmjs.com/package/@vllnt/convex-consent): an append-only
|
|
232
|
+
consent ledger stored in Convex. Use it when you need a server-side record of consent per
|
|
233
|
+
subject and purpose; this package only keeps the visitor's choice in the browser.
|
|
234
|
+
|
|
201
235
|
## License
|
|
202
236
|
|
|
203
237
|
[MIT](LICENSE)
|
|
238
|
+
|
|
239
|
+
---
|
|
240
|
+
|
|
241
|
+
Part of vllnt oss: https://oss.vllnt.com
|
|
@@ -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 };
|