Monitor a WeChat mini program
Integrate the WeChat mini program monitoring SDK to monitor a WeChat mini program with ARMS Browser Monitoring. This topic describes how to obtain and initialize the SDK, enable automatic instrumentation, and configure the security domain. It also describes the SDK parameters, the available API methods, and advanced use cases.
Background information
For background information about WeChat mini programs, see the WeChat mini program documentation.
Basic usage
To monitor a WeChat mini program with ARMS Browser Monitoring, complete the following three steps:
-
Obtain the WeChat mini program monitoring SDK and initialize it:
- In the /utils directory of your WeChat mini program project, create a file named wxLogger.js. Copy the contents of the monitoring SDK JS file and paste them into the wxLogger.js file.
- In the /utils directory, create a file named monitor.js and add the following code to the monitor.js file to initialize the SDK.Note You can customize the file names and 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: set to 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: set to cn for China, or sg for regions near Singapore. }); export default Monitor;
Set region to the deployment region of your application:
cnfor China, orsgfor regions outside China that are close to Singapore.Note For detailed parameter configurations, see SDK parameters. -
-
Enable automatic instrumentation to collect PV, error, API, performance, and health data:
-
In app.js, use the Monitor.hookApp(options) method to automatically capture error logs. The options parameter is the Object configuration of the App layer.
import Monitor from '/utils/monitor'; App(Monitor.hookApp({ onError(err) { console.log('Enter onError:', err); }, onLaunch() { console.log('Enter onLaunch'); }, onShow(options) { }, onHide() { } })); -
In the JS file of each page, use the Monitor.hookPage(options) method to automatically report API requests, PV, and health data.
import Monitor from '/utils/monitor'; // After you use hookPage, lifecycle APIs are automatically instrumented. Page(Monitor.hookPage({ data: {}, onLoad(query) { }, onReady() { // The page is loaded. }, onShow() { }, onHide() { }, onUnload() { } }));
-
-
Configure the security domain: add the ARMS domain that corresponds to your region value to the list of valid request domains of your WeChat mini program.
-
If region is set to
cn, addhttps://arms-retcode.aliyuncs.comto the list of valid request domains. -
If region is set to
sg, addhttps://arms-retcode-sg.aliyuncs.comto the list of valid request domains.
-
SDK parameters
ARMS Browser Monitoring provides SDK configuration parameters that you can set to meet additional requirements. The following table describes the common configuration parameters that apply to WeChat mini programs.
| 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 |
|
ARMS Browser Monitoring also provides additional SDK configuration parameters for further requirements. For more information, see SDK configuration parameters.
Automatic instrumentation APIs
The following table describes the methods that the WeChat mini program monitoring SDK provides for automatic lifecycle instrumentation.
| Method | Parameter | Parameter description | Behavior |
hookApp |
{} |
Pass the original App parameters. | Automatically instruments the App lifecycle. |
hookPage |
{} |
Pass the original Page parameters. | Automatically instruments the Page lifecycle. |
Other configuration APIs
The following table describes the methods that you can call to configure the WeChat mini program monitoring SDK and report data.
| Method | Parameter | Description |
setCommonInfo |
{[key: string]: string;} |
Sets basic log fields. This method is useful in scenarios such as canary releases. |
setConfig |
{[key: string]: string;} |
Sets configuration fields. For more information, see SDK configuration parameters. |
pageShow |
{} |
Reports a Page Show event and sends PV data. |
pageHide |
{} |
Reports a Page Hide event and sends health data. |
error |
String/Object | Reports an error log. |
api |
See api method parameters. | Reports an API log. |
sum/avg |
String | Reports custom sum or average logs. |
Advanced use cases
When the automatic instrumentation methods described in Basic usage do not meet your requirements, use one of the following advanced configurations:
-
Manually report API information instead of using automatic instrumentation:
-
Set disableHook to
trueto disable automatic log reporting for wx.request requests. -
Manually call the api() method to report API information.
-
-
Disable automatic instrumentation and instrument your pages manually:
-
Do not use the hookApp or hookPage methods in the JS files of App and Page.
-
To send PV data for the current page, call the pageShow() method in the onShow method of the page.
Note Do not use this method together with hookPage(). Otherwise, duplicate PV data is reported.import Monitor from '/utils/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 in the onHide and onUnload methods of the page.
Note Do not use this method together with hookPage(). Otherwise, duplicate health data is reported.import Monitor from '/utils/monitor'; Page({ onHide: function() { Monitor.pageHide(); }, onUnload: function() { Monitor.pageHide(); } // Other lifecycle methods. })
-