Skip to content

Commit

Permalink
Merge pull request #303 from team-haribo/feature/302-add-custom-event…
Browse files Browse the repository at this point in the history
…-to-firebase-analytics

🔀 :: (#302) - add custom event to firebase analytics
  • Loading branch information
diejdkll authored Aug 6, 2024
2 parents 3d8fd38 + 326a669 commit 65ce4c0
Show file tree
Hide file tree
Showing 31 changed files with 250 additions and 12 deletions.
1 change: 1 addition & 0 deletions .idea/gradle.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions app/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ android {
}

dependencies {
implementation(project(":core:analytics"))
implementation(project(":core:common"))
implementation(project(":core:design-system"))
implementation(project(":core:datastore"))
Expand Down
6 changes: 6 additions & 0 deletions app/src/main/java/com/goms/goms_android_v2/MainActivity.kt
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.setValue
import androidx.core.splashscreen.SplashScreen.Companion.installSplashScreen
import androidx.lifecycle.lifecycleScope
import com.goms.analytics.AnalyticsHelper
import com.goms.goms_android_v2.ui.GomsApp
import com.goms.ui.createToast
import com.google.android.play.core.appupdate.AppUpdateManagerFactory
Expand All @@ -22,6 +23,7 @@ import com.google.firebase.messaging.FirebaseMessaging
import dagger.hilt.android.AndroidEntryPoint
import kotlinx.coroutines.launch
import kotlinx.coroutines.runBlocking
import javax.inject.Inject

@AndroidEntryPoint
class MainActivity : ComponentActivity() {
Expand All @@ -33,6 +35,9 @@ class MainActivity : ComponentActivity() {
}
}

@Inject
lateinit var analyticsHelper: AnalyticsHelper

private val viewModel: MainActivityViewModel by viewModels()

@OptIn(ExperimentalMaterial3WindowSizeClassApi::class)
Expand Down Expand Up @@ -67,6 +72,7 @@ class MainActivity : ComponentActivity() {
onAlarmOff = { viewModel.deleteDeviceToken() },
onAlarmOn = { saveNotification() },
uiState = uiState,
analyticsHelper = analyticsHelper
)
}
}
Expand Down
8 changes: 6 additions & 2 deletions app/src/main/java/com/goms/goms_android_v2/ui/GomsApp.kt
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
package com.goms.goms_android_v2.ui

import android.util.Log
import androidx.compose.material3.windowsizeclass.WindowSizeClass
import androidx.compose.runtime.Composable
import androidx.compose.runtime.CompositionLocalProvider
import androidx.compose.runtime.collectAsState
import androidx.compose.runtime.getValue
import androidx.hilt.navigation.compose.hiltViewModel
import com.goms.analytics.AnalyticsHelper
import com.goms.analytics.LocalAnalyticsHelper
import com.goms.design_system.theme.GomsTheme
import com.goms.goms_android_v2.MainActivityUiState
import com.goms.goms_android_v2.MainActivityViewModel
Expand All @@ -25,6 +26,7 @@ fun GomsApp(
onAlarmOff: () -> Unit,
onAlarmOn: () -> Unit,
uiState: MainActivityUiState,
analyticsHelper: AnalyticsHelper,
viewModel: MainActivityViewModel = hiltViewModel(),
) {
val themeState by viewModel.themeState.collectAsState()
Expand All @@ -34,7 +36,9 @@ fun GomsApp(
if (alarmState == Switch.ON.value) onAlarmOn()

GomsTheme(themeMode = themeState) {
CompositionLocalProvider {
CompositionLocalProvider(
LocalAnalyticsHelper provides analyticsHelper
) {
GomsNavHost(
appState = appState,
qrcodeState = qrcodeState,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ class AndroidFeatureConventionPlugin : Plugin<Project> {
add("implementation", project(":core:domain"))
add("implementation", project(":core:common"))
add("implementation", project(":core:datastore"))
add("implementation", project(":core:analytics"))

add("implementation", libs.findLibrary("coil.kt").get())

Expand Down
1 change: 1 addition & 0 deletions core/analytics/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
/build
17 changes: 17 additions & 0 deletions core/analytics/build.gradle.kts
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
@Suppress("DSL_SCOPE_VIOLATION") // TODO: Remove once KTIJ-19369 is fixed
plugins {
id("goms.android.core")
id("goms.android.hilt")
id("goms.android.compose")
}

android {
namespace = "com.goms.analytics"
}

dependencies {
implementation(libs.androidx.compose.runtime)

implementation(platform(libs.firebase.bom))
implementation(libs.firebase.analytics)
}
Empty file.
21 changes: 21 additions & 0 deletions core/analytics/proguard-rules.pro
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
# Add project specific ProGuard rules here.
# You can control the set of applied configuration files using the
# proguardFiles setting in build.gradle.
#
# For more details, see
# http://developer.android.com/guide/developing/tools/proguard.html

# If your project uses WebView with JS, uncomment the following
# and specify the fully qualified class name to the JavaScript interface
# class:
#-keepclassmembers class fqcn.of.javascript.interface.for.webview {
# public *;
#}

# Uncomment this to preserve the line number information for
# debugging stack traces.
#-keepattributes SourceFile,LineNumberTable

# If you keep the line number information, uncomment this to
# hide the original source file name.
#-renamesourcefileattribute SourceFile
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package com.goms.analytics

import androidx.test.platform.app.InstrumentationRegistry
import androidx.test.ext.junit.runners.AndroidJUnit4

import org.junit.Test
import org.junit.runner.RunWith

import org.junit.Assert.*

/**
* Instrumented test, which will execute on an Android device.
*
* See [testing documentation](http://d.android.com/tools/testing).
*/
@RunWith(AndroidJUnit4::class)
class ExampleInstrumentedTest {
@Test
fun useAppContext() {
// Context of the app under test.
val appContext = InstrumentationRegistry.getInstrumentation().targetContext
assertEquals("com.goms.analytics.test", appContext.packageName)
}
}
4 changes: 4 additions & 0 deletions core/analytics/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android">

</manifest>
20 changes: 20 additions & 0 deletions core/analytics/src/main/java/com/goms/analytics/AnalyticsEvent.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package com.goms.analytics

data class AnalyticsEvent(
val type: String,
val extras: List<Param> = emptyList(),
) {
class Types {
companion object {
const val SCREEN_VIEW = "screen_view"
}
}

data class Param(val key: String, val value: String)

class ParamKeys {
companion object {
const val SCREEN_NAME = "screen_name"
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package com.goms.analytics

interface AnalyticsHelper {
fun logEvent(event: AnalyticsEvent)
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package com.goms.analytics

import com.google.firebase.analytics.FirebaseAnalytics
import com.google.firebase.analytics.ktx.logEvent
import javax.inject.Inject

internal class FirebaseAnalyticsHelper @Inject constructor(
private val firebaseAnalytics: FirebaseAnalytics,
) : AnalyticsHelper {

override fun logEvent(event: AnalyticsEvent) {
firebaseAnalytics.logEvent(event.type) {
for (extra in event.extras) {
param(
key = extra.key.take(40),
value = extra.value.take(100),
)
}
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package com.goms.analytics

class NoOpAnalyticsHelper : AnalyticsHelper {
override fun logEvent(event: AnalyticsEvent) = Unit
}
7 changes: 7 additions & 0 deletions core/analytics/src/main/java/com/goms/analytics/UiHelper.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package com.goms.analytics

import androidx.compose.runtime.staticCompositionLocalOf

val LocalAnalyticsHelper = staticCompositionLocalOf<AnalyticsHelper> {
NoOpAnalyticsHelper()
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package com.goms.analytics.di

import com.goms.analytics.AnalyticsHelper
import com.goms.analytics.FirebaseAnalyticsHelper
import com.google.firebase.analytics.FirebaseAnalytics
import com.google.firebase.analytics.ktx.analytics
import com.google.firebase.ktx.Firebase
import dagger.Binds
import dagger.Module
import dagger.Provides
import dagger.hilt.InstallIn
import dagger.hilt.components.SingletonComponent
import javax.inject.Singleton

@Module
@InstallIn(SingletonComponent::class)
internal abstract class AnalyticsModule {
@Binds
abstract fun bindsAnalyticsHelper(
analyticsHelperImpl: FirebaseAnalyticsHelper
): AnalyticsHelper

companion object {
@Provides
@Singleton
fun provideFirebaseAnalytics(): FirebaseAnalytics = Firebase.analytics
}
}
17 changes: 17 additions & 0 deletions core/analytics/src/test/java/com/goms/analytics/ExampleUnitTest.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package com.goms.analytics

import org.junit.Test

import org.junit.Assert.*

/**
* Example local unit test, which will execute on the development machine (host).
*
* See [testing documentation](http://d.android.com/tools/testing).
*/
class ExampleUnitTest {
@Test
fun addition_isCorrect() {
assertEquals(4, 2 + 2)
}
}
1 change: 1 addition & 0 deletions core/ui/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ android {
}

dependencies {
implementation(project(":core:analytics"))
implementation(project(":core:design-system"))
implementation(project(":core:model"))

Expand Down
30 changes: 30 additions & 0 deletions core/ui/src/main/java/com/goms/ui/AnalyticsExtension.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
package com.goms.ui

import androidx.compose.runtime.Composable
import androidx.compose.runtime.DisposableEffect
import com.goms.analytics.AnalyticsEvent
import com.goms.analytics.AnalyticsEvent.Types
import com.goms.analytics.AnalyticsEvent.ParamKeys
import com.goms.analytics.AnalyticsEvent.Param
import com.goms.analytics.AnalyticsHelper
import com.goms.analytics.LocalAnalyticsHelper

fun AnalyticsHelper.logScreenView(screenName: String) {
logEvent(
AnalyticsEvent(
type = Types.SCREEN_VIEW,
extras = listOf(
Param(ParamKeys.SCREEN_NAME, screenName),
),
),
)
}

@Composable
fun TrackScreenViewEvent(
screenName: String,
analyticsHelper: AnalyticsHelper = LocalAnalyticsHelper.current,
) = DisposableEffect(Unit) {
analyticsHelper.logScreenView(screenName)
onDispose {}
}
3 changes: 3 additions & 0 deletions feature/main/src/main/java/com/goms/main/LateListScreen.kt
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import androidx.compose.runtime.setValue
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.platform.LocalContext
import androidx.compose.ui.res.stringResource
import androidx.compose.ui.unit.dp
import androidx.hilt.navigation.compose.hiltViewModel
import androidx.lifecycle.compose.collectAsStateWithLifecycle
Expand All @@ -40,6 +41,7 @@ import com.goms.main.viewmodel.MainViewModel
import com.goms.model.enum.Authority
import com.goms.model.util.ResourceKeys
import com.goms.ui.GomsRoleBackButton
import com.goms.ui.TrackScreenViewEvent
import kotlinx.datetime.Instant
import kotlinx.datetime.LocalDate
import kotlinx.datetime.TimeZone
Expand Down Expand Up @@ -120,6 +122,7 @@ private fun LateListScreen(
}
)
}
TrackScreenViewEvent(screenName = stringResource(id = R.string.late_list_screen))
}

@ThemeDevicePreviews
Expand Down
3 changes: 3 additions & 0 deletions feature/main/src/main/java/com/goms/main/MainScreen.kt
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import androidx.compose.runtime.setValue
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.platform.LocalContext
import androidx.compose.ui.res.stringResource
import androidx.compose.ui.unit.dp
import androidx.hilt.navigation.compose.hiltViewModel
import androidx.lifecycle.compose.collectAsStateWithLifecycle
Expand All @@ -48,6 +49,7 @@ import com.goms.main.viewmodel.uistate.SaveTokenUiState
import com.goms.main.viewmodel.uistate.TokenRefreshUiState
import com.goms.model.enum.Switch
import com.goms.model.util.ResourceKeys
import com.goms.ui.TrackScreenViewEvent
import com.goms.ui.rememberMultiplePermissionsStateSafe
import com.google.accompanist.permissions.ExperimentalPermissionsApi
import com.google.accompanist.swiperefresh.SwipeRefresh
Expand Down Expand Up @@ -265,6 +267,7 @@ private fun MainScreen(
}
}
}
TrackScreenViewEvent(screenName = stringResource(id = R.string.main_screen))
}

@ThemeDevicePreviews
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ import com.goms.main.viewmodel.uistate.TokenRefreshUiState
import com.goms.model.enum.Authority
import com.goms.model.util.ResourceKeys
import com.goms.ui.GomsRoleBackButton
import com.goms.ui.TrackScreenViewEvent
import java.util.UUID

@Composable
Expand Down Expand Up @@ -176,6 +177,7 @@ private fun OutingStatusScreen(
}
}
}
TrackScreenViewEvent(screenName = stringResource(id = R.string.outing_status_screen))
}

@ThemeDevicePreviews
Expand Down
Loading

0 comments on commit 65ce4c0

Please sign in to comment.