Changes

Jump to: navigation, search

Firebase Analytics

9,458 bytes added, 14:11, 28 August 2017
Created page with "Firebase Analytics provides a way to learn about the user base of an app and to analyze and track the way in which those users engage and interact with the app. This chapter..."
Firebase Analytics provides a way to learn about the user base of an app and to analyze and track the way in which those users engage and interact with the app.

This chapter and the chapters that follow are intended to provide an overview of Firebase Analytics and to introduce the features that are provided by this service.

The chapter will begin by providing an overview of the different features provided by Firebase Analytics including events, user parameters and audiences. Subsequent chapters will explore the various screens that make up the Analytics section of the Firebase console before working through a tutorial involving events, user properties and audiences.

== An Overview of Firebase Analytics ==

Using Firebase Analytics, app developers can capture and analyze data about users and the way in which they interact with an app. Firebase Analytics consists of a library that integrates with the app combined with a set screens within the Firebase console designed to provide easily understandable visual representations of the collected data in the form of graphs, charts and tables.

Without the need to add a single line of code to an app project, Firebase Analytics will provide demographic data about an app user base including the age, gender and geographical location. A number of events are also captured automatically including the first time users open the app, Google Play in-app purchases, user engagement with the app, app crashes and the receipt and opening of Firebase Notification messages.

With the addition of a few lines of code, app developers can add custom events to track just about any type of user interaction within an app, allowing analytics data capture to be adapted to the specific requirements of the app.

Firebase Analytics also allows custom audiences to be defined, allowing users to be grouped together based on behavior patterns. Once created, audiences can be used within other Firebase features such as Notifications and Remote Config.

The four key elements of Firebase Analytics are the FirebaseAnalytics object, events, user properties and audiences, each of which will be covered in detail in the remainder of this chapter.

== The FirebaseAnalytics Object ==

The object responsible for providing Firebase Analytics support within an Android app is the FirebaseAnalytics object. The first step in implementing Analytics support within an app is to obtain a reference to this object instance as follows:

<pre>
FirebaseRemoteConfig fbAnalytics = FirebaseAnalytics.getInstance(this);
</pre>

== Events ==

Event logging provides a way to track user behavior within an app. Events are categorized by type, with each app allowed to define up to 500 different types of event. Although the number of event types is limited, there is no limit to the number of events that can be logged using Firebase Analytics.

Events may also include parameters which provide additional information relevant to the event. A flight booking app might, for example, log an event each time a user books a ticket and include the departure and destination airports as event parameters. These parameters can then be used when filtering results within the Firebase console.

Firebase Analytics allows the use of predefined, suggested and custom events types, each of which requires some explanation:

=== Predefined Events Types ===

Firebase Analytics automatically captures a set of predefined events without the need to add any additional code to the app. A full listing of events and event parameters that are captured automatically is available online at:

[https://support.google.com/firebase/answer/6317485?hl=en&ref_topic=6317484 https://support.google.com/firebase/answer/6317485?hl=en&ref_topic=6317484]

=== Suggested Event Types ===

In addition to the predefined events that are captured automatically, Firebase Analytics includes a set of suggested event types with predefined names and parameter sets. These event types are grouped into different categories such as retail, ecommerce, travel and games. A full listing of suggested events and corresponding parameters can be found at the following URL:

[https://support.google.com/firebase/topic/6317484?hl=en https://support.google.com/firebase/topic/6317484?hl=en]

Suggested events are not logged automatically and must be triggered from within code using the logEvent() method of the FirebaseAnalytics object. Consider, for example, a requirement to log an event each time a user signs up for an app. A review of the suggested events list will identify that the sign_up event would be a suitable candidate. According to the documentation, this particular event type is designed to include the sign up method (email, Google, Facebook etc.) as a parameter. The code to log a sign up event within an app would, therefore, read as follows:

<pre>
Bundle event = new Bundle();
event.putString(FirebaseAnalytics.Param.SIGN_UP_METHOD, "Google");
fbAnalytics.logEvent(FirebaseAnalytics.Event.SIGN_UP, event);
</pre>

=== Custom Event Types ===

In addition to predefined and suggested events, it is also possible to configure custom events. This is useful if none of the suggested events are a good match for the event type. Neither the predefined nor suggested events, for example, include an event for tracking when a user logs out of an app. Such an event would, therefore, need to be created as a custom event. The code to log a custom event of this type, including parameters for the authentication provider and the number of seconds the user was logged in would read as follows:

<pre>
Bundle custom = new Bundle();
custom.putString("logout", "Facebook");
custom.putLong("duration", 1200);
fbAnalytics.logEvent("user_logout", custom);
</pre>

When the above event is triggered, an event named user_logout will be logged within Firebase Analytics containing the two parameters.

== Viewing Events in the LogCat Window ==

When implementing events within a project, it can be useful to enable logcat debugging. This causes events to appear in the Android Studio logcat panel as they are logged. To enable this mode, connect a device or start an emulator and execute the following adb commands within a terminal or command-prompt window:

<pre>
adb shell setprop log.tag.FA VERBOSE
adb shell setprop log.tag.FA-SVC VERBOSE
</pre>

In addition to the logcat panel, debugging output may also be viewed within the command-prompt or terminal window using the following command:

<pre>
adb logcat -v time -s FA FA-SVC
</pre>

Realtime event tracking during development is also available in the Analytics DebugView screen, a topic which is covered in the chapter entitled [[An Overview of the Firebase Analytics Screens]].

== User Properties ==

User properties are key-value pairs that are created within an app which, once defined, are sent with every event that is subsequently logged. The purpose of user properties is to store information about the user.
Once declared, user properties can be used to filter the results in the Firebase Analytics console screens, and also to create audiences.

A food delivery app might, for example, set a user property to indicate each user’s most frequently ordered item. That property could then be used to create an audience consisting of all users who frequently order pizza. The resulting audience could then be targeted with Firebase Notification messages or Remote Config app changes to promote a special offer on pizza deliveries.

User properties are created using the setUserProperty() method of the FirebaseAnalytics object, for example:

<pre>
fbAnalytics.setUserProperty("most_ordered", "pizza");
</pre>

Each app can declare up to 25 different user properties. When working with user properties it is important to be aware that it can take up to 24 hours before the properties start appearing in the Firebase Analytics results.

== Audiences ==

Google Analytics essentially uses event logging, event parameters and user properties to gather and store detailed information about the user base of an app. With access to all of this information it becomes possible to segment users into groups referred to as audiences. The data associated with these audiences can then be analyzed independently from the rest of the user base, or used to target those users via Firebase services like Notifications and Remote Config.

The possible combinations of ways to create audiences is limited only by the different types of events and user properties captured for an app. Assuming appropriate use of events and user properties, it would, for example, be entirely possible for a flight booking app to create an audience of 39-45 year-old men living in Germany who have made a purchase within the app in the last two weeks of a ticket to fly from Berlin to London Heathrow Airport.

Audiences are created within the Firebase console using the steps covered in the chapter entitled An Overview of the Firebase Analytics Screens.

== Summary ==

Firebase Analytics is a service that allows app developers to gain an understanding of the users of an app including user demographics and app engagement. Firebase Analytics consists of a series of screens within the Firebase console and a library integrated into app projects. The key elements of Firebase Analytics are events, user properties and audiences each of which has been described in this chapter and will be covered in greater detail in the following chapters.

Navigation menu