Integrating Huawei Analytics Kit to Flutter Projects and Sending Events

Bengisu Özkul
Huawei Developers
Published in
6 min readAug 4, 2020

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

For more information about the kit, please refer here.

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.

Setting Data Storage Location
Enabling Analytics Kit

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.

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.

Integrate HMS to your project

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

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

TO-DOs in app-level build.gradle

Add Analytics Kit to your project

In pub.dev copy the plugin and add it under dependencies as usual.

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.

And call it in initState

We will need a TextField and a button to check 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 user, also a replay button.

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

“Guess the number”

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

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

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

Before sending our first custom event

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

Add your attribute and click Save.

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

There is your custom event!

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.

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.

Monitoring events-1
Monitoring events-2

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

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.

--

--