2026-06-16 17:18:35 +08:00
|
|
|
|
# @xuqm/rn-bugcollect
|
2026-06-16 12:14:52 +08:00
|
|
|
|
|
2026-06-16 17:18:35 +08:00
|
|
|
|
XuqmGroup RN SDK 错误采集模块。提供日志采集、错误追踪、漏斗分析能力。
|
2026-06-16 12:14:52 +08:00
|
|
|
|
|
|
|
|
|
|
## 安装
|
|
|
|
|
|
|
|
|
|
|
|
```bash
|
2026-06-16 17:18:35 +08:00
|
|
|
|
yarn add @xuqm/rn-bugcollect
|
2026-06-16 12:14:52 +08:00
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
|
|
Peer dependencies:`@xuqm/rn-common >= 0.4.0`,`@react-native-async-storage/async-storage >= 1.21.0`,`react-native >= 0.76`
|
|
|
|
|
|
|
|
|
|
|
|
## 快速开始
|
|
|
|
|
|
|
|
|
|
|
|
```ts
|
2026-06-16 17:18:35 +08:00
|
|
|
|
import { BugCollect } from '@xuqm/rn-bugcollect'
|
2026-06-16 12:14:52 +08:00
|
|
|
|
|
|
|
|
|
|
// 1. 设置日志级别
|
2026-06-16 17:18:35 +08:00
|
|
|
|
BugCollect.setLogLevel('debug') // 'debug' | 'info' | 'warn' | 'error'
|
2026-06-16 12:14:52 +08:00
|
|
|
|
|
|
|
|
|
|
// 2. 设置环境标签
|
2026-06-16 17:18:35 +08:00
|
|
|
|
BugCollect.setEnvironment('production') // 'development' | 'staging' | 'production'
|
2026-06-16 12:14:52 +08:00
|
|
|
|
|
|
|
|
|
|
// 3. 开启全局错误捕获(App 启动时调用一次)
|
2026-06-16 17:18:35 +08:00
|
|
|
|
BugCollect.startCapture()
|
2026-06-16 12:14:52 +08:00
|
|
|
|
|
|
|
|
|
|
// 4. 记录自定义事件
|
2026-06-16 17:18:35 +08:00
|
|
|
|
BugCollect.event('page_view', { page: 'home' })
|
2026-06-16 12:14:52 +08:00
|
|
|
|
|
|
|
|
|
|
// 5. 上报错误
|
2026-06-16 17:18:35 +08:00
|
|
|
|
try { ... } catch (e) { BugCollect.captureError(e) }
|
2026-06-16 12:14:52 +08:00
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
|
|
## API
|
|
|
|
|
|
|
2026-06-16 17:18:35 +08:00
|
|
|
|
### BugCollect 对象
|
2026-06-16 12:14:52 +08:00
|
|
|
|
|
|
|
|
|
|
| API | 说明 |
|
|
|
|
|
|
|-----|------|
|
2026-06-16 17:18:35 +08:00
|
|
|
|
| `BugCollect.setLogLevel(level)` | 设置最低日志级别(低于此级别的事件被丢弃) |
|
|
|
|
|
|
| `BugCollect.setEnvironment(env)` | 设置环境标签(附加到每个事件) |
|
|
|
|
|
|
| `BugCollect.startCapture()` | 开启全局 JS 错误和未处理 Promise rejection 捕获 |
|
|
|
|
|
|
| `BugCollect.event(name, properties?)` | 记录自定义分析事件 |
|
|
|
|
|
|
| `BugCollect.captureError(error, metadata?)` | 上报 JS 异常 |
|
|
|
|
|
|
| `BugCollect.warn(message, metadata?)` | 记录警告(需级别允许) |
|
|
|
|
|
|
| `BugCollect.info(message, metadata?)` | 记录信息事件(需级别允许) |
|
|
|
|
|
|
| `BugCollect.defineFunnel(id, steps)` | 定义漏斗(按步骤顺序追踪转化) |
|
|
|
|
|
|
| `BugCollect.getFunnelProgress(funnelId)` | 获取客户端漏斗进度 |
|
|
|
|
|
|
| `BugCollect.flush()` | 立即刷新所有待发送事件(如 App 进后台前调用) |
|
2026-06-16 12:14:52 +08:00
|
|
|
|
|
|
|
|
|
|
### 事件类型
|
|
|
|
|
|
|
|
|
|
|
|
| 类型 | 说明 |
|
|
|
|
|
|
|------|------|
|
|
|
|
|
|
| `LogEvent` | 自定义事件(`type: 'event'`) |
|
|
|
|
|
|
| `IssueEvent` | 错误事件(`type: 'js_error'` / `'native_crash'` / `'api_error'` / `'warning'`) |
|
|
|
|
|
|
|
|
|
|
|
|
### 日志级别
|
|
|
|
|
|
|
|
|
|
|
|
`'debug'` (0) < `'info'` (1) < `'warn'` (2) < `'error'` (3)
|
|
|
|
|
|
|
|
|
|
|
|
### 漏斗分析示例
|
|
|
|
|
|
|
|
|
|
|
|
```ts
|
2026-06-16 17:18:35 +08:00
|
|
|
|
import { BugCollect } from '@xuqm/rn-bugcollect'
|
2026-06-16 12:14:52 +08:00
|
|
|
|
|
|
|
|
|
|
// 定义漏斗
|
2026-06-16 17:18:35 +08:00
|
|
|
|
BugCollect.defineFunnel('checkout', ['cart_view', 'checkout_start', 'payment_done'])
|
2026-06-16 12:14:52 +08:00
|
|
|
|
|
|
|
|
|
|
// 在业务代码中记录步骤事件(SDK 自动推进漏斗)
|
2026-06-16 17:18:35 +08:00
|
|
|
|
BugCollect.event('cart_view')
|
|
|
|
|
|
BugCollect.event('checkout_start')
|
|
|
|
|
|
BugCollect.event('payment_done')
|
2026-06-16 12:14:52 +08:00
|
|
|
|
|
|
|
|
|
|
// 查询进度
|
2026-06-16 17:18:35 +08:00
|
|
|
|
const progress = BugCollect.getFunnelProgress('checkout')
|
2026-06-16 12:14:52 +08:00
|
|
|
|
console.log(progress?.completedSteps) // ['cart_view', 'checkout_start', 'payment_done']
|
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
|
|
## 工作原理
|
|
|
|
|
|
|
2026-06-16 17:18:35 +08:00
|
|
|
|
1. **LogQueue**:事件先进入内存队列,按批次异步上传到 `bugCollectApiUrl`
|
2026-06-16 12:14:52 +08:00
|
|
|
|
2. **ErrorCapture**:`startCapture()` 注册全局 `ErrorUtils` handler,自动捕获未处理异常
|
|
|
|
|
|
3. **Fingerprint**:为每个错误生成指纹(基于 message + stack),用于服务端去重聚合
|
|
|
|
|
|
4. **FunnelTracker**:客户端维护漏斗进度,服务端跨 session 聚合
|
|
|
|
|
|
|
|
|
|
|
|
## 配置
|
|
|
|
|
|
|
2026-06-16 17:18:35 +08:00
|
|
|
|
`bugCollectApiUrl` 和 `bugCollectEnabled` 由 `@xuqm/rn-common` 在 init 后从 `/api/sdk/config` 自动获取,无需 App 传入。
|