DEV Community

Cover image for Firebase Analytics For Android Shouldn't Be This Hard
Offline Programmer
Offline Programmer

Posted on

Firebase Analytics For Android Shouldn't Be This Hard

Well, it is not. Let me show you how

Prerequisites

Add Firebase to your App

  • On the Firebase console, click on the Android icon to add your App.

Screen Shot 2020-10-31 at 7.41.00 PM

  • Enter your App package name and a nickname. Then click on the (Register app) button.

Screen Shot 2020-10-31 at 7.53.27 PM

  • Download the config file and add it to your project as per the screen below

Screen Shot 2020-10-31 at 8.08.10 PM

  • Follow the instructions in step 3 to update the gradle files.

Screen Shot 2020-10-31 at 8.22.24 PM

  • Now our app is ready for Firebase Integration.

Use the Analytics SDK on your App

  • Declare the FirebaseAnalytics object at the top of MainActivity.
private FirebaseAnalytics mFirebaseAnalytics;
Enter fullscreen mode Exit fullscreen mode
  • Initialize it in the onCreate() method
mFirebaseAnalytics = FirebaseAnalytics.getInstance(this);
Enter fullscreen mode Exit fullscreen mode
  • Run the App and check the Firebase Dashboard

Screen Shot 2020-10-31 at 8.50.33 PM

  • Add a button to your activity, update the onCreate as below to log the click event of the button
Button button = findViewById(R.id.button);
        button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                mFirebaseAnalytics.logEvent("button_clicked", null);
            }
        });
Enter fullscreen mode Exit fullscreen mode
  • Run the App and click on the button. Then check the Firebase Dashboard for the event.

  • Check the Logcat tab, and you will find the logged event.

Screen Shot 2020-10-31 at 9.59.35 PM

That's it... I told you it is easy.

Check the code here

Follow me on Twitter for more tips about #coding, #learning, #technology, #Java, #JavaScript, #Autism, #Parenting...etc.

Check my Apps on Google Play

Cover image Luke Chesser

Discussion (0)