@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
|
@@ -0,0 +1,587 @@
|
|
|
1
|
+
import { PostHog, PostHogOptions } from 'posthog-node';
|
|
2
|
+
import { posthog, PostHogConfig } from 'posthog-js';
|
|
3
|
+
|
|
4
|
+
interface XAdsFunction {
|
|
5
|
+
(...args: unknown[]): void;
|
|
6
|
+
exe?: (...args: unknown[]) => void;
|
|
7
|
+
queue?: unknown[][];
|
|
8
|
+
version?: string;
|
|
9
|
+
}
|
|
10
|
+
interface XAdsGlobalState {
|
|
11
|
+
configuredPixelIds: Set<string>;
|
|
12
|
+
scriptRequested: boolean;
|
|
13
|
+
}
|
|
14
|
+
declare global {
|
|
15
|
+
interface Window {
|
|
16
|
+
__LOBE_ANALYTICS_X_ADS_STATE__?: XAdsGlobalState;
|
|
17
|
+
twq?: XAdsFunction;
|
|
18
|
+
}
|
|
19
|
+
}
|
|
20
|
+
/**
|
|
21
|
+
* X Ads Analytics Provider
|
|
22
|
+
* Uses X Pixel (twq + uwt.js) for client-side conversion tracking
|
|
23
|
+
*/
|
|
24
|
+
declare class XAdsAnalyticsProvider extends BaseAnalytics {
|
|
25
|
+
private readonly config;
|
|
26
|
+
private initialized;
|
|
27
|
+
constructor(config: XAdsProviderAnalyticsConfig, business: string);
|
|
28
|
+
getProviderName(): string;
|
|
29
|
+
initialize(): Promise<void>;
|
|
30
|
+
track(event: AnalyticsEvent): Promise<void>;
|
|
31
|
+
identify(): Promise<void>;
|
|
32
|
+
trackPageView(): Promise<void>;
|
|
33
|
+
reset(): Promise<void>;
|
|
34
|
+
private ensureScript;
|
|
35
|
+
private ensureTwq;
|
|
36
|
+
private getGlobalState;
|
|
37
|
+
private getEventId;
|
|
38
|
+
private getEventParameters;
|
|
39
|
+
private mapPurchaseEventParameters;
|
|
40
|
+
private mapContents;
|
|
41
|
+
private toFiniteNumber;
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
/**
|
|
45
|
+
* PostHog Node.js Analytics Provider
|
|
46
|
+
* Uses official posthog-node SDK for server-side tracking
|
|
47
|
+
*/
|
|
48
|
+
declare class PostHogNodeAnalyticsProvider extends BaseAnalytics {
|
|
49
|
+
private readonly config;
|
|
50
|
+
private client;
|
|
51
|
+
private initialized;
|
|
52
|
+
constructor(config: PostHogNodeProviderAnalyticsConfig, business: string);
|
|
53
|
+
getProviderName(): string;
|
|
54
|
+
initialize(): Promise<void>;
|
|
55
|
+
track(event: AnalyticsEvent): Promise<void>;
|
|
56
|
+
identify(userId: string, properties?: Record<string, any>): Promise<void>;
|
|
57
|
+
trackPageView(page: string, properties?: Record<string, any>): Promise<void>;
|
|
58
|
+
reset(): Promise<void>;
|
|
59
|
+
/**
|
|
60
|
+
* Check if feature flag is enabled for a user
|
|
61
|
+
*/
|
|
62
|
+
isFeatureEnabled(flag: string, distinctId: string, groups?: Record<string, any>): Promise<boolean>;
|
|
63
|
+
/**
|
|
64
|
+
* Get feature flag payload for a user
|
|
65
|
+
*/
|
|
66
|
+
getFeatureFlag(flag: string, distinctId: string, groups?: Record<string, any>): Promise<any>;
|
|
67
|
+
/**
|
|
68
|
+
* Get all feature flags for a user
|
|
69
|
+
*/
|
|
70
|
+
getAllFlags(distinctId: string, groups?: Record<string, any>): Promise<Record<string, any>>;
|
|
71
|
+
/**
|
|
72
|
+
* Flush pending events
|
|
73
|
+
*/
|
|
74
|
+
flush(): Promise<void>;
|
|
75
|
+
/**
|
|
76
|
+
* Shutdown the client and flush remaining events
|
|
77
|
+
*/
|
|
78
|
+
shutdown(): Promise<void>;
|
|
79
|
+
/**
|
|
80
|
+
* Get the native PostHog Node instance for direct access to PostHog APIs
|
|
81
|
+
*/
|
|
82
|
+
getNativeInstance(): PostHog | null;
|
|
83
|
+
/**
|
|
84
|
+
* Group identify - associate user with a group
|
|
85
|
+
*/
|
|
86
|
+
groupIdentify(groupType: string, groupKey: string, properties?: Record<string, any>): Promise<void>;
|
|
87
|
+
/**
|
|
88
|
+
* Create alias between user IDs
|
|
89
|
+
*/
|
|
90
|
+
alias(distinctId: string, alias: string): Promise<void>;
|
|
91
|
+
/**
|
|
92
|
+
* Update the business context dynamically
|
|
93
|
+
* This will affect all future events
|
|
94
|
+
*/
|
|
95
|
+
updateBusiness(newBusiness: string): void;
|
|
96
|
+
/**
|
|
97
|
+
* Get current business context
|
|
98
|
+
*/
|
|
99
|
+
getCurrentBusiness(): string;
|
|
100
|
+
}
|
|
101
|
+
|
|
102
|
+
/**
|
|
103
|
+
* PostHog Analytics Provider
|
|
104
|
+
* Uses official posthog-js SDK
|
|
105
|
+
*/
|
|
106
|
+
declare class PostHogAnalyticsProvider extends BaseAnalytics {
|
|
107
|
+
private readonly config;
|
|
108
|
+
private initialized;
|
|
109
|
+
constructor(config: PostHogProviderAnalyticsConfig, business: string);
|
|
110
|
+
getProviderName(): string;
|
|
111
|
+
initialize(): Promise<void>;
|
|
112
|
+
track(event: AnalyticsEvent): Promise<void>;
|
|
113
|
+
identify(userId: string, properties?: Record<string, any>): Promise<void>;
|
|
114
|
+
trackPageView(page: string, properties?: Record<string, any>): Promise<void>;
|
|
115
|
+
reset(): Promise<void>;
|
|
116
|
+
/**
|
|
117
|
+
* Check if feature flag is enabled
|
|
118
|
+
*/
|
|
119
|
+
isFeatureEnabled(flag: string): boolean;
|
|
120
|
+
/**
|
|
121
|
+
* Get the native PostHog instance for direct access to PostHog APIs
|
|
122
|
+
*
|
|
123
|
+
* Note: When using the native instance directly, events will still include
|
|
124
|
+
* the business spm prefix because it's registered as a global property.
|
|
125
|
+
*
|
|
126
|
+
* @returns PostHog native instance or null if not initialized
|
|
127
|
+
*
|
|
128
|
+
* @example
|
|
129
|
+
* ```typescript
|
|
130
|
+
* const analytics = createAnalytics({ business: 'myapp', ... });
|
|
131
|
+
* const posthogProvider = analytics.getProvider('posthog');
|
|
132
|
+
* const posthog = posthogProvider.getNativeInstance();
|
|
133
|
+
*
|
|
134
|
+
* // These calls will automatically include spm: 'myapp'
|
|
135
|
+
* posthog?.capture('custom_event', { custom: 'data' });
|
|
136
|
+
* posthog?.isFeatureEnabled('new_feature');
|
|
137
|
+
* posthog?.group('company', 'company_123');
|
|
138
|
+
* ```
|
|
139
|
+
*/
|
|
140
|
+
getNativeInstance(): typeof posthog | null;
|
|
141
|
+
/**
|
|
142
|
+
* Create a before_send handler that adds business context to all events
|
|
143
|
+
* This ensures both wrapper calls and direct PostHog calls include business information
|
|
144
|
+
*/
|
|
145
|
+
private createBeforeSendHandler;
|
|
146
|
+
/**
|
|
147
|
+
* Update the business context dynamically
|
|
148
|
+
* This will affect all future events
|
|
149
|
+
*/
|
|
150
|
+
updateBusiness(newBusiness: string): void;
|
|
151
|
+
/**
|
|
152
|
+
* Get current business context
|
|
153
|
+
*/
|
|
154
|
+
getCurrentBusiness(): string;
|
|
155
|
+
}
|
|
156
|
+
|
|
157
|
+
/**
|
|
158
|
+
* Google Analytics 4 Provider
|
|
159
|
+
* Uses gtag.js library for client-side tracking
|
|
160
|
+
*/
|
|
161
|
+
|
|
162
|
+
/**
|
|
163
|
+
* Google Analytics 4 Analytics Provider
|
|
164
|
+
* Uses gtag.js for tracking events, page views, and user identification
|
|
165
|
+
*/
|
|
166
|
+
declare class GoogleAnalyticsProvider extends BaseAnalytics {
|
|
167
|
+
private readonly config;
|
|
168
|
+
private initialized;
|
|
169
|
+
constructor(config: GoogleAnalyticsProviderConfig, business: string);
|
|
170
|
+
getProviderName(): string;
|
|
171
|
+
initialize(): Promise<void>;
|
|
172
|
+
track(event: AnalyticsEvent): Promise<void>;
|
|
173
|
+
identify(userId: string, properties?: Record<string, any>): Promise<void>;
|
|
174
|
+
trackPageView(page: string, properties?: Record<string, any>): Promise<void>;
|
|
175
|
+
reset(): Promise<void>;
|
|
176
|
+
/**
|
|
177
|
+
* Check if feature flag is enabled
|
|
178
|
+
* Note: GA4 doesn't have built-in feature flags like PostHog,
|
|
179
|
+
* so this always returns false
|
|
180
|
+
*/
|
|
181
|
+
isFeatureEnabled(flag: string): boolean;
|
|
182
|
+
/**
|
|
183
|
+
* Get the native gtag function for direct access to GA4 APIs
|
|
184
|
+
*
|
|
185
|
+
* Note: When using the native gtag function directly, events will NOT automatically
|
|
186
|
+
* include the business and spm properties. You need to add them manually if desired.
|
|
187
|
+
*
|
|
188
|
+
* @returns gtag function or null if not initialized
|
|
189
|
+
*
|
|
190
|
+
* @example
|
|
191
|
+
* ```typescript
|
|
192
|
+
* const analytics = createAnalytics({ business: 'myapp', ... });
|
|
193
|
+
* const ga4Provider = analytics.getProvider('ga4');
|
|
194
|
+
* const gtag = ga4Provider.getNativeInstance();
|
|
195
|
+
*
|
|
196
|
+
* // Manual business context addition required for direct calls
|
|
197
|
+
* gtag?.('event', 'custom_event', {
|
|
198
|
+
* custom: 'data',
|
|
199
|
+
* business: 'myapp',
|
|
200
|
+
* spm: 'myapp.custom_section'
|
|
201
|
+
* });
|
|
202
|
+
* ```
|
|
203
|
+
*/
|
|
204
|
+
getNativeInstance(): ((...args: any[]) => void) | null;
|
|
205
|
+
/**
|
|
206
|
+
* Get current measurement ID
|
|
207
|
+
*/
|
|
208
|
+
getMeasurementId(): string;
|
|
209
|
+
/**
|
|
210
|
+
* Get current business context
|
|
211
|
+
*/
|
|
212
|
+
getCurrentBusiness(): string;
|
|
213
|
+
}
|
|
214
|
+
|
|
215
|
+
interface AnalyticsEvent {
|
|
216
|
+
/** Anonymous ID */
|
|
217
|
+
anonymousId?: string;
|
|
218
|
+
/** Event name, recommend using category:action format */
|
|
219
|
+
name: string;
|
|
220
|
+
/** Event properties */
|
|
221
|
+
properties?: Record<string, any>;
|
|
222
|
+
/** Event timestamp */
|
|
223
|
+
timestamp?: Date;
|
|
224
|
+
/** User ID */
|
|
225
|
+
userId?: string;
|
|
226
|
+
}
|
|
227
|
+
type Platform = 'web' | 'ios' | 'android' | 'desktop';
|
|
228
|
+
interface EventContext {
|
|
229
|
+
[key: string]: any;
|
|
230
|
+
page?: string;
|
|
231
|
+
section?: string;
|
|
232
|
+
session_id?: string;
|
|
233
|
+
user_properties?: Record<string, any>;
|
|
234
|
+
}
|
|
235
|
+
interface PredefinedEvents {
|
|
236
|
+
button_click: {
|
|
237
|
+
[key: string]: any;
|
|
238
|
+
button_name: string;
|
|
239
|
+
spm?: string;
|
|
240
|
+
};
|
|
241
|
+
page_view: {
|
|
242
|
+
[key: string]: any;
|
|
243
|
+
page: string;
|
|
244
|
+
spm?: string;
|
|
245
|
+
};
|
|
246
|
+
user_login: {
|
|
247
|
+
[key: string]: any;
|
|
248
|
+
spm?: string;
|
|
249
|
+
};
|
|
250
|
+
user_signup: {
|
|
251
|
+
[key: string]: any;
|
|
252
|
+
spm?: string;
|
|
253
|
+
};
|
|
254
|
+
}
|
|
255
|
+
interface ProviderConfig {
|
|
256
|
+
debug?: boolean;
|
|
257
|
+
enabled: boolean;
|
|
258
|
+
}
|
|
259
|
+
interface PostHogProviderAnalyticsConfig extends Partial<Omit<PostHogConfig, 'debug'>>, ProviderConfig {
|
|
260
|
+
host?: string;
|
|
261
|
+
key: string;
|
|
262
|
+
}
|
|
263
|
+
interface PostHogNodeProviderAnalyticsConfig extends Partial<PostHogOptions>, ProviderConfig {
|
|
264
|
+
key: string;
|
|
265
|
+
}
|
|
266
|
+
interface UmamiProviderAnalyticsConfig extends ProviderConfig {
|
|
267
|
+
scriptUrl?: string;
|
|
268
|
+
websiteId: string;
|
|
269
|
+
}
|
|
270
|
+
interface GoogleAnalyticsProviderConfig extends ProviderConfig {
|
|
271
|
+
gtagConfig?: {
|
|
272
|
+
[key: string]: any;
|
|
273
|
+
debug_mode?: boolean;
|
|
274
|
+
};
|
|
275
|
+
measurementId: string;
|
|
276
|
+
}
|
|
277
|
+
interface XAdsEventIdMap {
|
|
278
|
+
[eventName: string]: string | undefined;
|
|
279
|
+
}
|
|
280
|
+
interface XAdsProviderAnalyticsConfig extends ProviderConfig {
|
|
281
|
+
eventIds?: XAdsEventIdMap;
|
|
282
|
+
pixelId: string;
|
|
283
|
+
purchaseEventId?: string;
|
|
284
|
+
}
|
|
285
|
+
interface AnalyticsConfig {
|
|
286
|
+
business: string;
|
|
287
|
+
debug?: boolean;
|
|
288
|
+
providers: {
|
|
289
|
+
ga4?: GoogleAnalyticsProviderConfig;
|
|
290
|
+
posthog?: PostHogProviderAnalyticsConfig;
|
|
291
|
+
posthogNode?: PostHogNodeProviderAnalyticsConfig;
|
|
292
|
+
umami?: UmamiProviderAnalyticsConfig;
|
|
293
|
+
xAds?: XAdsProviderAnalyticsConfig;
|
|
294
|
+
};
|
|
295
|
+
}
|
|
296
|
+
interface ProviderTypeMap {
|
|
297
|
+
ga4: GoogleAnalyticsProvider;
|
|
298
|
+
posthog: PostHogAnalyticsProvider;
|
|
299
|
+
posthogNode: PostHogNodeAnalyticsProvider;
|
|
300
|
+
xAds: XAdsAnalyticsProvider;
|
|
301
|
+
}
|
|
302
|
+
|
|
303
|
+
/**
|
|
304
|
+
* Base abstract class for analytics providers
|
|
305
|
+
* All analytics implementations should extend this class
|
|
306
|
+
*/
|
|
307
|
+
declare abstract class BaseAnalytics {
|
|
308
|
+
protected readonly debug: boolean;
|
|
309
|
+
protected readonly enabled: boolean;
|
|
310
|
+
protected readonly business: string;
|
|
311
|
+
constructor(config: {
|
|
312
|
+
business: string;
|
|
313
|
+
debug?: boolean;
|
|
314
|
+
enabled?: boolean;
|
|
315
|
+
});
|
|
316
|
+
/**
|
|
317
|
+
* Initialize the analytics provider
|
|
318
|
+
*/
|
|
319
|
+
abstract initialize(): Promise<void>;
|
|
320
|
+
/**
|
|
321
|
+
* Track a single event
|
|
322
|
+
*/
|
|
323
|
+
abstract track(event: AnalyticsEvent): Promise<void>;
|
|
324
|
+
/**
|
|
325
|
+
* Identify a user
|
|
326
|
+
*/
|
|
327
|
+
abstract identify(userId: string, properties?: Record<string, any>): Promise<void>;
|
|
328
|
+
/**
|
|
329
|
+
* Track page view
|
|
330
|
+
*/
|
|
331
|
+
abstract trackPageView(page: string, properties?: Record<string, any>): Promise<void>;
|
|
332
|
+
/**
|
|
333
|
+
* Reset user identity
|
|
334
|
+
*/
|
|
335
|
+
abstract reset(): Promise<void>;
|
|
336
|
+
/**
|
|
337
|
+
* Get provider name for logging
|
|
338
|
+
*/
|
|
339
|
+
abstract getProviderName(): string;
|
|
340
|
+
/**
|
|
341
|
+
* Check if provider is enabled
|
|
342
|
+
*/
|
|
343
|
+
protected isEnabled(): boolean;
|
|
344
|
+
/**
|
|
345
|
+
* Validate event data
|
|
346
|
+
*/
|
|
347
|
+
protected validateEvent(event: AnalyticsEvent): boolean;
|
|
348
|
+
/**
|
|
349
|
+
* Log debug message
|
|
350
|
+
*/
|
|
351
|
+
protected log(message: string, data?: any): void;
|
|
352
|
+
/**
|
|
353
|
+
* Log error message
|
|
354
|
+
*/
|
|
355
|
+
protected logError(message: string, error?: any): void;
|
|
356
|
+
/**
|
|
357
|
+
* 丰富属性数据,自动处理 spm 前缀
|
|
358
|
+
* 确保无论通过哪种方式调用都会添加 business 前缀
|
|
359
|
+
*/
|
|
360
|
+
protected enrichProperties(properties?: Record<string, any>): Record<string, any>;
|
|
361
|
+
}
|
|
362
|
+
|
|
363
|
+
/**
|
|
364
|
+
* Analytics 管理器
|
|
365
|
+
* 统一管理多个分析工具提供商
|
|
366
|
+
*/
|
|
367
|
+
declare class AnalyticsManager {
|
|
368
|
+
private readonly providers;
|
|
369
|
+
private readonly business;
|
|
370
|
+
private globalContext;
|
|
371
|
+
private initialized;
|
|
372
|
+
private readonly debug;
|
|
373
|
+
constructor(business: string, debug?: boolean);
|
|
374
|
+
/**
|
|
375
|
+
* 注册分析工具提供商
|
|
376
|
+
*/
|
|
377
|
+
registerProvider(name: string, provider: BaseAnalytics): this;
|
|
378
|
+
/**
|
|
379
|
+
* 移除分析工具提供商
|
|
380
|
+
*/
|
|
381
|
+
unregisterProvider(name: string): this;
|
|
382
|
+
/**
|
|
383
|
+
* 获取指定的提供商
|
|
384
|
+
*/
|
|
385
|
+
getProvider<T extends keyof ProviderTypeMap>(name: T): ProviderTypeMap[T] | undefined;
|
|
386
|
+
getProvider(name: string): BaseAnalytics | undefined;
|
|
387
|
+
/**
|
|
388
|
+
* 获取所有提供商
|
|
389
|
+
*/
|
|
390
|
+
getAllProviders(): BaseAnalytics[];
|
|
391
|
+
/**
|
|
392
|
+
* 初始化所有提供商
|
|
393
|
+
*/
|
|
394
|
+
initialize(): Promise<void>;
|
|
395
|
+
/**
|
|
396
|
+
* 追踪事件到所有提供商
|
|
397
|
+
*/
|
|
398
|
+
track(event: AnalyticsEvent): Promise<void>;
|
|
399
|
+
/**
|
|
400
|
+
* 类型安全的事件追踪
|
|
401
|
+
*/
|
|
402
|
+
trackEvent<K extends keyof PredefinedEvents>(eventName: K, properties: PredefinedEvents[K]): Promise<void>;
|
|
403
|
+
/**
|
|
404
|
+
* 识别用户
|
|
405
|
+
*/
|
|
406
|
+
identify(userId: string, properties?: Record<string, any>): Promise<void>;
|
|
407
|
+
/**
|
|
408
|
+
* 追踪页面浏览
|
|
409
|
+
*/
|
|
410
|
+
trackPageView(page: string, properties?: Record<string, any>): Promise<void>;
|
|
411
|
+
/**
|
|
412
|
+
* 重置用户身份
|
|
413
|
+
*/
|
|
414
|
+
reset(): Promise<void>;
|
|
415
|
+
/**
|
|
416
|
+
* 设置全局上下文
|
|
417
|
+
*/
|
|
418
|
+
setGlobalContext(context: EventContext): this;
|
|
419
|
+
/**
|
|
420
|
+
* 获取全局上下文
|
|
421
|
+
*/
|
|
422
|
+
getGlobalContext(): EventContext;
|
|
423
|
+
/**
|
|
424
|
+
* 获取管理器状态
|
|
425
|
+
*/
|
|
426
|
+
getStatus(): {
|
|
427
|
+
initialized: boolean;
|
|
428
|
+
providersCount: number;
|
|
429
|
+
};
|
|
430
|
+
/**
|
|
431
|
+
* 检查是否已初始化
|
|
432
|
+
*/
|
|
433
|
+
private ensureInitialized;
|
|
434
|
+
/**
|
|
435
|
+
* 在所有提供商上执行操作
|
|
436
|
+
*/
|
|
437
|
+
private executeOnAllProviders;
|
|
438
|
+
/**
|
|
439
|
+
* 丰富事件数据
|
|
440
|
+
*/
|
|
441
|
+
private enrichEvent;
|
|
442
|
+
/**
|
|
443
|
+
* 记录日志
|
|
444
|
+
*/
|
|
445
|
+
private log;
|
|
446
|
+
}
|
|
447
|
+
|
|
448
|
+
/**
|
|
449
|
+
* 扩展 globalThis 类型
|
|
450
|
+
*/
|
|
451
|
+
declare global {
|
|
452
|
+
var __LOBE_ANALYTICS_GLOBAL_STATE__: GlobalAnalyticsState | undefined;
|
|
453
|
+
}
|
|
454
|
+
/**
|
|
455
|
+
* 全局状态接口
|
|
456
|
+
*/
|
|
457
|
+
interface GlobalAnalyticsState {
|
|
458
|
+
instances: Map<string, AnalyticsManager>;
|
|
459
|
+
singletonConfig: AnalyticsConfig | null;
|
|
460
|
+
singletonInstance: AnalyticsManager | null;
|
|
461
|
+
}
|
|
462
|
+
/**
|
|
463
|
+
* 注册全局 Analytics 实例
|
|
464
|
+
*
|
|
465
|
+
* @param instance Analytics 管理器实例
|
|
466
|
+
* @param name 实例名称,默认为 '__default__'
|
|
467
|
+
*
|
|
468
|
+
* @example
|
|
469
|
+
* ```typescript
|
|
470
|
+
* const analytics = createAnalytics({ ... });
|
|
471
|
+
* setGlobalAnalytics(analytics);
|
|
472
|
+
* ```
|
|
473
|
+
*/
|
|
474
|
+
declare function setGlobalAnalytics(instance: AnalyticsManager, name?: string): void;
|
|
475
|
+
/**
|
|
476
|
+
* 获取全局 Analytics 实例
|
|
477
|
+
*
|
|
478
|
+
* @param name 实例名称,默认为 '__default__'
|
|
479
|
+
* @returns Analytics 管理器实例
|
|
480
|
+
* @throws {Error} 如果实例不存在
|
|
481
|
+
*
|
|
482
|
+
* @example
|
|
483
|
+
* ```typescript
|
|
484
|
+
* // 在任何文件中使用
|
|
485
|
+
* const analytics = getGlobalAnalytics();
|
|
486
|
+
* analytics.track({ name: 'some_event' });
|
|
487
|
+
* ```
|
|
488
|
+
*/
|
|
489
|
+
declare function getGlobalAnalytics(name?: string): AnalyticsManager;
|
|
490
|
+
/**
|
|
491
|
+
* 获取全局 Analytics 实例(可选,不抛出错误)
|
|
492
|
+
*
|
|
493
|
+
* @param name 实例名称,默认为 '__default__'
|
|
494
|
+
* @returns Analytics 管理器实例或 null
|
|
495
|
+
*
|
|
496
|
+
* @example
|
|
497
|
+
* ```typescript
|
|
498
|
+
* const analytics = getGlobalAnalyticsOptional();
|
|
499
|
+
* analytics?.track({ name: 'optional_event' });
|
|
500
|
+
* ```
|
|
501
|
+
*/
|
|
502
|
+
declare function getGlobalAnalyticsOptional(name?: string): AnalyticsManager | null;
|
|
503
|
+
/**
|
|
504
|
+
* 检查全局实例是否存在
|
|
505
|
+
*
|
|
506
|
+
* @param name 实例名称,默认为 '__default__'
|
|
507
|
+
* @returns 是否存在
|
|
508
|
+
*/
|
|
509
|
+
declare function hasGlobalAnalytics(name?: string): boolean;
|
|
510
|
+
/**
|
|
511
|
+
* 移除全局 Analytics 实例
|
|
512
|
+
*
|
|
513
|
+
* @param name 实例名称,默认为 '__default__'
|
|
514
|
+
* @returns 是否成功移除
|
|
515
|
+
*/
|
|
516
|
+
declare function removeGlobalAnalytics(name?: string): boolean;
|
|
517
|
+
/**
|
|
518
|
+
* 清除所有全局实例
|
|
519
|
+
*/
|
|
520
|
+
declare function clearGlobalAnalytics(): void;
|
|
521
|
+
/**
|
|
522
|
+
* 获取所有已注册的实例名称
|
|
523
|
+
*
|
|
524
|
+
* @returns 实例名称数组
|
|
525
|
+
*/
|
|
526
|
+
declare function getGlobalAnalyticsNames(): string[];
|
|
527
|
+
/**
|
|
528
|
+
* 创建或获取单例 Analytics 实例
|
|
529
|
+
*
|
|
530
|
+
* 单例模式适用于整个应用只需要一个 Analytics 配置的场景。
|
|
531
|
+
* 如果已存在单例且配置相同,返回现有实例;否则创建新实例。
|
|
532
|
+
*
|
|
533
|
+
* @param config Analytics 配置
|
|
534
|
+
* @returns Analytics 管理器实例
|
|
535
|
+
*
|
|
536
|
+
* @example
|
|
537
|
+
* ```typescript
|
|
538
|
+
* // 在应用启动时创建
|
|
539
|
+
* const analytics = createSingletonAnalytics({
|
|
540
|
+
* debug: true,
|
|
541
|
+
* providers: {
|
|
542
|
+
* posthog: {
|
|
543
|
+
* enabled: true,
|
|
544
|
+
* key: 'your_key',
|
|
545
|
+
* host: 'https://app.posthog.com',
|
|
546
|
+
* },
|
|
547
|
+
* },
|
|
548
|
+
* });
|
|
549
|
+
*
|
|
550
|
+
* // 在其他地方获取同一实例
|
|
551
|
+
* const sameAnalytics = getSingletonAnalytics();
|
|
552
|
+
* ```
|
|
553
|
+
*/
|
|
554
|
+
declare function createSingletonAnalytics(config: AnalyticsConfig): AnalyticsManager;
|
|
555
|
+
/**
|
|
556
|
+
* 获取单例 Analytics 实例
|
|
557
|
+
*
|
|
558
|
+
* @returns Analytics 管理器实例
|
|
559
|
+
* @throws {Error} 如果单例未创建
|
|
560
|
+
*
|
|
561
|
+
* @example
|
|
562
|
+
* ```typescript
|
|
563
|
+
* const analytics = getSingletonAnalytics();
|
|
564
|
+
* analytics.track({ name: 'some_event' });
|
|
565
|
+
* ```
|
|
566
|
+
*/
|
|
567
|
+
declare function getSingletonAnalytics(): AnalyticsManager;
|
|
568
|
+
/**
|
|
569
|
+
* 获取单例 Analytics 实例(可选,不抛出错误)
|
|
570
|
+
*
|
|
571
|
+
* @returns Analytics 管理器实例或 null
|
|
572
|
+
*/
|
|
573
|
+
declare function getSingletonAnalyticsOptional(): AnalyticsManager | null;
|
|
574
|
+
/**
|
|
575
|
+
* 检查单例是否已创建
|
|
576
|
+
*
|
|
577
|
+
* @returns 是否存在单例
|
|
578
|
+
*/
|
|
579
|
+
declare function hasSingletonAnalytics(): boolean;
|
|
580
|
+
/**
|
|
581
|
+
* 重置单例实例
|
|
582
|
+
*
|
|
583
|
+
* 主要用于测试场景
|
|
584
|
+
*/
|
|
585
|
+
declare function resetSingletonAnalytics(): void;
|
|
586
|
+
|
|
587
|
+
export { type AnalyticsConfig as A, BaseAnalytics as B, type EventContext as E, GoogleAnalyticsProvider as G, PostHogAnalyticsProvider as P, type UmamiProviderAnalyticsConfig as U, XAdsAnalyticsProvider as X, AnalyticsManager as a, type AnalyticsEvent as b, type GoogleAnalyticsProviderConfig as c, type PostHogNodeProviderAnalyticsConfig as d, type PostHogProviderAnalyticsConfig as e, type PredefinedEvents as f, type ProviderConfig as g, type XAdsEventIdMap as h, type XAdsProviderAnalyticsConfig as i, clearGlobalAnalytics as j, createSingletonAnalytics as k, getGlobalAnalytics as l, getGlobalAnalyticsNames as m, getGlobalAnalyticsOptional as n, getSingletonAnalytics as o, getSingletonAnalyticsOptional as p, hasGlobalAnalytics as q, hasSingletonAnalytics as r, removeGlobalAnalytics as s, resetSingletonAnalytics as t, setGlobalAnalytics as u, type Platform as v, PostHogNodeAnalyticsProvider as w, type ProviderTypeMap as x };
|
package/dist/index.d.mts
ADDED
|
@@ -0,0 +1,38 @@
|
|
|
1
|
+
import { A as AnalyticsConfig, a as AnalyticsManager } from './global-CLXWNSdY.mjs';
|
|
2
|
+
export { b as AnalyticsEvent, B as BaseAnalytics, E as EventContext, G as GoogleAnalyticsProvider, c as GoogleAnalyticsProviderConfig, P as PostHogAnalyticsProvider, d as PostHogNodeProviderAnalyticsConfig, e as PostHogProviderAnalyticsConfig, f as PredefinedEvents, g as ProviderConfig, U as UmamiProviderAnalyticsConfig, X as XAdsAnalyticsProvider, h as XAdsEventIdMap, i as XAdsProviderAnalyticsConfig, j as clearGlobalAnalytics, k as createSingletonAnalytics, l as getGlobalAnalytics, m as getGlobalAnalyticsNames, n as getGlobalAnalyticsOptional, o as getSingletonAnalytics, p as getSingletonAnalyticsOptional, q as hasGlobalAnalytics, r as hasSingletonAnalytics, s as removeGlobalAnalytics, t as resetSingletonAnalytics, u as setGlobalAnalytics } from './global-CLXWNSdY.mjs';
|
|
3
|
+
import 'posthog-node';
|
|
4
|
+
import 'posthog-js';
|
|
5
|
+
|
|
6
|
+
/**
|
|
7
|
+
* Create a configured Analytics manager with providers
|
|
8
|
+
*
|
|
9
|
+
* @param config Analytics configuration
|
|
10
|
+
* @returns Configured AnalyticsManager instance
|
|
11
|
+
*
|
|
12
|
+
* @example
|
|
13
|
+
* ```typescript
|
|
14
|
+
* const analytics = createAnalytics({
|
|
15
|
+
* business: 'myapp',
|
|
16
|
+
* debug: true,
|
|
17
|
+
* providers: {
|
|
18
|
+
* posthog: {
|
|
19
|
+
* enabled: true,
|
|
20
|
+
* key: 'phc_your_key',
|
|
21
|
+
* host: 'https://app.posthog.com',
|
|
22
|
+
* },
|
|
23
|
+
* ga4: {
|
|
24
|
+
* enabled: true,
|
|
25
|
+
* measurementId: 'G-XXXXXXXXXX',
|
|
26
|
+
* gtagConfig: {
|
|
27
|
+
* debug_mode: true,
|
|
28
|
+
* },
|
|
29
|
+
* },
|
|
30
|
+
* },
|
|
31
|
+
* });
|
|
32
|
+
*
|
|
33
|
+
* await analytics.initialize();
|
|
34
|
+
* ```
|
|
35
|
+
*/
|
|
36
|
+
declare function createAnalytics(config: AnalyticsConfig): AnalyticsManager;
|
|
37
|
+
|
|
38
|
+
export { AnalyticsConfig, AnalyticsManager, createAnalytics, AnalyticsManager as default };
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,38 @@
|
|
|
1
|
+
import { A as AnalyticsConfig, a as AnalyticsManager } from './global-CLXWNSdY.js';
|
|
2
|
+
export { b as AnalyticsEvent, B as BaseAnalytics, E as EventContext, G as GoogleAnalyticsProvider, c as GoogleAnalyticsProviderConfig, P as PostHogAnalyticsProvider, d as PostHogNodeProviderAnalyticsConfig, e as PostHogProviderAnalyticsConfig, f as PredefinedEvents, g as ProviderConfig, U as UmamiProviderAnalyticsConfig, X as XAdsAnalyticsProvider, h as XAdsEventIdMap, i as XAdsProviderAnalyticsConfig, j as clearGlobalAnalytics, k as createSingletonAnalytics, l as getGlobalAnalytics, m as getGlobalAnalyticsNames, n as getGlobalAnalyticsOptional, o as getSingletonAnalytics, p as getSingletonAnalyticsOptional, q as hasGlobalAnalytics, r as hasSingletonAnalytics, s as removeGlobalAnalytics, t as resetSingletonAnalytics, u as setGlobalAnalytics } from './global-CLXWNSdY.js';
|
|
3
|
+
import 'posthog-node';
|
|
4
|
+
import 'posthog-js';
|
|
5
|
+
|
|
6
|
+
/**
|
|
7
|
+
* Create a configured Analytics manager with providers
|
|
8
|
+
*
|
|
9
|
+
* @param config Analytics configuration
|
|
10
|
+
* @returns Configured AnalyticsManager instance
|
|
11
|
+
*
|
|
12
|
+
* @example
|
|
13
|
+
* ```typescript
|
|
14
|
+
* const analytics = createAnalytics({
|
|
15
|
+
* business: 'myapp',
|
|
16
|
+
* debug: true,
|
|
17
|
+
* providers: {
|
|
18
|
+
* posthog: {
|
|
19
|
+
* enabled: true,
|
|
20
|
+
* key: 'phc_your_key',
|
|
21
|
+
* host: 'https://app.posthog.com',
|
|
22
|
+
* },
|
|
23
|
+
* ga4: {
|
|
24
|
+
* enabled: true,
|
|
25
|
+
* measurementId: 'G-XXXXXXXXXX',
|
|
26
|
+
* gtagConfig: {
|
|
27
|
+
* debug_mode: true,
|
|
28
|
+
* },
|
|
29
|
+
* },
|
|
30
|
+
* },
|
|
31
|
+
* });
|
|
32
|
+
*
|
|
33
|
+
* await analytics.initialize();
|
|
34
|
+
* ```
|
|
35
|
+
*/
|
|
36
|
+
declare function createAnalytics(config: AnalyticsConfig): AnalyticsManager;
|
|
37
|
+
|
|
38
|
+
export { AnalyticsConfig, AnalyticsManager, createAnalytics, AnalyticsManager as default };
|