cwj_monitoring 0.0.22 → 0.0.24

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 CHANGED
@@ -1,14 +1,31 @@
1
- # 🚀 前端监控 SDK DEMO
1
+ # 🚀 CWJ 前端监控 SDK
2
2
 
3
- > ⚠️ **注意**:这是一个简易的前端监控 SDK DEMO,仅供学习使用,请勿在生产环境中使用!
3
+ > 现代化、轻量级的前端监控 SDK,适用于 Web 应用。轻松追踪用户行为、性能指标和错误信息。
4
4
 
5
- ## 🌟 功能概览
5
+ [![npm version](https://img.shields.io/npm/v/cwj_monitoring)](https://www.npmjs.com/package/cwj_monitoring)
6
+ [![TypeScript](https://img.shields.io/badge/TypeScript-5.1-blue)](https://www.typescriptlang.org/)
7
+ [![License: MIT](https://img.shields.io/badge/License-MIT-yellow.svg)](https://opensource.org/licenses/MIT)
6
8
 
7
- | 功能类别 | 具体功能 | 状态 |
8
- |----------------|--------------------------------------------------------------------------|------|
9
- | **行为监控** | 🔍 点击监控、🔄 页面跳转监控、⏱️ 页面停留时间监控 | ✅ |
10
- | **错误监控** | ❌ JS错误、🖼️ 资源加载错误、💻 console.error错误、🤞 Promise未捕获错误 | ✅ |
11
- | **性能监控** | DCL、FP、FCP、LCP、Load、FPS 等性能指标 | ✅ |
9
+ ## 功能特性
10
+
11
+ ### 📊 全面监控
12
+
13
+ - **🔍 行为追踪** - 点击事件、页面导航、用户旅程
14
+ - **⚡ 性能指标** - Core Web Vitals (LCP, FID, CLS, FCP, TTFB)、页面加载时间
15
+ - **❌ 错误追踪** - JavaScript 错误、资源加载失败、Promise 拒绝、控制台错误
16
+ - **🛣️ 路由监控** - SPA 导航追踪(Hash 和 History 模式)
17
+
18
+ ### 🎯 开发者友好
19
+
20
+ - **TypeScript 优先** - 完整的类型安全
21
+ - **插件架构** - 模块化和可扩展设计,支持上下文注入和高度配置化
22
+ - **自定义全局变量** - 避免命名冲突
23
+
24
+ ### 🔒 安全与性能
25
+
26
+ - **批量与重试** - 优化的数据传输,自动重试机制
27
+
28
+ ---
12
29
 
13
30
  ## 📦 安装
14
31
 
@@ -16,36 +33,185 @@
16
33
  npm install cwj_monitoring
17
34
  ```
18
35
 
19
- ## 🛠️ 使用指南
36
+ ## 🚀 快速开始
20
37
 
21
- 1.基础用法
38
+ ### 基础用法
22
39
 
23
- ```js
24
- import { init, TYPES } from 'cwj_monitoring';
40
+ ```typescript
41
+ import { createMonitor, ErrorPlugin, PerformancePlugin, XHRPlugin, FetchPlugin } from 'cwj_monitoring';
25
42
 
26
- init({
27
- url: 'http://localhost:8080', // 🔴 必填 - 数据上报地址
28
- max: 10, // 🔵 可选 - 最大缓存数(默认5)
29
- time: 60000, // 🔵 可选 - 最大缓存时间(默认30s)
30
- plugin: [ // 🔵 可选 - 要启用的插件,不传plugin则启动所有插件
31
- TYPES.ERROR, // 错误监控
32
- TYPES.CLICK, // 点击监控
33
- TYPES.PERFORMANCE, // 性能监控
34
- TYPES.ROUTER // 路由监控
35
- ],
36
- data: { // 🔵 可选 - 自定义元数据
37
- vs: '0.1.1', // 版本号
38
- env: 'production' // 环境标识
39
- }
43
+ const monitor = createMonitor({
44
+ url: 'https://your-api.com/collect', // 必填:数据收集接口
45
+ data: {
46
+ appVersion: '1.2.3',
47
+ environment: 'production',
48
+ },
40
49
  });
50
+
51
+ monitor.use(ErrorPlugin()).use(PerformancePlugin()).use(XHRPlugin()).use(FetchPlugin()).run();
52
+ ```
53
+
54
+ ---
55
+
56
+ ## ⚙️ 配置
57
+
58
+ **配置项:**
59
+
60
+ | 属性 | 类型 | 必填 | 默认值 | 描述 |
61
+ | :---------- | :-------------------- | :--- | :----------- | :---------------------------- |
62
+ | `url` | `string` | ✅ | - | 数据收集的后端 URL |
63
+ | `data` | `Record<string, any>` | ❌ | `{}` | 附加到所有事件的自定义元数据 |
64
+ | `transport` | `TransportConfig` | ❌ | 见下文 | 数据传输设置 |
65
+ | `globalKey` | `string` | ❌ | `$track` | 挂载在 window 上的全局变量名 |
66
+ | `uuidKey` | `string` | ❌ | `track_uuid` | 存储 UUID 的 localStorage key |
67
+
68
+ **TransportConfig:**
69
+
70
+ ```typescript
71
+ interface TransportConfig {
72
+ maxBatchSize?: number; // 默认:累积 5 个事件后发送
73
+ maxWaitTime?: number; // 默认:30000ms,或 30 秒后发送
74
+ }
41
75
  ```
42
- 2.手动上报错误
43
- ```js
44
- // 适用于Vue/React等框架的错误捕获
45
- window.$track.emit('ERROR_TYPE', {
46
- message: '自定义错误信息',
47
- // 其他错误详情...
76
+
77
+ ---
78
+
79
+ ## 🔌 插件
80
+
81
+ 所有插件都支持在构造函数中传入 `filter` 函数来过滤不需要记录的事件。
82
+
83
+ ### 错误插件 (`ErrorPlugin`)
84
+
85
+ 捕获 JS 错误、资源加载失败、Promise 拒绝和 console.error。支持通过 `filter` 过滤特定错误。
86
+
87
+ ### 性能插件 (`PerformancePlugin`)
88
+
89
+ 追踪核心性能指标和页面加载体验:
90
+
91
+ - **FP (First Paint)**: 首次绘制时间
92
+ - **FCP (First Contentful Paint)**: 首次内容绘制时间
93
+ - **LCP (Largest Contentful Paint)**: 最大内容绘制时间
94
+ - **INP (Interaction to Next Paint)**: 交互到下一次绘制的延迟(关注交互响应性)
95
+ - **Long Task**: 超过阈值的长任务(关注主线程阻塞)
96
+ - **Resource**: 仅监听 `fetch` 与 `xmlhttprequest` 的网络请求耗时
97
+
98
+ **配置项:**
99
+
100
+ - `longTaskThreshold`: 长任务阈值 (ms),默认 `100`
101
+ - `resourceThreshold`: 资源加载阈值 (ms),默认 `1000`
102
+ - `inpThreshold`: INP 阈值 (ms),默认 `200`
103
+ - `filter`: 过滤函数,支持按类型过滤指标
104
+
105
+ 示例:
106
+
107
+ ```typescript
108
+ PerformancePlugin({
109
+ longTaskThreshold: 200, // 仅记录超过 200ms 的长任务
110
+ resourceThreshold: 2000, // 仅记录超过 2s 的请求
48
111
  });
49
112
  ```
50
- ## 🤝 参与贡献
51
- 欢迎提交Issue或PR!🎉
113
+
114
+ ### 行为插件 (`BehaviorPlugin`)
115
+
116
+ 监控用户点击交互。支持通过 `filter` 过滤特定元素,支持通过 `throttleDelay` 设置点击节流时间(默认 500ms)。
117
+
118
+ ### 路由插件 (`PVPlugin`)
119
+
120
+ 追踪单页应用的页面跳转。支持通过 `filter` 过滤特定路由。
121
+
122
+ ### XHR 插件 (`XHRPlugin`)
123
+
124
+ 监控 XMLHttpRequest 请求详情,仅记录失败的请求(状态码非 2xx)。支持通过 `filter` 根据 URL 或方法过滤请求。
125
+
126
+ ### Fetch 插件 (`FetchPlugin`)
127
+
128
+ 监控 fetch 请求详情,仅记录失败的请求(状态码非 2xx 或网络错误)。支持通过 `filter` 根据 URL 或方法过滤请求。
129
+
130
+ ---
131
+
132
+ ## 🎯 进阶用法
133
+
134
+ ### 自定义全局变量名
135
+
136
+ ```typescript
137
+ import { createMonitor } from 'cwj_monitoring';
138
+
139
+ createMonitor({
140
+ url: '...',
141
+ globalKey: '$myMonitor',
142
+ }).run();
143
+
144
+ // 使用自定义键名发送事件
145
+ window.$myMonitor.emit('custom_event', { foo: 'bar' });
146
+ ```
147
+
148
+ ### 插件高级配置 (过滤)
149
+
150
+ 你可以为插件传入配置对象,例如只监控特定按钮的点击:
151
+
152
+ ```typescript
153
+ import { createMonitor, BehaviorPlugin, XHRPlugin, PerformancePlugin, EMIT_TYPE } from 'cwj_monitoring';
154
+
155
+ const monitor = createMonitor({ url: '...' });
156
+
157
+ monitor
158
+ .use(
159
+ BehaviorPlugin({
160
+ // 只记录带有 data-track 属性的元素点击
161
+ filter: (el) => el.hasAttribute('data-track'),
162
+ // 点击节流时间
163
+ throttleDelay: 300,
164
+ }),
165
+ )
166
+ .use(
167
+ XHRPlugin({
168
+ // 忽略特定 API 的错误监控
169
+ filter: (method, url) => !url.includes('/ignore-api/'),
170
+ }),
171
+ )
172
+ .use(
173
+ PerformancePlugin({
174
+ // 过滤掉资源加载监控,只保留核心性能指标
175
+ filter: (type) => type !== EMIT_TYPE.PERFORMANCE_RESOURCE,
176
+ }),
177
+ )
178
+ .run();
179
+ ```
180
+
181
+ ---
182
+
183
+ ## 📊 数据格式
184
+
185
+ ```typescript
186
+ interface MonitoringPayload {
187
+ device: {
188
+ browser: { name: string; version: string };
189
+ os: { name: string; version: string };
190
+ platform: { type: string };
191
+ ratio: number;
192
+ wh: { width: number; height: number };
193
+ };
194
+ uuid: string; // 唯一访客 ID(指纹)
195
+ type: string; // 事件类型
196
+ data: any; // 事件特定数据
197
+ date: string; // ISO 8601 时间戳
198
+ userData?: object; // 你的自定义元数据
199
+ }
200
+ ```
201
+
202
+ ---
203
+
204
+ ## 🛠️ 开发
205
+
206
+ ```bash
207
+ npm install
208
+ npm run dev # 开发模式
209
+ npm run build # 生产构建
210
+ npm run lint # 代码检查
211
+ ```
212
+
213
+ ---
214
+
215
+ ## 📄 许可证
216
+
217
+ MIT © [cwjbjy](https://github.com/cwjbjy)