Monitor a WeChat mini program

更新时间:
复制 MD 格式

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.

  1. Install and initialize the SDK:

    1. 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.
    2. 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.
  2. Use the following methods to automatically instrument page view (PV), error, API, performance, and health data:

    1. 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() {
        }
      }));
    2. 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() {
      
          }     
      }));
  3. Configure the security domain:

    • If you set region to cn, add https://arms-retcode.aliyuncs.com to your valid domain list.

    • If you set region to sg, add https://arms-retcode-sg.aliyuncs.com to 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.
Note To use hookApp and hookPage for lifecycle instrumentation, your mini program must follow the standard App and Page specifications. The App object must include an onError method, and Page objects must include onShow, onHide, and onUnload methods. For usage examples, see Basic usage.

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:

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

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

  • To use manual instrumentation:

    1. Do not use the hookApp and hookPage methods in your App and Page JavaScript files.

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

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

For additional SDK configuration parameters, see SDK reference.