Firebase Analytics

Firebase Analytics - It is another feature of Firebase. It is a powerful tool that tracks how user your app.

Analytics provides you with unlimited reporting for up to 500 distinct events that you can define using the firebase SDK.Firebase Analytics reports you understand clearly how your user behave, which enables you to make informed decisions regarding app marketing and performance optimizations.


Creating Firebase Project

 1) First got to https://firebase.google.com/  and  click "GET STARTED FOR FREE" make an                  account and create your first project.

 2) After creating account it redirects you to firebase console. click on "Add Project". A dialog will          open and enter project name and country. click on "Create Project".

3) After creating project, it redirect to firebase console page and click there on "Add firebase to your       android app"



4) Here enter the package name of firebase app and you can also enter the nickname and SHA1 but it     is not compulsory.



5) Click on REGISTER APP and download google-service.json file.



6) click on FINISH.



Create Android Studio Project



1. create a new project in Android studio. File -> New Project use the same package name that you         have put in firebase app.
2. Copy the google-services.json file that you have download from firebase console  paste it in app           folder.
3. Open the build.gradle of project level and add the google dependency

build.gradle
dependencies {
        classpath 'com.android.tools.build:gradle:2.2.0'
        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
    }

4. Open app->build.gradle and add firebase database dependency.At the very bottom of the file,
apply plugin



dependencies {
    // Adding support library for this demo app
    compile 'com.android.support:design:24.2.1'
    compile 'com.google.firebase:firebase-core:9.6.1'
}
apply plugin: 'com.google.gms.google-services'


5. We need to add permission in menifest.xml file:

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


6.  Model class use for log event

Product.java


package com.androidheight.firebaseanalytics;
public class Product {
private int id;
private String name;
private String price;
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;
}
public String getPrice() {
return price;
}
public void setPrice(String price) {
this.price = price;
}
}



7. MainActivity.java


package com.androidheight.firebaseanalytics; import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import com.androidheight.firebaseanalytics.adapter.CategoryListAdapter;
import com.google.firebase.analytics.FirebaseAnalytics;
import java.util.Random;
public class MainActivity extends AppCompatActivity implements CategoryListAdapter.OnViewClickListener {
String[] categoryList;
private RecyclerView mRecyclerView;
private RecyclerView.Adapter mAdapter;
private RecyclerView.LayoutManager mLayoutManager;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
categoryList = new String[]{"Shirt", "Paint", "T-shirt", "Shoes", "Watch"};
mRecyclerView = (RecyclerView) findViewById(R.id.rvCategoryList);
mRecyclerView.setHasFixedSize(true);
// use a linear layout manager
mLayoutManager = new LinearLayoutManager(this);
mRecyclerView.setLayoutManager(mLayoutManager);
// specify an adapter (see also next example)
mAdapter = new CategoryListAdapter(categoryList,MainActivity.this);
mRecyclerView.setAdapter(mAdapter);
}
@Override
public void onViewClicked(View view, int position) {
Intent intent = new Intent(MainActivity.this,ProductListActivity.class);
intent.putExtra("category",categoryList[position]);
startActivity(intent);
}
}



8. activity_main.xml


<?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.androidheight.firebaseanalytics.MainActivity">
<android.support.v7.widget.RecyclerView
android:id="@+id/rvCategoryList"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
</RelativeLayout>




9. ProductListActivity.java


package com.androidheight.firebaseanalytics; import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.MenuItem;
import com.androidheight.firebaseanalytics.adapter.CategoryListAdapter;
import com.google.firebase.analytics.FirebaseAnalytics;
import java.util.Random;
public class ProductListActivity extends AppCompatActivity {
private FirebaseAnalytics firebaseAnalytics;
String Category;
String[] productlist;
private RecyclerView mRecyclerView;
private RecyclerView.Adapter mAdapter;
private RecyclerView.LayoutManager mLayoutManager;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_product_list);
productlist = new String[]{"product 1", "product 2", "product 3", "product 4", "product 5"};
Category = getIntent().getStringExtra("category");
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
getSupportActionBar().setDisplayShowHomeEnabled(true);
getSupportActionBar().setTitle(Category);
mRecyclerView = (RecyclerView) findViewById(R.id.rvProductList);
mRecyclerView.setHasFixedSize(true);
// use a linear layout manager
mLayoutManager = new LinearLayoutManager(this);
mRecyclerView.setLayoutManager(mLayoutManager);
// specify an adapter (see also next example)
mAdapter = new CategoryListAdapter(productlist,ProductListActivity.this);
mRecyclerView.setAdapter(mAdapter);
firebaseAnalytics = FirebaseAnalytics.getInstance(this);
Product product = new Product();
product.setId(1);
//select random product name
product.setName(productlist[randomIndex()]);
Bundle bundle = new Bundle();
bundle.putInt(FirebaseAnalytics.Param.ITEM_ID, product.getId());
bundle.putString(FirebaseAnalytics.Param.ITEM_NAME, product.getName());
bundle.putString(FirebaseAnalytics.Param.CONTENT_TYPE, Category);
//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(product.getId()));
//Sets a user property to a given value.

firebaseAnalytics.setUserProperty("Product", product.getName());
}
/**/
// }


private int randomIndex() {
int min = 0;
int max = productlist.length - 1;
Random rand = new Random();
return min + rand.nextInt((max - min) + 1);
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// handle arrow click here
if (item.getItemId() == android.R.id.home) {
finish(); // close this activity and return to preview activity (if there is any)
}
return super.onOptionsItemSelected(item);
}
}

10. activity_product_list.xml


<?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_product_list"
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.androidheight.firebaseanalytics.ProductListActivity">
<android.support.v7.widget.RecyclerView
android:id="@+id/rvProductList"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
</RelativeLayout>


Firebase Analytics Event

Firebase analysis has build in events. when user interact with your app events automatic triggered.
You can report up to 500 different types of events per app and you can associate up to 25 unique parameters with each Event type. Some common events are suggested below, but you may also choose to specify custom Event types that are associated with your specific app.





The following event names are reserved and can not be used:

  • ad_activeView
  • ad_click
  • ad_exposure
  • ad_impression
  • ad_query
  • adunit_exposure
  • app_clear_data
  • app_uninstall
  • app_update
  • error
  • first_open
  • first_visit
  • in_app_purchase
  • notification_dismiss
  • notification_foreground
  • notification_open
  • notification_receive
  • os_update
  • screen_view
  • session_start
  • user_engagement



output



Download project from  GUTHUB





SHARE

About prabhakar jha

    Blogger Comment
    Facebook Comment

0 comments:

Post a Comment