31
votes

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?

5

5 Answers

22
votes

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"
  ...
12
votes

Screen tracking can now be done with only one line

**Your ApplicationClass**
    public FirebaseAnalytics mFirebaseAnalytics;
    @Override
        public void onCreate() {
     mFirebaseAnalytics = FirebaseAnalytics.getInstance(this);
    }

    public FirebaseAnalytics getmFirebaseAnalytics() {
            return mFirebaseAnalytics;
        }

After that create Base Activity and call the FirebaseAnalytics getter from here. Then use .setCurrentScreen as follows below

**Your BaseActivity**
    @Override
        protected void onResume() {

            FirebaseAnalytics firebaseAnalytics = ((ApplicationClass) getApplication()).getmFirebaseAnalytics();
            firebaseAnalytics.setCurrentScreen(this, getClass().getSimpleName(), null);
            Log.d("FAnalytics", "setCurrentScreen: " + getClass().getSimpleName());
            super.onResume();
        }

Dont forget ! All your Activity has to be extends from BaseActivity https://firebase.google.com/docs/analytics/screenviews

8
votes

For screen reporting, you do not need to call FirebaseAnalytics.setCurrentScreen() in every Activity because this is done for you automatically. The official docs state:

Note that screen reporting is enabled automatically and records the class name of the current Activity for you without requiring you to call this function.

Presumably, for this to work, you need to call FirebaseAnalytics.getInstance() in your Application subclass onCreate() method.

4
votes

Automatic Screen Tracking is not yet supported in Firebase Analytics, but this is something we are carefully considering right now.

1
votes

Firebase Automatically tracks screens activities now, however, you can still track them manually.

mFirebaseAnalytics.setCurrentScreen(this, screenName, null /* class override */);

source:

https://firebase.google.com/docs/analytics/screenviews