How to Integrate and Use Google Firebase Android Analytics?

Google added another interesting feature “Firebase” for the Android developers and it’s been around the development world for a while now.

It is free, simple, and unlimited.

Sounds like a scam right?

But fortunately it isn’t.

The Firebase Android analytics basically provides unlimited reporting for events that can be integrated using Firebase SDK in an Android app.

In simple terms, Firebase Android analytics report helps you to understand your app users’ behaviour, which ultimately helps you make informed decisions for your app marketing as well as performance improvements.

How Firebase Exactly Works?

First of all, Firebase automatically captures app user properties and number of events that allows developer to define your own custom events. By doing so, developer can easily measure the records that matters most to your business.

After capturing these data – developer can easily view in dashboard through Firebase console. The Firebase dashboard provides all user insights about your app data, from present active users to identifying most visited tab or most purchased products.

It’s Not Over Yet! Read on…

The Firebase Android analytics also provides number of other features. For instance, Firebase automatically logs events that is related to your Firebase notifications. Furthermore, Firebase also offers reporting on each campaign. This way, developers now can easily track their app data, without missing a single detail. And due to this reason only, we always recommend our clients as well as our readers to integrate Firebase Android analytics in their Android apps.

Now that you know everything about Firebase, it’s time to create one demo to understand it practically.

Getting Started

Create a new project under file menu and click on next.

Step 1 Integrate and Use Google Firebase Android Analytics

Select the device on which you want to run your project.

Step 2 Integrate and Use Google Firebase Android Analytics

In the next tab, select “Empty Activity” and click on next.

Step 3 Integrate and Use Google Firebase Android Analytics

Finally, name your activity and click on Finish button.

Step 4 Integrate and Use Google Firebase Android Analytics

After creating new project, open Firebase tools by Android Studio 2.2 tools -> Firebase.

Step 5 Integrate and Use Google Firebase Android Analytics

Now setup the Firebase Analytics.

Step 5 Integrate and Use Google Firebase Android Analytics

Click  “Connect to Firebase” button.

Step 7 Integrate and Use Google Firebase Android Analytics

Step 8 Integrate and Use Google Firebase Android Analytics

Next, add this analytics to app module. Click “add analytics to your app”.

Step 9 Integrate and Use Google Firebase Android Analytics

Declare the FirebaseAnalytics object at the top of your activity.

private FirebaseAnalytics firebaseAnalytics;

Then initialize it in the onCreate() method.

// Obtain the Firebase Analytics instance.
firebaseAnalytics = FirebaseAnalytics.getInstance(this);

Check your analytics from Firebase Console.

1

2

 

 

Add Permission in AndroidManifest File.

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

Now the below code is separate for you to understand each query.

1) Open Screen Event

Screenshot_12

Screenshot_13

Bundle params = new Bundle();
params.putString(FirebaseAnalytics.Param.ITEM_CATEGORY, "screen");
params.putString(FirebaseAnalytics.Param.ITEM_NAME, "Notification Screen");
firebaseAnalytics.logEvent(FirebaseAnalytics.Event.VIEW_ITEM, params);

2) Add Custom Event

Screenshot_15

Bundle bundle = new Bundle();
bundle.putString(FirebaseAnalytics.Param.ITEM_ID, "ID 1");
bundle.putString(FirebaseAnalytics.Param.ITEM_NAME, "Custom Name");
bundle.putString(FirebaseAnalytics.Param.CONTENT_TYPE, "image");
firebaseAnalytics.logEvent(FirebaseAnalytics.Event.SELECT_CONTENT, bundle);

3) Add User Property Event

Screenshot_17

private FirebaseAnalytics firebaseAnalytics;
String[] names = new String[]{"Adam", "Binny", "Germish", "Mick", "Oren"};

// Obtain the Firebase Analytics instance.
firebaseAnalytics = FirebaseAnalytics.getInstance(this);

public void onAddUserPropertyClick(View view) {
Person name = new Person();
name.setId(1);
// choose random food name from the list
name.setName(names[randomIndex()]);
Bundle bundle = new Bundle();
bundle.putInt(FirebaseAnalytics.Param.ITEM_ID, name.getId());
bundle.putString(FirebaseAnalytics.Param.ITEM_NAME, name.getName());
//Logs an app event.
firebaseAnalytics.logEvent(FirebaseAnalytics.Event.SELECT_CONTENT, bundle);
//Sets whether analytics collection is enabled for this app on this device.
firebaseAnalytics.setAnalyticsCollectionEnabled(true);
//Sets the minimum engagement time required before starting a session. The default value is 10000 (10 seconds). Let's make it 20 seconds just for the fun
firebaseAnalytics.setMinimumSessionDuration(20000);
//Sets the duration of inactivity that terminates the current session. The default value is 1800000 (30 minutes).
firebaseAnalytics.setSessionTimeoutDuration(500);
//Sets the user ID property.
firebaseAnalytics.setUserId(String.valueOf(name.getId()));
//Sets a user property to a given value.
firebaseAnalytics.setUserProperty("Person", name.getName());
}
private int randomIndex() {
int min = 0;
int max = names.length - 1;
Random rand = new Random();
return min + rand.nextInt((max - min) + 1);
}

Start Code Integration in Your App

  • MainActivity.java
//Starting Actvity

