真实用户监控 - 自定义 API
自定义 API 用于为用户 ID 设置动态值、捕获 JavaScript(JS)错误、指定会话超时等。本文档将介绍 Site24x7 中可用的各类自定义 API 及其使用语法。
请确保已部署最新的真实用户监控(RUM)脚本,自定义 API 方可正常使用。
可用 API:
1. 自定义用户 ID
默认情况下,RUM 脚本会生成唯一的用户 ID。如果您想自定义用户 ID,可以通过实现以下语法来实现。
当您需要追踪特定用户的指标或调试其特定问题时,此功能非常有用。
语法:
/**
* @param {String} RUM API to set user name
* @param {String} Unique user identifier to be sent to site24x7 RUM
*/
s247r('userId',"user@example.com")
设置自定义面包屑导航,便于在 JS 错误中快速定位。
语法:
/**
* @param {String} RUM API to add breadcrumbs in error reporting
* @param {String} Breadcrumbs to be sent to site24x7 RUM error reporting
*/
s247r('addBreadCrumbs',"setTimeoutFunction");
3. 追踪控制台事件(JS 错误)
默认情况下,控制台事件不会被追踪。使用以下 API 可追踪控制台事件,用于将控制台事件添加到 JS 错误的面包屑导航中。
语法:
/**
* @param {String} RUM API to track console events {WARNING,LOG,ERROR,INFO} to view in breadcrumbs
* @param {Boolean} enables tracking console events . Default value is false .
*/
s247r('trackConsoleEvents',true);
4. 豁免关键字(通用)
默认情况下,事务名称中的字母数字关键字会被混淆。例如,名为 home/site24x7.html 的事务将显示为 home/site*x*.html。如果您希望某个关键字不被混淆,请将其以逗号分隔的字符串形式提供,如以下代码片段所示。
语法:
/**
* @param {String} RUM API to exempt keywords
* @param {String} Comma separated strings to be exempted
*/
/** in the below example, txns containing site24x7 and real-user-monitor will be exempted from obfuscation*/
s247r('exemptKeywords','site24x7,real-user-monitor');
5. 最大会话超时时长(用户会话)
默认会话超时时长为 900000 毫秒(15*60*1000,即 15 分钟)。可使用以下 JS 代码片段更改此值。
语法:
/**
* @param {String} RUM API to manually set the maximum session duration
* @param {Number} Session duration in millis
*/
/** in the below example the timeout is set to 1min ( 1*60*1000 )*/
s247r('maxSessionDuration',60000);
最大会话时长最多可设置为 1 小时。如果配置的值超过 1 小时,将自动回退到默认值 15 分钟。
6. 结束当前会话(用户会话)
如需结束当前会话并将下一次页面导航作为新会话开始,请使用以下语法。
语法:
/**
* @param {String} RUM API to end the current session
*/
s247r('endCurrentSession');
site24x7RumApiEndPoint:(用户会话)用于提供 URL,通过您自己的服务器将有效载荷代理到 Site24x7 的选项。
语法:
/**
* @param {String} RUM API to set the api endpoint
* @param {String} url for the api endpoint
*/
s247r('site24x7RumApiEndPoint','https://localhost:6443');
7. 捕获 JS 错误(JS 错误)
可以使用以下代码片段手动捕获 JS 错误并发送到 site24x7 RUM 服务器。这在通过拦截全局错误处理程序发送错误时特别有用。
语法:
try{
unKnownFunction();
}
catch(err){
/**
* @param {String} RUM API to manually capture js errors
* @param {Error} Error object for site24x7 error reporting
*/
s247r('captureException',err);
}
8. 环境
设置自定义环境详情,以便在各种环境配置(如开发、调试、生产等)中筛选 RUM 数据。
语法:
/**
* @param {String} RUM API to set the environment setup like production,development,debug, etc
* @param {String} Custom environment string for filtering RUM data
*/
s247r("environment", "production");
环境版本必须为字符串类型,最大支持长度为 100 个字符。
9. 追踪异步回调
支持对异步浏览器函数回调(如 setTimeout/setInterval)进行全局错误处理。
语法:
/**
* @param {String} RUM API to track js errors in asynchronous functions
* @param {Boolean} Enables|Disables tracking the above functionality. The Default value is false
*/ s247r("trackAsynchronousCallbacks", true);
10. 追踪带查询参数的网页
默认情况下,网页中的查询参数不会被捕获;如需追踪带查询参数的网页,请使用以下 API 追踪含查询参数的事务。
语法:
/**
* @param {String} RUM API to track webpages with queryparams. By default the queryparams are ignored
* @param {Boolean} Enables|Disables tracking the above functionality . The Default value is false
*/ s247r("trackTransactionsWithQueryParams", true);
各 SPA 框架的代码片段:
Angular JS
angular.module("app").factory('$exceptionHandler',['$log', function($log) {
return function myExceptionHandler(exception, cause) {
$log.warn(exception,cause);
s247r('addBreadCrumbs',cause);
s247r('captureException',exception);
};
}]);
Angular 2
import { ErrorHandler, Injectable} from '@angular/core';
@Injectable()
export class GlobalErrorHandler implements ErrorHandler {
constructor() { }
handleError(error) {
s247r('captureException',error);
}
}
VueJS
Import Vue from 'vue';
Vue.config.errorHandler = (err, vm, info) => {
//component in which error occured
s247r('addBreadCrumbs',vm);
//info Vue specific error information such as lifecycle hooks,events etc
s247r('addBreadCrumbs',info);
s247r('captureException',err);
};
捕获自定义错误消息
var error = new Error("my custom error message");
s247r('captureException',error);
11. 配置采样率
RUM 中的采样率功能可根据需求申请开启。
12. 发布版本
设置自定义发布版本详情,以便根据特定应用版本筛选和分析 RUM 数据。
语法:
/**
* @param {String} RUM API to set the application release version
* @param {String} Custom release version string (e.g., "1.1.3")
*/
s247r("release", "1.1.3");
发布版本必须为字符串类型,最大支持长度为 100 个字符。
