2.2.0 • Published 2 years ago

th-gather v2.2.0

Weekly downloads
-
License
MIT
Repository
github
Last release
2 years ago

th-gather NPM License

th-gather是简单易用且可扩展的前端全埋点脚本。可以统计上报接口请求,页面停留时间,用户设备等信息。为业务需求迭代提供数据参考。

一、安装和使用

安装

npm i th-gather

或者

yarn add th-gather

使用

import init from 'th-gather';

init({
  projectKey: 'pc_project',
  url: '/log/record',
  isLog: true,
  beforeInit() {
    console.log('init start...');
  },
  mergeMsg() {
    //  request ...
    return new Promise((resolve) => {
      setTimeout(() => {
        resolve({ name: '测试用户', age: 12, id: 'user_id' });
      }, 1000);
    });
  },
  beforeSendMsg(data) {
    const newData = { ...data, newKey: 'add new value' };
    return newData;
  },
});

二、初始化选项

参数类型描述默认值
projectKeystring自定义的项目标识,方便区分多个项目-
urlstring上报的地址-
gatherKeysBaseMsg[]配置需要收集的基本信息defaultKeys
frequencynumber上报频率,默认为每满 10 条上报一次10
isDiscardboolean是否在窗口关闭时,上报剩余的数据true
isRoutesboolean是否收集路由跳转信息true
isPerformanceboolean是否监听浏览器性能信息true
isPromiseErrorboolean是否监听 promise 错误(没有被 reject 处理的 promise 错误)true
isResourceErrorboolean是否监听页面资源错误true
isRequestboolean是否监听接口请求信息true
isLogboolean是否需要打印日志-
beforeInit()=>void脚本初始化之前运行-
mergeMsg() => (Record<string,any> or Promise<Record<string,any>>)返回的值会被合并到每一条上报数据中, 可以用于从接口中读取用户信息, 此函数优先级在内置埋点事件开始监听之前-
beforeSendMsg(data: ExtraMsg & BaseMsg) => ExtraMsg & BaseMsg上报数据之前,可以在此对数据进行加工-
headersObject上报请求头, 因为页面卸载上报的时候无法设置请求头,可能会导致部分数据丢失-

三、上报数据格式

上报的数据由基本数据 + 对应类型数据组成(使用 gatherKeys 编辑的时候,只能编辑基本数据字段),收集的示例数据如下所示:

image

四、收集字段含义

4.1 基本数据

默认包含以下选项['key','o','ua','ul','ct','vp','sr','logId','gmt','dpr','rf']

字段描述
key项目 key 值
o当前页面路径
uaua
ul浏览器语言
ct网络类型
vp浏览器宽高
sr屏幕宽高
logId每条记录的 id 值
gmt数据生成时间
dprdevicePixelRatio
rfreferer

4.2 浏览器性能数据

字段描述
type标识为 performance
dnsdns 查询时间
tcptcp 连接耗时
ttfb读取页面第一个字节的时间
bt白屏时间
dt解析 dom 树耗时
drtdom 完成时间
rtrequest 请求耗时
lt页面完成时间
nv当前页面如何导航到此处

4.3 路由信息

字段描述
type标识为 pv
pathname当前路由地址
from上一个跳转地址
title网页标题
stay停留时间

4.4 接口信息

tips: 收集所有由XMLHttpRequest发起的请求

字段描述
type标识为 interface
url接口请求地址
tc接口耗时
cd状态码
rq发送请求时间
rp接受响应时间

4.5 js 错误信息

字段描述
type标识为 error
st错误类型 js
file文件名
lineerror.lineno
colerror.coln
msg错误信息

4.6 资源异常信息

字段描述
type标识为 error
st错误类型 resource
file资源链接
msg错误信息

4.7 promise 错误信息

字段描述
type标识为 error
st错误类型 promise
file' '
msg错误信息
2.2.0

2 years ago

2.1.2

2 years ago

2.0.3

2 years ago

2.1.1

2 years ago

2.0.2

2 years ago

2.1.3

2 years ago

2.1.0

2 years ago

2.0.1

2 years ago

2.0.0

2 years ago

1.0.1

2 years ago

1.0.0

2 years ago

0.1.7

2 years ago

0.1.6

2 years ago

0.1.5

2 years ago

0.1.4

2 years ago

0.1.3

2 years ago

0.1.2

2 years ago

0.1.1

2 years ago

0.1.0

2 years ago