Improving Your Games with Google Analytics for Firebase on Unity

Paul Ruiz
Firebase Developers
8 min readJun 29, 2020

One of the most important things you can do as a game developer (or any developer, for that matter) is understand how your users are using your game. This allows you to make more informed decisions around things like game difficulty, how frequently users should be rewarded for playing, or whether you should add more of a specific kind of level.

In order to help you get that insight, this article will introduce you to using Google Analytics for Firebase on Unity. It is written from the perspective of updating an existing mobile game to use Google Analytics, though readers who would rather just review the content without creating or editing an app should be able to follow along easily.

Initial App

Before you dive into using Google Analytics for Firebase, you will need to create a base Unity application. Since Unity Technologies has already created a substantial amount of great content for getting started with their platform, I will rely on one of their tutorials for creating a simple Flappy Bird clone as the initial app for this tutorial.

You can either follow along with their video content to create your app:

Or you can download my finished copy of the game from GitHub:

Setting Up Firebase

If you don’t already have a Firebase project, then you will need to create one for this tutorial. You can find the official documentation here, or follow along by going to the Firebase Console, logging in with a Google account, and following the prompts for creating a new project. You will be prompted to enable Google Analytics for your Firebase project during step two — as you might expect, this is necessary if you want to use Google Analytics for Firebase in your Unity app.

Following this, you will be prompted to link an existing Google Analytics account, or create a new one. After that you will be taken to the main screen of the Firebase console once your project has finished being created.

Next you will need to add a new app to Firebase. In the screenshot above you should see a Unity icon in the lower left corner. Back in your console, click on that icon. For this article you will only register an Android app, though you can find details on adding an iOS app in this documentation.

You can find or change the package name for your app in Unity by going to File -> Build Settings -> Android, and clicking on the Player Settings button in the lower left corner of the Build Settings screen.

Once you’re on the Player Settings window, scroll down to Other Settings and find the Identification section. You can set your package name here.

Back in your console, hit the blue Register App button, then download the google-services.json configuration file on the next step.

This should get placed in the Assets directory (or some sub-path of it, if you prefer) of your Unity project.

The next step will prompt you to download the Firebase Unity SDK. Once that has finished downloading, you can unzip it on your computer and import the Google Analytics package into your Unity project by going to Assets -> Import Package -> Custom Package…. From there you can navigate into your newly unzipped directory, go into dotnet4, and select the FirebaseAnalytics.unitypackage file. It’s worth noting that if you’re using a version of Unity that is older than the 2019 release, then you can use the dotnet3 versions of these unitypackage files.

You will then be prompted to import everything within that Unity package.

After a moment of resolving Android dependencies, you should be ready to start using Google Analytics for Firebase in your game. If, however, you run into the same issue that I did: “MissingMethodException: Method not found: ‘Google.EditorMeasurement.set_InstallSourceFilename’.”, then you can fix it by following the steps in this GitHub issue. As we all know, technology isn’t perfect and something is bound to break :) It’s also worth noting that you will want to install your game on a physical Android device from this point forward for this tutorial due to an unsupported architecture in the emulator.

If everything has gone as expected, your app should install on your device and you will see the location for your test drive show up in Firebase’s Analytics dashboard. To confirm that everything is going well, you can go to the StreamView for Users in last 30 minutes dashboard to see semi-live data that is batched together (though with a sample size of one, it should indicate your general region).

At this point you might be wondering why there’s some values showing up in the dashboard that you haven’t configured. Google Analytics for Firebase has a set of events that are automatically collected for you, such as first_open, ad_click, and notification_open. You can find out more about these pre-defined events from Firebase’s official support documentation.

Log Events

Now that you’ve seen default values come through, it’s time to add additional events to track. Like so many things with Firebase, this is super easy to implement. Let’s take a look at how you could record user scores whenever they lose the game.

You can start by opening the GameController.cs class in our sample. In there you have a method that is called, albeit a little grimly, BirdDied(). As you might guess, this is called when the player hits the ground or pillar, causing the game to be over. You can add the following code to the bottom of this method to take their final score and submit it to Firebase:

Let’s take a look at each part of this code block. Number 1 is a call to logging our event, which is pretty straight-forward. The second item is the Event that’s being logged. You can find a list of these pre-defined Event items in Firebase’s official documentation, though you can also add your own string values here to reflect your own unique requirements and custom events for your games. The third and fourth items are the parameters that are associated with the event. You’ll notice that you can use an array here to include multiple parameters. Since this example case is simple, you only need to include the score. If you were to modify your app to record the amount of time it takes for a player to reach a score, you could include that as an additional parameter as well.

View Event Stream

You might be a bit disappointed at this point if you run your app and try to see your new events in the Firebase Console. Unfortunately, it can take up to 24 hours for events to show up in the dashboard. Events are also batched together every hour or so after that initial 24 hour period, so don’t be surprised if you don’t see your test events coming through immediately on your second day of testing :)

You can, however, get around this for testing purposes by opening up a terminal/command line and using the Android Debug Bridge (ADB) tool to view events from your device as they’re sent to Firebase. If you don’t currently have the ADB tool installed, you can find instructions for that here. You can do this by running these three commands in series:

Now you should see the Google Analytics events as they are sent from your connected Android device.

If you prefer to use the Firebase console, you can also look into using the DebugView dashboard.

User Properties

The final thing to talk about is User Properties. The are properties specific to your users (surprise, surprise!) that are tracked as they play the game, and that represent the user’s state as an event occurred, rather than just describing the event. This could be used in situations like tracking when a user is using a power-up when they’re doing an action, or their character class in an RPG.

Let’s return to GameController.cs and update the Awake() method, which is called when the program is first started, to create a new user property. For this example you’ll just mark every user as a premium_user, though you could set any value that’s relevant to your game

Now you’ll get the user’s score as well as their user property in Firebase’s Analytics dashboard whenever they lose the game.

Conclusion

In this article you have taken a look at setting up and using Google Analytics for Firebase with the Unity game engine. While this tool is simple to implement, it’s also highly customizable and provides invaluable insights into your products for your developers and analysts. The information and user groupings that can be surfaced through Google Analytics are also useful for other tools, such as BigQuery, Firebase Remote Config, Firebase A/B Testing, and Firebase Cloud Messaging, in order to improve the experience for your users and keep them coming back to your games. You can also take what you’ve learned here and apply it to other tools in the Firebase suite in order to make your apps even better.

Additional Resources

--

--

Paul Ruiz
Firebase Developers

Developer Programs Engineer on Android, Maker, @ptruiz_dev