@anker-in/analysis 0.4.0 → 0.4.1

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.
@@ -0,0 +1,512 @@
1
+ import * as react_jsx_runtime from 'react/jsx-runtime';
2
+ import React from 'react';
3
+
4
+ /**
5
+ * Gtag 平台 Track 类型定义
6
+ * 专注于转化事件追踪
7
+ */
8
+
9
+ /** 转化事件名称 */
10
+ type GtagEventName = "conversion" | "generate_lead" | "sign_up" | "purchase" | "add_to_cart" | "remove_from_cart" | "begin_checkout" | "view_item" | string;
11
+ /**
12
+ * Gtag 转化事件参数
13
+ * 对应 gtag('event', eventName, eventParams)
14
+ */
15
+ interface GtagTrackParams {
16
+ eventName: GtagEventName;
17
+ send_to?: string;
18
+ value?: number;
19
+ currency?: Currency;
20
+ transaction_id?: string;
21
+ transactionId?: string;
22
+ measurementId?: string;
23
+ [key: string]: any;
24
+ }
25
+ /**
26
+ * Gtag Config 配置
27
+ * 对应 gtag('config', measurementId, configParams)
28
+ */
29
+ interface GtagConfigParams {
30
+ command: "config";
31
+ measurementId: string;
32
+ configParams?: {
33
+ send_page_view?: boolean;
34
+ [key: string]: any;
35
+ };
36
+ }
37
+
38
+ /**
39
+ * Meta Pixel 平台 Track 类型定义
40
+ */
41
+ interface MetaTrackParams {
42
+ event: string;
43
+ data?: Record<string, any>;
44
+ }
45
+
46
+ /**
47
+ * Track 通用类型定义
48
+ */
49
+
50
+ /**
51
+ * 货币类型
52
+ */
53
+ type Currency = "CNY" | "USD" | "EUR" | string;
54
+ /**
55
+ * 平台参数映射
56
+ */
57
+ type PlatformTrackParamsMap = {
58
+ gtag: GtagTrackParams;
59
+ meta: MetaTrackParams;
60
+ };
61
+ /**
62
+ * Track 配置类型
63
+ */
64
+ type TrackConfig<P extends PlatformType> = {
65
+ platform: P;
66
+ } & PlatformTrackParamsMap[P];
67
+ /**
68
+ * withTracking 配置选项
69
+ */
70
+ interface WithTrackingConfig<P extends PlatformType = PlatformType> {
71
+ /**
72
+ * 埋点配置
73
+ * - 可以是静态配置对象
74
+ * - 可以是函数,接收原函数参数和返回值,动态生成配置
75
+ */
76
+ trackConfig?: TrackConfig<P> | ((args: any[], result?: any) => TrackConfig<P> | TrackConfig<P>[]);
77
+ /**
78
+ * 触发时机
79
+ * - before: 函数执行前触发(默认)
80
+ * - after: 函数执行后触发
81
+ * - both: 执行前后都触发
82
+ */
83
+ timing?: "before" | "after" | "both";
84
+ /**
85
+ * 条件判断:返回 true 才触发埋点
86
+ */
87
+ condition?: (args: any[], result?: any) => boolean;
88
+ /**
89
+ * 错误处理:函数执行失败时是否仍然上报
90
+ */
91
+ trackOnError?: boolean;
92
+ /**
93
+ * 错误时的埋点配置
94
+ */
95
+ errorTrackConfig?: (error: Error, args: any[]) => TrackConfig<P>;
96
+ /**
97
+ * 防抖时间(毫秒),避免重复上报
98
+ */
99
+ debounce?: number;
100
+ /**
101
+ * 节流时间(毫秒),限制上报频率
102
+ */
103
+ throttle?: number;
104
+ /**
105
+ * 用于 AST 解析的标识
106
+ */
107
+ trackPoint?: string;
108
+ }
109
+
110
+ /**
111
+ * TrackByTags 相关类型定义
112
+ */
113
+ /**
114
+ * TrackByTags 事件数据接口
115
+ */
116
+ interface TrackByTagsData {
117
+ [key: string]: any;
118
+ }
119
+ /**
120
+ * TrackByTags 函数参数接口
121
+ */
122
+ interface TrackByTagsParams {
123
+ tags: string[];
124
+ data?: TrackByTagsData[];
125
+ }
126
+ /**
127
+ * 解析后的标签结构(内部使用)
128
+ */
129
+ interface ParsedTag {
130
+ media: string;
131
+ event: string;
132
+ target: string | null;
133
+ }
134
+
135
+ /**
136
+ * 埋点平台类型
137
+ */
138
+ type PlatformType = "gtag" | "meta";
139
+ /**
140
+ * 埋点事件类型
141
+ */
142
+ type TrackEventType = "ads" | "pv" | "click" | "buynow" | "add2cart" | "checkout" | "purchase" | "search" | "view_item";
143
+ /**
144
+ * 平台配置基础接口
145
+ */
146
+ interface BasePlatformConfig {
147
+ pixelIds: string | Array<string>;
148
+ cookieConsentEnabled: boolean;
149
+ debug?: boolean;
150
+ strategy?: "lazyOnload" | "afterInteractive" | "beforeInteractive" | "worker";
151
+ onPixelLoaded?: () => void;
152
+ }
153
+ interface PixelsManagerConfig extends BasePlatformConfig {
154
+ type: PlatformType;
155
+ }
156
+ /**
157
+ * Gtag 配置
158
+ */
159
+ interface GtagConfig extends BasePlatformConfig {
160
+ }
161
+ /**
162
+ * Meta Pixel 配置
163
+ */
164
+ interface MetaPixelConfig extends BasePlatformConfig {
165
+ }
166
+ /**
167
+ * TikTok Pixel 配置
168
+ */
169
+ interface TikTokPixelConfig extends BasePlatformConfig {
170
+ pixelId: string;
171
+ }
172
+ /**
173
+ * Scarab 配置
174
+ */
175
+ interface ScarabConfig extends BasePlatformConfig {
176
+ customerId: string;
177
+ [key: string]: any;
178
+ }
179
+ /**
180
+ * SDK 初始化配置
181
+ */
182
+ interface AnalysisConfig {
183
+ gtag?: GtagConfig;
184
+ metaPixel?: MetaPixelConfig;
185
+ tiktokPixel?: TikTokPixelConfig;
186
+ scarab?: ScarabConfig;
187
+ debug?: boolean;
188
+ }
189
+ /**
190
+ * 埋点参数
191
+ */
192
+ interface TrackParams {
193
+ type: TrackEventType;
194
+ params?: Record<string, any>;
195
+ platform?: PlatformType[];
196
+ }
197
+ /**
198
+ * Tracker 实例
199
+ */
200
+ interface Tracker {
201
+ platform: PlatformType;
202
+ isReady: boolean;
203
+ init: (config: any) => Promise<void> | void;
204
+ track: (type: TrackEventType, params?: Record<string, any>) => void;
205
+ }
206
+ /**
207
+ * 自动埋点配置
208
+ */
209
+ interface AutoTrackConfig {
210
+ pv?: boolean;
211
+ click?: boolean;
212
+ clickSelector?: string;
213
+ }
214
+
215
+ /**
216
+ * 埋点调度器模块
217
+ * 负责根据平台类型分发事件到对应的适配器
218
+ */
219
+
220
+ declare function track(config: TrackConfig<"gtag">): void;
221
+ declare function track(config: TrackConfig<"meta">): void;
222
+
223
+ /**
224
+ * 埋点装饰器模块
225
+ * 提供函数包装器和 React Hooks,为业务函数添加埋点能力
226
+ */
227
+
228
+ declare function withTracking<T extends (...args: any[]) => any>(fn: T, config: WithTrackingConfig): T;
229
+ /**
230
+ * React Hooks 专用
231
+ *
232
+ * 为 React 组件提供埋点 Hook
233
+ *
234
+ * @example
235
+ * ```typescript
236
+ * function ProductCard({ product }) {
237
+ * const trackAddToCart = useTracking({
238
+ * trackConfig: {
239
+ * platform: "gtag",
240
+ * eventName: "add_to_cart"
241
+ * }
242
+ * });
243
+ *
244
+ * const handleClick = () => {
245
+ * trackAddToCart(() => {
246
+ * addToCart(product.id);
247
+ * });
248
+ * };
249
+ *
250
+ * return <button onClick={handleClick}>Add to Cart</button>;
251
+ * }
252
+ * ```
253
+ */
254
+ declare function useTracking(config: WithTrackingConfig): <T extends (...args: any[]) => any>(fn: T) => T;
255
+
256
+ /**
257
+ * 基于标签进行埋点上报(简化版)
258
+ *
259
+ * @param tags - 标签数组,格式: ['fbq:atc:1234567890', 'gtag:atc:AW-123456789/abc123']
260
+ * @param data - 事件数据数组,包含事件相关的额外数据
261
+ *
262
+ * @example
263
+ * ```typescript
264
+ * trackByTags({
265
+ * tags: ['fbq:atc:1234567890', 'gtag:atc:AW-123456789/abc123'],
266
+ * data: [{ event: 'custom' }]
267
+ * });
268
+ * ```
269
+ */
270
+ declare function trackByTags({ tags, data }: TrackByTagsParams): void;
271
+
272
+ declare const logger: {
273
+ setEnabled(value: boolean): void;
274
+ log(...args: any[]): void;
275
+ warn(...args: any[]): void;
276
+ error(...args: any[]): void;
277
+ };
278
+
279
+ /**
280
+ * 更新脚本的 type 属性配置
281
+ */
282
+ interface UpdateScriptTypeOptions {
283
+ /** 脚本元素的 ID */
284
+ id: string;
285
+ /** 是否启用脚本 */
286
+ enabled: boolean;
287
+ /** 是否在启用时重新执行脚本内容 */
288
+ executeOnEnable?: boolean;
289
+ /** 错误处理回调 */
290
+ onError?: (error: Error) => void;
291
+ }
292
+ /**
293
+ * 动态更新脚本的 type 属性
294
+ * 用于根据 cookie consent 状态动态启用/禁用脚本
295
+ *
296
+ * @param options - 配置选项
297
+ * @returns 是否成功更新
298
+ */
299
+ declare function updateScriptType(options: UpdateScriptTypeOptions): boolean;
300
+ /**
301
+ * 批量更新多个脚本的 type 属性
302
+ *
303
+ * @param scriptIds - 脚本 ID 数组或生成 ID 的函数
304
+ * @param enabled - 是否启用脚本
305
+ * @param executeOnEnable - 是否在启用时重新执行脚本内容
306
+ * @returns 成功更新的脚本数量
307
+ */
308
+ declare function updateScriptsType(scriptIds: string[] | ((index: number) => string), enabled: boolean, executeOnEnable?: boolean): number;
309
+
310
+ /**
311
+ * 平台检测模块
312
+ * 检测各个埋点平台的可用性
313
+ */
314
+
315
+ /**
316
+ * 检测指定平台是否可用
317
+ */
318
+ declare const isPlatformAvailable: (platform: PlatformType) => boolean;
319
+
320
+ /**
321
+ * 触发埋点上报工具
322
+ *
323
+ * 从 sessionStorage 读取 trackTags 配置,并根据 trackPoint 触发对应的埋点上报
324
+ */
325
+ /**
326
+ * TrackTags 配置类型
327
+ * key: trackPoint 标识
328
+ * value: 标签数组
329
+ */
330
+ interface TrackTagsConfig {
331
+ [trackPoint: string]: string[];
332
+ }
333
+ /**
334
+ * 埋点触发选项
335
+ */
336
+ interface TriggerTrackOptions {
337
+ tags?: Array<string>;
338
+ /** 自定义事件数据 */
339
+ eventData?: Record<string, unknown>;
340
+ /** 自定义存储键名,默认为 'trackTags' */
341
+ storageKey?: string;
342
+ /** 是否静默失败(不输出警告日志),默认为 false */
343
+ silent?: boolean;
344
+ }
345
+ /**
346
+ * 触发埋点上报
347
+ *
348
+ * 从 sessionStorage 读取 trackTags 配置,根据 trackPoint 获取对应的标签数组,
349
+ * 然后调用 trackByTags 进行埋点上报。
350
+ *
351
+ * @param trackPoint - 埋点标识,对应 trackTags 中的 key
352
+ * @param options - 可选配置项
353
+ *
354
+ * @example
355
+ * ```typescript
356
+ * // 基础用法
357
+ * triggerTrack('add_to_cart');
358
+ *
359
+ * // 带自定义数据
360
+ * triggerTrack('purchase', {
361
+ * eventData: {
362
+ * value: 99.99,
363
+ * currency: 'USD',
364
+ * transactionId: 'ORDER-123'
365
+ * }
366
+ * });
367
+ *
368
+ * // 使用自定义存储键
369
+ * triggerTrack('custom_event', {
370
+ * storageKey: 'customTrackTags'
371
+ * });
372
+ * ```
373
+ */
374
+ declare function triggerTrack(trackPoint: string, options?: TriggerTrackOptions): void;
375
+ /**
376
+ * 批量触发多个埋点
377
+ *
378
+ * @param trackPoints - 埋点标识数组
379
+ * @param options - 可选配置项(应用到所有埋点)
380
+ *
381
+ * @example
382
+ * ```typescript
383
+ * // 批量触发多个埋点
384
+ * triggerTrackBatch(['view_item', 'add_to_cart']);
385
+ *
386
+ * // 带共享的事件数据
387
+ * triggerTrackBatch(['event1', 'event2'], {
388
+ * eventData: { sessionId: '12345' }
389
+ * });
390
+ * ```
391
+ */
392
+ declare function triggerTrackBatch(trackPoints: string[], options?: TriggerTrackOptions): void;
393
+ /**
394
+ * 设置 trackTags 到 sessionStorage
395
+ *
396
+ * @param trackTags - TrackTags 配置对象
397
+ * @param storageKey - 存储键名,默认为 'trackTags'
398
+ *
399
+ * @example
400
+ * ```typescript
401
+ * setTrackTags({
402
+ * 'add_to_cart': ['fbq:atc:123456', 'gtag:atc:AW-123/abc'],
403
+ * 'purchase': ['fbq:purchase:123456', 'gtag:purchase:AW-123/xyz']
404
+ * });
405
+ * ```
406
+ */
407
+ declare function setTrackTags(trackTags: TrackTagsConfig, storageKey?: string): void;
408
+ /**
409
+ * 清除 trackTags
410
+ *
411
+ * @param storageKey - 存储键名,默认为 'trackTags'
412
+ */
413
+ declare function clearTrackTags(storageKey?: string): void;
414
+ /**
415
+ * 检查某个 trackPoint 是否存在
416
+ *
417
+ * @param trackPoint - 埋点标识
418
+ * @param storageKey - 存储键名,默认为 'trackTags'
419
+ * @returns 是否存在对应的标签配置
420
+ */
421
+ declare function hasTrackPoint(trackPoint: string, storageKey?: string): boolean;
422
+
423
+ /**
424
+ * Google Analytics / gtag.js 适配器
425
+ * 使用标准的 window.gtag() API
426
+ * 专注于转化事件追踪
427
+ */
428
+ /**
429
+ * gtag 命令类型
430
+ */
431
+ type GtagCommand = "config" | "event" | "set" | "get";
432
+ /**
433
+ * gtag 埋点函数
434
+ * 调用 window.gtag() 标准 API
435
+ */
436
+ declare const gtagTrack: (command: GtagCommand, targetOrEventName: string, params?: Record<string, any>) => void;
437
+
438
+ /**
439
+ * Meta Pixel 适配器
440
+ * 提供 Facebook/Meta Pixel 的埋点功能
441
+ */
442
+ /**
443
+ * Meta Pixel 埋点函数
444
+ */
445
+ declare const metaTrack: (event: string, data?: object) => void;
446
+
447
+ interface UseDataTrackOptions {
448
+ /**
449
+ * 埋点标签映射对象
450
+ * 键为 data-track 属性值,值为标签数组
451
+ */
452
+ trackTags?: Record<string, string[]>;
453
+ }
454
+ /**
455
+ * 自动追踪带有 data-track 属性的元素点击事件的 Hook
456
+ *
457
+ * @param options - 配置选项
458
+ * @param options.trackTags - 埋点标签映射对象,键为 data-track 属性值,值为标签数组
459
+ * @example
460
+ * ```tsx
461
+ * function MyComponent() {
462
+ * useDataTrack({
463
+ * trackTags: {
464
+ * 'add-to-cart': ['fbq:atc:1234567890', 'gtag:atc:AW-123456789/abc123'],
465
+ * 'checkout': ['fbq:checkout:1234567890']
466
+ * }
467
+ * });
468
+ *
469
+ * return (
470
+ * <button data-track="add-to-cart">Add to Cart</button>
471
+ * );
472
+ * }
473
+ * ```
474
+ */
475
+ declare function useDataTrack({ trackTags }: UseDataTrackOptions): void;
476
+
477
+ interface GtagPixelProps extends GtagConfig, React.ScriptHTMLAttributes<HTMLScriptElement> {
478
+ }
479
+ declare const GtagPixel: (props: GtagPixelProps) => react_jsx_runtime.JSX.Element;
480
+
481
+ interface MetaPixelProps extends BasePlatformConfig, React.ScriptHTMLAttributes<HTMLScriptElement> {
482
+ }
483
+ declare const MetaPixel: (props: MetaPixelProps) => react_jsx_runtime.JSX.Element;
484
+
485
+ declare const PixelsManager: React.FC<PixelsManagerConfig>;
486
+
487
+ /**
488
+ * 标签版本映射表
489
+ * 用于兼容旧版本标签,将旧事件名映射到新事件名
490
+ */
491
+ declare const TrackTagsVersionMap: {
492
+ readonly "fbq:act": "fbq:atc";
493
+ readonly "gtag:conversion": "gtag:atc";
494
+ };
495
+
496
+ declare const _default: {
497
+ track: typeof track;
498
+ trackByTags: typeof trackByTags;
499
+ withTracking: typeof withTracking;
500
+ useTracking: typeof useTracking;
501
+ logger: {
502
+ setEnabled(value: boolean): void;
503
+ log(...args: any[]): void;
504
+ warn(...args: any[]): void;
505
+ error(...args: any[]): void;
506
+ };
507
+ isPlatformAvailable: (platform: PlatformType) => boolean;
508
+ updateScriptType: typeof updateScriptType;
509
+ updateScriptsType: typeof updateScriptsType;
510
+ };
511
+
512
+ export { type AnalysisConfig, type AutoTrackConfig, type BasePlatformConfig, type Currency, type GtagConfig, type GtagConfigParams, type GtagEventName, GtagPixel, type GtagTrackParams, MetaPixel, type MetaPixelConfig, type MetaTrackParams, type ParsedTag, PixelsManager, type PixelsManagerConfig, type PlatformType, type ScarabConfig, type TikTokPixelConfig, type TrackByTagsData, type TrackByTagsParams, type TrackConfig, type TrackEventType, type TrackParams, type TrackTagsConfig, TrackTagsVersionMap, type Tracker, type TriggerTrackOptions, type UpdateScriptTypeOptions, type UseDataTrackOptions, clearTrackTags, _default as default, gtagTrack, hasTrackPoint, isPlatformAvailable, logger, metaTrack, setTrackTags, track, trackByTags, triggerTrack, triggerTrackBatch, updateScriptType, updateScriptsType, useDataTrack, useTracking, withTracking };
@@ -0,0 +1,512 @@
1
+ import * as react_jsx_runtime from 'react/jsx-runtime';
2
+ import React from 'react';
3
+
4
+ /**
5
+ * Gtag 平台 Track 类型定义
6
+ * 专注于转化事件追踪
7
+ */
8
+
9
+ /** 转化事件名称 */
10
+ type GtagEventName = "conversion" | "generate_lead" | "sign_up" | "purchase" | "add_to_cart" | "remove_from_cart" | "begin_checkout" | "view_item" | string;
11
+ /**
12
+ * Gtag 转化事件参数
13
+ * 对应 gtag('event', eventName, eventParams)
14
+ */
15
+ interface GtagTrackParams {
16
+ eventName: GtagEventName;
17
+ send_to?: string;
18
+ value?: number;
19
+ currency?: Currency;
20
+ transaction_id?: string;
21
+ transactionId?: string;
22
+ measurementId?: string;
23
+ [key: string]: any;
24
+ }
25
+ /**
26
+ * Gtag Config 配置
27
+ * 对应 gtag('config', measurementId, configParams)
28
+ */
29
+ interface GtagConfigParams {
30
+ command: "config";
31
+ measurementId: string;
32
+ configParams?: {
33
+ send_page_view?: boolean;
34
+ [key: string]: any;
35
+ };
36
+ }
37
+
38
+ /**
39
+ * Meta Pixel 平台 Track 类型定义
40
+ */
41
+ interface MetaTrackParams {
42
+ event: string;
43
+ data?: Record<string, any>;
44
+ }
45
+
46
+ /**
47
+ * Track 通用类型定义
48
+ */
49
+
50
+ /**
51
+ * 货币类型
52
+ */
53
+ type Currency = "CNY" | "USD" | "EUR" | string;
54
+ /**
55
+ * 平台参数映射
56
+ */
57
+ type PlatformTrackParamsMap = {
58
+ gtag: GtagTrackParams;
59
+ meta: MetaTrackParams;
60
+ };
61
+ /**
62
+ * Track 配置类型
63
+ */
64
+ type TrackConfig<P extends PlatformType> = {
65
+ platform: P;
66
+ } & PlatformTrackParamsMap[P];
67
+ /**
68
+ * withTracking 配置选项
69
+ */
70
+ interface WithTrackingConfig<P extends PlatformType = PlatformType> {
71
+ /**
72
+ * 埋点配置
73
+ * - 可以是静态配置对象
74
+ * - 可以是函数,接收原函数参数和返回值,动态生成配置
75
+ */
76
+ trackConfig?: TrackConfig<P> | ((args: any[], result?: any) => TrackConfig<P> | TrackConfig<P>[]);
77
+ /**
78
+ * 触发时机
79
+ * - before: 函数执行前触发(默认)
80
+ * - after: 函数执行后触发
81
+ * - both: 执行前后都触发
82
+ */
83
+ timing?: "before" | "after" | "both";
84
+ /**
85
+ * 条件判断:返回 true 才触发埋点
86
+ */
87
+ condition?: (args: any[], result?: any) => boolean;
88
+ /**
89
+ * 错误处理:函数执行失败时是否仍然上报
90
+ */
91
+ trackOnError?: boolean;
92
+ /**
93
+ * 错误时的埋点配置
94
+ */
95
+ errorTrackConfig?: (error: Error, args: any[]) => TrackConfig<P>;
96
+ /**
97
+ * 防抖时间(毫秒),避免重复上报
98
+ */
99
+ debounce?: number;
100
+ /**
101
+ * 节流时间(毫秒),限制上报频率
102
+ */
103
+ throttle?: number;
104
+ /**
105
+ * 用于 AST 解析的标识
106
+ */
107
+ trackPoint?: string;
108
+ }
109
+
110
+ /**
111
+ * TrackByTags 相关类型定义
112
+ */
113
+ /**
114
+ * TrackByTags 事件数据接口
115
+ */
116
+ interface TrackByTagsData {
117
+ [key: string]: any;
118
+ }
119
+ /**
120
+ * TrackByTags 函数参数接口
121
+ */
122
+ interface TrackByTagsParams {
123
+ tags: string[];
124
+ data?: TrackByTagsData[];
125
+ }
126
+ /**
127
+ * 解析后的标签结构(内部使用)
128
+ */
129
+ interface ParsedTag {
130
+ media: string;
131
+ event: string;
132
+ target: string | null;
133
+ }
134
+
135
+ /**
136
+ * 埋点平台类型
137
+ */
138
+ type PlatformType = "gtag" | "meta";
139
+ /**
140
+ * 埋点事件类型
141
+ */
142
+ type TrackEventType = "ads" | "pv" | "click" | "buynow" | "add2cart" | "checkout" | "purchase" | "search" | "view_item";
143
+ /**
144
+ * 平台配置基础接口
145
+ */
146
+ interface BasePlatformConfig {
147
+ pixelIds: string | Array<string>;
148
+ cookieConsentEnabled: boolean;
149
+ debug?: boolean;
150
+ strategy?: "lazyOnload" | "afterInteractive" | "beforeInteractive" | "worker";
151
+ onPixelLoaded?: () => void;
152
+ }
153
+ interface PixelsManagerConfig extends BasePlatformConfig {
154
+ type: PlatformType;
155
+ }
156
+ /**
157
+ * Gtag 配置
158
+ */
159
+ interface GtagConfig extends BasePlatformConfig {
160
+ }
161
+ /**
162
+ * Meta Pixel 配置
163
+ */
164
+ interface MetaPixelConfig extends BasePlatformConfig {
165
+ }
166
+ /**
167
+ * TikTok Pixel 配置
168
+ */
169
+ interface TikTokPixelConfig extends BasePlatformConfig {
170
+ pixelId: string;
171
+ }
172
+ /**
173
+ * Scarab 配置
174
+ */
175
+ interface ScarabConfig extends BasePlatformConfig {
176
+ customerId: string;
177
+ [key: string]: any;
178
+ }
179
+ /**
180
+ * SDK 初始化配置
181
+ */
182
+ interface AnalysisConfig {
183
+ gtag?: GtagConfig;
184
+ metaPixel?: MetaPixelConfig;
185
+ tiktokPixel?: TikTokPixelConfig;
186
+ scarab?: ScarabConfig;
187
+ debug?: boolean;
188
+ }
189
+ /**
190
+ * 埋点参数
191
+ */
192
+ interface TrackParams {
193
+ type: TrackEventType;
194
+ params?: Record<string, any>;
195
+ platform?: PlatformType[];
196
+ }
197
+ /**
198
+ * Tracker 实例
199
+ */
200
+ interface Tracker {
201
+ platform: PlatformType;
202
+ isReady: boolean;
203
+ init: (config: any) => Promise<void> | void;
204
+ track: (type: TrackEventType, params?: Record<string, any>) => void;
205
+ }
206
+ /**
207
+ * 自动埋点配置
208
+ */
209
+ interface AutoTrackConfig {
210
+ pv?: boolean;
211
+ click?: boolean;
212
+ clickSelector?: string;
213
+ }
214
+
215
+ /**
216
+ * 埋点调度器模块
217
+ * 负责根据平台类型分发事件到对应的适配器
218
+ */
219
+
220
+ declare function track(config: TrackConfig<"gtag">): void;
221
+ declare function track(config: TrackConfig<"meta">): void;
222
+
223
+ /**
224
+ * 埋点装饰器模块
225
+ * 提供函数包装器和 React Hooks,为业务函数添加埋点能力
226
+ */
227
+
228
+ declare function withTracking<T extends (...args: any[]) => any>(fn: T, config: WithTrackingConfig): T;
229
+ /**
230
+ * React Hooks 专用
231
+ *
232
+ * 为 React 组件提供埋点 Hook
233
+ *
234
+ * @example
235
+ * ```typescript
236
+ * function ProductCard({ product }) {
237
+ * const trackAddToCart = useTracking({
238
+ * trackConfig: {
239
+ * platform: "gtag",
240
+ * eventName: "add_to_cart"
241
+ * }
242
+ * });
243
+ *
244
+ * const handleClick = () => {
245
+ * trackAddToCart(() => {
246
+ * addToCart(product.id);
247
+ * });
248
+ * };
249
+ *
250
+ * return <button onClick={handleClick}>Add to Cart</button>;
251
+ * }
252
+ * ```
253
+ */
254
+ declare function useTracking(config: WithTrackingConfig): <T extends (...args: any[]) => any>(fn: T) => T;
255
+
256
+ /**
257
+ * 基于标签进行埋点上报(简化版)
258
+ *
259
+ * @param tags - 标签数组,格式: ['fbq:atc:1234567890', 'gtag:atc:AW-123456789/abc123']
260
+ * @param data - 事件数据数组,包含事件相关的额外数据
261
+ *
262
+ * @example
263
+ * ```typescript
264
+ * trackByTags({
265
+ * tags: ['fbq:atc:1234567890', 'gtag:atc:AW-123456789/abc123'],
266
+ * data: [{ event: 'custom' }]
267
+ * });
268
+ * ```
269
+ */
270
+ declare function trackByTags({ tags, data }: TrackByTagsParams): void;
271
+
272
+ declare const logger: {
273
+ setEnabled(value: boolean): void;
274
+ log(...args: any[]): void;
275
+ warn(...args: any[]): void;
276
+ error(...args: any[]): void;
277
+ };
278
+
279
+ /**
280
+ * 更新脚本的 type 属性配置
281
+ */
282
+ interface UpdateScriptTypeOptions {
283
+ /** 脚本元素的 ID */
284
+ id: string;
285
+ /** 是否启用脚本 */
286
+ enabled: boolean;
287
+ /** 是否在启用时重新执行脚本内容 */
288
+ executeOnEnable?: boolean;
289
+ /** 错误处理回调 */
290
+ onError?: (error: Error) => void;
291
+ }
292
+ /**
293
+ * 动态更新脚本的 type 属性
294
+ * 用于根据 cookie consent 状态动态启用/禁用脚本
295
+ *
296
+ * @param options - 配置选项
297
+ * @returns 是否成功更新
298
+ */
299
+ declare function updateScriptType(options: UpdateScriptTypeOptions): boolean;
300
+ /**
301
+ * 批量更新多个脚本的 type 属性
302
+ *
303
+ * @param scriptIds - 脚本 ID 数组或生成 ID 的函数
304
+ * @param enabled - 是否启用脚本
305
+ * @param executeOnEnable - 是否在启用时重新执行脚本内容
306
+ * @returns 成功更新的脚本数量
307
+ */
308
+ declare function updateScriptsType(scriptIds: string[] | ((index: number) => string), enabled: boolean, executeOnEnable?: boolean): number;
309
+
310
+ /**
311
+ * 平台检测模块
312
+ * 检测各个埋点平台的可用性
313
+ */
314
+
315
+ /**
316
+ * 检测指定平台是否可用
317
+ */
318
+ declare const isPlatformAvailable: (platform: PlatformType) => boolean;
319
+
320
+ /**
321
+ * 触发埋点上报工具
322
+ *
323
+ * 从 sessionStorage 读取 trackTags 配置,并根据 trackPoint 触发对应的埋点上报
324
+ */
325
+ /**
326
+ * TrackTags 配置类型
327
+ * key: trackPoint 标识
328
+ * value: 标签数组
329
+ */
330
+ interface TrackTagsConfig {
331
+ [trackPoint: string]: string[];
332
+ }
333
+ /**
334
+ * 埋点触发选项
335
+ */
336
+ interface TriggerTrackOptions {
337
+ tags?: Array<string>;
338
+ /** 自定义事件数据 */
339
+ eventData?: Record<string, unknown>;
340
+ /** 自定义存储键名,默认为 'trackTags' */
341
+ storageKey?: string;
342
+ /** 是否静默失败(不输出警告日志),默认为 false */
343
+ silent?: boolean;
344
+ }
345
+ /**
346
+ * 触发埋点上报
347
+ *
348
+ * 从 sessionStorage 读取 trackTags 配置,根据 trackPoint 获取对应的标签数组,
349
+ * 然后调用 trackByTags 进行埋点上报。
350
+ *
351
+ * @param trackPoint - 埋点标识,对应 trackTags 中的 key
352
+ * @param options - 可选配置项
353
+ *
354
+ * @example
355
+ * ```typescript
356
+ * // 基础用法
357
+ * triggerTrack('add_to_cart');
358
+ *
359
+ * // 带自定义数据
360
+ * triggerTrack('purchase', {
361
+ * eventData: {
362
+ * value: 99.99,
363
+ * currency: 'USD',
364
+ * transactionId: 'ORDER-123'
365
+ * }
366
+ * });
367
+ *
368
+ * // 使用自定义存储键
369
+ * triggerTrack('custom_event', {
370
+ * storageKey: 'customTrackTags'
371
+ * });
372
+ * ```
373
+ */
374
+ declare function triggerTrack(trackPoint: string, options?: TriggerTrackOptions): void;
375
+ /**
376
+ * 批量触发多个埋点
377
+ *
378
+ * @param trackPoints - 埋点标识数组
379
+ * @param options - 可选配置项(应用到所有埋点)
380
+ *
381
+ * @example
382
+ * ```typescript
383
+ * // 批量触发多个埋点
384
+ * triggerTrackBatch(['view_item', 'add_to_cart']);
385
+ *
386
+ * // 带共享的事件数据
387
+ * triggerTrackBatch(['event1', 'event2'], {
388
+ * eventData: { sessionId: '12345' }
389
+ * });
390
+ * ```
391
+ */
392
+ declare function triggerTrackBatch(trackPoints: string[], options?: TriggerTrackOptions): void;
393
+ /**
394
+ * 设置 trackTags 到 sessionStorage
395
+ *
396
+ * @param trackTags - TrackTags 配置对象
397
+ * @param storageKey - 存储键名,默认为 'trackTags'
398
+ *
399
+ * @example
400
+ * ```typescript
401
+ * setTrackTags({
402
+ * 'add_to_cart': ['fbq:atc:123456', 'gtag:atc:AW-123/abc'],
403
+ * 'purchase': ['fbq:purchase:123456', 'gtag:purchase:AW-123/xyz']
404
+ * });
405
+ * ```
406
+ */
407
+ declare function setTrackTags(trackTags: TrackTagsConfig, storageKey?: string): void;
408
+ /**
409
+ * 清除 trackTags
410
+ *
411
+ * @param storageKey - 存储键名,默认为 'trackTags'
412
+ */
413
+ declare function clearTrackTags(storageKey?: string): void;
414
+ /**
415
+ * 检查某个 trackPoint 是否存在
416
+ *
417
+ * @param trackPoint - 埋点标识
418
+ * @param storageKey - 存储键名,默认为 'trackTags'
419
+ * @returns 是否存在对应的标签配置
420
+ */
421
+ declare function hasTrackPoint(trackPoint: string, storageKey?: string): boolean;
422
+
423
+ /**
424
+ * Google Analytics / gtag.js 适配器
425
+ * 使用标准的 window.gtag() API
426
+ * 专注于转化事件追踪
427
+ */
428
+ /**
429
+ * gtag 命令类型
430
+ */
431
+ type GtagCommand = "config" | "event" | "set" | "get";
432
+ /**
433
+ * gtag 埋点函数
434
+ * 调用 window.gtag() 标准 API
435
+ */
436
+ declare const gtagTrack: (command: GtagCommand, targetOrEventName: string, params?: Record<string, any>) => void;
437
+
438
+ /**
439
+ * Meta Pixel 适配器
440
+ * 提供 Facebook/Meta Pixel 的埋点功能
441
+ */
442
+ /**
443
+ * Meta Pixel 埋点函数
444
+ */
445
+ declare const metaTrack: (event: string, data?: object) => void;
446
+
447
+ interface UseDataTrackOptions {
448
+ /**
449
+ * 埋点标签映射对象
450
+ * 键为 data-track 属性值,值为标签数组
451
+ */
452
+ trackTags?: Record<string, string[]>;
453
+ }
454
+ /**
455
+ * 自动追踪带有 data-track 属性的元素点击事件的 Hook
456
+ *
457
+ * @param options - 配置选项
458
+ * @param options.trackTags - 埋点标签映射对象,键为 data-track 属性值,值为标签数组
459
+ * @example
460
+ * ```tsx
461
+ * function MyComponent() {
462
+ * useDataTrack({
463
+ * trackTags: {
464
+ * 'add-to-cart': ['fbq:atc:1234567890', 'gtag:atc:AW-123456789/abc123'],
465
+ * 'checkout': ['fbq:checkout:1234567890']
466
+ * }
467
+ * });
468
+ *
469
+ * return (
470
+ * <button data-track="add-to-cart">Add to Cart</button>
471
+ * );
472
+ * }
473
+ * ```
474
+ */
475
+ declare function useDataTrack({ trackTags }: UseDataTrackOptions): void;
476
+
477
+ interface GtagPixelProps extends GtagConfig, React.ScriptHTMLAttributes<HTMLScriptElement> {
478
+ }
479
+ declare const GtagPixel: (props: GtagPixelProps) => react_jsx_runtime.JSX.Element;
480
+
481
+ interface MetaPixelProps extends BasePlatformConfig, React.ScriptHTMLAttributes<HTMLScriptElement> {
482
+ }
483
+ declare const MetaPixel: (props: MetaPixelProps) => react_jsx_runtime.JSX.Element;
484
+
485
+ declare const PixelsManager: React.FC<PixelsManagerConfig>;
486
+
487
+ /**
488
+ * 标签版本映射表
489
+ * 用于兼容旧版本标签,将旧事件名映射到新事件名
490
+ */
491
+ declare const TrackTagsVersionMap: {
492
+ readonly "fbq:act": "fbq:atc";
493
+ readonly "gtag:conversion": "gtag:atc";
494
+ };
495
+
496
+ declare const _default: {
497
+ track: typeof track;
498
+ trackByTags: typeof trackByTags;
499
+ withTracking: typeof withTracking;
500
+ useTracking: typeof useTracking;
501
+ logger: {
502
+ setEnabled(value: boolean): void;
503
+ log(...args: any[]): void;
504
+ warn(...args: any[]): void;
505
+ error(...args: any[]): void;
506
+ };
507
+ isPlatformAvailable: (platform: PlatformType) => boolean;
508
+ updateScriptType: typeof updateScriptType;
509
+ updateScriptsType: typeof updateScriptsType;
510
+ };
511
+
512
+ export { type AnalysisConfig, type AutoTrackConfig, type BasePlatformConfig, type Currency, type GtagConfig, type GtagConfigParams, type GtagEventName, GtagPixel, type GtagTrackParams, MetaPixel, type MetaPixelConfig, type MetaTrackParams, type ParsedTag, PixelsManager, type PixelsManagerConfig, type PlatformType, type ScarabConfig, type TikTokPixelConfig, type TrackByTagsData, type TrackByTagsParams, type TrackConfig, type TrackEventType, type TrackParams, type TrackTagsConfig, TrackTagsVersionMap, type Tracker, type TriggerTrackOptions, type UpdateScriptTypeOptions, type UseDataTrackOptions, clearTrackTags, _default as default, gtagTrack, hasTrackPoint, isPlatformAvailable, logger, metaTrack, setTrackTags, track, trackByTags, triggerTrack, triggerTrackBatch, updateScriptType, updateScriptsType, useDataTrack, useTracking, withTracking };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@anker-in/analysis",
3
- "version": "0.4.0",
3
+ "version": "0.4.1",
4
4
  "description": "Unified analytics SDK for multi-platform tracking (Google Analytics, Meta Pixel, TikTok Pixel)",
5
5
  "main": "./dist/index.cjs",
6
6
  "module": "./dist/index.js",
@@ -50,8 +50,8 @@
50
50
  "eslint": "^9.34.0",
51
51
  "tsup": "^8.5.1",
52
52
  "typescript": "5.9.2",
53
- "@repo/eslint-config": "0.0.1",
54
- "@repo/typescript-config": "0.0.1"
53
+ "@repo/typescript-config": "0.0.1",
54
+ "@repo/eslint-config": "0.0.1"
55
55
  },
56
56
  "dependencies": {
57
57
  "react": "^19.1.0",