Firebase Analytics


Create a Firebase project in the Firebase console, if you don't already have one. If you already have an existing Google project associated with your mobile app, click Import a Google Project. Otherwise, click Create New Project and follow the setup steps.

When prompted, enter your app's package name. It's important to enter the package name your app is using, this can only be set when you add an app to your Firebase project.

To move on, please, open the settings of your Firebase App and scroll down a little bit to the list of you Apps in current project. The most important thing highlighted on the screenshot above.

Important: You should NOT import google-service.json file into your Unity project. It's not necessary for Android platform at all. Provide your Firebase App ID (see the screenshot above) into plugin configuration resources. You have to add new tag into Assets/Plugins/Android/AN_Res/res/values/ids.xml file like shown below.

<?xml version="1.0" encoding="utf-8"?>
    <!-- Your other IDs located here -->
    <string name="google_app_id">YOUR FIREBASE APP ID HERE</string>

If you would like to integrate the Firebase libraries into your project, you need to make a few basic steps to prepare your Unity project.

The very first thing you have to do is enable the Firebase Analytics in the Android Native plugin settings. Navigate to Window->Stan's Assets->Android Native->Edit Settings. Open up Android Native Lib foldout and enable Firebase Analytics option. Please, see the next attached screenshot.

That's all for setup part. Now you are ready to move to adding the events tracking implementation on the Unity C# side.

Programming guidelines

Let's start with  FirebaseAnalytics.Init method call at the very start point of your application.

// Use this for initialization
void Start () {
	FirebaseAnalytics.Init ();

Important: Just make sure, that this method call going first. This method Initializes the default FirebaseApp instance. The creation of the default instance is automatically triggered at app startup time if Firebase configuration values are available from resources - populated from Assets/Plugins/Android/AN_Res/res/values/ids.xml file.

All the logic for current app Firebase configuration going next.

FirebaseAnalytics.SetEnabled (bool enabled)

Sets whether analytics collection is enabled for this app on this device (enabled parameter).

FirebaseAnalytics.SetMinimumSessionDuration (long milliseconds);

Sets the minimum engagement time required before starting a session. The default value is 10000 (10 seconds).

FirebaseAnalytics.SetSessionTimeoutDuration (long milliseconds);

Sets the duration of inactivity that terminates the current session. The default value is 1800000 (30 minutes).

FirebaseAnalytics.SetUserId (string id);

Sets the string user ID property.

FirebaseAnalytics.SetUserProperty (string name, string value);
Sets a user property to a given value.
FirebaseAnalytics.LogEvent(string name, Dictionary<string, object> data = null)

Logs an app event. The event can have up to 25 parameters. Events with the same name must have the same parameters. Up to 500 event names are supported.

The name of the event. Should contain 1 to 32 alphanumeric characters or underscores. The name must start with an alphabetic character. Some event names are reserved. The "firebase_" prefix is reserved and should not be used. Note that event names are case-sensitive and that logging two events whose names differ only in case will result in two distinct events.

The Dictionary<string, object> of event parameters. Passing null indicates that the event has no parameters. Parameter names can be up to 24 characters long and must start with an alphabetic character and contain only alphanumeric characters and underscores. Only string, long and double param types are supported. String parameter values can be up to 36 characters long. The "firebase_" prefix is reserved and should not be used for parameter names.

Once you have initialized the FirebaseAnalytics and configured all the analytics session properties, you can use it to log either predefined or custom event with the LogEvent method. You can explore the predefined events and parameters in the FirebaseAnalytics.Event and reference FirebaseAnalytics.Param documentation.

You can enable verbose logging to monitor logging of events by the SDK to help verify that events are being logged properly. This includes both automatically and manually logged events.

You can enable verbose logging with a series of adb commands:

adb shell setprop log.tag.FA VERBOSE
adb shell setprop log.tag.FA-SVC VERBOSE
adb logcat -v time -s FA FA-SVC

This command displays your events in the Android Studio logcat, helping you immediately verify that events are being sent.

If you have any kind of problems with events tracking, please, enable advanced logging in your Android Studio.
You will be able to check the log records from your device and be sure, that your events have been logged correctly. More detailed information according to this scenario you can find in official Firebase documentation.