Skip to content

Commit

Permalink
[feature/#1009] AmplitudeTracker 추상화하여 Preview에 Fake 넣을 수 있게 수정 (#1010)
Browse files Browse the repository at this point in the history
* [#1009] AmplitudeTracker 추상화 및 패키지 이동

* [#1009] TrackerModule에 구현체 주입

* [#1009] Preview용 FakeTracker 생성

* [#1009] LocalTracker 관련 코드 추가

* [#1009] 기존 코드 AmplitudeTracker -> Tracker로 리팩토링

* [#1009] 수정 예시 추가

* [#1009] Spotless Apply
  • Loading branch information
l2hyunwoo authored Jan 2, 2025
1 parent d719334 commit 35b3dc9
Show file tree
Hide file tree
Showing 23 changed files with 248 additions and 77 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -49,8 +49,8 @@ import kotlinx.coroutines.flow.launchIn
import kotlinx.coroutines.flow.onEach
import kotlinx.coroutines.launch
import org.sopt.official.R
import org.sopt.official.analytics.AmplitudeTracker
import org.sopt.official.analytics.EventType
import org.sopt.official.analytics.Tracker
import org.sopt.official.auth.model.UserActiveState
import org.sopt.official.auth.model.UserStatus
import org.sopt.official.common.navigator.DeepLinkType
Expand Down Expand Up @@ -90,7 +90,7 @@ class HomeActivity : AppCompatActivity() {
private val args by serializableExtra(StartArgs(UserStatus.UNAUTHENTICATED))

@Inject
lateinit var tracker: AmplitudeTracker
lateinit var tracker: Tracker
private val smallBlockAdapter: SmallBlockAdapter?
get() = binding.smallBlockList.adapter as? SmallBlockAdapter

Expand Down
1 change: 1 addition & 0 deletions core/analytics/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@

plugins {
sopt("feature")
sopt("compose")
}

android {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
/*
* MIT License
* Copyright 2024 SOPT - Shout Our Passion Together
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/
package org.sopt.official.analytics

interface Tracker {
fun track(type: EventType, name: String, properties: Map<String, Any?> = emptyMap())
fun setNotificationStateToUserProperties(value: Boolean)
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
/*
* MIT License
* Copyright 2024 SOPT - Shout Our Passion Together
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/
package org.sopt.official.analytics

import dagger.Binds
import dagger.Module
import dagger.hilt.InstallIn
import dagger.hilt.components.SingletonComponent
import org.sopt.official.analytics.impl.AmplitudeTracker
import javax.inject.Singleton

@Module
@InstallIn(SingletonComponent::class)
interface TrackerModule {
@Binds
@Singleton
fun bindAmplitudeTracker(amplitudeTracker: AmplitudeTracker): Tracker
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
/*
* MIT License
* Copyright 2024 SOPT - Shout Our Passion Together
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/
package org.sopt.official.analytics.compose

import androidx.compose.runtime.Composable
import androidx.compose.runtime.CompositionLocalProvider
import org.sopt.official.analytics.Tracker

@Composable
fun ProvideTracker(
tracker: Tracker,
content: @Composable () -> Unit
) {
CompositionLocalProvider(
LocalTracker provides tracker
) {
content()
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
/*
* MIT License
* Copyright 2024 SOPT - Shout Our Passion Together
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/
package org.sopt.official.analytics.compose

import androidx.compose.runtime.staticCompositionLocalOf
import org.sopt.official.analytics.Tracker

val LocalTracker = staticCompositionLocalOf<Tracker> {
error("No Tracker provided")
}
Original file line number Diff line number Diff line change
Expand Up @@ -22,19 +22,22 @@
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/
package org.sopt.official.analytics
package org.sopt.official.analytics.impl

import android.content.Context
import com.amplitude.android.Amplitude
import com.amplitude.android.Configuration
import com.amplitude.android.events.Identify
import dagger.hilt.android.qualifiers.ApplicationContext
import javax.inject.Inject
import org.sopt.official.analytics.BuildConfig
import org.sopt.official.analytics.EventType
import org.sopt.official.analytics.Tracker
import timber.log.Timber
import javax.inject.Inject

class AmplitudeTracker @Inject constructor(
@ApplicationContext private val context: Context
) {
) : Tracker {
private val amplitude = Amplitude(
Configuration(
apiKey = if (BuildConfig.DEBUG) {
Expand All @@ -46,14 +49,14 @@ class AmplitudeTracker @Inject constructor(
)
)

fun track(type: EventType, name: String, properties: Map<String, Any?> = emptyMap()) {
override fun track(type: EventType, name: String, properties: Map<String, Any?>) {
if (BuildConfig.DEBUG) {
Timber.d("Amplitude: ${type.prefix}_$name properties: $properties")
}
amplitude.track(eventType = "${type.prefix}_$name", eventProperties = properties)
}

fun setNotificationStateToUserProperties(value: Boolean) {
override fun setNotificationStateToUserProperties(value: Boolean) {
val identify = Identify()
identify.setOnce("state_of_push_notification", value)
amplitude.identify(identify)
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
/*
* MIT License
* Copyright 2024 SOPT - Shout Our Passion Together
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/
package org.sopt.official.analytics.impl

import org.sopt.official.analytics.EventType
import org.sopt.official.analytics.Tracker

object FakeTracker : Tracker {
override fun track(type: EventType, name: String, properties: Map<String, Any?>) = Unit
override fun setNotificationStateToUserProperties(value: Boolean) = Unit
}
Original file line number Diff line number Diff line change
Expand Up @@ -29,12 +29,11 @@ import android.content.Intent
import android.os.Bundle
import androidx.activity.compose.setContent
import androidx.appcompat.app.AppCompatActivity
import androidx.compose.runtime.CompositionLocalProvider
import androidx.compose.runtime.staticCompositionLocalOf
import com.airbnb.deeplinkdispatch.DeepLink
import dagger.hilt.android.AndroidEntryPoint
import dagger.hilt.android.EntryPointAccessors
import org.sopt.official.analytics.AmplitudeTracker
import org.sopt.official.analytics.compose.ProvideTracker
import org.sopt.official.analytics.impl.AmplitudeTracker
import org.sopt.official.common.context.appContext
import org.sopt.official.common.navigator.NavigatorEntryPoint
import org.sopt.official.designsystem.SoptTheme
Expand All @@ -47,10 +46,6 @@ private val navigator by lazy {
).navigatorProvider()
}

internal val LocalAmplitudeTracker = staticCompositionLocalOf<AmplitudeTracker> {
error("No AmplitudeTracker provided")
}

@AndroidEntryPoint
@DeepLink("sopt://fortune")
class FortuneActivity : AppCompatActivity() {
Expand All @@ -62,7 +57,7 @@ class FortuneActivity : AppCompatActivity() {
super.onCreate(savedInstanceState)
setContent {
SoptTheme {
CompositionLocalProvider(LocalAmplitudeTracker provides amplitudeTracker) {
ProvideTracker(amplitudeTracker) {
FoundationScreen(
onClickLeadingIcon = ::finish,
navigateToHome = {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,8 @@ import androidx.compose.ui.unit.dp
import androidx.navigation.NavHostController
import androidx.navigation.compose.NavHost
import androidx.navigation.compose.rememberNavController
import org.sopt.official.analytics.compose.ProvideTracker
import org.sopt.official.analytics.impl.FakeTracker
import org.sopt.official.designsystem.SoptTheme
import org.sopt.official.feature.fortune.R.drawable.ic_alert
import org.sopt.official.feature.fortune.R.drawable.ic_poke_check
Expand Down Expand Up @@ -141,9 +143,11 @@ fun FoundationScreen(
@Composable
fun FoundationScreenPreview() {
SoptTheme {
FoundationScreen(
onClickLeadingIcon = {},
navigateToHome = {}
)
ProvideTracker(FakeTracker) {
FoundationScreen(
onClickLeadingIcon = {},
navigateToHome = {}
)
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -36,17 +36,17 @@ import androidx.compose.ui.Modifier
import androidx.compose.ui.tooling.preview.Preview
import androidx.compose.ui.unit.dp
import org.sopt.official.analytics.EventType
import org.sopt.official.analytics.compose.LocalTracker
import org.sopt.official.designsystem.SoptTheme
import org.sopt.official.designsystem.SoptTheme.colors
import org.sopt.official.feature.fortune.LocalAmplitudeTracker

@Composable
fun FortuneTopBar(
onClickNavigationIcon: () -> Unit,
modifier: Modifier = Modifier,
isEnabled: Boolean = true,
) {
val amplitudeTracker = LocalAmplitudeTracker.current
val amplitudeTracker = LocalTracker.current

Box(modifier = modifier.fillMaxWidth()) {
Icon(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,8 +46,8 @@ import androidx.compose.ui.unit.dp
import androidx.hilt.navigation.compose.hiltViewModel
import androidx.lifecycle.compose.collectAsStateWithLifecycle
import org.sopt.official.analytics.EventType
import org.sopt.official.analytics.compose.LocalTracker
import org.sopt.official.designsystem.SoptTheme
import org.sopt.official.feature.fortune.LocalAmplitudeTracker
import org.sopt.official.feature.fortune.component.CircleShapeBorderButton
import org.sopt.official.feature.fortune.component.UrlImage

Expand All @@ -56,7 +56,7 @@ internal fun FortuneAmuletRoute(
onHomeClick: () -> Unit,
viewModel: FortuneAmuletViewModel = hiltViewModel(),
) {
val amplitudeTracker = LocalAmplitudeTracker.current
val amplitudeTracker = LocalTracker.current
val state by viewModel.state.collectAsStateWithLifecycle()

when {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,8 +47,8 @@ import androidx.lifecycle.compose.collectAsStateWithLifecycle
import kotlinx.coroutines.launch
import org.sopt.official.analytics.EventType.CLICK
import org.sopt.official.analytics.EventType.VIEW
import org.sopt.official.analytics.compose.LocalTracker
import org.sopt.official.designsystem.SoptTheme.colors
import org.sopt.official.feature.fortune.LocalAmplitudeTracker
import org.sopt.official.feature.fortune.feature.fortuneDetail.component.PokeMessageBottomSheetScreen
import org.sopt.official.feature.fortune.feature.fortuneDetail.model.FortuneDetailUiState.Success
import org.sopt.official.feature.fortune.feature.fortuneDetail.model.SnackBarUiState
Expand All @@ -73,7 +73,7 @@ internal fun FortuneDetailRoute(
var selectedIndex by remember { mutableIntStateOf(DEFAULT_ID) }
val bottomSheetState = rememberModalBottomSheetState(initialValue = Hidden)
val scope = rememberCoroutineScope()
val amplitudeTracker = LocalAmplitudeTracker.current
val amplitudeTracker = LocalTracker.current

LaunchedEffect(key1 = uiState) {
if (uiState is Success) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,8 +41,8 @@ import androidx.compose.ui.res.vectorResource
import androidx.compose.ui.tooling.preview.Preview
import androidx.compose.ui.unit.dp
import org.sopt.official.analytics.EventType
import org.sopt.official.analytics.compose.LocalTracker
import org.sopt.official.designsystem.SoptTheme
import org.sopt.official.feature.fortune.LocalAmplitudeTracker
import org.sopt.official.feature.fortune.R
import org.sopt.official.feature.fortune.component.FortuneButton
import java.time.LocalDate
Expand All @@ -55,7 +55,7 @@ internal fun HomeRoute(
onFortuneDetailClick: (String) -> Unit,
) {
val date = rememberSaveable { getToday() }
val amplitudeTracker = LocalAmplitudeTracker.current.also {
val amplitudeTracker = LocalTracker.current.also {
it.track(
type = EventType.VIEW,
name = "view_soptmadi_title",
Expand Down
Loading

0 comments on commit 35b3dc9

Please sign in to comment.