目录

Accessing Analytics Kit

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.

Procedure

  1. Import and initialize the Analytics SDK.
    1. Import the Analytics SDK.
      import agconnect from '@agconnect/api'; 
      import '@agconnect/instance'; 
      import '@hw-hmscore/analytics-sdk-miniprogram';
    2. Initialize the Analytics SDK.

      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();
  2. Record defined events using the onEvent API.

    For details about event definitions, please refer to Event Description.

  3. Call APIs of Analytics Kit to implement the corresponding functions. For details, please refer to API Reference.

Sample Code

  1. Import the Analytics SDK.
    import agconnect from '@agconnect/api'; 
    import '@agconnect/instance'; 
    import '@hw-hmscore/analytics-sdk-miniprogram';
  2. After copying the initialization configuration successfully, initialize the Analytics SDK at the entry of the app.js file to obtain a WxHiAnalytics instance. If the copied configuration is incorrect, event tracking cannot be performed.
    // 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();
  3. Enable tracking of the custom event in proper positions of the code.
    // 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);
  4. Enable tracking of the predefined event in proper positions of the code.
    // 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);

Viewing the Real-Time Overview

After the data is successfully reported, you can go to HUAWEI Analytics > Overview > Real-time overview 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.

Setting the Debug Mode

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.

[h2]Enabling the Debug Mode

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.

Disabling Logging for AppGallery Connect

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';