Monday, November 21, 2016

Firebase Analytics


Firebase Analytics is a free, out-of-the-box analytics solution that inspires actionable insights based on app usage and user engagement.




 Points to integrate Firebase Analytics in your project.
  1. Crate a project in android studio.
  2. Open build.gradle(project) and add a class path to it(
    classpath 'com.google.gms:google-services:3.0.0'). After adding the class path it will be look like this-
    // 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.2.0'
    
            //________class path for google service
            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
    }
    
      
  3. Open build.gradle(app).
  4. Add a dependancy (<project>/<app-module>/build.gradle)-
    •  compile 'com.google.firebase:firebase-core:9.0.0'
  5. Add a plugin in the bottom of build.gradle(<project>/<app-module>/build.gradle)-
    •  apply plugin: 'com.google.gms.google-services' .
  6. After adding dependency and plugin your app level build.gradle will be look like this- 
    apply plugin: 'com.android.application'
    android {
    
        compileSdkVersion 23
        buildToolsVersion "23.0.2"
    
        defaultConfig {
            applicationId "com.pankaj.firebaseanalytics"
            minSdkVersion 15
            targetSdkVersion 23
            versionCode 1
            versionName "1.0"
            testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
        }
    
        buildTypes {
            release {
                minifyEnabled false
                proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
            }
        }
    }
    
    dependencies {
        compile fileTree(dir: 'libs', include: ['*.jar'])
        androidTestCompile('com.android.support.test.espresso:espresso-core:2.2.2', {
            exclude group: 'com.android.support', module: 'support-annotations'
        })
        compile 'com.android.support:appcompat-v7:23.2.1'
        testCompile 'junit:junit:4.12'
    
    /*______________________for firebase analytics______________________*/
        compile 'com.google.firebase:firebase-core:9.0.0'
      
    }
    
    apply plugin: 'com.google.gms.google-services'
    
      


  7. Now your updated manifest.xml will be look like this-

    <?xml version="1.0" encoding="utf-8"?>
    <manifest xmlns:android="http://schemas.android.com/apk/res/android"
        package="com.pankaj.firebaseanalytics">
    
        <!--permissions-->
        <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">
    
            <!--activities-->
            <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> 

  8.  Now go to the Firebase developer console and create a new project

  9.  Enter your application name and select your country. then click on "create project"

  10.  Now you will see a screen like this
  11. then click on "add Firebase to your Android app".
  12. Now put 
    1. Package name  
    2. App nickname (optional)
    3. Debug signing certificate SHA-1 (optional) 

  13.  Copy and paste the google-services.json file into the application root directory.



  14. Open main activity and put the following codes.

    package com.pankaj.firebaseanalytics;
    import android.os.Bundle;
    import android.support.v7.app.AppCompatActivity;
    import com.google.firebase.analytics.FirebaseAnalytics;
    
    public class MainActivity extends AppCompatActivity {
    
        private FirebaseAnalytics mFirebaseAnalytics;
        private String APP_OPEN = "APP_OPEN";
    
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
            initializeAnalitics();
        }
    
        /*_________________________ Initialize Analytics ___________________________*/
        private void initializeAnalitics() {
            mFirebaseAnalytics = FirebaseAnalytics.getInstance(this);
            addEventsToAnalitics(APP_OPEN, APP_OPEN);
        }
    
         /*_________________________ Send Event ___________________________*/
        public void addEventsToAnalitics(String event, String message) {
            Bundle params = new Bundle();
            params.putString("message", message);
            mFirebaseAnalytics.logEvent(event, params);
        }
    }
    
      
  15. Now firebase analytics have integrated in application. Run this app and go to the firebase console to check analytics data, then we will see the window like-



  16. Analytics data will be show within 24 hours. After 24 hours above window shows data like this-

  17. Good bye, Thanks to read this blog.


No comments:

Post a Comment