Add Firebase Google Analytics to the Android Project Using the Firebase Assistant

Google Analytics for Firebase is an analytics solution that helps the developers understand user behavior in their applications.

In this article, we will take a look at creating a Firebase project and adding Analytics to the android project with the help of Firebase Assistant available in the Android Studio IDE.

1. Open an existing Android project or create a new project in the Android Studio.

2. Tap on the Tools tab and select the Firebase option.

tools-firebase-option

3. This opens the Firebase Assistant window on the right side of the Android Studio. Expand Analytics and tap on Log an analytics event.

4. Now, we can see the instructions to integrate Analytics. Click on the Connect to Firebasebutton.

firebase-assistant-log-analytics

[If we are not logged in to the google account, a Sign in with Google page is opened in the browser. Once logged in, it is redirected to another webpage where we need to select Login to Firebase option.]

This opens a dialog with the options to select the project name from an existing Firebase or Google project or Create a new Firebase project. Select the preferred option and the country.

5. Click on Connect to Firebase button.

create-firebase-project

This creates the project in the Firebase console or links the current Android project to an existing Firebase project.

6. Now tap on the Add Analytics to your app button.

firebase-dependencies

This shows a dialog displaying the changes that will happen to the project level build.gradle and module level build.gradle files.

Click on Accept Changes which triggers the gradle sync. Once finished, we can start logging the firebase analytics events.

Log Custom Events

Get an instance of Firebase analytics object by passing the context.

private val firebaseAnalytics = context?.let { FirebaseAnalytics.getInstance(it) }

Firebase Analytics automatically logs some events like app open, uninstalls and few more. In case, we want to log custom events, we can do so as shown below.

firebaseAnalytics?.logEvent(FIREBASE_ANALYTICS_EVENT_CAPTURE_IMAGE, null)

To log bundle extras along with the event, we have to create a bundle and pass it when logging the events.

val bundle = Bundle().apply {
    putString(BUNDLE_EXTRA_CAMERA_PERMISSION_GRANTED, isCameraGranted.toString())
    putString(BUNDLE_EXTRA_STORAGE_PERMISSION_GRANTED, isStorageGranted.toString())
}
firebaseAnalytics?.logEvent(FIREBASE_ANALYTICS_EVENT_RUNTIME_PERMISSION, bundle)

We can monitor the events in the Firebase Console but the extras are only visible in the Big Query. The events to appear in the Firebase Console takes around 4 - 6 hrs. If we want to monitor the events in realtime, we can use the Debug View.

Popular posts from this blog

Arithmetic Operations in Tensorflow

How to Create Facebook App

How to Create Facebook Page