This topic describes how to use ARMS Browser Monitoring to monitor your WeChat mini program, including SDK setup, common configurations, API methods, and advanced use cases.
Background
For background information, see the official WeChat mini program documentation.
Basic usage
To get started, follow these three steps: installing and initializing the SDK, enabling automatic instrumentation, and configuring the security domain.
-
Install and initialize the SDK:
- In the /utils directory of your WeChat mini program project, create a file named wxLogger.js. Copy the contents of the SDK JS file and paste them into wxLogger.js.
- In the /utils directory, create another file named monitor.js and add the following initialization code.Note You can customize the file names and their storage locations.
-
If your project uses Node.js modules (require), add the following code:
const WXLogger = require('./wxLogger.js'); const Monitor = WXLogger.init({ pid: 'xxx', region: 'cn', // Specify the deployment region. Use 'cn' for China or 'sg' for regions near Singapore. }); export default Monitor; -
If your project uses ES modules (import), add the following code:
import WXLogger from './wxLogger.js'; const Monitor = WXLogger.init({ pid: 'xxx', region: 'cn', // Specify the deployment region. Use 'cn' for China or 'sg' for regions near Singapore. }); export default Monitor;
Note For detailed parameter configurations, see SDK parameters. -
-
Use the following methods to automatically instrument page view (PV), error, API, performance, and health data:
-
In app.js, use the Monitor.hookApp(options) method to automatically capture error logs. Pass your application's original configuration object as the options parameter.
import Monitor from '/util/monitor'; App(Monitor.hookApp({ onError(err) { console.log('Enter onError:', err); }, onLaunch() { console.log('Enter onLaunch'); }, onShow(options) { }, onHide() { } })); -
In each page's JS file, use the Monitor.hookPage(options) method to automatically report API requests, PVs, and health data.
import Monitor from '/util/monitor'; // After you use hookPage, lifecycle APIs are automatically instrumented. Page(Monitor.hookPage({ data: {}, onLoad(query) { }, onReady() { // The page has loaded. }, onShow() { }, onLoad(query) { }, onHide() { }, onUnload() { } }));
-
-
Configure the security domain:
-
If you set region to
cn, addhttps://arms-retcode.aliyuncs.comto your valid domain list. -
If you set region to
sg, addhttps://arms-retcode-sg.aliyuncs.comto your valid domain list.
-
Automatic instrumentation API
| Method | Parameter | Description | Example |
| hookApp | {} | Pass the original App parameters. | Automatically instrument the App lifecycle. |
| hookPage | {} | Pass the original Page parameters. | Automatically instrument the Page lifecycle. |
Other APIs
| Method | Parameter | Description |
| setCommonInfo | {[key: string]: string;} | Sets basic log fields, which is useful for scenarios like canary releases. |
| setConfig | {[key: string]: string;} | Sets configuration fields. For more information, see SDK reference. Note In mini programs, the method does not support the uid parameter. Use the setUsernameuid method to identify a user instead. |
| pageShow | {} | Instruments a Page Show event and sends PV data. |
| pageHide | {} | Instruments a Page Hide event and sends health data. |
| error | String/Object | Reports an error log. |
| api | See Front-end API Reference. | Reports an API log. |
| sum/avg | String | Reports custom sum or average logs. |
Advanced use cases
If the basic methods are insufficient, consider the following advanced use cases.
-
To manually report API information:
-
Set disableHook to
trueto disable automatic log reporting for wx.request calls. -
Manually call the api() method to report API information.
-
-
To use manual instrumentation:
-
Do not use the
hookAppandhookPagemethods in yourAppandPageJavaScript files. -
To send PV data for the current page, call the pageShow() method within the page's onShow method.
Note To avoid duplicate log reporting, do not use this method with hookPage().import Monitor from '/util/monitor'; Page({ onShow: function() { Monitor.pageShow(); } }) -
To send health data for the current page, which includes page health and time on page, call the pageHide() method within the page's onHide and onUnload methods.
Note To avoid duplicate log reporting, do not use this method with hookPage().import Monitor from '/util/monitor'; Page({ onHide: function() { Monitor.pageHide(); }, onUnload: function() { Monitor.pageHide(); } ... })
-
SDK parameters
The ARMS Browser Monitoring SDK offers various configuration parameters. The following table describes common parameters for this scenario.
| Parameter | Type | Description | Required | Default |
|
pid |
String |
The unique ID of the project. It is automatically generated by ARMS when it creates a site. |
Yes |
None |
|
uid |
String |
The ID of the user. The value is an identifier of the user and can be used to search for the user. You can specify a custom value. If you do not specify this parameter, the SDK is automatically generated and updated every six months. |
No |
Automatically generated by the SDK |
|
tag |
String |
The input tag. Each log carries a tag. |
No |
None |
|
release |
String |
The version of the application. We recommend that you configure this parameter to view the report information of different versions. |
No |
|
|
environment |
String |
The environment field. Valid values: prod, gray, pre, daily, and local.
|
No |
|
|
sample |
Integer |
The log sampling configuration. The value is an integer from 1 to 100. The performance logs and success API logs are sampled at the |
No |
|
|
behavior |
Boolean |
Specifies whether to record the user behavior that reports errors for easy troubleshooting. |
No |
|
|
enableLinkTrace |
Boolean |
For more information about back-to-back Tracing Analysis, see Diagnose API errors with front-to-back tracing. |
No |
|
For additional SDK configuration parameters, see SDK reference.