Made With Unity | Firebase Analytics Introduction

What Is Firebase Analytics?

Firebase Analytics is a free app that helps developers to get an insight into the usage of their app/game and how the users of their app behave. It has a default measurement solution that provides insight into how the app/game behaves when is open or when it closes or when the user makes a purchase; you can also make custom measurements for specific events on your app/game.

Setting Up Firebase Analytics On Unity

Setting Up Unity

A simple scene with 4 UI buttons and an empty game object named [ — Managers — ]

Go to the Project Settings and make sure you have a valid Company Name and Product name.

The combination of the project's Package Name is from the Company Name and the Product Name. It is also called the Package ID, in this project my Package Name / Package ID is com.RehtseStudio.Firebase. This ID will be asked when you are setting the app/game in the Firebase Console.

Go to Package Manager and download the Android Logcat package.

Make sure you are running the Unity project as Android.

Firebase Console

Go to the Firebase Console, it will prompt you to make an account if you don’t have one, press on Create a project -> name the project as you like -> enable Google Analytics ->choose Default Account for Firebase if you don’t have a Google Analytics account.

Inside your Firebase Console Project, select Unity

Register your app/game, whether an Apple or Android, where it says Apple bundle ID or Android package name, is the project ID that starts with “com” in your Unity project.

Download the JSON file to your Unity project, to have everything organized, create a new folder and name it Firebase google-services json and place the JSON file there.

Once the Firebase SDK has been downloaded, on your Unity project, go to Import Package -> Custom Package, and select the dotnet folder that resembles the API compatibility Level you currently use with your project. You can find the API compatibility Level in your Player’s setting.

Select the FirebaseAnalytics package and import everything.

Running Firebase Analytics

Once the settings are done we want to enable Firebase Analytics in our Unity project. Create an empty game object and named it FirebaseManager with a newly created script called FirebaseManager

FirebaseManageritsript

Add another empty game object named GameManager, with its component script called GameManager.

Notice that on both scripts we added using Firebase, using Firebase.Analytics and using Firebase.Extension. On the GameManager script, on the Start() method, we are using one of the many default events that Firebase Analytics provides.

FirebaseAnalytics.LogEvent(FirebaseAnalytics.EventAppOpen) is used to identify when a app/game is being open / used. Inside the PressButton(string colorButtonPressed) method, we create our custom event. The custom event will tell me what button the player press.

Each button, on its On Click() button component, has a reference for the GameManager calling the PressButton() method and passing a string with their designated color.

Testing Firebase Analytics

Firebase Analytics events are shown on the Firebase console around 1h or 24h after you finish setting up. To see if the event work locally we need to use the ADB console and a physical device or an android emulator device.

Android Debug Bridge (ADB) is a versatile command-line tool that lets you communicate with a device. The ADB command facilitates a variety of device actions, such as installing and debugging apps, and it provides access to a Unix shell that you can use to run a variety of commands on a device.

Building App/Game

Before using the ADB to test the event, first we must build our app/game. Activate your Android Virtual Device.

On Unity, Build And Run your APK selecting your Virtual Device.

Once the build is done, the Android Logcat will popup; extend the window, and under Tools select Open Terminal

Let’s use the followings commands:

adb shell setprop debug.firebase.analytics.app “packageID” will make the events appear on the debug view AKA console terminal.

adb shell setprop log.tag.FA VERBOSE & adb shell setprop log.tag.FAV-SVC VERBOSE, these two will print analytics to the terminal.

adb logcat -s “FA”, “FA-SVC” will turn to debug ON

For every button I press, the event is shown on the terminal

Firebase Analytics Introduction has come to a close😃

🎮To Be Continued🎮

--

--

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store

Rehtse Studio

148 Followers

Game Developer sharing content around Unity, programing and tips and tricks in game dev.