[Huawei Developers] Freemind R:
More information like this, you can visit HUAWEI Developer Forum​

Hello everyone,

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:50510820054828:2800:6074973E77033D9072EACEC94CC2057EFA738EAFDC32C8B247778C0F13120182.jpg"]https://communityfile-dre.op.hicloud.com/FileServer/getFile/cmtybbs/026/272/220/5190090000026272220.20200805095444.97188569206510090826718711417675:50510820054828:2800:6074973E77033D9072EACEC94CC2057EFA738EAFDC32C8B247778C0F13120182.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:50510820054828:2800:22FB94F3A0832B6375AB6019642DD4F0EC96C53D6674BBF9F42752FE956739BA.png"]https://communityfile-dre.op.hicloud.com/FileServer/getFile/cmtybbs/026/272/220/5190090000026272220.20200805100338.89185249956516703113484037255348:50510820054828:2800:22FB94F3A0832B6375AB6019642DD4F0EC96C53D6674BBF9F42752FE956739BA.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:50510820054828:2800:A847C296F09D25E54651FFBA46F908E8D40373E2AF854307224EE750E7F27AFF.png"]https://communityfile-dre.op.hicloud.com/FileServer/getFile/cmtybbs/026/272/220/5190090000026272220.20200805100441.94633390249235578350879538624584:50510820054828:2800:A847C296F09D25E54651FFBA46F908E8D40373E2AF854307224EE750E7F27AFF.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:50510820054828:2800:4C26E7C91207EACA53CE2BC02ED1F79FA6E472662A3E374AD86713B0DF70A3A9.png"]https://communityfile-dre.op.hicloud.com/FileServer/getFile/cmtybbs/026/272/220/5190090000026272220.20200805100613.38189259511118469785008790920153:50510820054828:2800:4C26E7C91207EACA53CE2BC02ED1F79FA6E472662A3E374AD86713B0DF70A3A9.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:50510820054828:2800:1949C66CCD5610A3EC4429C03CDEDEABE8BF36714927E15BCDABF2229723A883.png"]https://communityfile-dre.op.hicloud.com/FileServer/getFile/cmtybbs/026/272/220/5190090000026272220.20200805100648.01985431098015887949172411877308:50510820054828:2800:1949C66CCD5610A3EC4429C03CDEDEABE8BF36714927E15BCDABF2229723A883.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:50510820054828:2800:5A227EC5444C49583E1CF96287D3920CE223D75FE378C7AA06CCFEB61D5602BA.png"]https://communityfile-dre.op.hicloud.com/FileServer/getFile/cmtybbs/026/272/220/5190090000026272220.20200805100728.47909485922783308799077940774607:50510820054828:2800:5A227EC5444C49583E1CF96287D3920CE223D75FE378C7AA06CCFEB61D5602BA.png[/IMG]

Integrate HMS to your project

[IMG="https://communityfile-dre.op.hicloud.com/FileServer/getFile/cmtybbs/026/272/220/5190090000026272220.20200805100811.69449365698489842923573258003527:50510820054828:2800:37CB09A68E510A4B0C4358516BE8EECE102CE43A0E70E0AB4BAF098A2326234A.png"]https://communityfile-dre.op.hicloud.com/FileServer/getFile/cmtybbs/026/272/220/5190090000026272220.20200805100811.69449365698489842923573258003527:50510820054828:2800:37CB09A68E510A4B0C4358516BE8EECE102CE43A0E70E0AB4BAF098A2326234A.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:50510820054828:2800:AE7EA823C2661990594D535CCAF4CA387BF2711A7837FDCF797A917B7B6EDD69.png"]https://communityfile-dre.op.hicloud.com/FileServer/getFile/cmtybbs/026/272/220/5190090000026272220.20200805100844.15351796983123627524473136996986:50510820054828:2800:AE7EA823C2661990594D535CCAF4CA387BF2711A7837FDCF797A917B7B6EDD69.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:50510820054828:2800:FA35199AAC4B871D29763D805C9440492EC0A1A779B4661C8DD3B63FF04BA76E.png"]https://communityfile-dre.op.hicloud.com/FileServer/getFile/cmtybbs/026/272/220/5190090000026272220.20200805101643.88578772030138965521040608705996:50510820054828:2800:FA35199AAC4B871D29763D805C9440492EC0A1A779B4661C8DD3B63FF04BA76E.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:50510820054828:2800:C8E3A50E22C6591646B23C380F683DED94B0216A3129AD9A344DC4BCF83EE0AB.png"]https://communityfile-dre.op.hicloud.com/FileServer/getFile/cmtybbs/026/272/220/5190090000026272220.20200805101714.32335025485682348828404834330065:50510820054828:2800:C8E3A50E22C6591646B23C380F683DED94B0216A3129AD9A344DC4BCF83EE0AB.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.