public class MainActivity extends AppCompatActivity {
private FirebaseAnalytics firebaseAnalytics;
String[] names = new String[]{"Adam", "Binny", "Germish", "Mick", "Oren"};;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// Obtain the Firebase Analytics instance.
firebaseAnalytics = FirebaseAnalytics.getInstance(this);
}
public void onNotificationClick(View view) {
Intent iHome = new Intent(this, NotificationActivity.class);
startActivity(iHome);
}
public void onCustomEventClick(View view) {
Bundle bundle = new Bundle();
bundle.putString(FirebaseAnalytics.Param.ITEM_ID, "ID 1");
bundle.putString(FirebaseAnalytics.Param.ITEM_NAME, "Custom Name");
bundle.putString(FirebaseAnalytics.Param.CONTENT_TYPE, "image");
firebaseAnalytics.logEvent(FirebaseAnalytics.Event.SELECT_CONTENT, bundle);
}
public void onAddUserPropertyClick(View view) {
Person name = new Person();
name.setId(1);
// choose random food name from the list
name.setName(names[randomIndex()]);
Bundle bundle = new Bundle();
bundle.putInt(FirebaseAnalytics.Param.ITEM_ID, name.getId());
bundle.putString(FirebaseAnalytics.Param.ITEM_NAME, name.getName());
//Logs an app event.
firebaseAnalytics.logEvent(FirebaseAnalytics.Event.SELECT_CONTENT, bundle);
//Sets whether analytics collection is enabled for this app on this device.
firebaseAnalytics.setAnalyticsCollectionEnabled(true);
//Sets the minimum engagement time required before starting a session. The default value is 10000 (10 seconds). Let's make it 20 seconds just for the fun
firebaseAnalytics.setMinimumSessionDuration(20000);
//Sets the duration of inactivity that terminates the current session. The default value is 1800000 (30 minutes).
firebaseAnalytics.setSessionTimeoutDuration(500);
/

/Sets the user ID property.
firebaseAnalytics.setUserId(String.valueOf(name.getId()));
//Sets a user property to a given value.
firebaseAnalytics.setUserProperty("Person", name.getName());
}

private int randomIndex() {
int min = 0;
int max = names.length - 1;
Random rand = new Random();
return min + rand.nextInt((max - min) + 1);
}
}
  • Person.java
//Custom Class to send custom Event
public class Person {

private int id;
private String name;

public int getId() {
return id;
}

public void setId(int id) {
this.id = id;
}

public String getName() {
return name;
}

public void setName(String name) {
this.name = name;
}
}
  • activity_main.xml
//Layout

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/activity_main"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="com.firebaseanalytics.MainActivity">
<TextView
android:id="@+id/tvTitle"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="10dp"
android:textSize="25sp"
android:textColor="@color/colorPrimary"
android:text="Welcome to Firebase Analytics Demo" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@+id/tvTitle"
android:layout_marginTop="10dp"
android:orientation="vertical">
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:clickable="true"
android:onClick="onNotificationClick"
android:text="Open Notification Screen" />
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:clickable="true"
android:onClick="onCustomEventClick"
android:text="Add Custom Event" />

<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:clickable="true"
android:onClick="onAddUserPropertyClick"
android:text="Add User Property" />
</LinearLayout>
</RelativeLayout>
  • NotificationActivity.java
//New Screen To Add Screen Open Event

public class NotificationActivity extends AppCompatActivity {
private FirebaseAnalytics firebaseAnalytics;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_notification);
// Obtain the Firebase Analytics instance.
firebaseAnalytics = FirebaseAnalytics.getInstance(this);
Bundle params = new Bundle();
params.putString(FirebaseAnalytics.Param.ITEM_CATEGORY, "screen");
params.putString(FirebaseAnalytics.Param.ITEM_NAME, "Notification Screen");
firebaseAnalytics.logEvent(FirebaseAnalytics.Event.VIEW_ITEM, params);
}
}
  • activity_notification.xml
//Layout

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/activity_notification"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="com.firebaseanalytics.NotificationActivity">

<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:gravity="center"
android:text="Notification Data"
android:textSize="25sp" />
</RelativeLayout>

Once you run the demo, it will look like below:

device-2016-10-15-143413

 

Now, perform more than one different activities to check the insights in the FIrebase.

Once you perform the activities, it will take within 24 hours for Firebase to update your app user behaviour. Till then, it will show like below:

Screenshot_10

Done!

Simple as it should be!

Hope you enjoyed our tutorial. And if you faced any problem implementing it, we’re there to help out.

In case you’re a beginner just getting started with Firebase, we have also written articles on Firebase database integration as well as Firebase Push notification implementation.

Overall, it seems like Google is paying attention to the needs of Android developers. And it’s not just about developers, Startups are also getting benefits from it. Startups can revamp their app with Firebase integration for detailed insights, in fact the upcoming startups already include Firebase in their app development plan.

So, if you would like to add it in your Android app as well, you can hire Android app developer from us. We’re an award winning Android app development company with a team of 100+ highly experienced developers. Moreover, having experience of developing 2000+ mobile apps throughout our journey, we always make sure that clients get a quality and successful mobile app.

You can grab a copy of Firebase Android Analytics demo from Github for free.

This page was last edited on October 19th, 2016, at 10:57.
 
 

Have an App Idea?

Get your free consultation now