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 a
As 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, this@BaseFragment.javaClass.simpleName)
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: