Overview

HUAWEI Analytics Kit predefines a large number of 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.
HUAWEI 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 HUAWEI Analytics Kit tenants: 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 Kit). HUAWEI Analytics Kit conducts big data analytics based on the collected information.

What You Will Learn

Hardware Requirements

Software Requirements

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

For details, please refer to Preparations for Integrating HUAWEI HMS Core.

Adding SDK Dependencies

  1. Add the Maven repository address and AppGallery Connect service dependencies in the build.gradle file of the primary project. Open the build.gradle file in the directory of the primary project.
  2. Add the Maven repository address to repositories.

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

    Add AppGallery Connect dependencies to dependencies.

    dependencies { classpath 'com.huawei.agconnect:agcp:1.2.1.301' }
  3. Add the HUAWEI Analytics Kit dependency to the build.gradle file in the app directory. (Set the dependency version based on the release version of each service.) Open the build.gradle file in the app directory.
  4. Add build dependencies to dependencies.

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

    Add the plug-in configuration of AppGallery Connect.

    apply plugin: 'com.huawei.agconnect'
  5. Click Sync now or Sync Project with Gradle Files to build a project.

Configuring Obfuscation Scripts

1.If you have used AndResGuard, add it to the whitelist 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 HUAWEI Analytics Kit. In this section, you will try to write a demo to learn how to use main APIs of HUAWEI Analytics Kit. This is a demo with the question answering function. You do not need to care about service logic. We have prepared ready-made code for you. You need to follow the instructions in each step below to learn how to use HUAWEI Analytics Kit to report the events you care about, such as answering questions, calculating scores, and setting user attributes.

Downloading Sample Code

Download

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

Add following code:

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

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

Modifying the MainActivity Class

Add following code to import classes from HUAWEI 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 following code to define a variable for the instance of HUAWEI Analytics Kit:

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

Add following code in the onCreate Method to initiate HUAWEI 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 following code to finish 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 predefined event. instance.onEvent("Answer", bundle); }

Add following code to finish 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 following code to import classes from HUAWEI Analytics Kit:

// TODO: import classes from Analytics Kit import com.huawei.hms.analytics.HiAnalytics; import com.huawei.hms.analytics.HiAnalyticsInstance;

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

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

Add following code to initialize HUAWEI Analytics Kit:

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

Add 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 Index.

Start your app in the simulator.

Tap some buttons to report events.

  1. Sign in to HUAWEI Developer and click Analytics.
  2. Select an app that has analytics data that you want to check.
  3. Click the AdvancedAnalytics tab.
  4. Check the data. For details, please refer to Huawei Analytics User 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 in App debugging on the Advanced Analysis tab 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: Perform initialization with the onCreate method for the first app activity. Otherwise, the processing of lifecycle events on the automatic collection page may be affected. HUAWEI Analytics Kit must be initialized in the main thread.
2.How does HUAWEI Analytics Kit identify users?
Answer: HUAWEI Analytics Kit identifies users by anonymous application identifier (AAID).
3.When will the AAID be reset? How does HUAWEI Analytics Kit collect statistics on users after the reset?
Answer: The AAID is reset in the following scenarios: (1) Uninstall or reinstall the app. (2) The user clears the app data.
After the AAID is reset, the user will be counted as a new user.
4.Which permissions does HUAWEI Analytics Kit require?
Answer: HUAWEI Analytics Kit requires the following permissions, which have been preset in HUAWEI Analytics Kit. You do not need to apply for permissions.

5.What can I do if I cannot view analysis results of the current reported data in the test?

Answer: (1) When app debugging is disabled, some analysis results can be viewed in Real-time analysis. Other analysis results such as Event analysis and User Behavior can be viewed after processing in the morning of the next day; (2) When app debugging is enabled, the reported test data will not be processed and analyzed, but will only be viewed in App debugging.

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

For more information, please click the following link:

Documentation

You can download the source code in github.com/Huawei/Consumer/tree/master/Codelabs/AnalyticsKit

You can also click the button below to download the source code.

Download

Code copied