ARMS 使用者體驗監控提供一系列SDK配置項,讓您能夠通過設定參數來滿足額外需求。本文介紹小遊戲應用常用的 SDK 配置。
SDK 配置
參數 | 類型 | 描述 | 是否必填 | 預設值 |
pid | string | 應用ID | 是 | - |
endpoint | string | 上報地址 | 是 | - |
env | string | 推薦的應用環境值:
| 否 | prod |
version | string | 應用版本 | 否 | - |
user | object | User配置,user.id由SDK預設產生 | 否 | - |
beforeReport | function | 發送RUM事件之前 | 否 | - |
reportConfig | object | 上報配置,詳細說明請參見reportConfig 配置。 | 否 | - |
sessionConfig | object | Session的採樣、儲存等配置,詳細說明請參見sessionConfig 配置。 | 否 | - |
collectors | object | 否 | - | |
parseResourceName | function | 解析資源名稱(resource.name),入參為當前資源的URL。 | 否 | - |
evaluateApi | function | 自訂解析API事件,詳細說明請參見 evaluateApi 配置。 | 否 | - |
filters | object | 事件過濾配置,詳情說明請參見 filters 配置。 | 否 | - |
properties | object | 自訂屬性,可對所有事件生效,詳情說明請參見 properties 配置。 | 否 | - |
user 配置
參數 | 類型 | 描述 | 是否必填 | 預設值 |
id | string | 使用者ID,不可修改。 | 否 | 由SDK預設產生 |
tags | string | 標籤 | 否 | - |
name | string | 名稱 | 否 | - |
樣本
如果需要關聯業務自有帳號體系,建議使用user.name或者user.tags,而不是修改user.id。強制覆蓋SDK產生的user.id,會影響UV的計算。
armsRum.init({
pid: "your app id",
endpoint: "your endpoint",
user: {
name: 'your user.name',
tags: 'your user.tags',
},
});
reportConfig 配置
參數 | 類型 | 描述 | 是否必填 | 預設值 |
flushTime | number | 上報時間間隔 取值範圍:[0, 10000] | 否 | 3000 |
maxEventCount | number | 一次上報最大數量 取值範圍:[1, 100] | 否 | 20 |
樣本
armsRum.init({
pid: "your app id",
endpoint: "your endpoint",
reportConfig: {
flushTime: 0, // 立即上報
maxEventCount: 50, // 一次最多上報數量
},
});
sessionConfig 配置
參數 | 類型 | 描述 | 是否必填 | 預設值 |
sampleRate | number | Session採樣率:[0, 1] 50%採樣率就是0.5。 | 否 | 1 |
maxDuration | number | Session持續時間上限,預設24小時。單位為毫秒(ms) | 否 | 86400000 |
overtime | number | Session逾時時間,預設一小時。單位為毫秒(ms) | 否 | 3600000 |
本機存放區了user.id和session資訊 :
_arms_uid:唯一標識使用者ID,對應user.id。
_arms_session:具備語義化的Session資訊。
sessionId:Session唯一標識。
sampled:是否命中採樣。
startTime:Session的開始時間戳。
lastTime:Session最後一次活躍時間戳記。
`${sessionId}-${sampled}-${startTime}-${lastTime}`樣本
armsRum.init({
pid: "your app id",
endpoint: "your endpoint",
sessionConfig: {
sampleRate: 0.5, // 採樣率50%
maxDuration: 86400000,
overtime: 3600000,
},
});
collectors 配置
Collector是RUM SDK用於收集小遊戲監控資料的最小單元。目前RUM SDK內建了API、Exception等豐富的採集器。
參數 | 類型 | 描述 | 是否必填 | 預設值 |
perf | boolean | object | 監聽應用的效能資料。 | 否 | true |
api | boolean | object | 監聽API請求。 | 否 | true |
staticResource | boolean | object | 監聽靜態資源請求。 | 否 | true |
consoleError | boolean | object | 監聽Console錯誤。 | 否 | true |
jsError | boolean | object | 監聽JS錯誤。 | 否 | true |
action | boolean | object | 監聽使用者行為。 | 否 | true |
樣本
關閉監聽使用者Click行為。
armsRum.init({
pid: "your app id",
endpoint: "your endpoint",
collectors: {
api: false,
},
});
evaluateApi 配置
evaluateApi提供對API(request)事件的自訂解析。以下為SDK傳入的三個參數:
參數 | 類型 | 描述 |
options | object | 請求參數,包括url、headers、data等,根據具體請求方式有所差異。 |
response | object | 請求響應體。 |
error | Error | 選擇性參數,當請求失敗才會傳入。 |
該函數支援非同步函數,返回Promise<IApiBaseAttr>,IApiBaseAttr的定義如下:
參數 | 類型 | 描述 | 是否必填 |
name | string | API名稱,一般是對URL的收斂,最大1000字元。 例如URL為 重要 該欄位的優先順序高於parseResourceName返回的內容。 | 否 |
message | string | API資訊,一個簡短的用於描述API概況字串,最大1000字元。 | 否 |
success | number | 請求成功狀態:
| 否 |
duration | number | API總耗時。 | |
status_code | number | string | API狀態代碼。 | 否 |
snapshots | string | API快照。 說明 可用於儲存reqHeaders、params、resHeaders等,具體欄位組成方式可自行決定。該欄位主要用於排查介面異常的原因,沒有索引,不能根據該欄位進行篩選或彙總,只接受字串類型,最大5000字元。 | 否 |
樣本
armsRum.init({
pid: "your app id",
endpoint: "your endpoint",
evaluateApi: async (options, response, error) => {
// 返回的欄位會覆蓋預設內容,不返回的欄位會依然使用SDK自定產生內容
return {
name: 'my-custom-api',
success: error ? 0 : 1,
// 以下可選
snapshots: JSON.stringify({
params: 'page=1&size=10', // 請求入參
response: '', // 傳回值
reqHeaders: '', // 要求標頭
resHeaders: '', // 回應標頭
}),
properties: {
prop_msg: 'custom msg',
prop_num: 1,
},
};
},
});
filters 配置
filters 用於過濾無需上報的事件,目前支援 resource 和 exception 兩種事件。
參數 | 類型 | 描述 | 是否必填 |
resource | MatchOption | MatchOption[] | 對採集到的資源事件進行過濾,包括靜態資源和API(XMLHttpRequest/fetch)。 | 否 |
exception | MatchOption | MatchOption[] | 對採集到的例外狀況事件進行過濾。 | 否 |
MatchOption
type MatchOption = string | RegExp | ((value: string, error?: Error) => boolean);string:匹配任何以該值開頭的URL,如
https://api.aliyun.com可以匹配https://api.aliyun.com/v1/resource。RegExp:使用提供的RegExp和URL執行test。
function:以URL作為參數進行計算,返回true表示匹配。當過濾異常的時候會傳入第二個參數為Error對象。
當傳入為 MatchOption[] 時,條件順序執行,有一項滿足就會被過濾。
樣本
armsRum.init({
pid: "your app id",
endpoint: "your endpoint",
filters: {
// 過濾異常
exception: [
'Test error', // 以'Test error'開頭的error.message
/^Script error\.?$/, // Regex匹配 error.message
(msg, error) => {
return msg.includes('example-error');
},
],
// 過濾資源或API
resource: [
'https://example.com/', // 以'https://example.com/'開頭的resource
/localhost/i,
(url) => {
return url.includes('example-resource');
},
],
},
});
properties 配置
Properties是RUM提供的自訂屬性,可為任何上報事件配置該屬性。
參數 | 類型 | 描述 | 是否必填 |
[key: string] | string | number |
| 否 |
使用evaluateApi、sendCustom、sendException、sendResource等可以單獨為某個事件增加properties,僅對單個事件生效。
全域properties和事件properties在儲存時會進行合并。事件properties優先順序高於全域properties,合并時相同的key,事件properties會覆蓋全域properties。合并後properties索引值對數量不可超過20對,超出會基於key進行排序後移除。
樣本
全域配置properties時,所有上報的事件都會擁有。
armsRum.init({
pid: "your app id",
endpoint: "your endpoint",
properties: {
prop_string: 'xx',
prop_number: 2,
// key 和 value 超出範圍會被截斷
more_than_50_key_limit_012345678901234567890123456789: 'yy',
more_than_2000_value_limit: new Array(2003).join('1'),
// 以下不符合要求的索引值對會被移除
prop_null: null,
prop_undefined: undefined,
prop_bool: true,
},
});
其他配置
RUM SDK支援配置基於IP和UserAgent等解析所得到的公用屬性,主動配置欄位優先順序高於自動解析。
參數 | 類型 | 描述 | 是否必填 |
device | object | 裝置資訊 | 否 |
os | object | 系統、容器資訊 | 否 |
geo | object | 行政地理資訊 | 否 |
isp | object | 電訊廠商資訊 | 否 |
net | object | 網路資訊 | 否 |
以上欄位具體可配置項請參考日誌資料說明中公用屬性部分。
樣本
armsRum.init({
pid: "your app id",
endpoint: "your endpoint",
geo: {
country: 'your custom country info',
city: 'your custom city info',
},
});
SDK API
RUM SDK開放API,用於修改上報自訂資料,動態修改SDK配置等。
getConfig
擷取SDK配置。
setConfig
修改SDK配置。
// 指定 key 設定
armsRum.setConfig('env', 'pre');
// 覆蓋設定
const config = armsRum.getConfig();
armsRum.setConfig({
...config,
version: '1.0.0',
env: 'pre',
});sendCustom
上報自訂資料,必須包含type和name兩個屬性,否則無法上報。屬性的具體業務意義可參考下表,實際使用需要自行定義業務語義。
參數 | 類型 | 描述 | 是否必填 |
type | string | 類型 | 是 |
name | string | 名稱 | 是 |
group | string | 分組 | 否 |
value | number | 值 | 否 |
properties | object | 自訂屬性 | 否 |
armsRum.sendCustom({
// 必選
type: 'CustomEvnetType1',
name: 'customEventName2',
// 可選
group: 'customEventGroup3',
value: 111.11,
properties: {
prop_msg: 'custom msg',
prop_num: 1,
},
});
sendException
上報自訂異常資料,必須包含name和message兩個屬性,否則無法上報。
參數 | 類型 | 描述 | 是否必填 |
name | string | 異常名稱 | 是 |
message | string | 異常資訊 | 是 |
file | string | 異常發生檔案 | 否 |
stack | string | 異常堆棧資訊 | 否 |
line | number | 異常發生的行數 | 否 |
column | number | 異常發生的列數 | 否 |
properties | object | 自訂屬性 | 否 |
armsRum.sendException({
// 必選
name: 'customErrorName',
message: 'custom error message',
// 可選
file: 'custom exception filename',
stack: 'custom exception error.stack',
line: 1,
column: 2,
properties: {
prop_msg: 'custom msg',
prop_num: 1,
},
});
sendResource
上報自訂資源,必須包含name、type和duration三個屬性,否則無法上報。
參數 | 類型 | 描述 | 是否必填 |
name | string | 資源名。 | 是 |
type | string | 資源類型,例如:css、javascript、xmlhttprequest、fetch、api、image、font、other。 | 是 |
duration | string | 請求耗時。 | 是 |
success | number | 請求成功狀態:
| 否 |
method | string | 要求方法。 | 否 |
status_code | number | string | 請求狀態代碼。 | 否 |
message | string | 請求訊息。 | 否 |
url | string | 請求地址。 | 否 |
trace_id | string | 鏈路追蹤ID。 | 否 |
properties | object | 自訂屬性。 | 否 |
armsRum.sendResource({
// 以下必選
name: 'getListByPage',
message: 'success',
duration: 800,
// 以下可選
url: 'https://www.aliyun.com/data/getListByPage',
properties: {
prop_msg: 'custom msg',
prop_num: 1,
},
});