Skip to main content

Integrating Google Firebase Analytics to your Android Project

Firebase is a cloud service provider and backend as a server for mobile and web app. Firebase was founded in 2011 and launched with a real time cloud database in April 2012. Firebase has many features; most of them are free while some are paid. Firebase include mobile app analytics (Android, iOS, etc), cloud messaging, firebase auth, storage, hosting, remote config, test lab for android, crash reporting, send notification to the users, app indexing, dynamic links and send personalized email and SMS invitation, measuring the impact of invitation.

https://www.viralandroid.com/2016/07/integrating-google-firebase-analytics-to-your-android-project.html

In this tutorial, you will learn to integrate or add Google firebase analytics to your android project. Firebase analytics collects usage and behavior data for your application or game. If you already integrate any of the firebase services in your android app or game then adding firebase analytic is very easy. If not, don’t worry. In this tutorial, I will show you step-by-step guide to integrate Google firebase and firebase analytic in your android application with simple example and you can download this sample project source code from GitHub at last.

Related:
Android Push Notification Tutorial Using Firebase
How to Integrate Google AdMob in Android
Google Maps Android API Getting Started Tutorial : Part 1

Integrating Google Firebase Analytics to your Android Project: Get Started with Firebase Analytics for Android


Before starting to add google firebase analytic to your android project, make sure your device is running Android 2.3 (Gingerbread) or newer, and Google Play services 9.2.1 or newer and Android Studio 1.5 or higher.

Integrating Google Firebase Analytics to your Android Project: Get Started with Firebase Analytics for Android

1. Create a new Android Project using Android Studio


Create a new android project with the package name: Add Firebase Analytics to Android Project and select minimum SDK: API 9 Android 2.3 (Gingerbread) or higher.

2. Install the Firebase ADK to your Android Project


To integrate the firebase libraries into your own android project, you need to perform certain tasks. 

2.1. Open your project level (top level) build.gradle file and add classpath 'com.google.gms:google-services:3.0.0'as a dependencies. Your project level build.gradle file will look like below.

Project level build.gradle file
// Top-level build file where you can add configuration options common to all sub-projects/modules.
buildscript {
repositories {
jcenter()
}
dependencies {
classpath 'com.android.tools.build:gradle:2.1.2'
classpath 'com.google.gms:google-services:3.0.0'
// NOTE: Do not place your application dependencies here; they belong
// in the individual module build.gradle files
}
}
allprojects {
repositories {
jcenter()
}
}
task clean(type: Delete) {
delete rootProject.buildDir
}
view rawbuild.gradle hosted with ❤ by GitHub

2.2. Now open your app level build.gradle file and add compile 'com.google.firebase:firebase-core:9.2.1' as app dependencies and apply plugin: 'com.google.gms.google-services' at the bottom. Your app level build.gradle file will look like below.

App level build.gradle file
apply plugin: 'com.android.application'
android {
compileSdkVersion 23
buildToolsVersion "22.0.1"
defaultConfig {
applicationId "com.viralandroid.addfirebaseanalyticstoandroidproject"
minSdkVersion 14
targetSdkVersion 23
versionCode 1
versionName "1.0"
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
}
}
dependencies {
compile fileTree(dir: 'libs', include: ['*.jar'])
testCompile 'junit:junit:4.12'
compile 'com.android.support:appcompat-v7:23.4.0'
compile 'com.google.firebase:firebase-core:9.2.1'
}
apply plugin: 'com.google.gms.google-services'
view rawbuild.gradle hosted with ❤ by GitHub

3. Adding Internet Permission in your Application AndroidManifest.xml File


In your app’s AndroidManifest.xml file you need to add internet user permission. Open AndroidManifest.xml file and add android.permission.INTERNET which is look like below.

AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.viralandroid.addfirebaseanalyticstoandroidproject">
<uses-permission android:name="android.permission.INTERNET" />
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
view rawAndroidManifest.xml hosted with ❤ by GitHub

4. Add Your App to Your Firebase Project in the Firebase Console


4.1. Go to Google Firebase Developer Console

Add Your App to Your Firebase Project in the Firebase Console

4.2. Create new project.
4.3. A dialog box will appear, add project name: Google Firebase Analytics and choose your country and click Create Project button. 
4.4. After creating new project in Google firebase developer console, new screen will appear like below.

Add Your App to Your Firebase Project in the Firebase Console

4.5. Click add firebase to your android app. Here you need to provide your app package name.
4.6. Add project package name in the box and click Add App button. A file google-services.json is downloaded. Click Continue and Finish button.

5. Adding google-services.json File to Your Project


5.1. Go to android studio. Switch to the project view in Android Studio.
5.2. Add google-services.json file that you just downloaded to your android app directory like the screenshot below.

Adding google-services.json File to Your Project

6. Adding Firebase Analytics for Android to Your Android App


In android studio, again open app level build.gradle file and add compile 'com.google.android.gms:play-services-analytics:9.2.1' as app dependencies. Following is the final content of app level build.gradle file.

