简体   繁体   中英

It is necessary to initialize Firebase Analytics in every Activity?

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?

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

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

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.

Firebase Analytics尚不支持自动屏幕跟踪,但我们现在正在仔细考虑这一点。

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM