Created
December 4, 2022 23:40
-
-
Save mertceyhan/158ecab43547012b7eea4a6d71ec7043 to your computer and use it in GitHub Desktop.
A base screen composable to send screen view event.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* 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