有奖捉虫:办公协同&微信生态&物联网文档专题 HOT
您可以使用钩子函数对某些资源的测速上报进行自定义配置,系统将会为您计算、统计函数执行时间等。您可以在 自定义测速 多维度分析函数执行耗时。

beforeRequest

该钩子将会在所有的数据上报前执行,来帮助用户在数据上报前对其进行屏蔽和修改,当 beforeRequest 返回 false 就可以不上报该条日志,返回修改过后的 data 就可以实现对上报数据的修改,例如:
注意
SDK 版本应大于等于1.24.44。
const aegis = new Aegis({
id: 'pGUVFTCZyewxxxxx',
beforeRequest: function(data) {
// 入参 data 的数据结构:{logs: {…}, logType: "log"}
if (data.logType === 'log' && data.logs.msg.indexOf('otheve.beacon.qq.com') > -1) {
// 拦截:日志类型为 log,且内容包含 otheve.beacon.qq.com 的请求
return false;
}
// 入参 data 数据结构:{logs: {}, logType: "speed"}
if (data.logType === 'speed' && data.logs.url.indexOf('otheve.beacon.qq.com') > -1) {
// 拦截:日志类型为 speed,并且接口 url 包含 otheve.beacon.qq.com 的请求
return false;
}
if (data.logType === 'performance') {
// 修改:将性能数据的首屏渲染时间改为2s
data.logs.firstScreenTiming = 2000;
}
return data;
}
});
其中,data 将会有以下几个字段:
logType:日志类型,有以下值:
custom:自定义测速
event:自定义事件
log:日志
performance:页面测速
pv:页面 PV
speed:接口和静态资源测速
vitals:web vitals
logs:上报的日志内容:
当 logType 为 'custom' 时,logs 数据类型为 {name: "白屏时间", duration: 3015.7000000178814, ext1: '', ext2: '', ext3: ''}
当 logType 为 'event' 时,logs 数据类型为 {name: "ios", ext1: "", ext2: "", ext3: ""}
当 logType 为 'performance' 时,logs 数据类型为 {contentDownload: 2, dnsLookup: 0, domParse: 501, firstScreenTiming: 2315, resourceDownload: 2660, ssl: 4, tcp: 4, ttfb: 5}
当 logType 为 'speed' 时,logs 数据类型为 {connectTime: 0, domainLookup: 0, duration: 508.2, isHttps: true, method: "get", status: 200, type: "static", url: "https://xxxxxx", urlQuery: "max_age=1296000"}
当 logType 为 'vitals' 时,logs 数据类型为 {delta: 1100, entries: [PerformancePaintTiming], id: "v1-1629344653118-4916457684758", name: "LCP", value: 1100}
当 logType 为 'log' 时,logs 数据类型为 {msg: "日志详情", level: '4', ext1: '', ext2: '', ext3: '', trace: ''}
说明
其中 level 枚举值如下:
{ level: '1', name: '接口请求日志(白名单日志)' }
{ level: '2', name: '一般日志(aegis.info 或者 aegis.infoAll)' }
{ level: '4', name: 'JS 执行错误' }
{ level: '8', name: 'Promise 错误' }
{ level: '16', name: 'Ajax 请求异常' }
{ level: '32', name: 'JS 加载异常' }
{ level: '64', name: '图片加载异常' }
{ level: '128', name: 'css 加载异常' }
{ level: '256', name: 'console.error (未启用)' }
{ level: '512', name: '音视频资源异常' }
{ level: '1024', name: 'retcode 异常' }
{ level: '2048', name: 'aegis report' }
{ level: 4096, name: 'PV' }
{ level: 8192, name: '自定义事件' }
{ level: 16384, name: '小程序 页面不存在' }
{ level: 32768, name: 'websocket错误' }
该钩子返回 false 时,本条日志将不会进行上报,该功能可用来过滤某些不需要上报的错误,可以用来过滤不希望上报的日志。

afterRequest

该钩子将会在测速数据上报后被执行,例如:
注意
SDK 版本应大于等于1.24.44。
const aegis = new Aegis({
id: "pGUVFTCZyewxxxxx",
afterRequest: function(data) {
// {isErr: false, result: Array(1), logType: "log", logs: Array(4)}
console.log(data);
}
});
其中,data 将会有以下几个字段:
isErr:请求上报接口是否错误
result:上报接口的返回结果
logs:上报的日志内容
logType:日志类型,同 beforeRequest 中的 logType