Using Both Firebase Analytics and Huawei Analytics Kit in Android Application
Easy way for using two analytics tool on Android application seamlessly
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:
- Google Analytics, analytics tool developed by Google.
- HMS Analytics Kit, analytics tool developed by Huawei.
- Koin, dependency injection for Android Application.
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.
Super Class
Now, let’s start by creating new class in your project like this:
This class defines functions that we will be inherit to its’ subclasses, such as :
logEvent
, function to track events that happens in the applicationsetUserPropety
, function to add user property that we will connect to every events that occured nextsetUserId
, 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 deviceinstanceId
, function to get unique installation id
Subclass
After creating BaseAnalytics
, we will create sub-class from it based on two analytics tools:
GMS Analytics
HMS Analytics
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:
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:
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