Firebase – Analytics

Hello droiders, welcome to the sixth part of this tutorial series on Firebase. A quick glimpse of previous parts:

  • Part 1, Firebase – Cloud Messaging, formerly known as Google Cloud Messaging, is a new and improved way of delivering Push Notifications to major platforms that include Android, iOS, and Web.
  • Part 2, Firebase – Crash Reporting, a comprehensive tool to receive detailed information about the Crash.
  • Part 3, Firebase – Storage, a storage for app developers like us, comes with storage backed with Google Cloud Storage.
  • Part 4, Firebase – Basic Authentication, a type of authentication which authenticates a user with his email and password.
  • Part 5, Firebase – Social Authentication, a type of authentication which authenticates a user with their identity using either Google, Facebook or Twitter.

In this part

In this article, We are going to discuss how firebase analytics integrate into our application. Firebase Analytics allows us to track users activity in our application it will help us to track which features and functionality user most like. Firebase Analytics collect some events automatically for that we need to link account like AdMob, AdWords, Google Play Store account for the in-app purchases etc.

Firebase logs primary two types of information.

  1. Events: user action, system events, screen flow, errors etc.
  2. User Properties: language preference, geographical locations, user gender, user age etc.

Getting started

In this post, We are going to create a demo that will perform following operations:

  • Firebase Analytics Events
  • Firebase Analytics User Properties

Pre-requisites

  • Android Studio 1.5 or above
  • Google Play Services 9.0.2 or above

Add Firebase to your app

  1. Go to Firebase console and click on Add Project to create a new project. Fill the Project Name and select country then click on CREATE PROJECT.

    Create a project
    Create a project
  2. Click on Add Firebase to your Android app.

    Add firebase to android app
    Add firebase to Android app
  3. Enter package name, App nickname (optional) and Debug Signing certificate SHA-1 (optional) and then click on REGISTER APP.

    Register App
    Register App
  4. Download google-services.json file.

    Download google-services.json
    Download google-services.json

The Google services plugin for Gradle loads the google-services.json file you just downloaded. Modify your build.gradle files to use the plugin.

1. Add the following lines to your Project-level build.gradle file.

buildscript {
    repositories {
        jcenter()
    }
    dependencies {
        ...
        classpath 'com.google.gms:google-services:3.1.0'
    }
}

2. Add the following lines to your App-level build.gradle file.

dependencies {
    ...
    compile 'com.google.firebase:firebase-core:10.0.1'
    ...
}
// add this line at the bottom of build.gradle file
apply plugin: 'com.google.gms.google-services'

3. Add Internet permission in your AndroidManifest.xml file

<uses-permission android:name="android.permission.INTERNET" />

Firebase Analytics currently provides following methods:

1. Returns the singleton Firebase Analytics interface.

getInstance(Context context)

2. Logs an app event.

logEvent(String name, Bundle params)

3. Sets whether analytics collection is enabled for this app on this device.

setAnalyticsCollectionEnabled(boolean enabled)

4. Sets the current screen name, which specifies the current visual context in your app.

setCurrentScreen(Activity activity, String screenName, String screenClassOverride)

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

setMinimumSessionDuration(long milliseconds)

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

setSessionTimeoutDuration(long milliseconds)

7. Sets the user ID property.

setUserId(String id)

8. Sets a user property to a given value. User property name are case sensitive.

setUserProperty(String name, String value)

Code

1. For Events

In your activity write following lines to log event,

FirebaseAnalytics analytics = FirebaseAnalytics.getInstance(this);
Bundle bundle = new Bundle();
bundle.putInt(FirebaseAnalytics.Param.ITEM_ID, 1);
bundle.putString(FirebaseAnalytics.Param.ITEM_NAME, "Android Phone");
analytics.logEvent(FirebaseAnalytics.Event.SELECT_CONTENT, bundle);

There are many Parameters and Events available in Firebase analytics. But if your application has some specific Parameter and Events then you can use your own custom Parameters and Events.

FirebaseAnalytics analytics = FirebaseAnalytics.getInstance(this); 
Bundle bundle = new Bundle(); 
bundle.putString("ProductId", "P123"); 
bundle.putString("ProductName", "Camera"); 
analytics.logEvent("Product", bundle);

2. For User Properties

FirebaseAnalytics analytics = FirebaseAnalytics.getInstance(this);
analytics.setUserProperty("user_name", "John Doe");
analytics.setUserProperty("user_gender", "Male");

Note: Data in the Analytics reporting dashboard refreshes periodically throughout the day. You might not see changes right away. It will take up to 24 hr to reflect the analytics data in the dashboard.

Want to work with us? We're hiring!