It is necessary to initialize Firebase Analytics in every Activity?

Óscar picture Óscar · Jul 22, 2016 · Viewed 11.2k times · Source

I don´t want to send any special logs to the Firebase Analytics console, just check in which screens is the user spending more time and so on.

When I used AnalyticsTracker it was compulsory to add it everywhere, so do you can set the specific name of every screen with the Tracker.xml file.

The official documentation says:

Add the dependency for Firebase Analytics to your app-level build.gradle file:

compile 'com.google.firebase:firebase-core:9.2.1'

Declare the FirebaseAnalytics object at the top of your activity:

private FirebaseAnalytics mFirebaseAnalytics;

Then initialize it in the onCreate() method:

mFirebaseAnalytics = FirebaseAnalytics.getInstance(this);

So I guess I´ve to do this in every page where I want to get data, haven´t I?

Answer

Hoshouns picture Hoshouns · Jul 22, 2016

No. You just need to create global variable in an Class which extends Application class



    public class MyApplication extends Application {
    public static  FirebaseAnalytics mFirebaseAnalytics;
    @Override
        public void onCreate() {
            super.onCreate();
          mFirebaseAnalytics = FirebaseAnalytics.getInstance(this);
    }
    }

After, you add the following line in your manifest, in the Application tag

<application
  android:name=".MyApplication"
  ...