简体   繁体   中英

Which is the best way to initiate Google Firebase Analytic instance in Android Applications?

I wanted to use Firebase Analytic in my project.But I am confused with the three approaches I come across while reading how to use Firebase Analytics.

1 Using Activity. 2 Using Application. 3 Using Content Provider.

I have also scene the following post.

[Question]:calling-firebase-analytics-getinstance-every-time-vs-storing-instance Calling Firebase Analytic's getInstance() every time vs. storing instance as a static variable in Application class

[Blog]: how-does-firebase-initialize-on-android https://firebase.googleblog.com/2016/12/how-does-firebase-initialize-on-android.html

So, Friends I would like to know in which scenario we should prefer the following approach.How Activity cycle affect the analytic data posted to firebase if I use Activity context vs Application Context. I am worried that Firebase might use the context to derive something about life-time or flow of application.

Firebase Analytics automatically logs some events and user properties; you don't need to add any code to enable them. https://firebase.google.com/docs/analytics/android/start

You just need to call FirebaseAnalytics.getInstance() in onCreate method in your application class, not need to call it in every activity.

First, you need to understand the difference between 1 Using Activity. 2 Using Application.

  1. Activity context has a limited scope it is available only on when activity is available and context is null when activity is destroyed

  2. The application context is available throughout the app and it is initialized only once when application open the first time

ideally, you should use application context to avoid Null pointer and Firebase Analytics need to set only once.

Firebase Analytics automatically logs some events including the activity name but it will not log the custom widget.

For a custom widget, you need to create a custom event and send to Firebase Analytics:

Bundle bundle = new Bundle();
bundle.putString(FirebaseAnalytics.Param.ITEM_ID, id);
bundle.putString(FirebaseAnalytics.Param.ITEM_NAME, name);
bundle.putString(FirebaseAnalytics.Param.CONTENT_TYPE, "image");
mFirebaseAnalytics.logEvent(FirebaseAnalytics.Event.SELECT_CONTENT, bundle);

Referee this link

Hope it helps!!!

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