How to Add Google Analytics in Android And Flutter Application

How to Add Google Analytics in Android And Flutter Application





 

Why do We Have To Use Google Analytics?🤔🤔

Google Analytics is one of the most popular digital analytics software.

-It provides valuable insights that can help you shape your business's success strategy.

- Adding Analytics in a mobile application is a great idea, which is really helpful for tracking the app usage and user actions.

- Also, It would assist the business to know the widely used functionality of the customers and for further enhancements.


Below are some reasons why you use Google Analytics to get better insights into your website and visitors.


1. It's free



Google does not charge you anything for using Google Analytics. You don’t have to pay anything thing to use this product.

2. Automatic collection of data

You can even access your reports immediately without any delay. This feature of Google Analytics not only saves your work effort but also gives you immediate access to the reports. With this, you can soon implement strategies for the better performance of your website.


Android Project Setup

1)Create New Project 

2)Create a Custome Design Like This In Your Main XML File


<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginTop="20dp"
tools:context=".MainActivity">

<Button
android:id="@+id/addToCart_btn"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Add To Cart"
android:layout_above="@id/cod_btn"
/>

<Button
android:id="@+id/cod_btn"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Case On Delivery"
android:layout_above="@id/directTransfer_btn"
/>

<Button
android:id="@+id/directTransfer_btn"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Direct Transfer"
android:layout_alignParentBottom="true"
/>

</RelativeLayout>

3)Add FireBase Dependency Like This In-App Level Build. Gradle


//Firebase Dependency
implementation platform('com.google.firebase:firebase-bom:31.2.0')
implementation 'com.google.firebase:firebase-analytics'


Also, Add on the top of the App Level Build. Gradle File

plugins {
id 'com.android.application'
id 'com.google.gms.google-services'
}


4)Add This Line In Your Project Level Dependency 

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

5)After Sync The Project

6)While Syncing the Project Let's Create New Firebase Project So We Can Simple Add Google.json File to our project.

-Create New Project With Any Name

-Follow The Steps Provide Firebase 



-After Creating the Project Go To the Application Click on android and fill in All things that are required.

-Download Json File And Add it In App





7)After That Give Permission For a Network Like This In Android Studio

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


8)For Analytic Add This Code In Your MainActivity.java File 

public class MainActivity extends AppCompatActivity {

FirebaseAnalytics analytics;
Button addToCart_btn,cod_btn,directTransfer_btn;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

analytics= FirebaseAnalytics.getInstance(this);

addToCart_btn=findViewById(R.id.addToCart_btn);
cod_btn=findViewById(R.id.cod_btn);
directTransfer_btn=findViewById(R.id.directTransfer_btn);

addToCart_btn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Bundle bundle=new Bundle();
bundle.putString("Cart","Cart Button");
analytics.logEvent("Cart_Button",bundle);
}
});

cod_btn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Bundle bundle=new Bundle();
bundle.putString("Cod","Cod Button");
analytics.logEvent("Cod_Button",bundle);
}
});

directTransfer_btn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Bundle bundle=new Bundle();
bundle.putString("directTransfer","directTransfer Button");
analytics.logEvent("directTransfer_Button",bundle);
}
});

}
}

9)For Check If This Is Work Or Not Go To Firebase Analytic In DebugView



For Device Detact In Debug View Add This Command In Your Android Terminal

adb shell setprop debug.firebase.analytics.app com.example.firebase_analytics_with_custom_events(This Is Your PackeName)


Just Run The Code...




If You Got Any Problem Related to This Do Let Me Know In Comment Section Till Then Bye.

Post a Comment

0 Comments