cwj_monitoring 0.0.23 → 0.0.25
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 +198 -34
- package/dist/index.cjs +893 -0
- package/dist/index.cjs.map +1 -0
- package/dist/index.d.ts +293 -16
- package/dist/index.js +883 -1
- package/dist/index.js.map +1 -0
- package/dist/index.min.js +2 -0
- package/dist/index.min.js.map +1 -0
- package/dist/index.umd.js +899 -0
- package/dist/index.umd.js.map +1 -0
- package/package.json +24 -9
package/README.md
CHANGED
|
@@ -1,14 +1,31 @@
|
|
|
1
|
-
# 🚀 前端监控 SDK
|
|
1
|
+
# 🚀 CWJ 前端监控 SDK
|
|
2
2
|
|
|
3
|
-
>
|
|
3
|
+
> 现代化、轻量级的前端监控 SDK,适用于 Web 应用。轻松追踪用户行为、性能指标和错误信息。
|
|
4
4
|
|
|
5
|
-
|
|
5
|
+
[](https://www.npmjs.com/package/cwj_monitoring)
|
|
6
|
+
[](https://www.typescriptlang.org/)
|
|
7
|
+
[](https://opensource.org/licenses/MIT)
|
|
6
8
|
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
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,183 @@
|
|
|
16
33
|
npm install cwj_monitoring
|
|
17
34
|
```
|
|
18
35
|
|
|
19
|
-
##
|
|
36
|
+
## 🚀 快速开始
|
|
20
37
|
|
|
21
|
-
|
|
38
|
+
### 基础用法
|
|
22
39
|
|
|
23
|
-
```
|
|
24
|
-
import {
|
|
40
|
+
```typescript
|
|
41
|
+
import { createMonitor, ErrorPlugin, PerformancePlugin, XHRPlugin, FetchPlugin } from 'cwj_monitoring';
|
|
25
42
|
|
|
26
|
-
|
|
27
|
-
url: '
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
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
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
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
|
+
- **LoAF (Long Animation Frame)**: 现代化的长任务监控,提供详尽的脚本级归因(Chrome 123+)
|
|
95
|
+
- **Resource**: 仅监听 `fetch` 与 `xmlhttprequest` 的网络请求耗时
|
|
96
|
+
|
|
97
|
+
**配置项:**
|
|
98
|
+
|
|
99
|
+
- `loafThreshold`: LoAF 阈值 (ms),默认 `50`
|
|
100
|
+
- `resourceThreshold`: 资源加载阈值 (ms),默认 `1000`
|
|
101
|
+
- `filter`: 过滤函数,支持按类型过滤指标
|
|
102
|
+
|
|
103
|
+
示例:
|
|
104
|
+
|
|
105
|
+
```typescript
|
|
106
|
+
PerformancePlugin({
|
|
107
|
+
loafThreshold: 150, // 仅记录超过 150ms 的 LoAF
|
|
108
|
+
resourceThreshold: 2000, // 仅记录超过 2s 的请求
|
|
48
109
|
});
|
|
49
110
|
```
|
|
50
|
-
|
|
51
|
-
|
|
111
|
+
|
|
112
|
+
### 行为插件 (`BehaviorPlugin`)
|
|
113
|
+
|
|
114
|
+
监控用户点击交互。支持通过 `filter` 过滤特定元素,支持通过 `throttleDelay` 设置点击节流时间(默认 500ms)。
|
|
115
|
+
|
|
116
|
+
### 路由插件 (`PVPlugin`)
|
|
117
|
+
|
|
118
|
+
追踪单页应用的页面跳转。支持通过 `filter` 过滤特定路由。
|
|
119
|
+
|
|
120
|
+
### XHR 插件 (`XHRPlugin`)
|
|
121
|
+
|
|
122
|
+
监控 XMLHttpRequest 请求详情,仅记录失败的请求(状态码非 2xx)。支持通过 `filter` 根据 URL 或方法过滤请求。
|
|
123
|
+
|
|
124
|
+
### Fetch 插件 (`FetchPlugin`)
|
|
125
|
+
|
|
126
|
+
监控 fetch 请求详情,仅记录失败的请求(状态码非 2xx 或网络错误)。支持通过 `filter` 根据 URL 或方法过滤请求。
|
|
127
|
+
|
|
128
|
+
---
|
|
129
|
+
|
|
130
|
+
## 🎯 进阶用法
|
|
131
|
+
|
|
132
|
+
### 自定义全局变量名
|
|
133
|
+
|
|
134
|
+
```typescript
|
|
135
|
+
import { createMonitor } from 'cwj_monitoring';
|
|
136
|
+
|
|
137
|
+
createMonitor({
|
|
138
|
+
url: '...',
|
|
139
|
+
globalKey: '$myMonitor',
|
|
140
|
+
}).run();
|
|
141
|
+
|
|
142
|
+
// 使用自定义键名发送事件
|
|
143
|
+
window.$myMonitor.emit('custom_event', { foo: 'bar' });
|
|
144
|
+
```
|
|
145
|
+
|
|
146
|
+
### 插件高级配置 (过滤)
|
|
147
|
+
|
|
148
|
+
你可以为插件传入配置对象,例如只监控特定按钮的点击:
|
|
149
|
+
|
|
150
|
+
```typescript
|
|
151
|
+
import { createMonitor, BehaviorPlugin, XHRPlugin, PerformancePlugin, EMIT_TYPE } from 'cwj_monitoring';
|
|
152
|
+
|
|
153
|
+
const monitor = createMonitor({ url: '...' });
|
|
154
|
+
|
|
155
|
+
monitor
|
|
156
|
+
.use(
|
|
157
|
+
BehaviorPlugin({
|
|
158
|
+
// 只记录带有 data-track 属性的元素点击
|
|
159
|
+
filter: (el) => el.hasAttribute('data-track'),
|
|
160
|
+
// 点击节流时间
|
|
161
|
+
throttleDelay: 300,
|
|
162
|
+
}),
|
|
163
|
+
)
|
|
164
|
+
.use(
|
|
165
|
+
XHRPlugin({
|
|
166
|
+
// 忽略特定 API 的错误监控
|
|
167
|
+
filter: (method, url) => !url.includes('/ignore-api/'),
|
|
168
|
+
}),
|
|
169
|
+
)
|
|
170
|
+
.use(
|
|
171
|
+
PerformancePlugin({
|
|
172
|
+
// 过滤掉资源加载监控,只保留核心性能指标
|
|
173
|
+
filter: (type) => type !== EMIT_TYPE.PERFORMANCE_RESOURCE,
|
|
174
|
+
}),
|
|
175
|
+
)
|
|
176
|
+
.run();
|
|
177
|
+
```
|
|
178
|
+
|
|
179
|
+
---
|
|
180
|
+
|
|
181
|
+
## 📊 数据格式
|
|
182
|
+
|
|
183
|
+
```typescript
|
|
184
|
+
interface MonitoringPayload {
|
|
185
|
+
device: {
|
|
186
|
+
browser: { name: string; version: string };
|
|
187
|
+
os: { name: string; version: string };
|
|
188
|
+
platform: { type: string };
|
|
189
|
+
ratio: number;
|
|
190
|
+
wh: { width: number; height: number };
|
|
191
|
+
};
|
|
192
|
+
uuid: string; // 唯一访客 ID(指纹)
|
|
193
|
+
type: string; // 事件类型
|
|
194
|
+
data: any; // 事件特定数据
|
|
195
|
+
date: string; // ISO 8601 时间戳
|
|
196
|
+
userData?: object; // 你的自定义元数据
|
|
197
|
+
}
|
|
198
|
+
```
|
|
199
|
+
|
|
200
|
+
---
|
|
201
|
+
|
|
202
|
+
## 🛠️ 开发
|
|
203
|
+
|
|
204
|
+
```bash
|
|
205
|
+
npm install
|
|
206
|
+
npm run dev # 开发模式
|
|
207
|
+
npm run build # 生产构建
|
|
208
|
+
npm run lint # 代码检查
|
|
209
|
+
```
|
|
210
|
+
|
|
211
|
+
---
|
|
212
|
+
|
|
213
|
+
## 📄 许可证
|
|
214
|
+
|
|
215
|
+
MIT © [cwjbjy](https://github.com/cwjbjy)
|