App level build.gradle file
apply plugin: 'com.android.application'
android {
compileSdkVersion 23
buildToolsVersion "22.0.1"
defaultConfig {
applicationId "com.viralandroid.addfirebaseanalyticstoandroidproject"
minSdkVersion 14
targetSdkVersion 23
versionCode 1
versionName "1.0"
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
}
}
dependencies {
compile fileTree(dir: 'libs', include: ['*.jar'])
testCompile 'junit:junit:4.12'
compile 'com.android.support:appcompat-v7:23.4.0'
compile 'com.google.firebase:firebase-core:9.2.1'
compile 'com.google.android.gms:play-services-analytics:9.2.1'
}
apply plugin: 'com.google.gms.google-services'
view rawbuild.gradle hosted with ❤ by GitHub


7. Check Your App Report in Google Firebase Analytics


7.1. Go to Firebase Developers Console.
7.2. Choose your app
7.3. Click Analytics from left menu. For the first time, it will look like below.

Check Your App Report in Google Firebase Analytics

After some hours or day you can see your app’s complete reports like below.

Check Your App Report in Google Firebase Analytics

That’s all. You have done all things. Now you can see your application’s complete report in Google firebase analytic. It’s very easy and simple to integrate. 


8. Java Activity File and XML Layout File


Following is the content of XML layout file.

res/activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
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.viralandroid.addfirebaseanalyticstoandroidproject.MainActivity">
<TextView
android:layout_width="match_parent"
android:layout_height="50dp"
android:background="#ffffff"
android:elevation="4dp"
android:gravity="center"
android:text="Firebase Analytics" />
<TextView
android:layout_width="match_parent"
android:layout_height="50dp"
android:layout_marginTop="10dp"
android:background="#ffffff"
android:elevation="4dp"
android:gravity="center"
android:text="Get started with Firebase Analytics" />
<TextView
android:layout_width="match_parent"
android:layout_height="50dp"
android:layout_marginTop="10dp"
android:background="#ffffff"
android:elevation="4dp"
android:gravity="center"
android:text="Add Firebase to your Android Project" />
<TextView
android:layout_width="match_parent"
android:layout_height="50dp"
android:layout_marginTop="10dp"
android:background="#ffffff"
android:elevation="4dp"
android:gravity="center"
android:text="Google launches Firebase Analytics for mobile apps" />
<TextView
android:layout_width="match_parent"
android:layout_height="50dp"
android:layout_marginTop="10dp"
android:background="#ffffff"
android:elevation="4dp"
android:gravity="center"
android:text="Android Firebase - Integrating Analytics" />
<TextView
android:layout_width="match_parent"
android:layout_height="50dp"
android:layout_marginTop="10dp"
android:background="#ffffff"
android:elevation="4dp"
android:gravity="center"
android:text="Firebase expands to become a unified app platform" />
<TextView
android:layout_width="match_parent"
android:layout_height="50dp"
android:layout_marginTop="10dp"
android:background="#ffffff"
android:elevation="4dp"
android:gravity="center"
android:text="Google expands Firebase with analytics, remote config, crash" />
<TextView
android:layout_width="match_parent"
android:layout_height="50dp"
android:layout_marginTop="10dp"
android:background="#ffffff"
android:elevation="4dp"
android:gravity="center"
android:text="Integrating Google Firebase Analytics to your Android Project" />
</LinearLayout>
view rawactivity_main.xml hosted with ❤ by GitHub

Following is the complete content of java activity file.

src/MainActivity.java
package com.viralandroid.addfirebaseanalyticstoandroidproject;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
}
view rawMainActivity.java hosted with ❤ by GitHub

Download Complete Project Source Code

You can download complete example project source code from GitHub.

Comments

Popular posts from this blog

web2apk

http://web2apk.com/create.aspx Create App   Intro   About   Changes   MalWare ?   Contact   Privacy Useful Links Bluetooth Mini Keyboards Android Mini PC Reset Android URL App Title Icon or

Android Bar Chart Using MpAndroidChart Library Tutorial

https://www.numetriclabz.com/android-bar-chart-using-mpandroidchart-library-tutorial/ Android Bar Chart Using MpAndroidChart Library Tutorial Objective In this tutorial we learn how to implement Bar Chart using MpAndroidChart Library in your Android App. Download Source Code       Step 1 Contents ·        1  Introduction ·        2  Creating Bar chart o    2.1  Create a new Project o    2.2  Adding library in Project o    2.3  Create Layout o    2.4  To Plot Bar Chart §   2.4.1  Initialize the graph id §   2.4.2  Creating a Dataset §   2.4.3  Defining X-axis labels §   2.4.4  Set the data §   2.4.5  Add the description to the chart §   2.4.6  Run your App § ...

how to retrieve image from sqlite database in android and display in listview

 Android platform provides several ways to store data in our application. 1. SQLite database 2. SharedPreferences etc For our post, we will only work with SQLite database. First and foremost, we need to understand what an SQLite database is? SQLite database  is an open source SQL database that stores data to a text file on a device. It executes SQL Commands to perform a set of functions, that is, create, read, update and delete operations. On my previous post, I showed how to  store data in SQLite database from edit text, retrieve and populate it in a listview . For this post, I will show the SQLite CRUD operations with images from gallery and text from EditText. We need to understand this; images are stored in SQLite database as BLOB data type. A BLOB is a large binary object that can hold a variable amount of data.  Note, we can only store images in the database as BLOB data type. We need to convert our image path to a bitmap th...