function changeURLArg(url,arg,arg_val) {var pattern=arg+' = ([^&]*)';var replaceText = arg+'='+arg_val;if (url.match(pattern)) {var tmp = '/('+ arg+'=)([^&]*)/gi';tmp = url.replace(eval(tmp),replaceText);return tmp;}return url;}const aegis = new Aegis({id: 'pGUVFTCZyewxxxxx',onBeforeRequest(log) {if (log.type === 'performance') {// Page speed test. Here, you can modify the content of `log`; for example, you can modify the `platform` for page speed testlog.url = changeURLArg(log.url, 'platform', type)}return log}});// SEND_TYPE {// LOG = 'log', // Log// SPEED = 'speed', // API and static resource speed test// PERFORMANCE = 'performance', // Page speed test// OFFLINE = 'offline', // Offline log upload// WHITE_LIST = 'whiteList', // Allowlist// VITALS = 'vitals', // Web Vitals// PV = 'pv', // Custom PV// EVENT = 'event', // Custom event// CUSTOM = 'custom', // Custom speed test// SDK_ERROR = 'sdkError', // SDK error// }
/collect?),例如:const aegis = new Aegis({id: 'pGUVFTCZyewxxxxx',beforeReport(log) {// Listen on the reported error belowconsole.log(log); // {level: "4", msg: "An error occurred."}return log;}});throw new Error('An error occurred.');
const aegis = new Aegis({id: 'pGUVFTCZyewxxxxx',beforeReport(log) {if (log.level === '4' && log.msg && log.msg.indexOf('An error that doesn't need to be reported') !== -1) {return false}return log;}});throw new Error('An error that doesn't need to be reported'); // This error will not be reported
An error that doesn't need to be reported,则不会报告给 RUM 后端。/speed?)被执行,例如:const aegis = new Aegis({id: 'pGUVFTCZyewxxxxx',reportApiSpeed: true,reportAssetSpeed: true,beforeReportSpeed(msg) {console.log(msg); // {url: "https://localhost:3001/example.e31bb0bc.js", method: "get", duration: 7.4, status: 200, type: "static"}return msg}});
beforeReportSpeed 钩子。const aegis = new Aegis({id: 'pGUVFTCZyewxxxxx',reportApiSpeed: true,reportAssetSpeed: true,beforeReportSpeed(msg) {msg.type = 'static';return msg;}});
https://example.com/api,其响应标头 Content-Type 为 text/html。正常情况下,RUM 会将此 API 报告为静态资源。但在您的业务中,必须报告为 API 请求,您可以使用以下钩子配置 Aegis 进行修正:const aegis = new Aegis({id: 'pGUVFTCZyewxxxxx',reportApiSpeed: true,reportAssetSpeed: true,beforeReportSpeed(msg) {if (msg.url === 'https://example.com/api') {msg.type = 'fetch';}}});
const aegis = new Aegis({id: 'pGUVFTCZyewxxxxx',reportApiSpeed: true,reportAssetSpeed: true,beforeReportSpeed(msg) {// Speed test logs for resources whose address contains `https://example.com/api` will not be reportedif (msg.url.indexOf('https://example.com/api') !== -1) {// If `false` is returned, the reporting of the speed test log will be blockedreturn false}}});
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') {// 修改:将性能数据的首屏渲染时间改为2sdata.logs.firstScreenTiming = 2000;}return data;}});
{name: "白屏时间", duration: 3015.7000000178814, ext1: '', ext2: '', ext3: ''}{name: "ios", ext1: "", ext2: "", ext3: ""}{contentDownload: 2, dnsLookup: 0, domParse: 501, firstScreenTiming: 2315, resourceDownload: 2660, ssl: 4, tcp: 4, ttfb: 5}{connectTime: 0, domainLookup: 0, duration: 508.2, isHttps: true, method: "get", status: 200, type: "static", url: "https://xxxxxx", urlQuery: "max_age=1296000"}{delta: 1100, entries: [PerformancePaintTiming], id: "v1-1629344653118-4916457684758", name: "LCP", value: 1100}{msg: "日志详情", level: '4', ext1: '', ext2: '', ext3: '', trace: ''}{ 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 错误' }{ level: 65536, name: 'js bridge 错误'}const aegis = new Aegis({id: "pGUVFTCZyewxxxxx",afterRequest: function(data) {// {isErr: false, result: Array(1), logType: "log", logs: Array(4)}console.log(data);}});
文档反馈