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:
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:
In this codelab, you will learn how to:
To integrate Analytics Kit, you must complete the following preparations:
dependencies {
classpath 'com.huawei.agconnect:agcp:1.6.0.300'
}
dependencies {
implementation 'com.huawei.hms:hianalytics:6.7.0.300'
}
apply plugin: 'com.huawei.agconnect'
In Android Studio 4.0 or later, add the plugin configuration
to the plugins block:
plugins {
id 'com.android.application'
// Add the following configuration:
id 'com.huawei.agconnect'
}
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 with the question answering function to learn how to use the main APIs of Analytics Kit. We have prepared the code for you, meaning you only need to follow the instructions in each step below to learn how to use Analytics Kit to report the events you are interested in, such as answering questions, calculating scores, and setting user attributes.
Copy all Kotlin files from the code directory to
your project. Change the package information according to the actual
situation.
Copy all resource files from the res directory to
your project.
<application>
……
<meta-data>
android:name="install_channel"
android:value="AppGallery">
</meta-data>
……
</application>
<application>
<activity android:name=".SettingActivity">
</activity>
</application>
Your app is so far loaded with the required functions. Next, you will use Analytics Kit to report the events that interest you.
// 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.SUBMITSCORE
import com.huawei.hms.analytics.type.HAParamType.SCORE
// TODO: Define a variable for the Analytics Kit instance.
private lateinit var instance: HiAnalyticsInstance
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
// TODO: Initialize Analytics Kit.
// Enable Analytics Kit logging.
HiAnalyticsTools.enableLog()
// Generate an Analytics Kit instance.
instance = HiAnalytics.getInstance(this)
private fun reportAnswerEvt(answer: String) {
// TODO: Report a custom event.
// Event name: Answer
// Event parameters:
// -- question: String
// -- answer:String
// -- answerTime: String
// Initialize parameters.
val bundle = Bundle()
bundle.putString("question", txtQuestion.text.toString().trim())
bundle.putString("answer", answer)
val sdf = SimpleDateFormat("yyyy-MM-dd HH:mm:ss", Locale.ENGLISH)
bundle.putString("answerTime", sdf.format(Date()))
// Report a custom event.
instance.onEvent("Answer", bundle)
}
private fun postScore() {
// TODO: Report a score through the SUBMITSCORE event.
// Initialize parameters.
val bundle = Bundle()
bundle.putLong(SCORE, score.toLong())
// Report a predefined event.
instance.onEvent(SUBMITSCORE, bundle)
}
// TODO: Import classes from Analytics Kit.
import com.huawei.hms.analytics.HiAnalytics
import com.huawei.hms.analytics.HiAnalyticsInstance
// TODO: Define a variable for the Analytics Kit instance.
internal lateinit var instance: HiAnalyticsInstance
// TODO: Generate an Analytics Kit instance.
instance = HiAnalytics.getInstance(this)
btnSave.setOnClickListener {
strFavorSport = editFavorSport.text.toString().trim()
// TODO: Set users' favorite sport using the setUserProfile API.
instance.setUserProfile("favor_sport", strFavorSport)
}
For details about APIs, please refer to the Analytics Kit API Reference.
Start the app in the emulator and trigger event reporting as
instructed.
adb shell setprop debug.huawei.hms.analytics.app <package_name>
After the debug mode is enabled, all triggered events are reported to the App debugging page in real time.
To disable the debug mode, run the following command:
adb shell setprop debug.huawei.hms.analytics.app .none.
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.
Analytics Kit identifies users through an Anonymous Application ID (AAID).
The AAID will be reset in the following scenarios:
Analytics Kit requires the following permissions, which have been preset in Analytics Kit. You do not need to apply for these permissions.
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: