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.


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 to gain insight into your products and contents. So that you can improve your skills in marketing your apps and optimizing your products.

HUAWEI Analytics Kit identifies users and collects statistics on users by an 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, a maximum of 25 parameters; for each app maximum of 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.
Huawei Analytics Kit

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 an 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.

data storage locations

manage API

How to Generate SHA-256 Fingerprint?

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

How to Generate SHA-256 Fingerprint?

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.


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

AppGallery Connect

Integrate HMS to Your Project

Integrate HMS to Your Project

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


Add Signing Configuration

Create a file named key.properties under the 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 the 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

TO-DOs in App-level Build.gradle

Add Analytics Kit to Your Project

There are 2 ways to do this step.

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


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


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 the 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 believe 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 on how many times you guessed.

Make a Simple Game With Flutter

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

And call it in initState

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

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

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

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

HMS analytics

Define HMS Analytics Kit and Send Events

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

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 the custom event, and a map value.

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

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


event management

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

edit event

Add your attribute and click Save.

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


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.

playing our games more

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

Under the graphics, there is an 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 the rest is collected automatically.

event analytics

event analytics

You can find the full code on my Github page. Here is the link for you. 



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.


Analytics Kit Document


For More Details, You Can go to:

  • Official website: https://developer.huawei.com/consumer/en/hms
  • Documentation page: https://developer.huawei.com/consumer/en/doc/development
  • Reddit to join our developer discussion: https://www.reddit.com/r/HMSCore/
  • GitHub: https://github.com/HMS-Core
  • Stack Overflow: https://stackoverflow.com/questions/tagged/huawei-mobile-services

Source link

Write A Comment