Blog

We believe there is something unique at every business that will ignite the fuse of innovation.

Analytics

What is Firebase?

Originally a real-time database BaaS (Back-end as a Service), Firebase was upgraded to a whole suite of mobile-focused services this year at Google I/O. Firebase is being pushed hard by Google as a one-stop shop of sorts for your mobile service needs and, after the announced decommissioning of Parse, may be a very attractive option for developers and clients alike. Firebase supports Android, iOS, and web implementations as well, bringing all platforms under one roof. This blog will focus on the core Firebase Analytics as well as Crash Reporting.

Firebase Project Setup

The Firebase documentation for Android and iOS contains thorough steps on adding Firebase to your app. Those steps can be found here for Android and here for iOS. We’ll skip over those steps and dive right into Analytics and Crash Reporting with Firebase.

Firebase Analytics

Firebase Analytics is the core of Firebase – literally, the core module of Firebase is only the Analytics module. This is because Firebase Analytics integrates deeply with the other Firebase features. Firebase Analytics moves away from the screen-view style tracking used in Google Analytics to a purely event-based analytics system, leaving the developer to define what they consider meaningful events for analytics tracking. Events may be seen in the Analytics dashboard for a Firebase Project and may be used when defining Audiences (more on Audiences later).

Now, let’s jump into the code for setting up Firebase Analytics as well as logging events and setting user properties.

Setup

Android:
Place this code into a base Activity/Fragment, or use dependency injection.
private FirebaseAnalytics firebaseAnalytics;

@Override protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    /* .. other initializations .. */
    firebaseAnalytics = FirebaseAnalytics.getInstance(this); // this is a Context
}

iOS:
Add to your application's AppDelegate.

import Firebase
import FirebaseAnalytics

func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
    FIRApp.configure()
    return true
  }

Log an Event

Firebase automatically logs some events (see link), and no additional code is required for them. Other events may be logged using the suggested events and prescribed parameters provided with Firebase Analytics. This sample will log a search event.
Android:
Suggested events can be found here, and the parameters for the events here.
Bundle bundle = new Bundle();
bundle.putString(FirebaseAnalytics.Param.SEARCH_TERM, “alex-townsend”);
firebaseAnalytics.logEvent(FirebaseAnalytics.Event.SEARCH, bundle);

iOS:
For suggested events see the FIREventNames.h header file, and for the parameters see the FIRParameterNames.h header file.

FIRAnalytics.logEventWithName(kFIREventSearch, parameters: [
      kFIRParameterSearchTerm: “alex-townsend”
      ])

Logging a Custom Event

Custom events are logged similarly to suggested events. A project may have up to 500 custom events defined.

Android:
Bundle bundle = new Bundle();
bundle.putString(“repo_name”, “RxJava”);
bundle.putString(“repo_owner”, “ReactiveX”);
firebaseAnalytics.logEvent(“view_repo”, bundle);

iOS:

FIRAnalytics.logEventWithName("view_repo", parameters: [
      "repo_name": “RxJava”,
      "repo_owner": “ReactiveX”
      ])

Setting a User Property

In addition to the automatically logged events mentioned above, Firebase Analytics also automatically sets several user properties (see link). An application may have up to 25 custom user properties, and the properties are stored as string pairs. User Properties can be created under the User Property tab in the Analytics dashboard, and can be used as a condition when creating an Audience.

Android:
firebaseAnalytics.setUserProperty(“favorite_food”, “pizza”);

iOS:

FIRAnalytics.setUserPropertyString(“pizza”, forName: "favorite_food")

Firebase Audiences

A Firebase Audience is a group of users that meet a set of developer defined criteria. Audiences can be created and viewed in the Audiences tab of the Analytics dashboard (all projects start with an "All Users" and "Purchasers" audience). Once an Audience has been added, Firebase will start adding Users that meet the specified conditions to Audiences. Audience population is not retroactive and valid Users will not be added to Audiences until after the Audience is created. An Audience can be created by selecting a number of User Properties or Events combined with AND/OR.
Adding an Audience

Adding an audience

Once an Audience is created and populated with users, it can be used to filter Analytics reports and Events from the Analytics dashboard, as well as integration with other Firebase features such as Remote Config. Audiences with under 10 users will not display events or analytics on the dashboard for user privacy reasons.

Firebase Crash Reporting

Firebase Crash Reporting is an easy-to-add module of the Firebase suite, giving applications quick and easy crash reporting support that hooks directly into Firebase Analytics. Fatal and uncaught exceptions are automatically reported, and non-fatal or caught exceptions can be manually reported. Crash Reports will appear in the Crash Reporting dashboard of the Firebase Project after about 20 minutes.

Do note, however, that Firebase Crash Reporting is currently in beta for Android while they work out some known issues.

Setup

Android:
// add to app gradle dependencies
compile ‘com.google.firebase:firebase-crash:9.4.0’

iOS:

// add dependency to Podfile
pod 'Firebase/Crash'

// add to allow crash messages
import FirebaseCrash

Creating Crash Reports

A Crash Report can be created by reporting a caught exception. Additionally, a message may be logged that will show up in the Crash Report on the Crash Reporting dashboard.

Android:

// logging a message for reported exception below
FirebaseCrash.log(“Whoops something happened here...”);

// reporting an exception
FirebaseCrash.report(new Exception(“Something kinda bad happened.”));

iOS:

FIRCrashMessage("Something kinda bad happened.")

When a Crash Report is generated it is created with the "app_exception" event. This event can be used as a condition when creating an Audience in the Analytics dashboard, allowing further reporting on app users.

Crash Report with additional logging

Crash Report with additional logging

Conclusion

Analytics and Crash Reporting are two powerful and easy-to-use tools that came from Google’s announcement of the new Firebase at I/O this year. While Crash Reporting is still in beta and working out final kinks, Analytics is ready to go and offers a lot of powerful options for analyzing your users and how they interact with your apps. With the full suite of Firebase offering even more useful functionality, Firebase should be kept in mind for current and future solutions.

Special thanks to my colleague, Jonathan Tang, for providing the iOS equivalent code snippets for iOS.

About the Author

Alex TownsendAlex is a Senior Consultant at CapTech, based in Washington, DC. Alex has a passion for Android application development and architecture, focusing on bringing the latest and greatest technologies and patterns to his work.


About the Author

Alex Townsend

Alex Townsend is a Senior Consultant at CapTech, based in Washington, DC. Alex has a passion for Android application development and architecture, focusing on bringing the latest and greatest technologies and patterns to his work.