Android

Firebase on Android

Firebase is a mobile platform that helps you quickly develop high-quality apps and grow your user base. Firebase is made up of complementary features that you can mix-and-match to fit your needs, with Google Analytics for Firebase at the core. You can consider Firebase as an option if you are unwilling and hesitant to build a backend from scratch when you are just starting an Android project.

Firebase is a platform that contains a lot many standard functionalities for your project and it offers features like Analytics, Real-Time Database, File Storage, Notifications. Authentication, Remote config, Test Lab, Crash Reporting and many more.

Let’s check how to set up an Android Project to make use of the Firebase platform and how to use analytics to send events to the Firebase Console.

Create a Firebase Project

To create a project in Firebase console login with your account and give a name to your project along with your country name. You can add multiple platforms to a single project. The project will tie together all your stats and metrics from different apps and you can view all your data on the project dashboard.

Add your Android Application

Create an your Android project and pick the package name. This will be available in your build.gradle file as applicationId property. You can optionally add the SHA-1 of the signing certificate of the debug keystore.

Add Config file

Once you have added your Android project to the Firebase project, it will generate a google-service.json file that should be copied to the root of your project. Open project view in android studio and paste the file.

Add Firebase Dependencies

Add firebase dependencies to Android Project build.gradle files.

Add to project’s build.gradle

buildscript {
    // ...
    dependencies {
        // ...
        classpath 'com.google.gms:google-services:3.0.0'
    }
}

Add to app module’s build.gradle file

dependencies {
  // ...
  compile 'com.google.firebase:firebase-core:9.0.2'
}

Apply plugin to app module’s build.gradle file

apply plugin: 'com.google.gms.google-services'

Now you have completed the setup to send analytics from your Android project to Firebase console.

Send Firebase Analytics Events

Sending analytics events from your android app is quite easy.

  1. In your code get a FirebaseAnalytics instance
  2. Create Bundle key value pairs of the property that you need to send
  3. And call the logevent() method

For example :

FirebaseAnalytics firebaseAnalytics = FirebaseAnalytics.getInstance(this);

Bundle bundle = new Bundle();

bundle.putString(FirebaseAnalytics.Param.ITEM_ID, "id");

bundle.putString(FirebaseAnalytics.Param.ITEM_NAME, "name");

bundle.putString(FirebaseAnalytics.Param.CONTENT_TYPE, "image");

firebaseAnalytics.logEvent(FirebaseAnalytics.Event.SELECT_CONTENT, bundle);

You can verify the recorded data on Firebase console, but the analytics will take upto 24 hours before the events will be shown.

Did you like this blog? Are you looking to develop an android app?

Fast Forward Innovation with Perfomatix. 

Talk to our experts now and let us be your innovation partner!