[IMG="https://communityfile-dre.op.hicloud.com/FileServer/getFile/cmtybbs/026/272/220/5190090000026272220.20200805101737.39925616270054290536541430762274:50510820054828:2800:6818BC279A583652FD72CFBB046936A40DEA2A148D6346AE62BD35E49ACDAFC5.png"]https://communityfile-dre.op.hicloud.com/FileServer/getFile/cmtybbs/026/272/220/5190090000026272220.20200805101737.39925616270054290536541430762274:50510820054828:2800:6818BC279A583652FD72CFBB046936A40DEA2A148D6346AE62BD35E49ACDAFC5.png[/IMG]

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.

Make a simple game with Flutter

First, let’s write the method to create a random number. You should import ‘dart:math’ for this.

_setRandomNumber() {
Random random = Random();
int number = random.nextInt(100); // from 0 to 99 included
return number;
}

And call it in initState

@override
void initState() {
randomNumber = _setRandomNumber();
super.initState();
}

We will need a TextField and a button to check user’s guess.

Column(
mainAxisAlignment: MainAxisAlignment.center,
crossAxisAlignment: CrossAxisAlignment.stretch,
children: <Widget>[
TextField(
controller: _controller,
decoration: InputDecoration(
hintText: "Enter Your Guess [0-99]",
border: new OutlineInputBorder(borderSide: BorderSide()),
),
keyboardType: TextInputType.number,
inputFormatters: <TextInputFormatter>[
WhitelistingTextInputFormatter.digitsOnly
],
onChanged: (value) {
guess = int.parse(value);
},
enabled: _isFound ? false : true, //If user guesses the number right, textfield will be disabled
),
RaisedButton(
child: Text("OK!"),
onPressed: () {
if (!_isFound) {
_controller.text = "";
_count++;
_compareValues();
}
},
),
],
)

We need a method if the user guessed the number right or not.

_compareValues() {
if (guess == randomNumber) {
setState(() {
_isFound = true;
_message =
"Correct! The number was $randomNumber.\nYou guessed it in $_count times.";
});
} else if (guess > randomNumber) {
setState(() {
_message = "Lower!";
});
} else {
setState(() {
_message = "Higher!";
});
}
}
}

Let’s add a message Text in Column widget to give hints to user, also a replay button.

Column(
...
Text(
_message,
textAlign: TextAlign.center,
style: TextStyle(fontSize: 24),
),
_isFound //If user guesses the number right, iconButton will appear, otherwise it won't
? IconButton(
icon: Icon(
Icons.refresh,
size: 40,
),
onPressed: () {
setState(() {
//reset all variables and set a new random number.
randomNumber = _setRandomNumber();
_isFound = false;
_count = 0;
_message = "";
});
},
)
: Text("")
],
),

We have done a simple but fun game. Let’s play it!

[IMG="https://communityfile-dre.op.hicloud.com/FileServer/getFile/cmtybbs/026/272/220/5190090000026272220.20200805102149.95365711385211087577790688536530:50510820054828:2800:83BBF111C6C0C6614C2CA21D0799BD3938F8E67708D0DDEC7F98B68A7227E7E7.gif"]https://communityfile-dre.op.hicloud.com/FileServer/getFile/cmtybbs/026/272/220/5190090000026272220.20200805102149.95365711385211087577790688536530:50510820054828:2800:83BBF111C6C0C6614C2CA21D0799BD3938F8E67708D0DDEC7F98B68A7227E7E7.gif[/IMG]

Define HMS Analytics Kit and send events

As we’re done with the widgets, we will define the kit and enable logs.

class _MyHomePageState extends State<MyHomePage> {
final HMSAnalytics hmsAnalytics = new HMSAnalytics();
Future<void> _enableLog() async {
await hmsAnalytics.enableLog();
}
...
@override
void initState() {
_enableLog();
randomNumber = _setRandomNumber();
super.initState();
}
}

Once we call _enableLog(), we are ready to see auto collected events on AppGallery Connect.

What about our custom events? How can we send custom events and see them?

We have _count variable and every time user clicks OK! button, it increases. Now we will map it and send it as a custom event. We need a name for custom event, and a map value.

Future<void> _sendEvent(int count) async {
String name = "USERS_RESULTS";
Map<String, String> value = {
'number_of_guesses': count.toString()
};
await hmsAnalytics.onEvent(name, value);
}

And we call it when we are sure that user guessed the number right. In _compareValues method.

_compareValues() {
if (guess == randomNumber) {
...
_sendEvent(_count); //We know that user guessed the number right.
} else if (guess > randomNumber) {
...
} else {
...
}
}
}

Let’s go back to AppGallery Connect. In the left panel, under Management section click Events.

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

