AppsChannels

Implementation Firebase Analytics in Android

Firebase Analytics is a free app measurement solution that provides insight on app usage and user engagement. Firebase Analytics reports help you understand clearly how your users behave, which enables you to make informed decisions regarding app marketing and performance optimizations.

Capabilities

Unlimited Reporting : Firebase Analytics provides unlimited reporting on up to 500 distinct events.

Audience Segmentation : Custom audiences can be defined in the Firebase console based on device data, custom events, or user properties. These audiences can be used with other Firebase features when targeting new features or notifications.

So let’s get started on having firebase analytics implemented in our apps, simply by following simple steps below.

Implementation

Connect your App to Firebase

1. Add Firebase to your app

To add Firebase to your app you’ll need a Firebase project and a Firebase configuration file for your app.Create a

  • Firebase project in the Firebase console, if you don’t already have one. If you already have an existing Google project associated with your mobile app, click Import Google Project. Otherwise, click Create New Project.
  • Click Add Firebase to your Android app and follow the setup steps. If you’re importing an existing Google project, this may happen automatically and you can just download the config file.
  • When prompted, enter your app’s package name. It’s important to enter the package name your app is using; this can only be set when you add an app to your Firebase project.
  • At the end, you’ll download a google-services.json file. You can download this file again at any time.
  • If you haven’t done so already, copy this into your project’s module folder, typically app/.

2. Add the SDK

First, add rules to your root-level build.gradle file, to include the google-services plugin:

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

Then, in your module Gradle file (usually the app/build.gradle), add the apply plugin line at the bottom of the file to enable the Gradle plugin:

apply plugin: 'com.android.application'

android {
    // ...
}

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

// ADD THIS AT THE BOTTOM
apply plugin: 'com.google.gms.google-services'

Add Analytics to your app

Add the dependency for Firebase Analytics to your app-level build.gradle file:

compile 'com.google.firebase:firebase-core:9.4.0'

Declare the com.google.firebase.analytics.FirebaseAnalytics object at the top of your activity

private FirebaseAnalytics mFirebaseAnalytics;

Then initialize it in the onCreate() method

// Obtain the FirebaseAnalytics instance.
mFirebaseAnalytics = FirebaseAnalytics.getInstance(this);

Log events
Once you have created a FirebaseAnalytics instance, you can use it to log either predefined or custom events with the logEvent() method. You can explore the predefined events and parameters in the FirebaseAnalytics.Event and FirebaseAnalytics.Param reference documentation.
The following code logs a SELECT_CONTENT Event when a user clicks on a specific element in your app.

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");
mFirebaseAnalytics.logEvent(FirebaseAnalytics.Event.SELECT_CONTENT, bundle);

Next Steps
Explore the guides on logging events and setting user properties.

Tags : analyticsAndroidfirebase
Ardiansa Rachmawan

The author Ardiansa Rachmawan

Leave a Response