All Products
Search
Document Center

Application Real-Time Monitoring Service:Monitor WeChat mini programs

Last Updated:Mar 11, 2026

When your WeChat mini program encounters JavaScript errors, slow API responses, or degraded page performance, diagnosing these issues without instrumentation requires guesswork. Application Real-Time Monitoring Service (ARMS) Browser Monitoring provides a lightweight SDK that collects page views, JavaScript errors, API requests, performance metrics, and health data from WeChat mini programs, giving you real-time visibility into user experience.

This guide walks you through SDK installation, automatic instrumentation, security domain configuration, and advanced reporting options.

Prerequisites

Before you begin, make sure you have:

  • A WeChat mini program project. For more information, see WeChat mini programs

  • An ARMS Browser Monitoring site with a valid pid. ARMS generates the pid automatically when it creates the site

Quick start

For experienced users, the following snippet shows the minimal integration. For detailed instructions, see the step-by-step sections below.

// 1. Copy https://retcode.alicdn.com/retcode/wl.js into /utils/wxLogger.js

// 2. Create /utils/monitor.js
import WXLogger from './wxLogger.js';
const Monitor = WXLogger.init({
    pid: '<your-pid>',       // From the ARMS console
    region: 'cn',            // cn: Chinese mainland | sg: nearest to Singapore
});
export default Monitor;

// 3. In app.js, hook the app lifecycle
import Monitor from '/utils/monitor';
App(Monitor.hookApp({
    onError(err) { console.log('onError:', err); },
    onLaunch() {},
    onShow() {},
    onHide() {}
}));

// 4. In each page.js, hook the page lifecycle
import Monitor from '/utils/monitor';
Page(Monitor.hookPage({
    data: {},
    onShow() {},
    onHide() {},
    onUnload() {}
}));

After setup, allowlist the ARMS reporting domain in your WeChat admin console. See Step 3: Allowlist security domains.

Step 1: Install and initialize the SDK

Download the SDK

  1. Create a wxLogger.js file in the /utils folder of your mini program project.

  2. Copy the content of the SDK JS file into wxLogger.js.

Note

The file name and storage path are customizable.

Initialize the SDK

Create a monitor.js file in the /utils folder and add the initialization code.

CommonJS (require):

const WXLogger = require('./wxLogger.js');
const Monitor = WXLogger.init({
    pid: '<your-pid>',       // Project ID from the ARMS console
    region: 'cn',            // cn: Chinese mainland | sg: nearest to Singapore
});
export default Monitor;

ES module (import):

import WXLogger from './wxLogger.js';
const Monitor = WXLogger.init({
    pid: '<your-pid>',       // Project ID from the ARMS console
    region: 'cn',            // cn: Chinese mainland | sg: nearest to Singapore
});
export default Monitor;

Replace <your-pid> with the project unique ID found in the ARMS console. ARMS automatically generates this ID when it creates a site.

For a full list of initialization parameters, see SDK parameters.

Step 2: Add instrumentation hooks

The SDK uses two hook methods to automatically collect PV, error, API, performance, and health data across your mini program.

Hook the app lifecycle

In app.js, wrap your App configuration with Monitor.hookApp(options) to capture error logs automatically.

import Monitor from '/utils/monitor';

App(Monitor.hookApp({
    onError(err) {
        console.log('Trigger onError:', err);
    },
    onLaunch() {
        console.log('Trigger onLaunch');
    },
    onShow(options) {
    },
    onHide() {
    }
}));

Hook page lifecycles

In each page.js, wrap your Page configuration with Monitor.hookPage(options) to report API calls, page views, and health data automatically.

import Monitor from '/utils/monitor';
// hookPage starts lifecycle-based instrumentation automatically.
Page(Monitor.hookPage({
    data: {},
    onLoad(query) {
    },
    onReady() {
        // The page is loaded.
    },
    onShow() {
    },
    onHide() {
    },
    onUnload() {
    }
}));

Lifecycle method requirements

Hook methodRequired lifecycle methodsWhere to use
hookApponErrorapp.js
hookPageonShow, onHide, onUnloadEach page.js

These lifecycle methods must be present in your code for instrumentation to work.

Step 3: Allowlist security domains

WeChat requires all outbound request domains to be allowlisted. Add the ARMS reporting endpoint to your mini program's valid domain list in the WeChat admin console.

region valueDomain to allowlist
cnhttps://arms-retcode.aliyuncs.com
sghttps://arms-retcode-sg.aliyuncs.com

Step 4: Verify the integration

After you complete the setup:

  1. Build and preview your mini program in WeChat Developer Tools.

  2. Open the Network panel in the developer tools.

  3. Navigate through a few pages in the mini program.

  4. Confirm that requests are sent to your configured ARMS reporting domain (arms-retcode.aliyuncs.com or arms-retcode-sg.aliyuncs.com).

If you see successful outbound requests to the ARMS domain, the SDK integration is working.

SDK methods

Beyond automatic instrumentation, the SDK provides methods for manual reporting and configuration.

MethodParameterDescription
setCommonInfo{[key: string]: string;}Set basic log fields for use cases such as canary releases.
setConfig{[key: string]: string;}Set SDK configuration fields. For details, see SDK reference. The uid parameter is not supported for mini programs. Use setUsername instead.
pageShowNoneReport a PV log entry.
pageHideNoneReport a health log entry.
errorString or ObjectReport an error log entry.
apiSee API reference.Report an API request log entry.
sum / avgStringReport custom sum and average log entries.

Advanced scenarios

Report API results manually

For full control over API reporting:

  1. Set disableHook to true in the SDK configuration to prevent automatic wx.request log reporting.

  2. Call api() to report API results as needed.

Track manually without automatic hooks

To handle all reporting manually instead of using hookApp and hookPage:

  1. Skip hookApp in app.js and hookPage in page.js.

  2. Report page views by calling pageShow() in the onShow method of each Page:

    Important

    Do not call pageShow() together with hookPage(). This causes duplicate PV logs.

        import Monitor from '/utils/monitor';
        Page({
            onShow: function() {
                Monitor.pageShow();
            }
        })
  3. Report health data (including browsing time) by calling pageHide() in the onHide and onUnload methods:

    Important

    Do not call pageHide() together with hookPage(). This causes duplicate health logs.

        import Monitor from '/utils/monitor';
        Page({
            onHide: function() {
                Monitor.pageHide();
            },
            onUnload: function() {
                Monitor.pageHide();
            }
        })

SDK parameters

The following table lists common SDK parameters for WeChat mini program monitoring. For the full parameter list, see SDK reference.

ParameterTypeRequiredDefaultDescription
pidStringYesNoneProject unique ID. Automatically generated when ARMS creates a site.
uidStringNoAuto-generated by SDKUser identifier. The SDK generates this value automatically and updates it every six months.
tagStringNoNoneTag attached to every log entry from this instance.
releaseStringNoundefinedApplication version. Configure this parameter to view reports by version.
environmentStringNoprodDeployment environment. Valid values: prod (online), gray (phased-release), pre (staging), daily (daily), local (local).
sampleIntegerNo1Log sampling ratio (1 to 100). Performance logs and successful API logs are sampled at a 1/sample ratio. For details, see Statistical metrics.
behaviorBooleanNofalseRecord user behavior leading up to errors for troubleshooting.
enableLinkTraceBooleanNofalseEnable front-to-back tracing. For details, see Use the front-to-back tracing feature to diagnose API errors.