[IMG="https://communityfile-dre.op.hicloud.com/FileServer/getFile/cmtybbs/026/272/220/5190090000026272220.20200805102515.78083202078694126419135621072521:50510820054828:2800:36003BBFED2B31A8B0B62CFE13A5CA69319C477FA7BEF3906895348CDE52FEBE.png"]https://communityfile-dre.op.hicloud.com/FileServer/getFile/cmtybbs/026/272/220/5190090000026272220.20200805102515.78083202078694126419135621072521:50510820054828:2800:36003BBFED2B31A8B0B62CFE13A5CA69319C477FA7BEF3906895348CDE52FEBE.png[/IMG]

After _sendEvent builds for the first time, you can see your custom event with the name you have entered in your code. Click Edit.

[IMG="https://communityfile-dre.op.hicloud.com/FileServer/getFile/cmtybbs/026/272/220/5190090000026272220.20200805102601.81221457423344072445648750568711:50510820054828:2800:580D43253B8E5EDA3D2797F770C88B6AC663671103E1FD140F4CA8A9B7F81D67.png"]https://communityfile-dre.op.hicloud.com/FileServer/getFile/cmtybbs/026/272/220/5190090000026272220.20200805102601.81221457423344072445648750568711:50510820054828:2800:580D43253B8E5EDA3D2797F770C88B6AC663671103E1FD140F4CA8A9B7F81D67.png[/IMG]

Add your attribute and click Save.

On the left panel, click Real Time Monitoring under Overview.

[IMG="https://communityfile-dre.op.hicloud.com/FileServer/getFile/cmtybbs/026/272/220/5190090000026272220.20200805102717.97798827059320714695427976237135:50510820054828:2800:B9908278ADAA3B5D51EB682D869CCFB961EE8E0CC1E0F25F9FAE5D8A23FBD52A.png"]https://communityfile-dre.op.hicloud.com/FileServer/getFile/cmtybbs/026/272/220/5190090000026272220.20200805102717.97798827059320714695427976237135:50510820054828:2800:B9908278ADAA3B5D51EB682D869CCFB961EE8E0CC1E0F25F9FAE5D8A23FBD52A.png[/IMG]

Now you can see the attribute and its value in your custom event. Also you can see how many times you get this value and its proportion of all values.

Let’s play our game a few times more.

[IMG="https://communityfile-dre.op.hicloud.com/FileServer/getFile/cmtybbs/026/272/220/5190090000026272220.20200805102903.36123519232188399841744660265688:50510820054828:2800:8413C04003E3366FD77B5B5984EA9E08DA37533AC5C0F5A84CA2E6BAAD36209E.png"]https://communityfile-dre.op.hicloud.com/FileServer/getFile/cmtybbs/026/272/220/5190090000026272220.20200805102903.36123519232188399841744660265688:50510820054828:2800:8413C04003E3366FD77B5B5984EA9E08DA37533AC5C0F5A84CA2E6BAAD36209E.png[/IMG]

Despite I am the only user, you see 2 users in AG Connect. That’s because I uninstalled the app and installed again. Now I have a different AAID as I mentioned in the first part.

Under the graphics, there is event analysis. Here you can see all events, all attributes you’ve added and statistics for both events and attributes. 11 of them are custom events that I have sent by playing the game. And rest are collected automatically.

[IMG="https://communityfile-dre.op.hicloud.com/FileServer/getFile/cmtybbs/026/272/220/5190090000026272220.20200805103010.85237962630768732657375834330563:50510820054828:2800:4ACBB5956CC981CE6A6E92CA7AAFF5751E7547D98CA8363540797D6BBB375ACA.png"]https://communityfile-dre.op.hicloud.com/FileServer/getFile/cmtybbs/026/272/220/5190090000026272220.20200805103010.85237962630768732657375834330563:50510820054828:2800:4ACBB5956CC981CE6A6E92CA7AAFF5751E7547D98CA8363540797D6BBB375ACA.png[/IMG]

[IMG="https://communityfile-dre.op.hicloud.com/FileServer/getFile/cmtybbs/026/272/220/5190090000026272220.20200805103012.07892474204715374008884284900418:50510820054828:2800:C0B747A0D9A375C9B193C34634CBBF2E8C9B34C6CEBA19FDB38C95B898C7E372.png"]https://communityfile-dre.op.hicloud.com/FileServer/getFile/cmtybbs/026/272/220/5190090000026272220.20200805103012.07892474204715374008884284900418:50510820054828:2800:C0B747A0D9A375C9B193C34634CBBF2E8C9B34C6CEBA19FDB38C95B898C7E372.png[/IMG]

You can find full code in my github page. Here is the link for you.

ozkulbeng/FlutterHMSAnalyticsKitTutorial

Conclusion

In this article you have learned how to integrate HMS Analytics to your Flutter projects, send custom events and monitor them in AppGallery Connect. You can use custom events in your apps to see user behaviors, so that you can improve your app depend on them.

Thank you for reading this article, I hope you enjoyed it.

References

Analytics Kit Document

HMS-Core/hms-ananlytics-demo-android