All Products
Search
Document Center

Application Real-Time Monitoring Service:Monitor a WeChat mini program

Last Updated:Aug 24, 2026

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:

  1. Obtain the WeChat mini program monitoring SDK and initialize it:

    1. 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.
    2. 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: cn for China, or sg for regions outside China that are close to Singapore.

      Note For detailed parameter configurations, see SDK parameters.
  2. Enable automatic instrumentation to collect PV, error, API, performance, and health data:

    1. 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() {
        }
      }));
    2. 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() {
        }
      }));
  3. 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, add https://arms-retcode.aliyuncs.com to the list of valid request domains.

    • If region is set to sg, add https://arms-retcode-sg.aliyuncs.com to 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

undefined

environment

String

The environment field. Valid values: prod, gray, pre, daily, and local.

  • The value prod indicates an online environment.

  • The value gray indicates a phased-release environment.

  • The value pre indicates a staging environment.

  • The value daily indicates a daily environment.

  • The value local indicates a local environment.

No

prod

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 1/sample ratio. For more information about the metrics of performance logs and success API logs, see Statistical metrics.

No

1

behavior

Boolean

Specifies whether to record the user behavior that reports errors for easy troubleshooting.

No

false

enableLinkTrace

Boolean

For more information about back-to-back Tracing Analysis, see Diagnose API errors with front-to-back tracing.

No

false

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.
Note To use hookApp and hookPage for lifecycle instrumentation, your mini program must follow the standard App and Page specifications. The App layer must include onError, and the Page layer must include onShow, onHide, and onUnload. For usage examples, see Basic usage.

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.
Note For the setConfig method, the uid parameter is not supported in mini programs. You can use setUsername instead of uid to identify a user.

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:

    1. Set disableHook to true to disable automatic log reporting for wx.request requests.

    2. Manually call the api() method to report API information.

  • Disable automatic instrumentation and instrument your pages manually:

    1. Do not use the hookApp or hookPage methods in the JS files of App and Page.

    2. 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();
        }
      })
    3. 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.
      })