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 informed decisions about app marketing and product optimization.
Analytics Kit implements the following functions using data collected from apps:
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.
In this codelab, you will learn how to:
To integrate HUAWEI HMS Core services, you must complete the following preparations:
For details, please refer to Integrating the HMS Core SDK.
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.4.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.1.0.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.
If you are using AndResGuard, add its trustlist to 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.
Copy all Java files from the code directory to your project.
Copy all resources files from the res directory to your project.
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.
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);
}
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 emulator.
Tap some buttons to trigger event reporting.
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.
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.
Answer:
Analytics Kit identifies users by their AAID.
Answer:
The AAID will be reset in the following scenarios:
Answer:
Analytics Kit requires the following permissions, which have been preset in Analytics Kit. You do not need to apply for permissions.
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.
Answer:
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:
You can click the button below to download the source code: