Overview

HUAWEI Analytics Kit predefines rich analytics models to help you clearly understand user behavior and gain in-depth insights into users, products, and content. As such, you can carry out data-driven operations and make strategic 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 can call APIs in the specified way to collect and report the following user device information to the Analytics Kit cloud: ROM version number, device model, app name, package name, channel ID, app version number, operating system version, system language, manufacturer, screen width, screen height, and operation time (some of the information is automatically collected by internal APIs of the HMS Core Analytics SDK). Analytics Kit then conducts big data analytics based on the collected information.

What You Will Learn

In this codelab, you will learn how to:

Hardware Requirements

Software Requirements

To integrate HUAWEI HMS Core services, you must complete the following preparations:

For details, please refer to Integrating the HMS Core SDK.

  1. Sign in to AppGallery Connect and select My projects.
  2. Click the app for which you need to enable Analytics Kit.
  3. Select any menu under HUAWEI Analytics and click Enable Analytics service. (You need the management permission to perform this operation.)
  4. Turn on Enable the API permission. This option is selected by default. If you deselect this option, data will not be reported. If you do not enable the API permission here but the API permission is required later, go to Project Setting > Manage APIs and manually enable the API permission for Analytics Kit.
  5. Download the configuration file.
  6. In Android Studio, switch to the Project view and move the agconnect-services.json file to the app directory in your Android project.

Adding SDK Dependencies

1. Add the Maven repository address and AppGallery Connect service dependencies to the build.gradle file of your project.

a) Open the build.gradle file in the directory of your project.

b) Add the Maven repository address to repositories.

repositories { maven {url 'https://developer.huawei.com/repo/'} }

c) Add the AppGallery Connect dependency to dependencies.

dependencies { classpath 'com.huawei.agconnect:agcp:1.3.1.300' }

2. Add the Analytics Kit dependency to the build.gradle file in the app directory.

a) Open the build.gradle file in the app directory.

b) Add the build dependency to dependencies.

dependencies { implementation 'com.huawei.hms:hianalytics:5.0.3.300' }

c) Add the AppGallery Connect plug-in dependency.

apply plugin: '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 it to the allowlist in the obfuscation script 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 the previous section, you have successfully integrated Analytics Kit. In this section, you will try to write a demo with the question answering function to learn how to use main APIs of Analytics Kit. You do not need to care about service logic. We have prepared ready-made code for you. You only need to follow the instructions in each step below to learn how to use Analytics Kit to report the events you care about, such as answering questions, calculating scores, and setting user attributes.

Downloading the Source Code

Download source code

Extracting Sample Code to the src Directory

Copying Code Files

Copy all Java files from the code directory to your project.

Copying Resource Files

Copy all resources files from the res directory to your project.

Modifying AndroidManifest.xml

Configure the app installation source, for example, AppGallery, in the meta-data field in the application section.

<application <meta-data android:name="install_channel" android:value="AppGallery"> </meta-data> </application>

Declare SettingActivity in the application section.

<application <activity android:name=".SettingActivity"> </activity> </application>

So far, your app has been equipped with the required service functions. Next, you will use Analytics Kit to report the events you care about.

Modifying the MainActivity Class

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 static com.huawei.hms.analytics.type.HAEventType.*; import static com.huawei.hms.analytics.type.HAParamType.*;

Add the following code to define a variable for the Analytics Kit instance:

private int score = 0; // TODO: Define a var for Analytics Instance HiAnalyticsInstance instance;

Add the following code in the onCreate method to initialize Analytics Kit:

@Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); // TODO: Initiate Analytics Kit // Enable Analytics Kit Log HiAnalyticsTools.enableLog(); // Generate the Analytics Instance instance = HiAnalytics.getInstance(this); txtQuestion = (TextView)findViewById(R.id.question_text_view); txtQuestion.setText(questions[curQuestionIdx]);

Add the following code to run the reportAnswerEvt method:

private void reportAnswerEvt(String answer) { // TODO: Report a customized Event // Event Name: Answer // Event Parameters: // -- question: String // -- answer:String // -- answerTime: String // Initialize parameters. Bundle bundle = new Bundle(); bundle.putString("question", txtQuestion.getText().toString().trim()); bundle.putString("answer",answer); SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss", Locale.ENGLISH); bundle.putString("answerTime",sdf.format(new Date())); // Report a custom event. instance.onEvent("Answer", bundle); }

Add the following code to run the postScore method:

private void postScore() { // TODO: Report score by using SUBMITSCORE Event // Initiate Parameters Bundle bundle = new Bundle(); bundle.putLong(SCORE, score); // Report a predefined Event instance.onEvent(SUBMITSCORE, bundle); }

Modifying the SettingActivity Class

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;

Add the following code to define a variable for the Analytics Kit instance:

// TODO: Define a var for Analytics Instance HiAnalyticsInstance instance;

Add the following code to initialize Analytics Kit:

// TODO: Generate Analytics Kit Instance instance = HiAnalytics.getInstance(this);

Add the following code to save user attributes:

btnSave.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { editFavorSport = (EditText)findViewById(R.id.edit_favoraite_sport); strFavorSport = editFavorSport.getText().toString().trim(); // TODO: save favorite sport by user setUserProperty instance.setUserProfile("favor_sport",strFavorSport); } });

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

Start your app in the simulator.

Tap some buttons to trigger event reporting.

  1. Sign in to AppGallery Connect and click My projects.
  2. Click the app to manage.
  3. Go to HUAWEI Analytics > Overview > Real-time monitoring.
  4. Check the data. For details, please refer to HUAWEI Analytics Kit Operation Guide.

App Debugging

Run the following command to enable the debug mode on an Android device:

adb shell setprop debug.huawei.hms.analytics.app <package_name>

After the debug mode is enabled, all events are reported in real time on the App debugging page. To disable the debug mode, run the following command:

adb shell setprop debug.huawei.hms.analytics.app .none.

View the reported debugging data.

FAQs

1. When should I perform initialization?

Answer:
Initialize Analytics Kit in the main thread by using the onCreate method of the first app activity. Otherwise, the processing of lifecycle events on the automatic collection page may be affected.

2. How does Analytics Kit identify users?

Answer:
Analytics Kit identifies users by their AAID.

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

Answer:
The AAID will be reset in the following scenarios:

4. What permissions are required for using Analytics Kit?

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

5. Why cannot I view the analysis result of the data reported during the test?

Answer:

(1) When app debugging is disabled, you can only view some analysis results on the Real-time monitoring page. Analysis results on the Event analysis and Activity 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.

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

For more information, please click the following link:

Documentation

Sample code on GitHub:

https://github.com/HMS-Core/hms-analytics-demo-android

You can click the button below to download the source code:

Download source code

Code copied