During the last I/O, Google announced their new super product: Firebase Analytics. Firebase, when compared to Google Analytics, is a huge improvement for cross-platform mobile analytics, crash reports, marketing campaigns coordination, mobile notifications and other useful features.
Given the amount of new useful goodies, it’s obvious why many teams want to move to Firebase ASAP. The only problem lies in a small but very important detail: you can not migrate your analytics data from Google Analytics to Firebase :(. It means that if you decide to move to Firebase you will lose all your historical analytics data and won’t be able to leverage it to improve your product. There is however a way to have the best of both worlds.
A reasonable solution for the problem, appointed by Google, is to setup Firebase with Google Tag Manager (Tag Manager) for your app and use Tag Manager to “replay” the events to Google Analytics. This will allow you to take advantage of the new Firebase features and keep the important Google Analytics historical data and realtime features.
In this post we will see how to setup some basic Screen View tracking and Custom User Events tracking which send data to Firebase and Google Analytics allowing you to keep your Google Analytics data and at the same time start enjoying the shiny features from Firebase.
What is Google Tag Manager?
Google Tag Manager can be thought of as an event listener. In summary, it receives all the events from your application and redirects them to other systems, but it also acts like a data store in which you can store variables that change your application runtime behaviour. This means at least two things:
It acts like a proxy to the analytics systems, allowing the user to process the events in the most appropriate way (blocking some events, or resending to multiple tracking systems)
It allows to change application runtime (e.g. change available features) using these variables without having to redeploy the application
The important point for this post is the first, given that we are interested in “intercepting” the Firebase events and resending them to Google Analytics.
Firebase and Tag Manager setup
Adding the Firebase and Tag Manager libraries to your application is a simple process. It is well documented in the link referenced above so you can follow the instructions to set it up in the application.
Configuring Screen Views
Using Google Analytics you probably had something similar to the following to track page views:
GoogleAnalytics analytics = GoogleAnalytics.getInstance(this); tracker = analytics.newTracker(R.xml.global_tracker); tracker.setScreenName(MyActivity.class.getSimpleName()); tracker.send(new HitBuilders.ScreenViewBuilder().build());
To do the equivalent in Firebase Analytics you would write something like:
mFirebaseAnalytics = FirebaseAnalytics.getInstance(this); Bundle screenView = new Bundle(); screenView.putString( FirebaseAnalytics.Param.ITEM_NAME, MyActivity.class.getSimpleName() ); mFirebaseAnalytics.logEvent( FirebaseAnalytics.Event.VIEW_ITEM, screenView );
Now what we need to do to make this screen view available in Google Analytics is to create a trigger in Tag Manager which extracts the data logged by Firebase and send that same data to Google Analytics. We start by creating a new Tag and choosing Google Analytics as the product.
The next step is to create a new variable of type Constant holding the Google Analytics Tracking ID, let’s call it
GA Tracking ID.
Now we select
Screen View for
Track Type and expand
More settings and
Fields to Set. This field will hold the screen name for our screen view event. For the
Field Name we select
screenName from the dropdown and for the
Value we will create a new variable of type
Event Parameter. Let’s call the variable
Item Name and select
view_item for the
Suggested Event and
Event Parameter. We can also set a default value in case our event doesn’t bring data in
item_name parameter (I just set it to “not set” as usually seen in Google Analytics).
Once those are in place, we can create a trigger which will be the mechanism that intercepts the type of event we are interested in. For that, in the
Fire On section, we select
Custom trigger type.
After the trigger is created, the only thing left is to confirm the tag creation.
And we have a complete integration for Screen View events in Firebase and Google Analytics using Tag Manager as the middleman :).
Configuring Custom User Events
Similarly to screen views, to track user actions with Google Analytics, you would have something like:
tracker.send(new HitBuilders.EventBuilder() .setCategory("my_category") .setAction("my_action") .build() );
To do the equivalent in Firebase:
Bundle eventAction = new Bundle(); eventAction.putString( FirebaseAnalytics.Param.CONTENT_TYPE, "my_category" ); eventAction.putString( FirebaseAnalytics.Param.ITEM_ID, "my_action" ); mFirebaseAnalytics.logEvent( FirebaseAnalytics.Event.SELECT_CONTENT, eventAction );
The configuration in Tag Manager for custom events is similar to what we just did with the screen view event. We start by creating a new Tag for Google Analytics, and we can reuse the
GA Tracking ID. The tracking type in this case has to be
Event and we will create two new variables, one for event category and another one for the event action.
The following picture shows the creation of the category variable of type
Event Parameter using the Firebase suggested parameters
select_content for the event type and
content_type for the event parameter.
To track the event action we create another variable using the same variable type and event type but this time we use the
item_id for the event_parameter field.
The last step is to create a Custom Trigger in the
Fire On section. We set the trigger to fire when the event name is
The overall Tag should look like the following
Now we can finally publish the container and download the JSON file to use in the app. This configuration should allow you to track the screen views and user events both in Firebase and Google Analytics. Build and run your app to check that you can see the correct screen views and events in Google Analytics realtime section. Hope this helps you to get the benefit from both Firebase and Google Analytics. Happy tracking!
Things to bear in mind
- Google Analytics library can be removed
- You might need to update
Google Play Servicesand
Android SDK Managerin order to have access to Firebase and Tag Manager libraries.
- Make sure you publish the container after creating the tags before you download the container JSON file.
- Make sure you choose the correct type of container in Tag Manager. There are two type of containers:
Firebase Android. The one we have to choose is
Firebase Android, as shown in the following screenshot.
The first version of this post was originally published on the authors page.