Analytics Kit operates with an event analysis data model.
The model uses the Event-Param-User pattern to record data. It describes events using parameters and describes users using attributes.
Term | Meaning | Description |
|---|---|---|
Event | Event | Describes a specific task completed by a user at a location using a method at a certain time point, including key elements such as who, when, where, how, and what. |
Param | Parameter | Indicates associated parameters reported in events, for example, the product name and product price parameters in the add-to-cart event. These parameters can be used for subsequent in-depth analysis. |
UserProfile | User attribute | Indicates a list of user attributes. Common user attributes include the device model, app version, and custom user attributes such as food preferences. |
Analytics Kit collects the AAID, which is the anonymous ID of an app. A unique AAID is allocated to each app on the same device. The AAID is used for connecting various Huawei services, such as Analytics Kit and Remote Configuration.
The AAID will be reset if the user clears the WeChat mini-program data.
import agconnect from '@agconnect/api'; import '@agconnect/instance'; import '@hw-hmscore/analytics-sdk-miniprogram';
During SDK initialization, a network request will be sent. Initialize the SDK only after users agree to the privacy agreement.
Sign in to AppGallery Connect, click My projects, and click the app for which you want to integrate the Analytics SDK. Go to Project settings, copy the agConnectConfig code snippet to the app.js file of your project, and initialize the Analytics SDK. In addition, you need to sign in to the WeChat Official Accounts Platform and add the url and collector_url configuration in the SDK code snippet to the domain name trustlist.

// TODO: Replace the following code with the configuration of your app.
const agConnectConfig = {
// ...
};
// Configure the AppGallery Connect instance.
agconnect.instance().configInstance(agConnectConfig);
// Initialize the Analytics Kit instance.
let analytics = agconnect.analytics(); import agconnect from '@agconnect/api'; import '@agconnect/instance'; import '@hw-hmscore/analytics-sdk-miniprogram';
// TODO: Replace the following code with the configuration of your app.
const agConnectConfig = {
// ...
};
// Configure the AppGallery Connect instance.
agconnect.instance().configInstance(agConnectConfig);
// Initialize the Analytics Kit instance.
let analytics = agconnect.analytics(); // Enable tracking of the custom event in proper positions of the code.
let eventId = "Complete_Exam";
let eventParams = {
"Score": 95,
"Level": "high"
};
analytics.onEvent(eventId, eventParams); // Enable tracking of the predefined event in proper positions of the code.
let preDefineParams = agconnect.analytics.ParamName;
let preDefineEvents = agconnect.analytics.EventName;
let eventParams = {};
eventParams[preDefineParams.PRODUCTID] = 'item_ID';
eventParams[preDefineParams.PRODUCTNAME] = 'name';
eventParams[preDefineParams.CATEGORY] = 'category';
eventParams[preDefineParams.QUANTITY] = 100;
eventParams[preDefineParams.PRICE] = 10.01;
eventParams[preDefineParams.REVENUE] = 10;
eventParams[preDefineParams.CURRNAME] = 'currency';
eventParams[preDefineParams.PLACEID] = 'location_ID';
analytics.onEvent(preDefineEvents.ADDPRODUCT2WISHLIST, eventParams); After the data is successfully reported, you can go to and view the real-time monitoring data, as shown in the following figure.

For details about how to view reported data in AppGallery Connect, please refer to Real-Time Overview.
During the development, you can enable the debug mode to view the event records in real time, observe the results, and adjust the event reporting policies as needed.
Before the SDK is initialized, add the following code to enable the debug mode. (Do not use the debug mode in the production environment.)
// Enable the debug mode. agconnect.analytics.InitSettings.debugMode = true; // Customize a device ID when the debug mode is enabled. agconnect.analytics.InitSettings.terminalName = "custom terminal name"; // Initialize the Analytics Kit instance. let analytics = agconnect.analytics();
After the debug mode is enabled, all events are reported in real time. You can view the reported data under HUAWEI Analytics > App debugging.

For details about how to view the data in AppGallery Connect, please refer to App Debugging.
By default, the SDK records logs of AppGallery Connect. To disable logging, configure as follows before initializing the SDK:
// Disable logging. agconnect.analytics.InitSettings.logDisabled = true; // Minimum log level, which is case-insensitive. The options are INFO, WARN, and ERROR. agconnect.analytics.InitSettings.logLevel = 'INFO';