Skip to content

Instantly share code, notes, and snippets.

@mertceyhan
Created December 4, 2022 23:40
Show Gist options
  • Save mertceyhan/158ecab43547012b7eea4a6d71ec7043 to your computer and use it in GitHub Desktop.
Save mertceyhan/158ecab43547012b7eea4a6d71ec7043 to your computer and use it in GitHub Desktop.
A base screen composable to send screen view event.
/**
* Add this into AndroidManifest.xml to disable the automatic screen views tracker.
*
* <meta-data
* android:name="google_analytics_automatic_screen_reporting_enabled"
* android:value="false" />
* */
@Composable
fun Screen(
context: Context = LocalContext.current,
screenName: String,
launchedEffect: suspend CoroutineScope.() -> Unit = {
logScreenViewEvent(
context = context,
screenName = screenName
)
},
content: @Composable () -> Unit
) {
content()
LaunchedEffect(screenName, launchedEffect)
}
fun logScreenViewEvent(context: Context, screenName: String) {
FirebaseAnalytics.getInstance(context).logEvent(
FirebaseAnalytics.Event.SCREEN_VIEW, bundleOf(
FirebaseAnalytics.Param.SCREEN_NAME to screenName,
FirebaseAnalytics.Param.SCREEN_CLASS to screenName
)
)
}
// An example screen
@Composable
fun HomeScreen() {
// Screen composable sent a screen view event by given screen name.
Screen(screenName = "HomeScreen") {
// Screen content
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment