[Huawei Developers] John Wo:
In this article, I am going to create a Flutter project –actually a tiny game- and explain how to implement Analytics Kit. But first, let me inform you about Huawei Analytics Kit a little.

[IMG="https://communityfile-dre.op.hicloud.com/FileServer/getFile/cmtybbs/026/272/220/5190090000026272220.20200805095444.97188569206510090826718711417675:50511012065652:2800:035CE70113E62DEF1F7B4951870EBDFDBD5F2D1C0669D9B9F4C6C58262DB0F66.jpg"]https://communityfile-dre.op.hicloud.com/FileServer/getFile/cmtybbs/026/272/220/5190090000026272220.20200805095444.97188569206510090826718711417675:50511012065652:2800:035CE70113E62DEF1F7B4951870EBDFDBD5F2D1C0669D9B9F4C6C58262DB0F66.jpg[/IMG]​

Huawei Analytics Kit

Huawei Analytics Kit offers you a range of analytics models that help you not only to analyze users’ behavior with preset and custom events, but also gain an insight into your products and contents. So that you can improve your skills about marketing your apps and optimizing your products.

HUAWEI Analytics Kit identifies users and collects statistics on users by anonymous application identifier (AAID). 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.

HUAWEI Analytics Kit supports event management. For each event, maximum 25 parameters; for each app maximum 100 parameters can be defined.

There are 3 types of events: Automatically collected, predefined and custom.

Automatically collected events are collected from the moment you enable the kit in your code. Event IDs are already reserved by HUAWEI Analytics Kit and cannot be reused.

Predefined events include their own Event IDs which are predefined by the HMS Core Analytics SDK based on common application scenarios. The ID of a custom event cannot be the same as a predefined event’s ID. If so, you will create a predefined event instead of a custom event.

Custom events are the events that you can create for your own requirements.

More info about the kit and events.

[IMG="https://communityfile-dre.op.hicloud.com/FileServer/getFile/cmtybbs/026/272/220/5190090000026272220.20200805100338.89185249956516703113484037255348:50511012065652:2800:52D74CA98698371BA5A6A5AE90CDA28DADEC6A174ED6402D3A3FB2F206053DA7.png"]https://communityfile-dre.op.hicloud.com/FileServer/getFile/cmtybbs/026/272/220/5190090000026272220.20200805100338.89185249956516703113484037255348:50511012065652:2800:52D74CA98698371BA5A6A5AE90CDA28DADEC6A174ED6402D3A3FB2F206053DA7.png[/IMG]​

Configuration in AppGallery Connect

Firstly, you will need a Huawei developer account. If you don’t have one, click here and register. It will be activated in 1–2 days.

After signing in to AppGallery Connect, you can add a new project or select an existing project. In the project you choose, add an app. While adding app, make sure you enter the package name right. It should be the same as your Flutter project’s package name.

Also, make sure you set data storage location, enable Analytics kit and add SHA-256 fingerprint to AppGallery Connect.

[IMG="https://communityfile-dre.op.hicloud.com/FileServer/getFile/cmtybbs/026/272/220/5190090000026272220.20200805100441.94633390249235578350879538624584:50511012065652:2800:854245EFBC49194A189CCECFC729A20DAED1519DB88357319F8957CA8148DC31.png"]https://communityfile-dre.op.hicloud.com/FileServer/getFile/cmtybbs/026/272/220/5190090000026272220.20200805100441.94633390249235578350879538624584:50511012065652:2800:854245EFBC49194A189CCECFC729A20DAED1519DB88357319F8957CA8148DC31.png[/IMG]

[IMG="https://communityfile-dre.op.hicloud.com/FileServer/getFile/cmtybbs/026/272/220/5190090000026272220.20200805100536.74432334077775398153961083946949:50511012065652:2800:47CC4C7265E43BE3753EDA4E0C35A2EFE115297E474FA0811ECEB552F362709A.png"]https://communityfile-dre.op.hicloud.com/FileServer/getFile/cmtybbs/026/272/220/5190090000026272220.20200805100536.74432334077775398153961083946949:50511012065652:2800:47CC4C7265E43BE3753EDA4E0C35A2EFE115297E474FA0811ECEB552F362709A.png[/IMG]

