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 thepidautomatically 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
Create a
wxLogger.jsfile in the/utilsfolder of your mini program project.Copy the content of the SDK JS file into
wxLogger.js.
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 method | Required lifecycle methods | Where to use |
|---|---|---|
hookApp | onError | app.js |
hookPage | onShow, onHide, onUnload | Each 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 value | Domain to allowlist |
|---|---|
cn | https://arms-retcode.aliyuncs.com |
sg | https://arms-retcode-sg.aliyuncs.com |
Step 4: Verify the integration
After you complete the setup:
Build and preview your mini program in WeChat Developer Tools.
Open the Network panel in the developer tools.
Navigate through a few pages in the mini program.
Confirm that requests are sent to your configured ARMS reporting domain (
arms-retcode.aliyuncs.comorarms-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.
| Method | Parameter | Description |
|---|---|---|
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. |
pageShow | None | Report a PV log entry. |
pageHide | None | Report a health log entry. |
error | String or Object | Report an error log entry. |
api | See API reference. | Report an API request log entry. |
sum / avg | String | Report custom sum and average log entries. |
Advanced scenarios
Report API results manually
For full control over API reporting:
Set
disableHooktotruein the SDK configuration to prevent automaticwx.requestlog reporting.Call
api()to report API results as needed.
Track manually without automatic hooks
To handle all reporting manually instead of using hookApp and hookPage:
Skip
hookAppinapp.jsandhookPageinpage.js.Report page views by calling
pageShow()in theonShowmethod of each Page:ImportantDo not call
pageShow()together withhookPage(). This causes duplicate PV logs.import Monitor from '/utils/monitor'; Page({ onShow: function() { Monitor.pageShow(); } })Report health data (including browsing time) by calling
pageHide()in theonHideandonUnloadmethods:ImportantDo not call
pageHide()together withhookPage(). 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.
| Parameter | Type | Required | Default | Description |
|---|---|---|---|---|
pid | String | Yes | None | Project unique ID. Automatically generated when ARMS creates a site. |
uid | String | No | Auto-generated by SDK | User identifier. The SDK generates this value automatically and updates it every six months. |
tag | String | No | None | Tag attached to every log entry from this instance. |
release | String | No | undefined | Application version. Configure this parameter to view reports by version. |
environment | String | No | prod | Deployment environment. Valid values: prod (online), gray (phased-release), pre (staging), daily (daily), local (local). |
sample | Integer | No | 1 | Log sampling ratio (1 to 100). Performance logs and successful API logs are sampled at a 1/sample ratio. For details, see Statistical metrics. |
behavior | Boolean | No | false | Record user behavior leading up to errors for troubleshooting. |
enableLinkTrace | Boolean | No | false | Enable front-to-back tracing. For details, see Use the front-to-back tracing feature to diagnose API errors. |