1

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.

Pratyush Khare
  • 71
  • 1
  • 2
  • 11
  • I have same doubt as one of the commented in the above mentioned post. That is, "I am worried that Firebase might use the context to derive something about life-time or flow of application. All guidelines I've read only states it should be put in Activity" – Pratyush Khare Jul 08 '19 at 09:01

2 Answers2

1

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.

Mahmoud Waked
  • 357
  • 2
  • 8
  • Thanks @Mahmoud Waked. I am using some dynamic widget too. And would like to know difference in creating firebase instance in context of Application vs Activity for user event too. – Pratyush Khare Jul 08 '19 at 05:26
  • 1
    Welcome bro, there is no different, just context of application or activity just detect the scope of read firebase analytics, if you call firebase analytics with context of application will send analytics from all application, if you call firebase analytics with context of activity will send analytics only from this activity. – Mahmoud Waked Jul 08 '19 at 06:33
1

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!!!

Harsh Shah
  • 2,162
  • 2
  • 19
  • 39
  • Thanks @Harsh Shah I have same doubt as one of the commenter had in th above mentioned post. That is, "I am worried that Firebase might use the context to derive something about life-time or flow of application. All guidelines I've read only states it should be put in Activity" – Pratyush Khare Jul 08 '19 at 07:46
  • glad it helped. if you feel that it's the correct answer then accept the answer so it might be useful for the others as well. Happy coding!!! – Harsh Shah Jul 08 '19 at 08:34