Overview

Analytics Kit predefines rich analysis models to help you easily understand user behavior and gain in-depth insights into users, products, and content. As such, you can carry out data-driven operations and make informed decisions about app marketing and product optimization.
Analytics Kit implements the following functions using data collected from apps:

What You Will Create

In this codelab, you will create a demo app based on the provided demo project. After creating a demo app, you will be able to:

What You Will Learn

In this codelab, you will learn how to:

Hardware Requirements

Software Requirements

Required Knowledge

To integrate Analytics Kit, you must complete the following preparations:

For details, please refer to Preparations.
  1. Sign in to AppGallery Connect and clickMy projects.
  2. Find your project and select the app that needs to integrate the Analytics SDK.
  3. Select any menu under HUAWEI Analytics and click Enable Analytics service. (You must have the app management permission to perform this operation.)
  4. On the Project access settings page displayed, set the data processing location and complete other settings. Then click Finish.
  5. Download the configuration file.
  6. In Android Studio, switch to the Project view and copy the agconnect-services.json file to your app-level directory.

Adding SDK Dependencies

  1. Add the Maven repository address and AppGallery Connect dependency to the build.gradle file of your project.
    a) Open the project-level build.gradle file.

    b) Go to buildscript > repositories and configure the Maven repository address for the HMS Core SDK.
    repositories { maven {url 'https://developer.huawei.com/repo/'} }

    c) Go to allprojects > repositories and configure the Maven repository address for the HMS Core SDK.
    repositories { maven {url 'https://developer.huawei.com/repo/'} }

    d) Add the AppGallery Connect dependency to dependencies.
    dependencies { classpath 'com.huawei.agconnect:agcp:1.4.2.300' }
  2. Add the Analytics Kit dependency to the app-level build.gradle file.
    a) Open the app-level build.gradle file.


    b) Add the build dependency to dependencies.
    dependencies { implementation 'com.huawei.hms:hianalytics:6.6.0.300' }

    c) Add the AppGallery Connect plugin configuration. Select a method as required.
    Method 1: Add the following configuration under the declaration in the file header:
    apply plugin: 'com.huawei.agconnect'

    Method 2: Add the plugin configuration in the plugins block.

    plugins { id 'com.android.application' // Add the following configuration: id 'com.huawei.agconnect' }
  3. Click Sync Now or Sync Project with Gradle Files to build a project.

Configuring Obfuscation Scripts

If you are using AndResGuard, add its trustlist to the obfuscation configuration file.

"R.string.hms*", "R.string.connect_server_fail_prompt_toast", "R.string.getting_message_fail_prompt_toast", "R.string.no_available_network_prompt_toast", "R.string.third_app_*", "R.string.upsdk_*", "R.layout.hms*", "R.layout.upsdk_*", "R.drawable.upsdk*", "R.color.upsdk*", "R.dimen.upsdk*", "R.style.upsdk*", "R.string.agc*"

In this section, you will try to build a demo to learn how to use the main APIs of Analytics Kit, for example, the API for recording custom events or setting user attributes.

Download

Initializing the Analytics SDK

  1. Add the following code to import classes from Analytics Kit:
    // TODO: Import classes from Analytics Kit. import com.huawei.hms.analytics.HiAnalytics; import com.huawei.hms.analytics.HiAnalyticsInstance; import com.huawei.hms.analytics.HiAnalyticsTools; import com.huawei.hms.analytics.type.HAEventType; import com.huawei.hms.analytics.type.HAParamType;
  2. Initialize the Analytics SDK using the onCreate method of the first activity to obtain a HiAnalyticsInstance instance.
    @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); // TODO: Initialize Analytics Kit. // Enable Analytics Kit logging. HiAnalyticsTools.enableLog(); // Generate an Analytics Kit instance. HiAnalyticsInstance instance = HiAnalytics.getInstance(this);

Configuring Tracking for a Predefined Event

Report the predefined event ADDPRODUCT2WISHLIST.

// TODO: Report a predefined event. Bundle bundle_pre = new Bundle(); bundle_pre.putString(HAParamType.PRODUCTID, "item_ID"); bundle_pre.putString(HAParamType.PRODUCTNAME, "name"); bundle_pre.putString(HAParamType.CATEGORY, "category"); bundle_pre.putLong(HAParamType.QUANTITY, 100L); bundle_pre.putDouble(HAParamType.PRICE, 10.01); bundle_pre.putDouble(HAParamType.REVENUE, 10.01); bundle_pre.putString(HAParamType.CURRNAME, "currency"); bundle_pre.putString(HAParamType.PLACEID, "location_ID"); instance.onEvent(HAEventType.ADDPRODUCT2WISHLIST, bundle_pre);

Setting User Attributes

Set custom user attributes.

// TODO: Call the setUserProfile method to set user attributes. instance.setUserProfile("userKey","value");

For details about APIs, please refer to the Analytics Kit API Reference.

Enable the debug mode and launch the app using a simulator or real device. Then you can view the reported data in real time.

Enabling the Debug Mode

  1. Run the following command. After the debug mode is enabled, all events will be reported in real time.
    adb shell setprop debug.huawei.hms.analytics.app <package_name>
  2. After the data is successfully reported, you can go to HUAWEI Analytics > App debugging to view the reported data.

Disabling the Debug Mode

Run the following command to disable the debug mode:

adb shell setprop debug.huawei.hms.analytics.app .none.
  1. Sign in to AppGallery Connect and click My projects.
  2. Find your project, and click the app to view its data.
  3. Go to HUAWEI Analytics > Overview > Real-time overview.
  4. Check the data. For details, please refer to the Analytics Kit operation guide.

FAQs

How should I perform initialization?

Initialize Analytics Kit in the main thread by using the onCreate() method of the first app activity. Otherwise, the processing of automatically collected lifecycle events may be affected.

How does Analytics Kit identify users?

Analytics Kit identifies users through an Anonymous Application ID (AAID).

When will the AAID be reset? How does Analytics Kit collect user statistics after AAID reset?

The AAID will be reset in the following scenarios:

What permissions are required for using Analytics Kit?

Analytics Kit requires the following permissions, which have been preset in Analytics Kit. You do not need to apply for these permissions.

Why can't I view the analysis result of the data reported during the test?

  1. When app debugging is disabled, you can only view some analysis results on the Real-time overview page. Analysis results on other pages such as Event analysis and Launch analysis are available only after the corresponding data is processed on the next day.
  2. When app debugging is enabled, the reported test data will not be analyzed and can only be viewed on the App debugging page.

What should I do if a gradle-wrapper.properties file error is reported?

This error may occur if the Gradle version configured in the sample code of Analytics Kit is later than the one you currently use. To correct the error, you are advised to change the Gradle version to an earlier version, for example, gradle-4.10.2-all.

Well done. You have successfully completed this codelab and learned how to:

For more information, please click the following link:

Related documents
To download the sample code, please click the button below:

Download

Code copied