Mini Program Access¶
By importing the SDK file, you can collect performance metrics, error logs, and resource request data from Mini Programs, and report them to the TrueWatch platform to visualize and analyze the performance of Mini Programs.
Prerequisites (DataKit Access)¶
- Install DataKit;
- Configure the RUM Collector;
- Make DataKit publicly accessible and install the IP geolocation database.
Getting Started¶
- Go to Real User Monitoring (RUM) > Create App > Mini Program;
- Enter the app name;
- Enter the app ID;
-
Select the app access method:
-
Public DataWay: Receives RUM data directly without installing the DataKit collector.
- Local Deployment: Receives RUM data after meeting the prerequisites.
Access Methods¶
- Ensure DataKit is installed and configured to be publicly accessible, and install the IP geolocation database;
- Obtain the
applicationId,env,versionand other parameters from the console, then start accessing the app; - When integrating the SDK, set
datakitOriginto the DataKit domain or IP.
- Obtain the
applicationId,clientToken,siteand other parameters from the console, then start accessing the app; - When integrating the SDK, no
datakitOriginconfiguration is required; data is sent to the public DataWay by default.
Usage¶
Import the code in the Mini Program's app.js file as follows:
Note: The import must be placed before App() initialization.
For the NPM package import method, refer to the official WeChat npm import guide
const { datafluxRum } = require('@cloudcare/rum-miniapp')
// Initialize Rum
datafluxRum.init({
datakitOrigin: '<DATAKIT ORIGIN>',// Required, Datakit domain address. You need to add the domain to the whitelist in the WeChat Mini Program management console.
site: "http://172.16.212.186:9529", // Domain of the site corresponding to the public DataWay
clientToken: "a993f53a8ea04bc6b9350e5e670a3a3b", // Client token required for public DataWay reporting, generated when creating the app in the TrueWatch console.
applicationId: '<App ID>', // Required, app ID generated by the dataflux platform
env: 'testing', // Optional, Mini Program environment
version: '1.0.0', // Optional, Mini Program version
service: 'miniapp', // Service name of the current app
trackInteractions: true,
traceType: 'ddtrace', // Optional, default is ddtrace. Currently supports 6 types: ddtrace, zipkin, skywalking_v3, jaeger, zipkin_single_header, w3c_traceparent
allowedTracingUrls: ['https://api.example.com/v1/', /https:\/\/.*\.my-api-domain\.com\/v2\//], // Matches full request URL
allowTraceHeaderWithoutSession: true, // Injects Trace Header even if the Session is not sampled, 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. You need to add the domain to the whitelist in the WeChat Mini Program management console.
site: "http://172.16.212.186:9529", // Domain of the site corresponding to the public DataWay
clientToken: "a993f53a8ea04bc6b9350e5e670a3a3b", // Client token required for public DataWay reporting, generated when creating the app in the TrueWatch console.
applicationId: '<App ID>', // Required, app ID generated by the dataflux platform
env: 'testing', // Optional, Mini Program environment
version: '1.0.0', // Optional, Mini Program version
service: 'miniapp', // Service name of the current app
trackInteractions: true,
traceType: 'ddtrace', // Optional, default is ddtrace. Currently supports 6 types: ddtrace, zipkin, skywalking_v3, jaeger, zipkin_single_header, w3c_traceparent
allowedTracingUrls: ['https://api.example.com/v1/', /https:\/\/.*\.my-api-domain\.com\/v2\//], // Matches full request URL
allowTraceHeaderWithoutSession: true, // Injects Trace Header even if the Session is not sampled, without reporting RUM data
})
Configuration¶
Initialization Parameters¶
| Parameter | Type | Required | Default | Description |
|---|---|---|---|---|
applicationId |
String | Yes | The app ID created from TrueWatch. | |
datakitOrigin |
String | Yes | DataKit data reporting Origin; ❗️ You need to add the request whitelist in the Mini Program management console. |
|
site |
String | Yes (required for public dataway reporting) |
Domain of the site corresponding to the public DataWay. Note: protocol (including //), domain (or IP address) [and port number] e.g., 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 app in the TrueWatch console. | |
env |
String | No | Current environment of the Mini Program app, e.g., prod: production; gray: canary; pre: pre-release; common: daily; local: local. | |
version |
String | No | Version number of the Mini Program app. | |
service |
String | No | Service name of the current app, default is miniapp, supports custom configuration. |
|
sampleRate |
Number | No | 100 |
Percentage of metric data collection: 100 means collect all, 0 means collect none. |
sessionSampleRate |
Number | No | 100 |
Compatibility 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, then asynchronously fetches and applies supported configuration items. |
remoteConfigration |
Boolean | No | false |
Legacy spelling compatibility for remoteConfiguration, not recommended for new projects. |
remoteConfigurationFetchTimeout |
Number | No | 3000 |
Timeout for remote configuration request in milliseconds. If the request fails or times out, local configuration continues to be used. |
trackInteractions |
Boolean | No | false |
Whether to enable user action collection. |
trackResourceQueryString |
Boolean | No | false |
Whether to collect the query string of the request URL. The query string may contain tokens or user IDs. Only enable 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 allowed for the response body of failed requests to be written into the error stack. Only effective when trackRequestErrorResponseBody is enabled. |
trackLaunchOptions |
Boolean | No | false |
Whether to collect query and referrerInfo from the Mini Program launch options. |
beforeSend |
Function | No | Callback before data enters the send queue, can modify the event; returning false discards non-View events. Exceptions in the callback will not interrupt business logic or SDK. |
|
userId / user_id |
String | No | Set 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. Default is ddtrace if not set. Currently supports 6 types: ddtrace, zipkin, skywalking_v3, jaeger, zipkin_single_header, w3c_traceparent.❗️ 1. opentelemetry supports zipkin_single_header, w3c_traceparent, zipkin, jaeger.2. Configuring the corresponding traceType requires setting different Access-Control-Allow-Headers on the corresponding API service. See Connect RUM with APM. |
traceId128Bit |
Boolean | No | false |
Whether to generate a 128-bit traceID, corresponding to traceType. Currently supports zipkin, jaeger. |
allowedTracingUrls |
Array |
No | [] |
List of full request URL matches allowed to inject Trace Headers. Strings are matched by URL prefix; RegExp and Function receive the full URL; Objects use { match, traceType } to specify the propagation type for a single rule. Requires version 2.2.19 or later. |
allowedTracingOrigins |
Array |
No | Deprecated compatibility configuration, only matches by request Origin exactly; new projects should use allowedTracingUrls. When both are set, allowedTracingUrls takes precedence. |
|
allowTraceHeaderWithoutSession |
Boolean | No | false |
Whether to still inject Trace Headers for requests matching allowedTracingUrls when the current Session is not sampled. Enabling this does not force sampling or report RUM data for that Session. |
isIntakeUrl |
Function | No | function(url) {return false} |
Custom method to determine whether to collect resource data for a given request resource URL. Default is to collect all. Returns: false means collect, true means do not collect.❗️ 1. The return value of this parameter method must be of type Boolean, otherwise it is considered invalid. 2. Requires version 2.1.10 or later. |
Trace Header URL Matching¶
allowedTracingUrls uses the full request URL for matching. Strings use prefix matching; RegExp and Function receive the full URL. To specify a different propagation type for a specific rule, use an object containing both match and traceType:
allowedTracingUrls: [
'https://api.example.com/v1/',
/https:\/\/.*\.my-api-domain\.com\/v2\//,
function (url) {
return url.indexOf('https://internal.example.com/') === 0
},
{ match: 'https://otel.example.com/', traceType: 'w3c_traceparent' },
]
allowedTracingOrigins is only for backward compatibility: both strings and regular expressions match against the request Origin. If both parameters are configured, the SDK only uses allowedTracingUrls.
Trace Header for Non-Sampled Sessions¶
allowTraceHeaderWithoutSession defaults to false. When set to true, even if the current Session is not sampled by RUM, the SDK still injects Trace Headers into requests that match allowedTracingUrls. This configuration does not force sampling or create a new Session, nor does it report View, Action, Resource, Error, or other RUM data for the non-sampled Session.
Notes¶
- The DataKit domain corresponding to
datakitOriginmust be added to the request whitelist in the Mini Program management console. - Because the current WeChat Mini Program request resource APIs
wx.requestandwx.downloadFiledo not return theprofilefield on iOS systems, the collected resource information related to timing data may be incomplete. There is currently no solution: request, downloadFile, API support status. - When
trackInteractions(user action collection) is enabled, due to the limitations of WeChat Mini Programs, the content and structure data of controls cannot be collected. Therefore, in the Mini Program SDK, we adopt declarative programming. By setting thedata-nameattribute in the wxml file, you can add names to interactive elements for easier identification of operation records later. For example: