How to track android fragments using firebase analytics
Since setCurrentScreen
is deprecated you can use firebaseAnalytics.logEvent(FirebaseAnalytics.Event.SCREEN_VIEW, bundle)
instead.
there is a blog post here that explains more about tracking screens manually.
here is an example:
private fun setCurrentScreen(screenName: String) = firebaseAnalytics?.run {
val bundle = Bundle()
bundle.putString(FirebaseAnalytics.Param.SCREEN_NAME, screenName)
bundle.putString(FirebaseAnalytics.Param.SCREEN_CLASS, [email protected])
logEvent(FirebaseAnalytics.Event.SCREEN_VIEW, bundle)
}
In addition, if you want to track screens automatically, you can call this function in one of your BaseFragment
lifecycle methods like onResume
. just keep in mind that some of the fragments may not have to change the current screen, like the ones which are being created in a ViewPager
, so I have declared an open val
which you can override
in order to change the default behavior.
here is the code in BaseFragment
:
protected open val trackScreenView: Boolean = true
override fun onResume() {
super.onResume()
if (trackScreenView) setCurrentScreen(this.javaClass.simpleName)
}
and you can disable it by overriding it in your target Fragment
:
override val trackScreenView: Boolean = false
By the way, if you are using NavigationUI
Component, currently there is no automatic solution for tracking screens, and it only track the single activity you have, so you can prevent firebase automatic screen reporting by putting this meta-data
in your app manifest:
<application
android:name=".App"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme">
<!-- .... -->
<meta-data
android:name="google_analytics_automatic_screen_reporting_enabled"
android:value="false" />
</application>
For projects using NavigationUI, you can use a listener NavController.OnDestinationChangedListener
Inside onCreate()
override fun onCreate() {
super.onCreate()
.
.
.
.
navController = Navigation.findNavController(context!!, R.id.nav_host_fragment)
navController?.addOnDestinationChangedListener(listener)
}
Of the 3 listener function parameters,
controller
is useful to obtain classnamedestination
is useful to obtain layout xml name string of destination as found inside nav_host_fragment by the attributeandroid:label
private val listener = NavController.OnDestinationChangedListener { controller, destination, arguments ->
val bundle = Bundle()
val currentFragmentClassName = (controller.currentDestination as FragmentNavigator.Destination).className
bundle.putString(FirebaseAnalytics.Param.SCREEN_NAME, destination.label.toString())
bundle.putString(FirebaseAnalytics.Param.SCREEN_CLASS, currentFragmentClassName)
FirebaseAnalytics.getInstance(requireContext()).logEvent(FirebaseAnalytics.Event.SCREEN_VIEW, bundle)
}
Don't forget cleanup
override fun onDestroy() {
super.onDestroy()
navController?.removeOnDestinationChangedListener(listener)
}
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
UPDATE
Since the setCurrentScreen
is deprecated, you can use logEvent
method
Bundle bundle = new Bundle();
bundle.putString(FirebaseAnalytics.Param.SCREEN_NAME, fragment.getClass().getSimpleName());
bundle.putString(FirebaseAnalytics.Param.SCREEN_CLASS, fragment.getClass().getSimpleName());
mFirebaseAnalytics.logEvent(FirebaseAnalytics.Event.SCREEN_VIEW, bundle);
I've used the following adb commands to check if all is working fine.
adb shell setprop log.tag.FA VERBOSE
adb shell setprop log.tag.FA-SVC VERBOSE
adb logcat -v time -s FA FA-SVC
Once you do that you'll see screen_view
events in the logcat. Like this one:
10-15 13:14:13.744 V/FA-SVC (20323): Logging event: origin=app,name=screen_view(_vs),params=Bundle[{ga_event_origin(_o)=app, engagement_time_msec(_et)=31600, ga_previous_class(_pc)=ContentsFragment, ga_previous_id(_pi)=8077407744361472421, ga_previous_screen(_pn)=ContentsFragment, ga_screen_class(_sc)=TestFragment, ga_screen_id(_si)=8077407744361472423, ga_screen(_sn)=TestFragment}]
Previous answer
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.