@philcrp/analytics 1.6.4 → 1.6.5
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 +664 -6
- package/dist/global-CLXWNSdY.d.mts +587 -0
- package/dist/global-CLXWNSdY.d.ts +587 -0
- package/dist/index.d.mts +38 -0
- package/dist/index.d.ts +38 -0
- package/dist/index.js +983 -0
- package/dist/index.js.map +1 -0
- package/dist/index.mjs +939 -0
- package/dist/index.mjs.map +1 -0
- package/dist/react/index.d.mts +168 -0
- package/dist/react/index.d.ts +168 -0
- package/dist/react/index.js +269 -0
- package/dist/react/index.js.map +1 -0
- package/dist/react/index.mjs +233 -0
- package/dist/react/index.mjs.map +1 -0
- package/dist/server.d.mts +16 -0
- package/dist/server.d.ts +16 -0
- package/dist/server.js +766 -0
- package/dist/server.js.map +1 -0
- package/dist/server.mjs +729 -0
- package/dist/server.mjs.map +1 -0
- package/package.json +8 -74
package/README.md
CHANGED
|
@@ -1,13 +1,671 @@
|
|
|
1
|
-
|
|
1
|
+
<div align="center"><a name="readme-top"></a>
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
<img height="120" src="https://registry.npmmirror.com/@lobehub/assets-logo/1.0.0/files/assets/logo-3d.webp">
|
|
4
|
+
<img height="120" src="https://gw.alipayobjects.com/zos/kitchen/qJ3l3EPsdW/split.svg">
|
|
5
|
+
<img height="120" src="https://registry.npmmirror.com/@lobehub/fluent-emoji-3d/latest/files/assets/1f4cd.webp">
|
|
4
6
|
|
|
5
|
-
|
|
7
|
+
<h1>Lobe Analytics</h1>
|
|
8
|
+
|
|
9
|
+
A modern, type-safe analytics library for tracking user events across multiple providers, built by LobeHub
|
|
10
|
+
|
|
11
|
+
[![][npm-release-shield]][npm-release-link]
|
|
12
|
+
[![][github-releasedate-shield]][github-releasedate-link]
|
|
13
|
+
[![][github-action-test-shield]][github-action-test-link]
|
|
14
|
+
[![][github-action-release-shield]][github-action-release-link]<br/>
|
|
15
|
+
[![][github-contributors-shield]][github-contributors-link]
|
|
16
|
+
[![][github-forks-shield]][github-forks-link]
|
|
17
|
+
[![][github-stars-shield]][github-stars-link]
|
|
18
|
+
[![][github-issues-shield]][github-issues-link]
|
|
19
|
+
[![][github-license-shield]][github-license-link]
|
|
20
|
+
|
|
21
|
+
[Changelog](./CHANGELOG.md) · [Report Bug][github-issues-link] · [Request Feature][github-issues-link]
|
|
22
|
+
|
|
23
|
+

