Mini Program Application Integration¶
By importing the SDK file, you can collect performance metrics, error logs, and resource request data from Mini Program applications, and report them to the Guance platform for visual analysis of Mini Program performance.
Prerequisites (DataKit Integration)¶
- Install DataKit;
- Configure the RUM Collector;
- Ensure DataKit is publicly accessible and has the IP geolocation database installed.
Getting Started¶
- Navigate to Real User Monitoring (RUM) > Create Application > Mini Program;
- Enter the application name;
- Enter the application ID;
-
Select the application integration method:
-
Public DataWay: Receives RUM data directly without installing the DataKit collector.
- Local Environment Deployment: Receives RUM data after meeting the prerequisites.
Integration Methods¶
- Ensure DataKit is installed and configured as publicly accessible with the IP geolocation database installed;
- Obtain parameters such as
applicationId,env, andversionfrom the console, then integrate the application; - When integrating the SDK, set
datakitOriginto the domain or IP of DataKit.
- Obtain parameters such as
applicationId,clientToken, andsitefrom the console, then integrate the application; - When integrating the SDK, you do not need to configure
datakitOrigin; data is sent to the public DataWay by default.
Usage¶
Import the code in the app.js file of your Mini Program as follows:
Note: The import must be placed before the App() initialization.
For the NPM package import method, refer to the WeChat official npm import guide.
const { datafluxRum } = require('@cloudcare/rum-miniapp')
// Initialize RUM
datafluxRum.init({
datakitOrigin: '<DATAKIT ORIGIN>', // Required, Datakit domain address. Add the domain to the whitelist in the WeChat Mini Program admin console.
site: "http://172.16.212.186:9529", // Domain of the public DataWay site
clientToken: "a993f53a8ea04bc6b9350e5e670a3a3b", // Client token required for public DataWay reporting, generated when creating the application in the Guance console
applicationId: '<Application ID>', // Required, application ID generated by the Guance platform
env: 'testing', // Optional, environment of the Mini Program
version: '1.0.0', // Optional, version of the Mini Program
service: 'miniapp', // Service name of the current application
trackInteractions: true,
traceType: 'ddtrace', // Optional, defaults to ddtrace. Currently supports 6 types: ddtrace, zipkin, skywalking_v3, jaeger, zipkin_single_header, w3c_traceparent
allowedTracingOrigins: ['https://api.example.com',/https:\/\/.*\.my-api-domain\.com/], // Optional, list of all requests allowed to inject the headers required by the trace collector. Can be request origins or regular expressions.
allowTraceHeaderWithoutSession: true, // Injects Trace Header even for unsampled sessions without reporting RUM data
})
Download the file and import it locally.
const { datafluxRum } = require('./lib/dataflux-rum-miniapp.js')
// Initialize RUM
datafluxRum.init({
datakitOrigin: '<DATAKIT ORIGIN>', // Required, Datakit domain address. Add the domain to the whitelist in the WeChat Mini Program admin console.
site: "http://172.16.212.186:9529", // Domain of the public DataWay site
clientToken: "a993f53a8ea04bc6b9350e5e670a3a3b", // Client token required for public DataWay reporting, generated when creating the application in the Guance console
applicationId: '<Application ID>', // Required, application ID generated by the Guance platform
env: 'testing', // Optional, environment of the Mini Program
version: '1.0.0', // Optional, version of the Mini Program
service: 'miniapp', // Service name of the current application
trackInteractions: true,
traceType: 'ddtrace', // Optional, defaults to ddtrace. Currently supports 6 types: ddtrace, zipkin, skywalking_v3, jaeger, zipkin_single_header, w3c_traceparent
allowedTracingOrigins: ['https://api.example.com',/https:\/\/.*\.my-api-domain\.com/], // Optional, list of all requests allowed to inject the headers required by the trace collector. Can be request origins or regular expressions.
allowTraceHeaderWithoutSession: true, // Injects Trace Header even for unsampled sessions without reporting RUM data
})
Configuration¶
Initialization Parameters¶
| Parameter | Type | Required | Default | Description |
|---|---|---|---|---|
applicationId |
String | Yes | Application ID created from Guance. | |
datakitOrigin |
String | Yes | DataKit data reporting origin; ❗️ You need to add the request whitelist in the Mini Program admin console. |
|
site |
String | Yes (required for public DataWay reporting) |
Domain of the public DataWay site. Note: protocol (including //), domain (or IP address) [and port number]. For example: https://www.dataway.com, http://100.20.34.3:8088 |
|
clientToken |
String | Yes (required for public DataWay) |
Client token required for public DataWay reporting, generated when creating the application in the Guance console. | |
env |
String | No | Current environment of the Mini Program application, e.g., prod: production environment; gray: canary environment; pre: pre-release environment; common: daily environment; local: local environment. | |
version |
String | No | Version number of the Mini Program application. | |
service |
String | No | Service name of the current application, defaults to miniapp. Customizable. |
|
sampleRate |
Number | No | 100 |
Percentage of metric data collection: 100 means collect all, 0 means collect none. |
sessionSampleRate |
Number | No | 100 |
Compatible alias for sampleRate. When both are set, sampleRate takes precedence. |
remoteConfiguration |
Boolean | No | false |
Whether to enable remote configuration. The SDK starts with local configuration and then asynchronously fetches and applies supported configuration items. |
remoteConfigration |
Boolean | No | false |
Legacy spelling compatibility alias for remoteConfiguration, not recommended for new projects. |
remoteConfigurationFetchTimeout |
Number | No | 3000 |
Timeout for remote configuration requests in milliseconds. If the request fails or times out, the local configuration continues to be used. |
trackInteractions |
Boolean | No | false |
Whether to enable user interaction collection. |
trackResourceQueryString |
Boolean | No | false |
Whether to collect the query string of request URLs. The query string may contain tokens or user IDs; enable only after confirming security. |
trackRequestErrorResponseBody |
Boolean | No | false |
Whether to write the response body of failed requests into the error stack. The response body may contain sensitive data. |
requestErrorResponseLengthLimit |
Number | No | 32768 |
Maximum number of characters from the failed request response body that can be written into the error stack. Only effective when trackRequestErrorResponseBody is enabled. |
trackLaunchOptions |
Boolean | No | false |
Whether to collect query and referrerInfo from Mini Program startup parameters. |
beforeSend |
Function | No | Callback before data enters the send queue, can modify events; returning false discards non-View events. Exceptions in the callback do not interrupt business logic or the SDK. |
|
userId / user_id |
String | No | Sets the logged-in user ID during initialization; can also be set after initialization by calling setUser({ id }). |
|
traceType |
Enum | No | ddtrace |
Configures the trace tool type. Defaults to ddtrace if not set. Currently supports 6 data types: ddtrace, zipkin, skywalking_v3, jaeger, zipkin_single_header, w3c_traceparent.❗️ 1. opentelemetry supports 4 types: zipkin_single_header, w3c_traceparent, zipkin, jaeger.2. To configure the corresponding traceType, you need to set different Access-Control-Allow-Headers for the corresponding API service. Refer to How APM Connects to RUM. |
traceId128Bit |
Boolean | No | false |
Whether to generate traceID in 128-bit format. Corresponds to traceType. Currently supports zipkin and jaeger. |
allowedTracingOrigins |
Array | No | [] |
[New] List of all requests allowed to inject the headers required by the ddtrace collector. Can be request origins or regular expressions. Origin: protocol (including //), domain (or IP address) [and port number]. Example: ["https://api.example.com", /https:\\/\\/._\\.my-api-domain\\.com/] |
allowTraceHeaderWithoutSession |
Boolean | No | false |
Whether to inject Trace Header into requests matching allowedTracingOrigins even when the current session is not sampled. Enabling this does not force sampling or reporting of RUM data for that session. |
isIntakeUrl |
Function | No | function(url) {return false} |
Custom function to determine whether to collect resource data based on the request URL. Defaults to collecting all. Returns false to collect, true to skip.❗️ 1. The return value of this function must be of Boolean type; otherwise it is considered invalid. 2. Requires SDK version 2.1.10 or higher. |
Trace Header for Unsampled Sessions¶
allowTraceHeaderWithoutSession defaults to false. When set to true, even if the current session is not sampled for RUM, the SDK still injects Trace Header into requests matching allowedTracingOrigins. This configuration does not force sampling or create a new session, and does not report RUM data (View, Action, Resource, Error, etc.) for unsampled sessions.
Notes¶
- The DataKit domain corresponding to
datakitOriginmust be added to the request whitelist in the WeChat Mini Program admin console. - Currently, the WeChat Mini Program resource request APIs
wx.requestandwx.downloadFiledo not return theprofilefield on iOS systems, which results in incomplete timing-related data in the collected resource information. There is currently no solution: request, downloadFile, API support status. - When
trackInteractions(user interaction collection) is enabled, due to limitations of WeChat Mini Programs, the content and structure data of controls cannot be collected. Therefore, the Mini Program SDK adopts a declarative programming approach: by setting thedata-nameattribute in the wxml file, you can add a name to interactive elements for easier identification of operation records. Example: