@gctrack/protocol 0.1.0

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/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2026 yinuo
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
package/README.md ADDED
@@ -0,0 +1,26 @@
1
+ # @gctrack/protocol
2
+
3
+ 前后端共享的事件协议与 TypeScript 类型 —— `@gctrack` 全家桶的**唯一真相源**。
4
+
5
+ 导出 `EventData`、`TrackerConfig`、`TrackerPlugin`、`ApiEventData` 以及 `EventType` / `Platform` 事件与平台常量。SDK 与 collector / bff 共用,杜绝协议漂移。
6
+
7
+ ## 安装
8
+
9
+ ```bash
10
+ npm i @gctrack/protocol
11
+ ```
12
+
13
+ ## 用法
14
+
15
+ ```ts
16
+ import { EventType, Platform, type EventData } from '@gctrack/protocol'
17
+
18
+ tracker.track(EventType.JsError, { message: 'boom', stack: '...' })
19
+
20
+ // context.platform 取 Platform 常量
21
+ const platform: string = Platform.Web
22
+ ```
23
+
24
+ ## License
25
+
26
+ MIT (c) yinuo
package/dist/index.cjs ADDED
@@ -0,0 +1,73 @@
1
+ Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
2
+ //#region src/index.ts
3
+ /**
4
+ * @gctrack/protocol — 前后端共享的事件协议与类型(唯一真相源)
5
+ *
6
+ * SDK(packages/*)与后端(apps/collector、apps/bff)共用本文件,
7
+ * 杜绝 EventData 等结构在前后端漂移。
8
+ */
9
+ /** 事件类型常量(见 design.md 附录 A) */
10
+ const EventType = {
11
+ PagePerformance: "page_performance",
12
+ SlowResource: "slow_resource",
13
+ LongTask: "long_task",
14
+ FirstInputDelay: "first_input_delay",
15
+ CumulativeLayoutShift: "cumulative_layout_shift",
16
+ LargestContentfulPaint: "largest_contentful_paint",
17
+ RouteChange: "route_change",
18
+ CustomPerformance: "custom_performance",
19
+ MpFirstRender: "mp_first_render",
20
+ JsError: "js_error",
21
+ CrossOriginError: "cross_origin_error",
22
+ PromiseError: "promise_error",
23
+ ResourceError: "resource_error",
24
+ ConsoleError: "console_error",
25
+ ManualError: "manual_error",
26
+ MemoryWarning: "memory_warning",
27
+ VueError: "vue_error",
28
+ MpScriptError: "mp_script_error",
29
+ ApiPerformance: "api_performance",
30
+ ApiError: "api_error",
31
+ PageView: "page_view",
32
+ PageStay: "page_stay",
33
+ Click: "click",
34
+ ScrollDepth: "scroll_depth",
35
+ VisibilityChange: "visibility_change",
36
+ UserInactive: "user_inactive",
37
+ FormStart: "form_start",
38
+ FormSubmit: "form_submit",
39
+ FormAbandon: "form_abandon",
40
+ Search: "search",
41
+ VideoAction: "video_action",
42
+ UserRegister: "user_register",
43
+ UserLogin: "user_login",
44
+ SearchConversion: "search_conversion",
45
+ OrderFunnel: "order_funnel",
46
+ PaymentResult: "payment_result",
47
+ CouponAction: "coupon_action",
48
+ Share: "share",
49
+ Review: "review",
50
+ CustomerService: "customer_service",
51
+ AbTest: "ab_test",
52
+ FunnelStep: "funnel_step",
53
+ FunnelLeave: "funnel_leave",
54
+ FunnelComplete: "funnel_complete"
55
+ };
56
+ /** 平台标识(写入 EventData.context.platform 与 ClickHouse platform 列) */
57
+ const Platform = {
58
+ Web: "web",
59
+ H5: "h5",
60
+ WeixinMp: "wx_mp",
61
+ AlipayMp: "alipay_mp",
62
+ JdMp: "jd_mp",
63
+ XhsMp: "xhs_mp",
64
+ BaiduMp: "baidu_mp",
65
+ QqMp: "qq_mp",
66
+ ToutiaoMp: "toutiao_mp",
67
+ KuaishouMp: "kuaishou_mp"
68
+ };
69
+ //#endregion
70
+ exports.EventType = EventType;
71
+ exports.Platform = Platform;
72
+
73
+ //# sourceMappingURL=index.cjs.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.cjs","names":[],"sources":["../src/index.ts"],"sourcesContent":["/**\n * @gctrack/protocol — 前后端共享的事件协议与类型(唯一真相源)\n *\n * SDK(packages/*)与后端(apps/collector、apps/bff)共用本文件,\n * 杜绝 EventData 等结构在前后端漂移。\n */\n\n/** 事件类型常量(见 design.md 附录 A) */\nexport const EventType = {\n // 性能\n PagePerformance: 'page_performance',\n SlowResource: 'slow_resource',\n LongTask: 'long_task',\n FirstInputDelay: 'first_input_delay',\n CumulativeLayoutShift: 'cumulative_layout_shift',\n LargestContentfulPaint: 'largest_contentful_paint',\n RouteChange: 'route_change',\n CustomPerformance: 'custom_performance',\n MpFirstRender: 'mp_first_render',\n // 错误\n JsError: 'js_error',\n CrossOriginError: 'cross_origin_error',\n PromiseError: 'promise_error',\n ResourceError: 'resource_error',\n ConsoleError: 'console_error',\n ManualError: 'manual_error',\n MemoryWarning: 'memory_warning',\n VueError: 'vue_error',\n MpScriptError: 'mp_script_error',\n // 接口\n ApiPerformance: 'api_performance',\n ApiError: 'api_error',\n // 行为\n PageView: 'page_view',\n PageStay: 'page_stay',\n Click: 'click',\n ScrollDepth: 'scroll_depth',\n VisibilityChange: 'visibility_change',\n UserInactive: 'user_inactive',\n FormStart: 'form_start',\n FormSubmit: 'form_submit',\n FormAbandon: 'form_abandon',\n Search: 'search',\n VideoAction: 'video_action',\n // 业务\n UserRegister: 'user_register',\n UserLogin: 'user_login',\n SearchConversion: 'search_conversion',\n OrderFunnel: 'order_funnel',\n PaymentResult: 'payment_result',\n CouponAction: 'coupon_action',\n Share: 'share',\n Review: 'review',\n CustomerService: 'customer_service',\n AbTest: 'ab_test',\n FunnelStep: 'funnel_step',\n FunnelLeave: 'funnel_leave',\n FunnelComplete: 'funnel_complete',\n} as const\nexport type EventTypeValue = (typeof EventType)[keyof typeof EventType]\n\n/** 平台标识(写入 EventData.context.platform 与 ClickHouse platform 列) */\nexport const Platform = {\n Web: 'web',\n H5: 'h5',\n WeixinMp: 'wx_mp',\n AlipayMp: 'alipay_mp',\n JdMp: 'jd_mp',\n XhsMp: 'xhs_mp',\n BaiduMp: 'baidu_mp',\n QqMp: 'qq_mp',\n ToutiaoMp: 'toutiao_mp',\n KuaishouMp: 'kuaishou_mp',\n} as const\nexport type PlatformValue = (typeof Platform)[keyof typeof Platform]\n\n/**\n * 事件数据 —— 一条监控记录的统一结构。\n * 后端 ClickHouse monitor_event 表、SDK 上报 body 均以此为准。\n */\nexport interface EventData {\n /** 事件类型,取 EventType 常量或业务自定义字符串 */\n eventType: string\n /** 事件 payload(错误堆栈 / 性能指标 / 接口信息等),自由结构 */\n data?: Record<string, any>\n /** 事件发生时间(毫秒) */\n timestamp: number\n /** 用户 ID(可能为空) */\n userId?: string | null\n /** 会话 ID */\n sessionId: string\n /** 页面 URL */\n url?: string\n /** 页面路径 */\n pathname?: string\n /** 页面标题 */\n title?: string\n /** 来源 URL */\n referrer?: string\n /** User-Agent(小程序端拼装) */\n userAgent?: string\n /** 自定义上下文(含 appName/appVersion/platform/environment/userProperties) */\n context?: Record<string, any>\n}\n\n/** 上报鉴权方式 */\nexport type SignMode = 'hmac' | 'token' | 'none'\n\n/**\n * 追踪器配置。\n * 标注「增强」的字段为 core 通用增强新增项,详见 design.md 5.2。\n */\nexport interface TrackerConfig {\n /** 上报地址(POST 目标) */\n reportUrl: string\n /** 应用标识(增强,collector 鉴权用) */\n appKey?: string\n /** 应用名称 */\n appName?: string\n /** 应用版本 */\n appVersion?: string\n /** 平台,见 Platform */\n platform?: string\n /** 环境:production | staging | dev */\n environment?: string\n /** 批量上报条数(默认 10) */\n batchSize?: number\n /** 定时上报间隔,毫秒(默认 5000) */\n flushInterval?: number\n /** 调试模式 */\n debug?: boolean\n /** 自定义上报函数(小程序适配用,包 uni.request) */\n sendFn?: (data: EventData[]) => Promise<void>\n /** 自定义上下文 */\n context?: Record<string, any>\n /** 自动收集设备信息 */\n autoCollectDeviceInfo?: boolean\n /** 自动收集页面信息 */\n autoCollectPageInfo?: boolean\n // ===== 以下为通用增强字段(core 增强阶段实现消费) =====\n /** 签名密钥(增强) */\n appSecret?: string\n /** 采样率 0-1(增强,按 sessionId 哈希稳定采样) */\n sampleRate?: number\n /** 队列上限(增强,默认 100,超限丢最旧) */\n maxQueueSize?: number\n /** 离线持久化方式(增强) */\n persistence?: 'none' | 'local' | 'session' | 'mp'\n /** 鉴权方式(增强,小程序默认 none) */\n signMode?: SignMode\n /** 自定义脱敏函数;传 null 关闭脱敏,默认用内置 defaultSanitize */\n sanitize?: ((event: EventData) => EventData) | null\n}\n\n/**\n * 插件接口。\n * 注意:beforeSend/afterSend 当前 core 未触发,core 增强阶段补齐。\n */\nexport interface TrackerPlugin {\n name: string\n install?: (tracker: any) => void\n beforeTrack?: (eventType: string, data?: Record<string, any>) => void | false\n afterTrack?: (eventType: string, data?: Record<string, any>) => void\n /** flush 前处理(过滤/脱敏/采样),返回 false 丢弃整批 */\n beforeSend?: (events: EventData[]) => EventData[] | false\n /** flush 成功后回调 */\n afterSend?: (events: EventData[]) => void\n}\n\n/** 接口监控事件 data 字段结构(api_performance / api_error) */\nexport interface ApiEventData {\n url: string\n method: string\n status: number\n duration: number\n requestBodySize?: number\n responseBodySize?: number\n reason?: string // api_error 时的失败原因\n}\n"],"mappings":";;;;;;;;;AAQA,MAAa,YAAY;CAEvB,iBAAiB;CACjB,cAAc;CACd,UAAU;CACV,iBAAiB;CACjB,uBAAuB;CACvB,wBAAwB;CACxB,aAAa;CACb,mBAAmB;CACnB,eAAe;CAEf,SAAS;CACT,kBAAkB;CAClB,cAAc;CACd,eAAe;CACf,cAAc;CACd,aAAa;CACb,eAAe;CACf,UAAU;CACV,eAAe;CAEf,gBAAgB;CAChB,UAAU;CAEV,UAAU;CACV,UAAU;CACV,OAAO;CACP,aAAa;CACb,kBAAkB;CAClB,cAAc;CACd,WAAW;CACX,YAAY;CACZ,aAAa;CACb,QAAQ;CACR,aAAa;CAEb,cAAc;CACd,WAAW;CACX,kBAAkB;CAClB,aAAa;CACb,eAAe;CACf,cAAc;CACd,OAAO;CACP,QAAQ;CACR,iBAAiB;CACjB,QAAQ;CACR,YAAY;CACZ,aAAa;CACb,gBAAgB;CACjB;;AAID,MAAa,WAAW;CACtB,KAAK;CACL,IAAI;CACJ,UAAU;CACV,UAAU;CACV,MAAM;CACN,OAAO;CACP,SAAS;CACT,MAAM;CACN,WAAW;CACX,YAAY;CACb"}
@@ -0,0 +1,112 @@
1
+ export declare const EventType: {
2
+ readonly PagePerformance: "page_performance";
3
+ readonly SlowResource: "slow_resource";
4
+ readonly LongTask: "long_task";
5
+ readonly FirstInputDelay: "first_input_delay";
6
+ readonly CumulativeLayoutShift: "cumulative_layout_shift";
7
+ readonly LargestContentfulPaint: "largest_contentful_paint";
8
+ readonly RouteChange: "route_change";
9
+ readonly CustomPerformance: "custom_performance";
10
+ readonly MpFirstRender: "mp_first_render";
11
+ readonly JsError: "js_error";
12
+ readonly CrossOriginError: "cross_origin_error";
13
+ readonly PromiseError: "promise_error";
14
+ readonly ResourceError: "resource_error";
15
+ readonly ConsoleError: "console_error";
16
+ readonly ManualError: "manual_error";
17
+ readonly MemoryWarning: "memory_warning";
18
+ readonly VueError: "vue_error";
19
+ readonly MpScriptError: "mp_script_error";
20
+ readonly ApiPerformance: "api_performance";
21
+ readonly ApiError: "api_error";
22
+ readonly PageView: "page_view";
23
+ readonly PageStay: "page_stay";
24
+ readonly Click: "click";
25
+ readonly ScrollDepth: "scroll_depth";
26
+ readonly VisibilityChange: "visibility_change";
27
+ readonly UserInactive: "user_inactive";
28
+ readonly FormStart: "form_start";
29
+ readonly FormSubmit: "form_submit";
30
+ readonly FormAbandon: "form_abandon";
31
+ readonly Search: "search";
32
+ readonly VideoAction: "video_action";
33
+ readonly UserRegister: "user_register";
34
+ readonly UserLogin: "user_login";
35
+ readonly SearchConversion: "search_conversion";
36
+ readonly OrderFunnel: "order_funnel";
37
+ readonly PaymentResult: "payment_result";
38
+ readonly CouponAction: "coupon_action";
39
+ readonly Share: "share";
40
+ readonly Review: "review";
41
+ readonly CustomerService: "customer_service";
42
+ readonly AbTest: "ab_test";
43
+ readonly FunnelStep: "funnel_step";
44
+ readonly FunnelLeave: "funnel_leave";
45
+ readonly FunnelComplete: "funnel_complete";
46
+ };
47
+ export type EventTypeValue = (typeof EventType)[keyof typeof EventType];
48
+ export declare const Platform: {
49
+ readonly Web: "web";
50
+ readonly H5: "h5";
51
+ readonly WeixinMp: "wx_mp";
52
+ readonly AlipayMp: "alipay_mp";
53
+ readonly JdMp: "jd_mp";
54
+ readonly XhsMp: "xhs_mp";
55
+ readonly BaiduMp: "baidu_mp";
56
+ readonly QqMp: "qq_mp";
57
+ readonly ToutiaoMp: "toutiao_mp";
58
+ readonly KuaishouMp: "kuaishou_mp";
59
+ };
60
+ export type PlatformValue = (typeof Platform)[keyof typeof Platform];
61
+ export interface EventData {
62
+ eventType: string;
63
+ data?: Record<string, any>;
64
+ timestamp: number;
65
+ userId?: string | null;
66
+ sessionId: string;
67
+ url?: string;
68
+ pathname?: string;
69
+ title?: string;
70
+ referrer?: string;
71
+ userAgent?: string;
72
+ context?: Record<string, any>;
73
+ }
74
+ export type SignMode = "hmac" | "token" | "none";
75
+ export interface TrackerConfig {
76
+ reportUrl: string;
77
+ appKey?: string;
78
+ appName?: string;
79
+ appVersion?: string;
80
+ platform?: string;
81
+ environment?: string;
82
+ batchSize?: number;
83
+ flushInterval?: number;
84
+ debug?: boolean;
85
+ sendFn?: (data: EventData[]) => Promise<void>;
86
+ context?: Record<string, any>;
87
+ autoCollectDeviceInfo?: boolean;
88
+ autoCollectPageInfo?: boolean;
89
+ appSecret?: string;
90
+ sampleRate?: number;
91
+ maxQueueSize?: number;
92
+ persistence?: "none" | "local" | "session" | "mp";
93
+ signMode?: SignMode;
94
+ sanitize?: ((event: EventData) => EventData) | null;
95
+ }
96
+ export interface TrackerPlugin {
97
+ name: string;
98
+ install?: (tracker: any) => void;
99
+ beforeTrack?: (eventType: string, data?: Record<string, any>) => void | false;
100
+ afterTrack?: (eventType: string, data?: Record<string, any>) => void;
101
+ beforeSend?: (events: EventData[]) => EventData[] | false;
102
+ afterSend?: (events: EventData[]) => void;
103
+ }
104
+ export interface ApiEventData {
105
+ url: string;
106
+ method: string;
107
+ status: number;
108
+ duration: number;
109
+ requestBodySize?: number;
110
+ responseBodySize?: number;
111
+ reason?: string;
112
+ }
@@ -0,0 +1,112 @@
1
+ export declare const EventType: {
2
+ readonly PagePerformance: "page_performance";
3
+ readonly SlowResource: "slow_resource";
4
+ readonly LongTask: "long_task";
5
+ readonly FirstInputDelay: "first_input_delay";
6
+ readonly CumulativeLayoutShift: "cumulative_layout_shift";
7
+ readonly LargestContentfulPaint: "largest_contentful_paint";
8
+ readonly RouteChange: "route_change";
9
+ readonly CustomPerformance: "custom_performance";
10
+ readonly MpFirstRender: "mp_first_render";
11
+ readonly JsError: "js_error";
12
+ readonly CrossOriginError: "cross_origin_error";
13
+ readonly PromiseError: "promise_error";
14
+ readonly ResourceError: "resource_error";
15
+ readonly ConsoleError: "console_error";
16
+ readonly ManualError: "manual_error";
17
+ readonly MemoryWarning: "memory_warning";
18
+ readonly VueError: "vue_error";
19
+ readonly MpScriptError: "mp_script_error";
20
+ readonly ApiPerformance: "api_performance";
21
+ readonly ApiError: "api_error";
22
+ readonly PageView: "page_view";
23
+ readonly PageStay: "page_stay";
24
+ readonly Click: "click";
25
+ readonly ScrollDepth: "scroll_depth";
26
+ readonly VisibilityChange: "visibility_change";
27
+ readonly UserInactive: "user_inactive";
28
+ readonly FormStart: "form_start";
29
+ readonly FormSubmit: "form_submit";
30
+ readonly FormAbandon: "form_abandon";
31
+ readonly Search: "search";
32
+ readonly VideoAction: "video_action";
33
+ readonly UserRegister: "user_register";
34
+ readonly UserLogin: "user_login";
35
+ readonly SearchConversion: "search_conversion";
36
+ readonly OrderFunnel: "order_funnel";
37
+ readonly PaymentResult: "payment_result";
38
+ readonly CouponAction: "coupon_action";
39
+ readonly Share: "share";
40
+ readonly Review: "review";
41
+ readonly CustomerService: "customer_service";
42
+ readonly AbTest: "ab_test";
43
+ readonly FunnelStep: "funnel_step";
44
+ readonly FunnelLeave: "funnel_leave";
45
+ readonly FunnelComplete: "funnel_complete";
46
+ };
47
+ export type EventTypeValue = (typeof EventType)[keyof typeof EventType];
48
+ export declare const Platform: {
49
+ readonly Web: "web";
50
+ readonly H5: "h5";
51
+ readonly WeixinMp: "wx_mp";
52
+ readonly AlipayMp: "alipay_mp";
53
+ readonly JdMp: "jd_mp";
54
+ readonly XhsMp: "xhs_mp";
55
+ readonly BaiduMp: "baidu_mp";
56
+ readonly QqMp: "qq_mp";
57
+ readonly ToutiaoMp: "toutiao_mp";
58
+ readonly KuaishouMp: "kuaishou_mp";
59
+ };
60
+ export type PlatformValue = (typeof Platform)[keyof typeof Platform];
61
+ export interface EventData {
62
+ eventType: string;
63
+ data?: Record<string, any>;
64
+ timestamp: number;
65
+ userId?: string | null;
66
+ sessionId: string;
67
+ url?: string;
68
+ pathname?: string;
69
+ title?: string;
70
+ referrer?: string;
71
+ userAgent?: string;
72
+ context?: Record<string, any>;
73
+ }
74
+ export type SignMode = "hmac" | "token" | "none";
75
+ export interface TrackerConfig {
76
+ reportUrl: string;
77
+ appKey?: string;
78
+ appName?: string;
79
+ appVersion?: string;
80
+ platform?: string;
81
+ environment?: string;
82
+ batchSize?: number;
83
+ flushInterval?: number;
84
+ debug?: boolean;
85
+ sendFn?: (data: EventData[]) => Promise<void>;
86
+ context?: Record<string, any>;
87
+ autoCollectDeviceInfo?: boolean;
88
+ autoCollectPageInfo?: boolean;
89
+ appSecret?: string;
90
+ sampleRate?: number;
91
+ maxQueueSize?: number;
92
+ persistence?: "none" | "local" | "session" | "mp";
93
+ signMode?: SignMode;
94
+ sanitize?: ((event: EventData) => EventData) | null;
95
+ }
96
+ export interface TrackerPlugin {
97
+ name: string;
98
+ install?: (tracker: any) => void;
99
+ beforeTrack?: (eventType: string, data?: Record<string, any>) => void | false;
100
+ afterTrack?: (eventType: string, data?: Record<string, any>) => void;
101
+ beforeSend?: (events: EventData[]) => EventData[] | false;
102
+ afterSend?: (events: EventData[]) => void;
103
+ }
104
+ export interface ApiEventData {
105
+ url: string;
106
+ method: string;
107
+ status: number;
108
+ duration: number;
109
+ requestBodySize?: number;
110
+ responseBodySize?: number;
111
+ reason?: string;
112
+ }
package/dist/index.js ADDED
@@ -0,0 +1,72 @@
1
+ import "node:module";
2
+ //#region src/index.ts
3
+ /**
4
+ * @gctrack/protocol — 前后端共享的事件协议与类型(唯一真相源)
5
+ *
6
+ * SDK(packages/*)与后端(apps/collector、apps/bff)共用本文件,
7
+ * 杜绝 EventData 等结构在前后端漂移。
8
+ */
9
+ /** 事件类型常量(见 design.md 附录 A) */
10
+ const EventType = {
11
+ PagePerformance: "page_performance",
12
+ SlowResource: "slow_resource",
13
+ LongTask: "long_task",
14
+ FirstInputDelay: "first_input_delay",
15
+ CumulativeLayoutShift: "cumulative_layout_shift",
16
+ LargestContentfulPaint: "largest_contentful_paint",
17
+ RouteChange: "route_change",
18
+ CustomPerformance: "custom_performance",
19
+ MpFirstRender: "mp_first_render",
20
+ JsError: "js_error",
21
+ CrossOriginError: "cross_origin_error",
22
+ PromiseError: "promise_error",
23
+ ResourceError: "resource_error",
24
+ ConsoleError: "console_error",
25
+ ManualError: "manual_error",
26
+ MemoryWarning: "memory_warning",
27
+ VueError: "vue_error",
28
+ MpScriptError: "mp_script_error",
29
+ ApiPerformance: "api_performance",
30
+ ApiError: "api_error",
31
+ PageView: "page_view",
32
+ PageStay: "page_stay",
33
+ Click: "click",
34
+ ScrollDepth: "scroll_depth",
35
+ VisibilityChange: "visibility_change",
36
+ UserInactive: "user_inactive",
37
+ FormStart: "form_start",
38
+ FormSubmit: "form_submit",
39
+ FormAbandon: "form_abandon",
40
+ Search: "search",
41
+ VideoAction: "video_action",
42
+ UserRegister: "user_register",
43
+ UserLogin: "user_login",
44
+ SearchConversion: "search_conversion",
45
+ OrderFunnel: "order_funnel",
46
+ PaymentResult: "payment_result",
47
+ CouponAction: "coupon_action",
48
+ Share: "share",
49
+ Review: "review",
50
+ CustomerService: "customer_service",
51
+ AbTest: "ab_test",
52
+ FunnelStep: "funnel_step",
53
+ FunnelLeave: "funnel_leave",
54
+ FunnelComplete: "funnel_complete"
55
+ };
56
+ /** 平台标识(写入 EventData.context.platform 与 ClickHouse platform 列) */
57
+ const Platform = {
58
+ Web: "web",
59
+ H5: "h5",
60
+ WeixinMp: "wx_mp",
61
+ AlipayMp: "alipay_mp",
62
+ JdMp: "jd_mp",
63
+ XhsMp: "xhs_mp",
64
+ BaiduMp: "baidu_mp",
65
+ QqMp: "qq_mp",
66
+ ToutiaoMp: "toutiao_mp",
67
+ KuaishouMp: "kuaishou_mp"
68
+ };
69
+ //#endregion
70
+ export { EventType, Platform };
71
+
72
+ //# sourceMappingURL=index.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.js","names":[],"sources":["../src/index.ts"],"sourcesContent":["/**\n * @gctrack/protocol — 前后端共享的事件协议与类型(唯一真相源)\n *\n * SDK(packages/*)与后端(apps/collector、apps/bff)共用本文件,\n * 杜绝 EventData 等结构在前后端漂移。\n */\n\n/** 事件类型常量(见 design.md 附录 A) */\nexport const EventType = {\n // 性能\n PagePerformance: 'page_performance',\n SlowResource: 'slow_resource',\n LongTask: 'long_task',\n FirstInputDelay: 'first_input_delay',\n CumulativeLayoutShift: 'cumulative_layout_shift',\n LargestContentfulPaint: 'largest_contentful_paint',\n RouteChange: 'route_change',\n CustomPerformance: 'custom_performance',\n MpFirstRender: 'mp_first_render',\n // 错误\n JsError: 'js_error',\n CrossOriginError: 'cross_origin_error',\n PromiseError: 'promise_error',\n ResourceError: 'resource_error',\n ConsoleError: 'console_error',\n ManualError: 'manual_error',\n MemoryWarning: 'memory_warning',\n VueError: 'vue_error',\n MpScriptError: 'mp_script_error',\n // 接口\n ApiPerformance: 'api_performance',\n ApiError: 'api_error',\n // 行为\n PageView: 'page_view',\n PageStay: 'page_stay',\n Click: 'click',\n ScrollDepth: 'scroll_depth',\n VisibilityChange: 'visibility_change',\n UserInactive: 'user_inactive',\n FormStart: 'form_start',\n FormSubmit: 'form_submit',\n FormAbandon: 'form_abandon',\n Search: 'search',\n VideoAction: 'video_action',\n // 业务\n UserRegister: 'user_register',\n UserLogin: 'user_login',\n SearchConversion: 'search_conversion',\n OrderFunnel: 'order_funnel',\n PaymentResult: 'payment_result',\n CouponAction: 'coupon_action',\n Share: 'share',\n Review: 'review',\n CustomerService: 'customer_service',\n AbTest: 'ab_test',\n FunnelStep: 'funnel_step',\n FunnelLeave: 'funnel_leave',\n FunnelComplete: 'funnel_complete',\n} as const\nexport type EventTypeValue = (typeof EventType)[keyof typeof EventType]\n\n/** 平台标识(写入 EventData.context.platform 与 ClickHouse platform 列) */\nexport const Platform = {\n Web: 'web',\n H5: 'h5',\n WeixinMp: 'wx_mp',\n AlipayMp: 'alipay_mp',\n JdMp: 'jd_mp',\n XhsMp: 'xhs_mp',\n BaiduMp: 'baidu_mp',\n QqMp: 'qq_mp',\n ToutiaoMp: 'toutiao_mp',\n KuaishouMp: 'kuaishou_mp',\n} as const\nexport type PlatformValue = (typeof Platform)[keyof typeof Platform]\n\n/**\n * 事件数据 —— 一条监控记录的统一结构。\n * 后端 ClickHouse monitor_event 表、SDK 上报 body 均以此为准。\n */\nexport interface EventData {\n /** 事件类型,取 EventType 常量或业务自定义字符串 */\n eventType: string\n /** 事件 payload(错误堆栈 / 性能指标 / 接口信息等),自由结构 */\n data?: Record<string, any>\n /** 事件发生时间(毫秒) */\n timestamp: number\n /** 用户 ID(可能为空) */\n userId?: string | null\n /** 会话 ID */\n sessionId: string\n /** 页面 URL */\n url?: string\n /** 页面路径 */\n pathname?: string\n /** 页面标题 */\n title?: string\n /** 来源 URL */\n referrer?: string\n /** User-Agent(小程序端拼装) */\n userAgent?: string\n /** 自定义上下文(含 appName/appVersion/platform/environment/userProperties) */\n context?: Record<string, any>\n}\n\n/** 上报鉴权方式 */\nexport type SignMode = 'hmac' | 'token' | 'none'\n\n/**\n * 追踪器配置。\n * 标注「增强」的字段为 core 通用增强新增项,详见 design.md 5.2。\n */\nexport interface TrackerConfig {\n /** 上报地址(POST 目标) */\n reportUrl: string\n /** 应用标识(增强,collector 鉴权用) */\n appKey?: string\n /** 应用名称 */\n appName?: string\n /** 应用版本 */\n appVersion?: string\n /** 平台,见 Platform */\n platform?: string\n /** 环境:production | staging | dev */\n environment?: string\n /** 批量上报条数(默认 10) */\n batchSize?: number\n /** 定时上报间隔,毫秒(默认 5000) */\n flushInterval?: number\n /** 调试模式 */\n debug?: boolean\n /** 自定义上报函数(小程序适配用,包 uni.request) */\n sendFn?: (data: EventData[]) => Promise<void>\n /** 自定义上下文 */\n context?: Record<string, any>\n /** 自动收集设备信息 */\n autoCollectDeviceInfo?: boolean\n /** 自动收集页面信息 */\n autoCollectPageInfo?: boolean\n // ===== 以下为通用增强字段(core 增强阶段实现消费) =====\n /** 签名密钥(增强) */\n appSecret?: string\n /** 采样率 0-1(增强,按 sessionId 哈希稳定采样) */\n sampleRate?: number\n /** 队列上限(增强,默认 100,超限丢最旧) */\n maxQueueSize?: number\n /** 离线持久化方式(增强) */\n persistence?: 'none' | 'local' | 'session' | 'mp'\n /** 鉴权方式(增强,小程序默认 none) */\n signMode?: SignMode\n /** 自定义脱敏函数;传 null 关闭脱敏,默认用内置 defaultSanitize */\n sanitize?: ((event: EventData) => EventData) | null\n}\n\n/**\n * 插件接口。\n * 注意:beforeSend/afterSend 当前 core 未触发,core 增强阶段补齐。\n */\nexport interface TrackerPlugin {\n name: string\n install?: (tracker: any) => void\n beforeTrack?: (eventType: string, data?: Record<string, any>) => void | false\n afterTrack?: (eventType: string, data?: Record<string, any>) => void\n /** flush 前处理(过滤/脱敏/采样),返回 false 丢弃整批 */\n beforeSend?: (events: EventData[]) => EventData[] | false\n /** flush 成功后回调 */\n afterSend?: (events: EventData[]) => void\n}\n\n/** 接口监控事件 data 字段结构(api_performance / api_error) */\nexport interface ApiEventData {\n url: string\n method: string\n status: number\n duration: number\n requestBodySize?: number\n responseBodySize?: number\n reason?: string // api_error 时的失败原因\n}\n"],"mappings":";;;;;;;;;AAQA,MAAa,YAAY;CAEvB,iBAAiB;CACjB,cAAc;CACd,UAAU;CACV,iBAAiB;CACjB,uBAAuB;CACvB,wBAAwB;CACxB,aAAa;CACb,mBAAmB;CACnB,eAAe;CAEf,SAAS;CACT,kBAAkB;CAClB,cAAc;CACd,eAAe;CACf,cAAc;CACd,aAAa;CACb,eAAe;CACf,UAAU;CACV,eAAe;CAEf,gBAAgB;CAChB,UAAU;CAEV,UAAU;CACV,UAAU;CACV,OAAO;CACP,aAAa;CACb,kBAAkB;CAClB,cAAc;CACd,WAAW;CACX,YAAY;CACZ,aAAa;CACb,QAAQ;CACR,aAAa;CAEb,cAAc;CACd,WAAW;CACX,kBAAkB;CAClB,aAAa;CACb,eAAe;CACf,cAAc;CACd,OAAO;CACP,QAAQ;CACR,iBAAiB;CACjB,QAAQ;CACR,YAAY;CACZ,aAAa;CACb,gBAAgB;CACjB;;AAID,MAAa,WAAW;CACtB,KAAK;CACL,IAAI;CACJ,UAAU;CACV,UAAU;CACV,MAAM;CACN,OAAO;CACP,SAAS;CACT,MAAM;CACN,WAAW;CACX,YAAY;CACb"}
package/package.json ADDED
@@ -0,0 +1,28 @@
1
+ {
2
+ "name": "@gctrack/protocol",
3
+ "version": "0.1.0",
4
+ "description": "前后端共享的事件协议与类型定义(唯一真相源)",
5
+ "type": "module",
6
+ "main": "./dist/index.cjs",
7
+ "module": "./dist/index.js",
8
+ "types": "./dist/index.d.ts",
9
+ "exports": {
10
+ ".": {
11
+ "types": "./dist/index.d.ts",
12
+ "import": "./dist/index.js",
13
+ "require": "./dist/index.cjs"
14
+ }
15
+ },
16
+ "files": [
17
+ "dist"
18
+ ],
19
+ "publishConfig": {
20
+ "access": "public"
21
+ },
22
+ "author": "yinuo",
23
+ "license": "MIT",
24
+ "scripts": {
25
+ "build": "tsdown",
26
+ "dev": "tsdown --watch"
27
+ }
28
+ }