|
|
24
|
+
|
|
25
|
+
</div>
|
|
26
|
+
|
|
27
|
+
<details>
|
|
28
|
+
<summary><kbd>Table of contents</kbd></summary>
|
|
29
|
+
|
|
30
|
+
#### TOC
|
|
31
|
+
|
|
32
|
+
- [✨ Features](#-features)
|
|
33
|
+
- [📦 Installation](#-installation)
|
|
34
|
+
- [⚠️ **Client-side vs Server-side Usage**](#️-client-side-vs-server-side-usage)
|
|
35
|
+
- [🌐 **Client-side Usage** (Browser Environment)](#-client-side-usage-browser-environment)
|
|
36
|
+
- [🖥️ **Server-side Usage** (Node.js Environment)](#️-server-side-usage-nodejs-environment)
|
|
37
|
+
- [Import Structure](#import-structure)
|
|
38
|
+
- [🚀 Quick Start](#-quick-start)
|
|
39
|
+
- [Basic Usage](#basic-usage)
|
|
40
|
+
- [Global Instance Management](#global-instance-management)
|
|
41
|
+
- [React Integration](#react-integration)
|
|
42
|
+
- [SPM (Source Page Medium) Auto-Prefixing](#spm-source-page-medium-auto-prefixing)
|
|
43
|
+
- [📖 API Reference](#-api-reference)
|
|
44
|
+
- [Core Functions](#core-functions)
|
|
45
|
+
- [Global Instance Management](#global-instance-management-1)
|
|
46
|
+
- [React Hooks & Provider](#react-hooks--provider)
|
|
47
|
+
- [Types](#types)
|
|
48
|
+
- [🛠️ Development](#️-development)
|
|
49
|
+
- [Project Structure](#project-structure)
|
|
50
|
+
- [Examples](#examples)
|
|
51
|
+
- [🤝 Contributing](#-contributing)
|
|
52
|
+
|
|
53
|
+
####
|
|
54
|
+
|
|
55
|
+
</details>
|
|
56
|
+
|
|
57
|
+
## ✨ Features
|
|
58
|
+
|
|
59
|
+
- 🎯 **Type-safe** - Full TypeScript support with predefined events
|
|
60
|
+
- 🔌 **Multi-provider** - Built-in PostHog support, extensible for other providers
|
|
61
|
+
- 🌐 **Global Instance Management** - Singleton pattern and named global instances
|
|
62
|
+
- 📊 **SPM Auto-Prefixing** - Automatic Source Page Medium tracking with business prefixes
|
|
63
|
+
- ⚛️ **React integration** - Enhanced Provider with auto-registration and hooks
|
|
64
|
+
- 🎛️ **Easy configuration** - Simple setup with business context
|
|
65
|
+
- 🪶 **Lightweight** - Minimal dependencies and optimized bundle size
|
|
66
|
+
- 🔧 **Developer-friendly** - Comprehensive error handling and debugging
|
|
67
|
+
- 🧪 **Test-friendly** - Built-in reset and cleanup functions
|
|
68
|
+
|
|
69
|
+
## 📦 Installation
|
|
70
|
+
|
|
71
|
+
To install `@lobehub/analytics`, run the following command:
|
|
72
|
+
|
|
73
|
+
```bash
|
|
74
|
+
npm install @lobehub/analytics
|
|
75
|
+
|
|
76
|
+
# Additional installation for server-side usage
|
|
77
|
+
npm install posthog-node # Server-side only
|
|
78
|
+
```
|
|
79
|
+
|
|
80
|
+
This library includes PostHog analytics provider out of the box. For React integration:
|
|
81
|
+
|
|
82
|
+
```bash
|
|
83
|
+
npm install react # if not already installed
|
|
84
|
+
```
|
|
85
|
+
|
|
86
|
+
## ⚠️ **Client-side vs Server-side Usage**
|
|
87
|
+
|
|
88
|
+
### 🌐 **Client-side Usage** (Browser Environment)
|
|
89
|
+
|
|
90
|
+
```typescript
|
|
91
|
+
import { createAnalytics } from '@lobehub/analytics';
|
|
92
|
+
|
|
93
|
+
const analytics = createAnalytics({
|
|
94
|
+
business: 'my-app',
|
|
95
|
+
providers: {
|
|
96
|
+
posthog: { enabled: true, key: 'client_key' },
|
|
97
|
+
},
|
|
98
|
+
});
|
|
99
|
+
```
|
|
100
|
+
|
|
101
|
+
### 🖥️ **Server-side Usage** (Node.js Environment)
|
|
102
|
+
|
|
103
|
+
```typescript
|
|
104
|
+
import { createServerAnalytics } from '@lobehub/analytics/server';
|
|
105
|
+
|
|
106
|
+
const analytics = createServerAnalytics({
|
|
107
|
+
business: 'my-app',
|
|
108
|
+
providers: {
|
|
109
|
+
// ✅ Client-side PostHog (browser compatible)
|
|
110
|
+
posthog: {
|
|
111
|
+
enabled: true,
|
|
112
|
+
key: 'phc_xxxxx',
|
|
113
|
+
api_host: 'https://app.posthog.com',
|
|
114
|
+
},
|
|
115
|
+
// ✅ Server-side PostHog (Node.js only)
|
|
116
|
+
posthogNode: {
|
|
117
|
+
enabled: true,
|
|
118
|
+
key: 'server_key',
|
|
119
|
+
host: 'https://app.posthog.com',
|
|
120
|
+
},
|
|
121
|
+
},
|
|
122
|
+
});
|
|
123
|
+
```
|
|
124
|
+
|
|
125
|
+
> **🔥 Separation Solution**: Clear entry point separation ensures `posthogNode` is completely excluded from client-side builds!
|
|
126
|
+
|
|
127
|
+
## Import Structure
|
|
128
|
+
|
|
129
|
+
The library provides separate entry points for client-side, server-side, and React integration:
|
|
130
|
+
|
|
131
|
+
```typescript
|
|
132
|
+
// Client-side analytics (browser safe)
|
|
133
|
+
import { AnalyticsManager, createAnalytics } from '@lobehub/analytics';
|
|
134
|
+
// Global instance management
|
|
135
|
+
import {
|
|
136
|
+
createSingletonAnalytics,
|
|
137
|
+
getGlobalAnalytics,
|
|
138
|
+
getSingletonAnalytics,
|
|
139
|
+
} from '@lobehub/analytics';
|
|
140
|
+
// React hooks (separate import)
|
|
141
|
+
import {
|
|
142
|
+
AnalyticsProvider,
|
|
143
|
+
useAnalytics,
|
|
144
|
+
useAnalyticsStrict,
|
|
145
|
+
useEventTracking,
|
|
146
|
+
} from '@lobehub/analytics/react';
|
|
147
|
+
// Server-side analytics (includes posthog-node)
|
|
148
|
+
import { PostHogNodeAnalyticsProvider, createServerAnalytics } from '@lobehub/analytics/server';
|
|
149
|
+
```
|
|
150
|
+
|
|
151
|
+
<div align="right">
|
|
152
|
+
|
|
153
|
+
[![][back-to-top]](#readme-top)
|
|
154
|
+
|
|
155
|
+
</div>
|
|
156
|
+
|
|
157
|
+
## 🚀 Quick Start
|
|
158
|
+
|
|
159
|
+
### Basic Usage
|
|
160
|
+
|
|
161
|
+
```typescript
|
|
162
|
+
import { createAnalytics } from '@lobehub/analytics';
|
|
163
|
+
|
|
164
|
+
// Configure analytics with business context
|
|
165
|
+
const analytics = createAnalytics({
|
|
166
|
+
business: 'my-app', // Required: business name for SPM prefixing
|
|
167
|
+
debug: process.env.NODE_ENV === 'development',
|
|
168
|
+
providers: {
|
|
169
|
+
posthog: {
|
|
170
|
+
enabled: !!process.env.POSTHOG_KEY,
|
|
171
|
+
key: process.env.POSTHOG_KEY!,
|
|
172
|
+
host: process.env.POSTHOG_HOST, // optional
|
|
173
|
+
},
|
|
174
|
+
},
|
|
175
|
+
});
|
|
176
|
+
|
|
177
|
+
// Initialize
|
|
178
|
+
await analytics.initialize();
|
|
179
|
+
|
|
180
|
+
// Track events with automatic SPM prefixing
|
|
181
|
+
await analytics.trackEvent('user_signup', {
|
|
182
|
+
method: 'email',
|
|
183
|
+
source: 'landing_page',
|
|
184
|
+
spm: 'homepage.cta', // Will become: 'my-app.homepage.cta'
|
|
185
|
+
});
|
|
186
|
+
|
|
187
|
+
// Identify users
|
|
188
|
+
await analytics.identify('user_123', {
|
|
189
|
+
email: 'user@example.com',
|
|
190
|
+
plan: 'pro',
|
|
191
|
+
});
|
|
192
|
+
```
|
|
193
|
+
|
|
194
|
+
### Global Instance Management
|
|
195
|
+
|
|
196
|
+
**Singleton Pattern (Recommended for simple apps):**
|
|
197
|
+
|
|
198
|
+
```typescript
|
|
199
|
+
import { createSingletonAnalytics, getSingletonAnalytics } from '@lobehub/analytics';
|
|
200
|
+
|
|
201
|
+
// Create singleton (usually in app initialization)
|
|
202
|
+
const analytics = createSingletonAnalytics({
|
|
203
|
+
business: 'my-app',
|
|
204
|
+
providers: {
|
|
205
|
+
posthog: {
|
|
206
|
+
enabled: true,
|
|
207
|
+
key: process.env.POSTHOG_KEY!,
|
|
208
|
+
},
|
|
209
|
+
},
|
|
210
|
+
});
|
|
211
|
+
|
|
212
|
+
await analytics.initialize();
|
|
213
|
+
|
|
214
|
+
// Access from anywhere in your application
|
|
215
|
+
export function trackUserAction() {
|
|
216
|
+
const analytics = getSingletonAnalytics();
|
|
217
|
+
analytics.trackEvent('button_click', {
|
|
218
|
+
button_name: 'signup',
|
|
219
|
+
page: 'home',
|
|
220
|
+
});
|
|
221
|
+
}
|
|
222
|
+
```
|
|
223
|
+
|
|
224
|
+
**Named Global Instances (For complex apps):**
|
|
225
|
+
|
|
226
|
+
```typescript
|
|
227
|
+
import { getGlobalAnalytics, setGlobalAnalytics } from '@lobehub/analytics';
|
|
228
|
+
|
|
229
|
+
// Register multiple instances
|
|
230
|
+
setGlobalAnalytics(mainAnalytics, 'main');
|
|
231
|
+
setGlobalAnalytics(adminAnalytics, 'admin');
|
|
232
|
+
|
|
233
|
+
// Use specific instances
|
|
234
|
+
getGlobalAnalytics('main').trackEvent('user_action', {});
|
|
235
|
+
getGlobalAnalytics('admin').trackEvent('admin_action', {});
|
|
236
|
+
```
|
|
237
|
+
|
|
238
|
+
### React Integration
|
|
239
|
+
|
|
240
|
+
**Enhanced AnalyticsProvider with auto-registration:**
|
|
241
|
+
|
|
242
|
+
```typescript
|
|
243
|
+
import React from 'react';
|
|
244
|
+
import { createAnalytics } from '@lobehub/analytics';
|
|
245
|
+
import {
|
|
246
|
+
AnalyticsProvider,
|
|
247
|
+
useAnalytics,
|
|
248
|
+
useAnalyticsStrict
|
|
249
|
+
} from '@lobehub/analytics/react';
|
|
250
|
+
|
|
251
|
+
// Create analytics instance
|
|
252
|
+
const analytics = createAnalytics({
|
|
253
|
+
business: 'my-app',
|
|
254
|
+
providers: {
|
|
255
|
+
posthog: {
|
|
256
|
+
enabled: true,
|
|
257
|
+
key: process.env.REACT_APP_POSTHOG_KEY!,
|
|
258
|
+
},
|
|
259
|
+
},
|
|
260
|
+
});
|
|
261
|
+
|
|
262
|
+
// Provider with auto-registration
|
|
263
|
+
function App() {
|
|
264
|
+
return (
|
|
265
|
+
<AnalyticsProvider
|
|
266
|
+
client={analytics}
|
|
267
|
+
autoInitialize={true} // Auto-initialize on mount
|
|
268
|
+
registerGlobal={true} // Auto-register as global instance
|
|
269
|
+
globalName="main" // Optional: custom global name
|
|
270
|
+
>
|
|
271
|
+
<MyComponent />
|
|
272
|
+
</AnalyticsProvider>
|
|
273
|
+
);
|
|
274
|
+
}
|
|
275
|
+
|
|
276
|
+
// Use in components
|
|
277
|
+
function MyComponent() {
|
|
278
|
+
const { analytics, isReady, isInitializing, error } = useAnalytics();
|
|
279
|
+
|
|
280
|
+
// Safe usage with state checking
|
|
281
|
+
if (isInitializing) return <div>Loading analytics...</div>;
|
|
282
|
+
if (error) return <div>Analytics error: {error.message}</div>;
|
|
283
|
+
if (!isReady) return <div>Analytics not ready</div>;
|
|
284
|
+
|
|
285
|
+
const handleClick = () => {
|
|
286
|
+
analytics?.trackEvent('button_click', {
|
|
287
|
+
button_name: 'cta',
|
|
288
|
+
page: 'home',
|
|
289
|
+
});
|
|
290
|
+
};
|
|
291
|
+
|
|
292
|
+
return <button onClick={handleClick}>Track Event</button>;
|
|
293
|
+
}
|
|
294
|
+
|
|
295
|
+
// Strict mode (throws if not initialized)
|
|
296
|
+
function StrictComponent() {
|
|
297
|
+
const analytics = useAnalyticsStrict(); // Throws if not ready
|
|
298
|
+
|
|
299
|
+
const handleClick = () => {
|
|
300
|
+
analytics.trackEvent('button_click', { button_name: 'strict-button' });
|
|
301
|
+
};
|
|
302
|
+
|
|
303
|
+
return <button onClick={handleClick}>Strict Track</button>;
|
|
304
|
+
}
|
|
305
|
+
```
|
|
306
|
+
|
|
307
|
+
**Access analytics outside React components:**
|
|
308
|
+
|
|
309
|
+
```typescript
|
|
310
|
+
import { getGlobalAnalytics } from '@lobehub/analytics/react';
|
|
311
|
+
|
|
312
|
+
// In service functions, API handlers, etc.
|
|
313
|
+
export async function apiCall() {
|
|
314
|
+
try {
|
|
315
|
+
const response = await fetch('/api/data');
|
|
316
|
+
|
|
317
|
+
// Track success
|
|
318
|
+
const analytics = getGlobalAnalytics('main');
|
|
319
|
+
analytics.trackEvent('api_call', {
|
|
320
|
+
endpoint: '/api/data',
|
|
321
|
+
success: true,
|
|
322
|
+
});
|
|
323
|
+
|
|
324
|
+
return response.json();
|
|
325
|
+
} catch (error) {
|
|
326
|
+
// Track error
|
|
327
|
+
const analytics = getGlobalAnalytics('main');
|
|
328
|
+
analytics.trackEvent('api_call', {
|
|
329
|
+
endpoint: '/api/data',
|
|
330
|
+
success: false,
|
|
331
|
+
error: error.message,
|
|
332
|
+
});
|
|
333
|
+
throw error;
|
|
334
|
+
}
|
|
335
|
+
}
|
|
336
|
+
```
|
|
337
|
+
|
|
338
|
+
### SPM (Source Page Medium) Auto-Prefixing
|
|
339
|
+
|
|
340
|
+
The library automatically handles SPM prefixing with your business name:
|
|
341
|
+
|
|
342
|
+
```typescript
|
|
343
|
+
const analytics = createAnalytics({
|
|
344
|
+
business: 'my-app', // This will prefix all SPM values
|
|
345
|
+
// ... other config
|
|
346
|
+
});
|
|
347
|
+
|
|
348
|
+
// These events will have SPM auto-prefixed:
|
|
349
|
+
analytics.trackEvent('button_click', {
|
|
350
|
+
button_name: 'signup',
|
|
351
|
+
spm: 'homepage.hero', // Becomes: 'my-app.homepage.hero'
|
|
352
|
+
});
|
|
353
|
+
|
|
354
|
+
analytics.trackEvent('page_view', {
|
|
355
|
+
page: '/dashboard',
|
|
356
|
+
spm: 'dashboard.main', // Becomes: 'my-app.dashboard.main'
|
|
357
|
+
});
|
|
358
|
+
|
|
359
|
+
// If no SPM provided, uses business name as default
|
|
360
|
+
analytics.trackEvent('user_login', {
|
|
361
|
+
method: 'email',
|
|
362
|
+
// spm will be: 'my-app'
|
|
363
|
+
});
|
|
364
|
+
```
|
|
365
|
+
|
|
366
|
+
<div align="right">
|
|
367
|
+
|
|
368
|
+
[![][back-to-top]](#readme-top)
|
|
369
|
+
|
|
370
|
+
</div>
|
|
371
|
+
|
|
372
|
+
## 📖 API Reference
|
|
373
|
+
|
|
374
|
+
### Core Functions
|
|
375
|
+
|
|
376
|
+
#### `createAnalytics(config: AnalyticsConfig): AnalyticsManager`
|
|
377
|
+
|
|
378
|
+
Creates a configured analytics manager.
|
|
379
|
+
|
|
380
|
+
#### `AnalyticsManager`
|
|
381
|
+
|
|
382
|
+
Main class for managing analytics providers.
|
|
383
|
+
|
|
384
|
+
**Methods:**
|
|
385
|
+
|
|
386
|
+
- `initialize(): Promise<void>` - Initialize all providers
|
|
387
|
+
- `track(event: AnalyticsEvent): Promise<void>` - Track custom event
|
|
388
|
+
- `trackEvent<K>(eventName: K, properties): Promise<void>` - Track predefined event
|
|
389
|
+
- `identify(userId: string, properties?): Promise<void>` - Identify user
|
|
390
|
+
- `trackPageView(page: string, properties?): Promise<void>` - Track page view
|
|
391
|
+
- `reset(): Promise<void>` - Reset user identity
|
|
392
|
+
- `setGlobalContext(context: EventContext): this` - Set global context
|
|
393
|
+
- `getStatus(): { initialized: boolean; providersCount: number }` - Get status
|
|
394
|
+
|
|
395
|
+
### Global Instance Management
|
|
396
|
+
|
|
397
|
+
#### Singleton Pattern
|
|
398
|
+
|
|
399
|
+
```typescript
|
|
400
|
+
// Create singleton
|
|
401
|
+
createSingletonAnalytics(config: AnalyticsConfig): AnalyticsManager
|
|
402
|
+
|
|
403
|
+
// Get singleton
|
|
404
|
+
getSingletonAnalytics(): AnalyticsManager
|
|
405
|
+
getSingletonAnalyticsOptional(): AnalyticsManager | null
|
|
406
|
+
|
|
407
|
+
// Check singleton
|
|
408
|
+
hasSingletonAnalytics(): boolean
|
|
409
|
+
resetSingletonAnalytics(): void // For testing
|
|
410
|
+
```
|
|
411
|
+
|
|
412
|
+
#### Named Global Instances
|
|
413
|
+
|
|
414
|
+
```typescript
|
|
415
|
+
// Register/unregister
|
|
416
|
+
setGlobalAnalytics(instance: AnalyticsManager, name?: string): void
|
|
417
|
+
removeGlobalAnalytics(name?: string): boolean
|
|
418
|
+
clearGlobalAnalytics(): void
|
|
419
|
+
|
|
420
|
+
// Access
|
|
421
|
+
getGlobalAnalytics(name?: string): AnalyticsManager
|
|
422
|
+
getGlobalAnalyticsOptional(name?: string): AnalyticsManager | null
|
|
423
|
+
|
|
424
|
+
// Utilities
|
|
425
|
+
hasGlobalAnalytics(name?: string): boolean
|
|
426
|
+
getGlobalAnalyticsNames(): string[]
|
|
427
|
+
```
|
|
428
|
+
|
|
429
|
+
### React Hooks & Provider
|
|
430
|
+
|
|
431
|
+
#### `AnalyticsProvider`
|
|
432
|
+
|
|
433
|
+
```typescript
|
|
434
|
+
<AnalyticsProvider
|
|
435
|
+
client={AnalyticsManager}
|
|
436
|
+
autoInitialize?: boolean // Default: true
|
|
437
|
+
registerGlobal?: boolean // Default: true
|
|
438
|
+
globalName?: string // Default: '__default__'
|
|
439
|
+
>
|
|
440
|
+
{children}
|
|
441
|
+
</AnalyticsProvider>
|
|
442
|
+
```
|
|
443
|
+
|
|
444
|
+
#### React Hooks
|
|
445
|
+
|
|
446
|
+
```typescript
|
|
447
|
+
// Safe access with state
|
|
448
|
+
useAnalytics(): {
|
|
449
|
+
analytics: AnalyticsManager | null;
|
|
450
|
+
isReady: boolean;
|
|
451
|
+
isInitialized: boolean;
|
|
452
|
+
isInitializing: boolean;
|
|
453
|
+
error: Error | null;
|
|
454
|
+
}
|
|
455
|
+
|
|
456
|
+
// Strict access (throws if not ready)
|
|
457
|
+
useAnalyticsStrict(): AnalyticsManager
|
|
458
|
+
|
|
459
|
+
// State only
|
|
460
|
+
useAnalyticsState(): {
|
|
461
|
+
isReady: boolean;
|
|
462
|
+
isInitialized: boolean;
|
|
463
|
+
isInitializing: boolean;
|
|
464
|
+
error: Error | null;
|
|
465
|
+
}
|
|
466
|
+
|
|
467
|
+
// Optional access
|
|
468
|
+
useAnalyticsOptional(): AnalyticsManager | null
|
|
469
|
+
|
|
470
|
+
// Legacy hook (for backward compatibility)
|
|
471
|
+
useEventTracking(manager: AnalyticsManager): {
|
|
472
|
+
trackButtonClick: (buttonName: string, properties?: any) => void;
|
|
473
|
+
// ... other convenience methods
|
|
474
|
+
}
|
|
475
|
+
```
|
|
476
|
+
|
|
477
|
+
### Types
|
|
478
|
+
|
|
479
|
+
#### `AnalyticsConfig`
|
|
480
|
+
|
|
481
|
+
```typescript
|
|
482
|
+
interface AnalyticsConfig {
|
|
483
|
+
business: string; // Required: business name for SPM prefixing
|
|
484
|
+
debug?: boolean;
|
|
485
|
+
providers: {
|
|
486
|
+
posthog?: PostHogProviderAnalyticsConfig;
|
|
487
|
+
umami?: UmamiProviderAnalyticsConfig;
|
|
488
|
+
ga?: GoogleProviderAnalyticsConfig;
|
|
489
|
+
};
|
|
490
|
+
}
|
|
491
|
+
```
|
|
492
|
+
|
|
493
|
+
#### `PostHogProviderAnalyticsConfig`
|
|
494
|
+
|
|
495
|
+
```typescript
|
|
496
|
+
interface PostHogProviderAnalyticsConfig {
|
|
497
|
+
enabled: boolean;
|
|
498
|
+
key: string;
|
|
499
|
+
host?: string;
|
|
500
|
+
debug?: boolean;
|
|
501
|
+
// ... other PostHog config options
|
|
502
|
+
}
|
|
503
|
+
```
|
|
504
|
+
|
|
505
|
+
#### `PredefinedEvents`
|
|
506
|
+
|
|
507
|
+
```typescript
|
|
508
|
+
interface PredefinedEvents {
|
|
509
|
+
// UI interactions
|
|
510
|
+
button_click: {
|
|
511
|
+
button_name: string;
|
|
512
|
+
page?: string;
|
|
513
|
+
section?: string;
|
|
514
|
+
spm?: string;
|
|
515
|
+
[key: string]: any;
|
|
516
|
+
};
|
|
517
|
+
|
|
518
|
+
// User actions
|
|
519
|
+
user_signup: {
|
|
520
|
+
method: 'email' | 'oauth' | 'phone';
|
|
521
|
+
source?: string;
|
|
522
|
+
spm?: string;
|
|
523
|
+
[key: string]: any;
|
|
524
|
+
};
|
|
525
|
+
|
|
526
|
+
user_login: {
|
|
527
|
+
method: 'email' | 'oauth' | 'phone';
|
|
528
|
+
spm?: string;
|
|
529
|
+
[key: string]: any;
|
|
530
|
+
};
|
|
531
|
+
|
|
532
|
+
// Chat interactions
|
|
533
|
+
chat_message_sent: {
|
|
534
|
+
message_length: number;
|
|
535
|
+
model?: string;
|
|
536
|
+
conversation_id?: string;
|
|
537
|
+
spm?: string;
|
|
538
|
+
[key: string]: any;
|
|
539
|
+
};
|
|
540
|
+
|
|
541
|
+
// Page tracking
|
|
542
|
+
page_view: {
|
|
543
|
+
page: string;
|
|
544
|
+
referrer?: string;
|
|
545
|
+
spm?: string;
|
|
546
|
+
[key: string]: any;
|
|
547
|
+
};
|
|
548
|
+
|
|
549
|
+
// Form interactions
|
|
550
|
+
form_submit: {
|
|
551
|
+
form_name: string;
|
|
552
|
+
success: boolean;
|
|
553
|
+
spm?: string;
|
|
554
|
+
[key: string]: any;
|
|
555
|
+
};
|
|
556
|
+
}
|
|
557
|
+
```
|
|
558
|
+
|
|
559
|
+
<div align="right">
|
|
560
|
+
|
|
561
|
+
[![][back-to-top]](#readme-top)
|
|
562
|
+
|
|
563
|
+
</div>
|
|
564
|
+
|
|
565
|
+
## 🛠️ Development
|
|
6
566
|
|
|
7
567
|
```bash
|
|
8
|
-
|
|
568
|
+
# Clone the repository
|
|
569
|
+
git clone https://github.com/lobehub/@lobehub/analytics.git
|
|
570
|
+
cd @lobehub/analytics
|
|
571
|
+
|
|
572
|
+
# Install dependencies
|
|
573
|
+
npm install
|
|
574
|
+
|
|
575
|
+
# Start development
|
|
576
|
+
npm run dev
|
|
577
|
+
|
|
578
|
+
# Build the library
|
|
579
|
+
npm run build
|
|
580
|
+
|
|
581
|
+
# Run tests
|
|
582
|
+
npm test
|
|
583
|
+
|
|
584
|
+
# Run examples
|
|
585
|
+
npm run example
|
|
9
586
|
```
|
|
10
587
|
|
|
11
|
-
|
|
588
|
+
### Project Structure
|
|
589
|
+
|
|
590
|
+
```
|
|
591
|
+
@lobehub/analytics/
|
|
592
|
+
├── src/
|
|
593
|
+
│ ├── base.ts # Base analytics provider
|
|
594
|
+
│ ├── manager.ts # Analytics manager
|
|
595
|
+
│ ├── config.ts # Configuration factory
|
|
596
|
+
│ ├── global.ts # Global instance management
|
|
597
|
+
│ ├── types.ts # TypeScript definitions
|
|
598
|
+
│ ├── providers/ # Analytics providers
|
|
599
|
+
│ │ └── posthog.ts # PostHog implementation
|
|
600
|
+
│ ├── react/ # React integration
|
|
601
|
+
│ │ ├── provider.tsx # Enhanced Provider component
|
|
602
|
+
│ │ └── hooks.ts # Legacy hooks
|
|
603
|
+
│ └── index.ts # Main exports
|
|
604
|
+
├── examples/ # Usage examples
|
|
605
|
+
└── dist/ # Built files (generated)
|
|
606
|
+
```
|
|
607
|
+
|
|
608
|
+
<div align="right">
|
|
609
|
+
|
|
610
|
+
[![][back-to-top]](#readme-top)
|
|
611
|
+
|
|
612
|
+
</div>
|
|
613
|
+
|
|
614
|
+
## Examples
|
|
615
|
+
|
|
616
|
+
See the `examples/` directory for comprehensive usage examples:
|
|
617
|
+
|
|
618
|
+
- `examples/library-usage.ts` - Basic library usage and design principles
|
|
619
|
+
- `examples/enhanced-react-usage.tsx` - Advanced React integration
|
|
620
|
+
- `examples/singleton-enhanced-usage.ts` - Singleton pattern examples
|
|
621
|
+
- `examples/global-usage.ts` - Global instance management
|
|
622
|
+
- `examples/business-spm-example.ts` - SPM prefixing demonstration
|
|
623
|
+
- `examples/usage-summary.md` - Complete feature overview
|
|
624
|
+
|
|
625
|
+
## 🤝 Contributing
|
|
626
|
+
|
|
627
|
+
Contributions of all types are more than welcome, if you are interested in contributing code, feel free to check out our GitHub [Issues][github-issues-link] to get stuck in to show us what you're made of.
|
|
628
|
+
|
|
629
|
+
[![][pr-welcome-shield]][pr-welcome-link]
|
|
630
|
+
|
|
631
|
+
[![][github-contrib-shield]][github-contrib-link]
|
|
632
|
+
|
|
633
|
+
<div align="right">
|
|
634
|
+
|
|
635
|
+
[![][back-to-top]](#readme-top)
|
|
636
|
+
|
|
637
|
+
</div>
|
|
638
|
+
|
|
639
|
+
---
|
|
640
|
+
|
|
641
|
+
#### 📝 License
|
|
642
|
+
|
|
643
|
+
Copyright © 2025 [LobeHub][profile-link]. <br />
|
|
644
|
+
This project is [MIT](./LICENSE) licensed.
|
|
645
|
+
|
|
646
|
+
<!-- LINK GROUP -->
|
|
12
647
|
|
|
13
|
-
|
|
648
|
+
[back-to-top]: https://img.shields.io/badge/-BACK_TO_TOP-black?style=flat-square
|
|
649
|
+
[github-action-release-link]: https://github.com/lobehub/lobe-analytics/actions/workflows/release.yml
|
|
650
|
+
[github-action-release-shield]: https://img.shields.io/github/actions/workflow/status/lobehub/lobe-analytics/release.yml?label=release&labelColor=black&logo=githubactions&logoColor=white&style=flat-square
|
|
651
|
+
[github-action-test-link]: https://github.com/lobehub/lobe-analytics/actions/workflows/test.yml
|
|
652
|
+
[github-action-test-shield]: https://img.shields.io/github/actions/workflow/status/lobehub/lobe-analytics/test.yml?label=test&labelColor=black&logo=githubactions&logoColor=white&style=flat-square
|
|
653
|
+
[github-contrib-link]: https://github.com/lobehub/lobe-analytics/graphs/contributors
|
|
654
|
+
[github-contrib-shield]: https://contrib.rocks/image?repo=lobehub%2Flobe-analytics
|
|
655
|
+
[github-contributors-link]: https://github.com/lobehub/lobe-analytics/graphs/contributors
|
|
656
|
+
[github-contributors-shield]: https://img.shields.io/github/contributors/lobehub/lobe-analytics?color=c4f042&labelColor=black&style=flat-square
|
|
657
|
+
[github-forks-link]: https://github.com/lobehub/lobe-analytics/network/members
|
|
658
|
+
[github-forks-shield]: https://img.shields.io/github/forks/lobehub/lobe-analytics?color=8ae8ff&labelColor=black&style=flat-square
|
|
659
|
+
[github-issues-link]: https://github.com/lobehub/lobe-analytics/issues
|
|
660
|
+
[github-issues-shield]: https://img.shields.io/github/issues/lobehub/lobe-analytics?color=ff80eb&labelColor=black&style=flat-square
|
|
661
|
+
[github-license-link]: https://github.com/lobehub/lobe-analytics/blob/master/LICENSE
|
|
662
|
+
[github-license-shield]: https://img.shields.io/github/license/lobehub/lobe-analytics?color=white&labelColor=black&style=flat-square
|
|
663
|
+
[github-releasedate-link]: https://github.com/lobehub/lobe-analytics/releases
|
|
664
|
+
[github-releasedate-shield]: https://img.shields.io/github/release-date/lobehub/lobe-analytics?labelColor=black&style=flat-square
|
|
665
|
+
[github-stars-link]: https://github.com/lobehub/lobe-analytics/network/stargazers
|
|
666
|
+
[github-stars-shield]: https://img.shields.io/github/stars/lobehub/lobe-analytics?color=ffcb47&labelColor=black&style=flat-square
|
|
667
|
+
[npm-release-link]: https://www.npmjs.com/package/@lobehub/analytics
|
|
668
|
+
[npm-release-shield]: https://img.shields.io/npm/v/@lobehub/analytics?color=369eff&labelColor=black&logo=npm&logoColor=white&style=flat-square
|
|
669
|
+
[pr-welcome-link]: https://github.com/lobehub/lobe-analytics/pulls
|
|
670
|
+
[pr-welcome-shield]: https://img.shields.io/badge/%F0%9F%A4%AF%20PR%20WELCOME-%E2%86%92-ffcb47?labelColor=black&style=for-the-badge
|
|
671
|
+
[profile-link]: https://github.com/lobehub
|