31

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?

Frank van Puffelen
  • 565,676
  • 79
  • 828
  • 807
Óscar
  • 1,143
  • 1
  • 19
  • 38

5 Answers5

22

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"
  ...
shoheikawano
  • 1,092
  • 3
  • 14
  • 31
Hoshouns
  • 2,420
  • 23
  • 24
  • 2
    Then why did Steve Ganem say it's not supported? – Rony Tesler Aug 23 '16 at 00:25
  • 2
    @H.S.H is not saying that Page/ScreenViews are supported. He only answered the question if it needs to be initialized in every activity. And the answer is no, you can instantiate it in Application class. – RenatoIvancic Sep 04 '16 at 08:33
  • Hello, i am Android noob, and am fascinated about what happened here. I think the real magic happened once `".MyApplication"` was added in manifest. Could you please take a moment and explain what exactly happened here? – Anuj Oct 21 '17 at 07:07
  • Also, i want to use the analytics instance to log different events, should i create a get method? – Anuj Oct 21 '17 at 07:38
  • 1
    @Anuj `android:name=".MyApplication"` just tells Android that it should use the `MyApplication` class as Application class. Without this your custom Application class would be ignored. And yes, if you want to log additional events you will have to create a getter. – tobltobs Dec 14 '18 at 10:52
  • 1
    @RonyTesler Because he thought that it's not supported. – Jaimin Modi Sep 06 '19 at 11:17
13

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

Emre Tekin
  • 462
  • 7
  • 18
  • There is nothing mentioning a BaseActivity in the doc you are linking to. Could you explain what you mean with BaseActivity? – tobltobs Dec 14 '18 at 10:44
  • If you use BaseActivity you need to call setCurrentScreen() just once. Otherwise you have to do that every activity. For Another solution using just once, you can open new class which is name AnalyticsHelper and put there method like public static void sendScreen(Activity activity, String eventName, String screenName) { ...} @tobltobs – Emre Tekin Dec 19 '18 at 12:56
8

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.

Mark
  • 7,446
  • 5
  • 55
  • 75
4

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

Steve Ganem
  • 10,591
  • 2
  • 39
  • 32
1

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

Kőne Mátyás
  • 312
  • 1
  • 2
  • 14