Using Google Firebase in Cordova apps

Firebase is a platform for developing mobile and web application that provides analytics, authentication, real-time databases, notifications, cloud messaging, crash reporting and other services for your application. In this post, I will discuss how to use the Firebase Analytics in Cordova applications for measuring app usage and user engagement.

Analytics for Firebase

This is a free service that can be used for both Android and iOS applications. It provides unlimited reporting of up to 500 distinct events and a number of user properties.

Events allow you to see what happens in an application, such as user actions, system events, or errors. There is a number of automatically collected events and you can define your own events. However, you have to keep in mind that:

  • there is a limit of 500 distinct events that can be reported.
  • each event can have up to 25 parameters associated with it.
  • event names can be up to 40 characters long, may only contain alphanumeric characters and underscores (“_”), and must start with an alphabetic character.
  • “firebase_”, “google_” and “ga_” prefixes are reserved and cannot be used.
  • names are case-sensitive and logging two events whose names differ only in case will result in two distinct events.

See FirebaseAnalytics.Event for more information.

User properties are information about the user that is recorded together with the events. Default collected user properties include user info (age, gender, country, language, interests), device info (brand, category, model) and app info (version, app store, first open time, new/established).

Appart from the implicitly collected user properties you can define your own. For that, however, you must use the Firebase console. You must be careful with the properties that you add because for now these cannot be renamed or deleted (you can only change their description).

Getting started

To be able to use Firebase for your Android or iOS app you need to enroll it. For that, you must create a project and then add applications to it.

  1. Open the Firebase console.
  2. Create a new project (or select an existing one).
  3. Add a new Android, iOS or web app.
  4. Follow to instructions to register the app, download the config files or perform other necessary setups.

If you need more help for setting up your app read the Firebase documentation or additional web resources.

Cordova plugin

There is an official Cordova plugin called cordova-plugin-firebase that provides push notifications, analytics, event tracking, crash reporting and other services from the Firebase platform for you Cordova application.

When you start using Firebase, make sure you have the config files properly setup (present and valid). Otherwise, your app will crash on boot or Firebase features won’t work.

Recording events and user properties

To record a user property you should call setUserProperty. This has the form:

The name is the case-sensitive name you defined in the Firebase console. In the example above the name was login_type and possible values were demo and live. The purpose of this is to indicate that for an application that supports a demo mode where the users don’t have to create an account to try the app, the user is either properly authenticated on or actually using the demo feature.

When this call is made the Debug view shows the change of the user property value in the live stream.

If you look at the recorded user properties for all following events you will see the logon_type with the demo value attached to it.

As mentioned ealier, there are a series of events that are automatically recorded. These include screen_view (collected whenever the page has changed) and user_engagement (collected periodically when the app is in foreground). However, you can send any other event, each with any number of parameters. To record events you must use the logEvent function that has the following form:

Let’s suppose that in your app users can bookmark items (whatever those items might be) but also remove them from the bookmarks (or favorites). You want to record the event of adding and removing an item. You can define an event called favorites_action with two parameters: action that can be either add or remove, and type, that indicates what kind of item was added or removed.

These are added automatically to the stream of events, recorded with both the the auto collected and user defined parameters and user properties.

All the events have several implicit parameters, including firebase_screen, firebase_screen_class, and firebase_screen_id. Screen class is the class name of the Activity, and the ID is a numerical ID that is probably hard to use to figure where the event came from. The screen name on the other hand (firebase_screen) is what you probably want to look at. This can be set with a call to setScreenName. All events will have the param firebase_screen set to the last value passed to setScreenName. Therefore, you probably want to make this call whenever the user opens a new page in your application. The call has the form:

Here is an example:

Debugging Firebase Analytics

The events recorded by Firebase are not instantly sent to the servers. Instead they are cached, archived and periodically dispatched. However, you can enable live streaming of events for debugging purposes, both on Android and iOS. This was used for all the screen shots used previously.

For Android devices:

  • To enable execute:
  • To disable execute:

For iOS devices:

  • To enable specify -FIRDebugEnabled as an argument in XCode
  • To disable specify -FIRDebugDisabled as an argument in XCode

For more information see Debugging Events.

3 Replies to “Using Google Firebase in Cordova apps”

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.