Using Both Firebase Analytics and Huawei Analytics Kit in Android Application

Easy way for using two analytics tool on Android application seamlessly

Jimly Asshiddiqy
3 min readDec 31, 2021
Analytics
Analytics for Android (?)

Pendahuluan

Using Analytics tools is an important key for developing application. Because by using it, we’re able to gain informations about our application such as:

  • Where does our users come from or downloading our app?
  • How users are interacting with our application?
  • Which feature of the application is used the most by users?

If asked what time is best for implementing analytics tool, the answer is right now! Because the more we’re delaying implementing analytics, the more information about our application will lost untracked.

Currently Huawei blocked from Google services (read more about it here) including analytics tool. So for us who already implementing Google Analytics and want to reach more market with Huawei, we need to implementing their analytics tool, that called HMS Analytics Kit.

Library and Tools

Before anything, let’s learn more about libraries and tools we will be using today:

Configure those three libraries to your Android Project, and we are ready for the next step.

Implementation

Some developers using build flavor for separate analytics tool implementation, the example for doing that here:

But doing it like that have some disadvantages, such as we need to build separate APKs for each analytics tool. It also affect scaling, when we need to use build flavor for other condition, so which means we will need more build flavor .

We’re not going to use build flavor , but we will use one of Object-Oriented Programming, which is inheritance.

Class Diagram untuk analytics
Class Diagram for Analytics Tools

Super Class

Now, let’s start by creating new class in your project like this:

Super class for Analytics

This class defines functions that we will be inherit to its’ subclasses, such as :

  • logEvent , function to track events that happens in the application
  • setUserPropety , function to add user property that we will connect to every events that occured next
  • setUserId , function to set id for the device. This id can be used for querying the events.
  • clearData , function to delete analytics data that stored on the device
  • instanceId , function to get unique installation id

Subclass

After creating BaseAnalytics , we will create sub-class from it based on two analytics tools:

GMS Analytics

Analytics implementation for GMS

HMS Analytics

Analytics implementation for HMS

Those two classes are implementing functions based on separate analytics tools

Injection

We will inject those classes using Koin, but before that we will define extension function to define which analytics tools to use based on device capability to use Google Services in our application, like this:

Extension Function to check Google Service

Then we will define Koin Module like this:

Don’t forget to add analyticsModule to your Koin Definition in Application class.

Usage

For usage, call BaseAnalytics where it’s needed like this:

Injecting analytics to Activity

If the device support Google Service (eg: Samsung) so GMSAnalytics will be used.

On the other hand, when the device does not support Google Service (eg: Huawei) so HMSAnalytics will be used.

Here is the example to track event:

analytics.logEvent("login",bundleOf(
Pair("username","Jimly")
Pair("method","indihome")
)
)

Conclusion

The advantages for doing it like this is:

  • No need to separate project, like separate repository/branch in Git
  • No need to use build flavor , so there’s no need to build separate APK to run on Android and Huawei
  • The application will automatically define which analytics tool to use on the device

--

--

Jimly Asshiddiqy

𝗶 𝗲𝗱𝗶𝘁 𝘁𝗲𝘅𝘁 𝗳𝗶𝗹𝗲𝘀 𝗳𝗼𝗿 𝗮 𝗹𝗶𝘃𝗶𝗻𝗴.