How to generate SHA-256 Fingerprint?

In Android Studio, right click on android folder under your project and select Flutter > Open Android module in Android Studio.

[IMG="https://communityfile-dre.op.hicloud.com/FileServer/getFile/cmtybbs/026/272/220/5190090000026272220.20200805100613.38189259511118469785008790920153:50511012065652:2800:2B81A040B9CD944E2F472993BF0C75DE6EC130CC1E110636D70CBE2E7B64F9D8.png"]https://communityfile-dre.op.hicloud.com/FileServer/getFile/cmtybbs/026/272/220/5190090000026272220.20200805100613.38189259511118469785008790920153:50511012065652:2800:2B81A040B9CD944E2F472993BF0C75DE6EC130CC1E110636D70CBE2E7B64F9D8.png[/IMG]

On the right panel, select Gradle and follow the steps that are shown in the picture below. Open signingReport and there is your SHA-256 fingerprint.

[IMG="https://communityfile-dre.op.hicloud.com/FileServer/getFile/cmtybbs/026/272/220/5190090000026272220.20200805100648.01985431098015887949172411877308:50511012065652:2800:1F800E0B713DEA8F0CF3CAFD39EB7A98D27D5A55F002CF8CEABFE9404425DA89.png"]https://communityfile-dre.op.hicloud.com/FileServer/getFile/cmtybbs/026/272/220/5190090000026272220.20200805100648.01985431098015887949172411877308:50511012065652:2800:1F800E0B713DEA8F0CF3CAFD39EB7A98D27D5A55F002CF8CEABFE9404425DA89.png[/IMG]

Copy the code and paste it on the project settings in the AppGallery Connect.

[IMG="https://communityfile-dre.op.hicloud.com/FileServer/getFile/cmtybbs/026/272/220/5190090000026272220.20200805100728.47909485922783308799077940774607:50511012065652:2800:2AC080120FD50CC78C2D718355C7CE3FE81B4EADED3C8FF0A19798BA15921638.png"]https://communityfile-dre.op.hicloud.com/FileServer/getFile/cmtybbs/026/272/220/5190090000026272220.20200805100728.47909485922783308799077940774607:50511012065652:2800:2AC080120FD50CC78C2D718355C7CE3FE81B4EADED3C8FF0A19798BA15921638.png[/IMG]

Integrate HMS to your project

[IMG="https://communityfile-dre.op.hicloud.com/FileServer/getFile/cmtybbs/026/272/220/5190090000026272220.20200805100811.69449365698489842923573258003527:50511012065652:2800:7BB47053D6099F9DC189989D38272D027F27E76E2DE0379334BED36CF6FE530D.png"]https://communityfile-dre.op.hicloud.com/FileServer/getFile/cmtybbs/026/272/220/5190090000026272220.20200805100811.69449365698489842923573258003527:50511012065652:2800:7BB47053D6099F9DC189989D38272D027F27E76E2DE0379334BED36CF6FE530D.png[/IMG]

Download agconnect-services.json file and place it under project_name > android > app.

[IMG="https://communityfile-dre.op.hicloud.com/FileServer/getFile/cmtybbs/026/272/220/5190090000026272220.20200805100844.15351796983123627524473136996986:50511012065652:2800:5579CC21BA8E9B9C2074FED0E9A05769BE89C6C7EDBB4A6DB2C0198131875A56.png"]https://communityfile-dre.op.hicloud.com/FileServer/getFile/cmtybbs/026/272/220/5190090000026272220.20200805100844.15351796983123627524473136996986:50511012065652:2800:5579CC21BA8E9B9C2074FED0E9A05769BE89C6C7EDBB4A6DB2C0198131875A56.png[/IMG]

