In my android application, I have an Activity that has 3 or 4 fragments that can be attached in sequence based on some user or server events.
I would like to track all these fragments as screens in firebase.
So ideally, if possible, is there an API i can invoke in the onCreate of the fragments, and tell firebase that user is currently in fragment1, fragment2 or fragment3?
There is a special method to set a current screen - setCurrentScreen
I used it as follows
mFirebaseAnalytics.setCurrentScreen(this, fragment.getClass().getSimpleName(), fragment.getClass().getSimpleName());
Once the method is called, the following message appears in the LogCat
Logging event (FE): screen_view(_vs), Bundle[{firebase_event_origin(_o)=auto, firebase_previous_class(_pc)=HomeFragment, firebase_previous_id(_pi)=4121566113087629222, firebase_previous_screen(_pn)=HomeFragment, firebase_screen_class(_sc)=StatisticsFragment, firebase_screen_id(_si)=4121566113087629223, firebase_screen(_sn)=StatisticsFragment}]
The following event appears on auto activity tracking:
Logging event (FE): screen_view(_vs), Bundle[{firebase_event_origin(_o)=auto, firebase_previous_class(_pc)=StatisticsFragment, firebase_previous_id(_pi)=4121566113087629223, firebase_previous_screen(_pn)=StatisticsFragment, firebase_screen_class(_sc)=LoginActivity, firebase_screen_id(_si)=4121566113087629224}]
As you see, they are almost the same, so setCurrentScreen
is working.
I'm able to see those classes in Firebase Console only on the next day. It is normal for Firebase - it takes time to process such amounts of data.
Adding a some more insight here to Artem Mostyaev answer. GA/Firebase panel was reflecting the class name in DEV version but not on PROD version.The main culprit here is
fragment.getClass().getSimpleName()
which obfuscate the fragment name in prod. So GA/Firebase was showing classname to be like (a,b,ah, etc)
getSimpleName() is also dangerous to use in other situation.
More literature : https://medium.com/@elye.project/the-danger-of-using-class-getsimplename-as-tag-for-fragment-5cdf3a35bfe2
Progaurd rules
-keepnames class com.somepackage.yourclass
来源:https://stackoverflow.com/questions/45201346/how-to-track-android-fragments-using-firebase-analytics