How to set up Google Analytics based on Firebase SDK using GTM

Maarten Van den Bossche
In The Pocket Insights
7 min readNov 21, 2018

👇🏻👇🏻👇🏻

IMPORTANT UPDATE: I’ve talked to Google Support and this solution will not survive the GA SDK sunset (unless, for now, you are on GA360). They have launched a new Google Analytics view, based on Firebase SDK but I suggest you also explore alternatives to Google Analytics for Firebase.

👆🏻👆🏻👆🏻

Mobile app analytics can be a very messy landscape. There’s a lot of tools and they don’t seem to work together that well. Even Google itself has two different ways to measure apps. You can use Google Analytics for Mobile Apps or you can use Google Analytics for Firebase (previously Firebase Analytics).

If you’re using Firebase already, the Google Analytics for Firebase is useful, but it’s not all-encompassing (yet). There’s no possibility to filter Retention on behavior for example.

While Google is probably working on a unified solution (probably extending the possibilities of Firebase), it is very useful to have both GA for Firebase and GA for Mobile Apps running at the same time to get as much out of your data as possible.

This article will show you how to have Google Analytics for Mobile Apps running on your initial Firebase Event Tracking through Google Tag Manager.

Disclaimer: It’s likely that Google will sunset Google Analytics for Mobile Apps altogether soon. I encourage you to explore these 5 alternatives for Google Analytics for Firebase.

Why would you do this?

  • While Firebase has unlimited reporting without sampling, it lacks some basic data visualization that Google Analytics does have. And the other way around.
  • Installing both SDK’s separately would require more development and maintenance every time you update your tracking requirements.

For more information on the differences between these two tools, check out this video:

Step 1: Installing Firebase SDK and GTM

Of course, the first step is to have Firebase SDK installed. If you don’t know how to do this, I advise you to check out the Google Developer information on how to install Firebase SDK for Android and iOS.

Next up is setting up Google Tag Manager for Firebase. This is a specific GTM container that’s working based on Firebase and will probably be the way Google will proceed for mobile app analytics. Again, there’s Google Developer information for installing it on Android and iOS.

Of course, you’ll also need a Google Analytics property that is set up for tracking Mobile Apps. Google will nudge you in every way possible to base this implementation on the Firebase SDK, so no worries. You’ll get there.

Step 2: Setting up Tracking Requirements

Before you start firing events in your app through Firebase, make sure you have separate Tracking Requirements for both Firebase and Google Analytics. They both have different ways of organizing data:

Some high-level differences between Firebase and Google Analytics app tracking.

So, if you have this event in your tracking requirements for Firebase:

account_creation_completed
account_level= premium|free

Then you’ll need to split it up to fit Google Analytics event tracking:

eventCategory: Account Creation (required)
eventAction: Account Created (required)
eventLabel: Premium/Free (optional)
eventValue: (none) (optional)

Thinking this through and documenting from the start can save you a lot of time later.

Step 3: Setting up the Firebase Event Tracking

Firebase has some automatically collected events and properties and enables you to track custom events and properties next to that.

It’s important to know that you will not be able to use the automatically collected ‘screen_view’-event. So you’ll need to create a custom event (e.g. ‘screenview’ or ‘screen_view_dup’) that fires at the same time and has a parameter ‘screen_name’ which… well, tells you which screen the user was on.

This is what you’ll see in Firebase -> Events once data is coming in.

If you’re thinking: ‘This makes no sense at all.’, you’re absolutely right. It would be much more convenient to be able to use this as an ‘Firebase Automatic Event’ you can convert to a Google Analytics Screen View, but no.

The list of automatically collected Firebase events you can use as Triggers in Google Tag Manager.

If anyone from the Google team is reading this: Please fix it. 🤞🏻

You’ll also want to make a custom User Property for user_id if you want to have a consistent view. This should contain the Firebase Instance ID.

Step 4: Setting up the Google Tag Manager container

First, you create a container for your apps based on Firebase:

The same thing goes for Android apps.

Within the workspace, you go to Variables and add the Built-in variables you want to listen to. This is completely to be set up to your preferences.

An example of the selected Built-in Variables.

The next thing you’ll need to do is to add every parameter you use in Firebase to the User-Defined Variables. For example the screen_name parameter you’ve created earlier for the screenview-event:

  • Click the ‘New’-button under User-Defind Variables
  • Select ‘Event Parameter’ under the ‘Firebase’ subtitle
  • Give it an appropriate name
  • GTM will show some suggested Firebase Parameter based on the automatically collected events and general events they suggest.
    In this case, you’ll need to select ‘Custom Parameter’ and type in the name of the parameter.

You’ll need to do the same thing for your User Properties. This can be done by selecting the ‘Firebase User Property’ instead of the Firebase Parameter.

You’ll also need to add the Tracking ID of your Google Analytics account as a Constant:

This will save you time.

This is just to make sure that if the Tracking ID ever changes, you’ll only have to make the change in 1 place within Google Tag Manager.

Next, you start adding your Google Analytics settings.

You create a New User-Defined Variable of the type ‘Google Analytics settings’ and enter the Constant you just created as the ‘Tracking ID’.

If you’ve done that. Click ‘More Settings’ then ‘Fields to Set’ and you add four fields:

  • screenName
  • appVersion
  • appName
  • userID

Then fill the values with the following parameters (that you’ve normally just selected or created):

  • {{Firebase Parameter — screen_name}} (User-defined variable)
  • {{App Version Code}} (Built-in Variable)
  • {{App Name}} (Built-in Variable)
  • {{User Property — User ID}} (User-defined variable)

It should look like this:

Step 5: Add Firebase Triggers and convert them to Tags

Now you’ve done this, you can start setting up triggers for all the events you want to send to Google Analytics. The first one will be the screen view.

Adding a Firebase Event as a trigger is as simple as giving in the name you used within Firebase:

Remember that GTM listens to the parameter containing the screen_name because we added this as a Variable earlier.

Once you’ve created the trigger you can go to tags and add a Universal Analytics tag. Select the Track Type ‘Screen View’ and add in the Google Analytics Settings we made in the previous step:

Google Analytics will know which screen has been viewed because we’ve added this in the Google Analytics Settings as a ‘Field to Set’ earlier.

So, now you have your screen views set up, you can add your events as well. This is where the second step gets important because the Firebase Events consist of one string (e.g. ‘account_creation_completed’) with possibly some parameters.

This will have to be translated into Google Analytics events that have at least a Event Category and Event Action.

Again, you make a trigger based on a certain Firebase Event:

Then use the trigger to fire a Universal Analytics Tag. This time, the Track Type should be set to ‘Event’, giving you more fields to set. Here you type in the names for the events you’ve set up in Step 2. For example here, we used the Event Category ‘Account Creation’, the Event Action ‘Completed’ and set the type of account as the Event Label, using a Firebase Parameter we’ve listened to.

Repeat this for all the events you want to track.

If you’ve done all this, you can submit your container and if you’ve done everything correctly, both screen views and events should be rolling into your account very soon.

Please note that it can take up to 24 hours for the new container to be downloaded by your app. So if you don’t see anything coming in, this might be the problem.

Hope this helps you out in setting up the Google Analytics for Apps tracking based on the Firebase SDK + Tag Manager. If it did, please leave a comment or clap so others can find this article too! 👇🏻

--

--