Add Signing Configuration

Create a file named key.properties under android folder and add your signing configs here.

storeFile file(‘<keystore_file>.jks’)
storePassword ‘<keystore_password>’
keyAlias ‘<key_alias>’
keyPassword ‘<key_password>’

Define your key.properties file by adding the code below, before android block in your app-level build.gradle file.

def keystoreProperties = new Properties()
def keystorePropertiesFile = rootProject.file(‘key.properties’)
if (keystorePropertiesFile.exists()) {
keystoreProperties.load(new FileInputStream(keystorePropertiesFile))
}

TO-DOs in project-level build.gradle

buildscript {
repositories {
google()
jcenter()
maven {url 'http://developer.huawei.com/repo/'} //add this line
}
dependencies {
classpath 'com.android.tools.build:gradle:3.5.0'
classpath 'com.huawei.agconnect:agcp:1.1.1.300' //add this line
}
}
allprojects {
repositories {
google()
jcenter()
maven {url 'http://developer.huawei.com/repo/'} //add this line
}
}

TO-DOs in app-level build.gradle

defaultConfig {
...
minSdkVersion 19 //Increase this to 19
}
//Add these lines
signingConfigs {
release {
keyAlias keystoreProperties['keyAlias']
keyPassword keystoreProperties['keyPassword']
storeFile keystoreProperties['storeFile'] ? file(keystoreProperties['storeFile']) : null
storePassword keystoreProperties['storePassword']
}
}
//Edit buildTypes
buildTypes {
debug {
signingConfig signingConfigs.release
}
release {
signingConfig signingConfigs.release
}
}
//Add dependencies
dependencies {
implementation 'com.huawei.agconnect:agconnect-core:1.0.0.300'
implementation 'com.huawei.hms:hianalytics:5.0.1.300'
}
apply plugin: 'com.huawei.agconnect' //Add this line to the bottom of the page

Add Analytics Kit to your project

There are 2 ways to do this step.

1) Go to developer website and download plugin. In Android Studio create a new folder in your root directory and name it “hms”. Unzip the plugin and paste it in “hms” folder.

[IMG="https://communityfile-dre.op.hicloud.com/FileServer/getFile/cmtybbs/026/272/220/5190090000026272220.20200805101643.88578772030138965521040608705996:50511012065652:2800:C7E44C63374D42F7B5C14EECDA1D28D6C9E9DDA53DAA599F47188B3C1E314C4E.png"]https://communityfile-dre.op.hicloud.com/FileServer/getFile/cmtybbs/026/272/220/5190090000026272220.20200805101643.88578772030138965521040608705996:50511012065652:2800:C7E44C63374D42F7B5C14EECDA1D28D6C9E9DDA53DAA599F47188B3C1E314C4E.png[/IMG]

Then, go to pubspec.yaml and add the plugin under dependencies.

[IMG="https://communityfile-dre.op.hicloud.com/FileServer/getFile/cmtybbs/026/272/220/5190090000026272220.20200805101714.32335025485682348828404834330065:50511012065652:2800:325BB32D9C9A84B63AE2C3832699F8D1E06D6F7348668F6C82EA0300B64F05AC.png"]https://communityfile-dre.op.hicloud.com/FileServer/getFile/cmtybbs/026/272/220/5190090000026272220.20200805101714.32335025485682348828404834330065:50511012065652:2800:325BB32D9C9A84B63AE2C3832699F8D1E06D6F7348668F6C82EA0300B64F05AC.png[/IMG]

2) This way is much easier and also more familiar to Flutter developers. In pub.dev copy the plugin and add it under dependencies as usual.

For both ways, after running pub get command, the plugin is ready to use!

For more information about HMS Core integration, click.

We are all done. Let’s begin coding.

I will make a tiny and very easy game that I belive most of you know the concept: Guess the number!

As you play the game and try to guess the number, Huawei Analytics Kit will collect statistics how many times you guessed.