Flutter — Build, Measure & Learn
Flutter — Build, Measure & Learn
When building a product or feature, it is extremely important to understand the impact of what we are doing, be it positive or negative. To continue down the same path or pivot and repeat this Create-Measure-Learn process.
In this article, we will see how to prepare our Flutter application to integrate different analytics tools in a straightforward way.
The first thing we are going to create is a service that allows us to interact with the trackers (Google Analytics, Amplitude, etc) that we integrate so that our application simply communicates with this service and does not have much knowledge of HOW the implementation of themselves.
The basic functionalities that we can find are:
- Init the trackers
- Configure the user properties that we want to register
- Register the different events of our application
Next, we will see in detail the implementation of each of these methods, but let's start by defining what a Tracker is.
This abstraction is what will define the behavior of each of the tools that we integrate later, that is, each of our trackers.
Knowing what a tracker can do, let's go back to our AnalyticsService to complete the pending implementations.
Many analytics tools require initialization to use them. Initializing each of the trackers simply involves running tracker.init() for each one.
This method will populate the service's internal _trackers list, which we will then use to mediate between the trackers and our application.
In most analytics tools we can register user properties to better understand the type of users that use our application, create cohorts (groups of people that share common characteristics), etc.
Again, whenever we want to set or update a user's properties on our trackers, we simply call tracker.setUserProperties(properties) for each configured tracker.
For the example, our UserProperties are defined as follows:
Last but not least of the methods we are missing. All analytics tools (at least the ones I've used) to log an event require an event name and (optional) event properties.
Because of this, we are going to define an abstraction for the different events of our application.
Now yes, knowing how our events are, we are going to register in the same way that we initialize and register user properties. Let's call tracker.track(event.name, event.properties) for each tracker.
Up to this point, the application does not know of any particular analytics tool, it simply knows that there is a service that will allow it to track user events and properties.
Once all the abstractions are defined, adding different analytics tools is very simple. Let's look at some examples.
This is a tracker that we can use for debugging purposes and, for example, verify that every time we use our service we do it correctly, that the events are "tracked" with the name and properties that we indicate, etc.
To create this tracker, we need to create a class that implements our Tracker abstraction.
Now suppose we want to add a real tracker, such as Amplitude. The "recipe" is the same. Implement our Tracker abstraction and define the behavior of the methods.
If we wanted to add Firebase Analytics for example, just repeat the steps of the previous trackers and that's it.
One point to note is that for each tracker we add, our application is not affected. It simply continues to use the main service without "knowing" that we add new trackers.
Ok, and now… How do we use all this?
To start using the service in our application, the only requirement is to call init(trackers) before we want to register user properties or events. For example, in our main.dart
Once our trackers are initialized, we can now register the user properties that we want.
To register the different events of our app, remember that we previously defined the Event abstraction, so we must first create the concrete implementations.
Suppose our app is a dating app and we have a button that allows us to "Like" a user. If we wanted to track this event, we would need to create a class that extends Event (our abstract event).
With this, we can now track the event in our trackers using our AnalyticsService.
And that's it.
We have created an analytics service with a certain ease of use and totally independent of our application, which could easily be an external package to which we indicate which trackers we want to use and that's it.
If you want to connect, you can do it through my networks:
More articles in our tech blog More Than Code 🙌