前端埋点(用户操作、错误收集上报)
1. 安装
npm i trace-collect
2. 说明
/**
* @uid 用户标识
* @requestUrl 上报的服务器地址
* @extra 额外的参数
* @historyCollect 是否开启浏览器history收集
* @hashCollect 是否开启浏览器hash收集
* @domCollect 是否开启用户操作dom收集
* @jsError 是否开启js报错收集
*/
export interface DefaultOptions {
uid: string;
extra: Record<string, any>;
requestUrl: string;
historyCollect: boolean;
hashCollect: boolean;
domCollect: boolean;
jsError: boolean;
}
export interface Options extends Partial<DefaultOptions> {
requestUrl: string;
}
4 种模式
- historyCollect --- (pushState、replaceState、 popState) 收集上报
- hashCollect --- (hashchange) 收集上报
- domCollect --- dom 元素操作上报 ('click'、 'dblclick'、 'contextmenu'、 'mouseout'、 'mouseover'、 'mousedown'、 'mouseup') 事件可选
- jsError --- (error、reject) js 报错上报
3. 用法
import TraceCollect from 'trace-collect';
const traceCollect = new TraceCollect({
uid: 'xxx',
extra: {},
requestUrl: 'http://xxx.com/xxx',
historyCollect: true,
hashCollect: true,
domCollect: true,
jsError: true,
})
traceCollect.setUid('xxx')
traceCollect.setExtra({})
traceCollect.sendCollect(data)
Object.assign({
options,
{},
{
time: Date.now(),
}
})
Object.assign({
options,
{
event: 'popstate | pushState | replaceState',
key: 'history',
href: window.location.href,
},
{
time: Date.now(),
}
})
Object.assign({
options,
{
event: 'hashchange',
key: 'hash',
hash: location.hash,
oldUrl: ev.oldURL,
newUrl: ev.newURL,
},
{
time: Date.now(),
}
})
Object.assign({
options,
{
event: "click | dbclick ...",
key: `dom-${targetKey}`,
},
{
time: Date.now(),
}
})
Object.assign({
options,
{
event: 'error | reject',
key: 'jsError',
message: e.message,
},
{
time: Date.now(),
}
})