Skip to content

Commit

Permalink
Merge pull request #335 from kosenda/refacotr/analytics-logging
Browse files Browse the repository at this point in the history
Analyticsで何を送るかのログを出力する
  • Loading branch information
kosenda authored Nov 11, 2023
2 parents ab2ac62 + dad056b commit 28809a4
Show file tree
Hide file tree
Showing 14 changed files with 142 additions and 141 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ import kotlinx.coroutines.delay
import kotlinx.coroutines.launch
import ksnd.hiraganaconverter.core.analytics.Analytics
import ksnd.hiraganaconverter.core.analytics.LocalAnalytics
import ksnd.hiraganaconverter.core.analytics.RequestReview
import ksnd.hiraganaconverter.core.data.inappupdate.InAppUpdateState
import ksnd.hiraganaconverter.core.domain.inappreview.InAppReviewManager
import ksnd.hiraganaconverter.core.model.ui.Theme
Expand Down Expand Up @@ -132,7 +133,7 @@ class MainActivity : AppCompatActivity() {

LaunchedEffect(uiState.needRequestReview) {
if (uiState.needRequestReview) {
analytics.logRequestReview()
analytics.logEvent(RequestReview())
}
}

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 @@ -11,4 +11,5 @@ dependencies {
implementation(libs.androidx.compose.runtime)
implementation(platform(libs.firebase.bom))
implementation(libs.firebase.analytics)
implementation(libs.timber)
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,5 @@ package ksnd.hiraganaconverter.core.analytics

interface Analytics {
fun logScreen(screen: Screen)
fun logConvert(hiraKanaType: String, inputTextLength: Int)
fun logConvertError(error: String)
fun logChangeHiraKanaType(hiraKanaType: String)
fun logClearAllText()
fun logUpdateTheme(theme: String)
fun logUpdateLanguage(language: String)
fun logUpdateFont(font: String)
fun logSwitchEnableInAppUpdate(isEnable: Boolean)
fun logTotalConvertCount(count: Int)
fun logRequestReview()
fun logEvent(analyticsEvent: AnalyticsEvent)
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,109 @@
package ksnd.hiraganaconverter.core.analytics


sealed class AnalyticsEvent {
abstract val event: Event
open val params: List<AnalyticsParam> = emptyList()
}

data class AnalyticsParam(
val param: Param,
val value: String,
)

class Convert(hiraKanaType: String, inputTextLength: Int) : AnalyticsEvent() {
override val event = Event.CONVERT
override val params = listOf(
AnalyticsParam(Param.CONVERT_TYPE, hiraKanaType),
AnalyticsParam(Param.INPUT_TEXT_LENGTH, inputTextLength.toString()),
)
}

class ConvertError(error: String) : AnalyticsEvent() {
override val event = Event.CONVERT_ERROR
override val params = listOf(
AnalyticsParam(Param.CONVERT_ERROR, error),
)
}

class ChangeHiraKanaType(hiraKanaType: String) : AnalyticsEvent() {
override val event = Event.CHANGE_HIRA_KANA_TYPE
override val params = listOf(
AnalyticsParam(Param.CONVERT_TYPE, hiraKanaType),
)
}

class ClearAllText : AnalyticsEvent() {
override val event = Event.CLEAR_ALL_TEXT
}

class UpdateTheme(theme: String) : AnalyticsEvent() {
override val event = Event.UPDATE_THEME
override val params = listOf(
AnalyticsParam(Param.THEME, theme),
)
}

class UpdateLanguage(language: String) : AnalyticsEvent() {
override val event = Event.UPDATE_LANGUAGE
override val params = listOf(
AnalyticsParam(Param.LANGUAGE, language),
)
}

class UpdateFont(font: String) : AnalyticsEvent() {
override val event = Event.UPDATE_FONT
override val params = listOf(
AnalyticsParam(Param.FONT, font),
)
}

class SwitchEnableInAppUpdate(isEnable: Boolean) : AnalyticsEvent() {
override val event = Event.SWITCH_ENABLE_IN_APP_UPDATE
override val params = listOf(
AnalyticsParam(Param.IS_ENABLE_IN_APP_UPDATE, isEnable.toString()),
)
}

class TotalConvertCount(count: Int) : AnalyticsEvent() {
override val event = Event.COUNT_UP_TOTAL_CONVERT_COUNT
override val params = listOf(
AnalyticsParam(Param.COUNT, count.toString()),
)
}

class RequestReview() : AnalyticsEvent() {
override val event = Event.REQUEST_REVIEW
}

enum class Screen {
CONVERTER,
HISTORY,
SETTING,
INFO,
PRIVACY_POLICY,
}

enum class Event {
CONVERT,
CONVERT_ERROR,
CHANGE_HIRA_KANA_TYPE,
CLEAR_ALL_TEXT,
UPDATE_THEME,
UPDATE_LANGUAGE,
UPDATE_FONT,
SWITCH_ENABLE_IN_APP_UPDATE,
COUNT_UP_TOTAL_CONVERT_COUNT,
REQUEST_REVIEW,
}

enum class Param {
CONVERT_TYPE,
CONVERT_ERROR,
INPUT_TEXT_LENGTH,
THEME,
LANGUAGE,
FONT,
IS_ENABLE_IN_APP_UPDATE,
COUNT,
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,71 +2,23 @@ package ksnd.hiraganaconverter.core.analytics

import com.google.firebase.analytics.FirebaseAnalytics
import com.google.firebase.analytics.logEvent
import timber.log.Timber
import javax.inject.Inject

class AnalyticsImpl @Inject constructor(
private val firebaseAnalytics: FirebaseAnalytics,
) : Analytics {
override fun logScreen(screen: Screen) {
firebaseAnalytics.logEvent(FirebaseAnalytics.Event.SCREEN_VIEW) {
Timber.d("logScreen: screen=${screen.name}")
param(FirebaseAnalytics.Param.SCREEN_NAME, screen.name)
}
}

override fun logConvert(hiraKanaType: String, inputTextLength: Int) {
firebaseAnalytics.logEvent(Event.CONVERT.name) {
param(Param.CONVERT_TYPE.name, hiraKanaType)
param(Param.INPUT_TEXT_LENGTH.name, inputTextLength.toString())
override fun logEvent(analyticsEvent: AnalyticsEvent) {
firebaseAnalytics.logEvent(analyticsEvent.event.name) {
Timber.d("logEvent: event=${analyticsEvent.event}, params=${analyticsEvent.params}")
analyticsEvent.params.forEach { param(it.param.name, it.value) }
}
}

override fun logConvertError(error: String) {
firebaseAnalytics.logEvent(Event.CONVERT_ERROR.name) {
param(Param.CONVERT_ERROR.name, error)
}
}

override fun logChangeHiraKanaType(hiraKanaType: String) {
firebaseAnalytics.logEvent(Event.CHANGE_HIRA_KANA_TYPE.name) {
param(Param.CONVERT_TYPE.name, hiraKanaType)
}
}

override fun logClearAllText() {
firebaseAnalytics.logEvent(Event.CLEAR_ALL_TEXT.name) {}
}

override fun logUpdateTheme(theme: String) {
firebaseAnalytics.logEvent(Event.UPDATE_THEME.name) {
param(Param.THEME.name, theme)
}
}

override fun logUpdateLanguage(language: String) {
firebaseAnalytics.logEvent(Event.UPDATE_LANGUAGE.name) {
param(Param.LANGUAGE.name, language)
}
}

override fun logUpdateFont(font: String) {
firebaseAnalytics.logEvent(Event.UPDATE_FONT.name) {
param(Param.FONT.name, font)
}
}

override fun logSwitchEnableInAppUpdate(isEnable: Boolean) {
firebaseAnalytics.logEvent(Event.SWITCH_ENABLE_IN_APP_UPDATE.name) {
param(Param.IS_ENABLE_IN_APP_UPDATE.name, isEnable.toString())
}
}

override fun logTotalConvertCount(count: Int) {
firebaseAnalytics.logEvent(Event.COUNT_UP_TOTAL_CONVERT_COUNT.name) {
param(Param.COUNT.name, count.toString())
}
}

override fun logRequestReview() {
firebaseAnalytics.logEvent(Event.REQUEST_REVIEW.name) {}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,5 @@ package ksnd.hiraganaconverter.core.analytics
*/
class MockAnalytics : Analytics {
override fun logScreen(screen: Screen) {}
override fun logConvert(hiraKanaType: String, inputTextLength: Int) {}
override fun logConvertError(error: String) {}
override fun logChangeHiraKanaType(hiraKanaType: String) {}
override fun logClearAllText() {}
override fun logUpdateTheme(theme: String) {}
override fun logUpdateLanguage(language: String) {}
override fun logUpdateFont(font: String) {}
override fun logSwitchEnableInAppUpdate(isEnable: Boolean) {}
override fun logTotalConvertCount(count: Int) {}
override fun logRequestReview() {}
override fun logEvent(analyticsEvent: AnalyticsEvent) {}
}

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import kotlinx.datetime.Clock
import kotlinx.datetime.TimeZone
import kotlinx.datetime.todayIn
import ksnd.hiraganaconverter.core.analytics.Analytics
import ksnd.hiraganaconverter.core.analytics.TotalConvertCount
import ksnd.hiraganaconverter.core.domain.repository.ReviewInfoRepository
import ksnd.hiraganaconverter.core.model.ReviewInfo
import javax.inject.Inject
Expand All @@ -20,7 +21,7 @@ class ReviewInfoRepositoryImpl @Inject constructor(
override suspend fun countUpTotalConvertCount(): Int {
val newCount = (reviewInfo.firstOrNull()?.totalConvertCount ?: 0) + 1
dataStore.updateData { it.copy(totalConvertCount = newCount) }
analytics.logTotalConvertCount(count = newCount)
analytics.logEvent(TotalConvertCount(count = newCount))
return newCount
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,6 @@ import androidx.datastore.preferences.preferencesDataStoreFile
import androidx.test.core.app.ApplicationProvider
import app.cash.turbine.test
import com.google.common.truth.Truth.assertThat
import io.mockk.coVerify
import io.mockk.spyk
import kotlinx.coroutines.test.runTest
import kotlinx.datetime.Clock
import kotlinx.datetime.TimeZone
Expand All @@ -31,10 +29,9 @@ class ReviewInfoRepositoryImplTest {
serializer = ReviewInfoSerializer,
produceFile = { context.preferencesDataStoreFile("TestReviewInfoDataStore") },
)
private val analytics = spyk<MockAnalytics>()
private val repository = ReviewInfoRepositoryImpl(
dataStore = dataStore,
analytics = analytics,
analytics = MockAnalytics(),
)

@Test
Expand All @@ -43,7 +40,6 @@ class ReviewInfoRepositoryImplTest {
assertThat(awaitItem()).isEqualTo(ReviewInfo())
assertThat(repository.countUpTotalConvertCount()).isEqualTo(1)
assertThat(awaitItem()).isEqualTo(ReviewInfo().copy(totalConvertCount = 1))
coVerify { analytics.logTotalConvertCount(count = 1) }
}
}

Expand All @@ -54,7 +50,6 @@ class ReviewInfoRepositoryImplTest {
repeat(5) {
assertThat(repository.countUpTotalConvertCount()).isEqualTo(it + 1)
assertThat(awaitItem()).isEqualTo(ReviewInfo().copy(totalConvertCount = it + 1))
coVerify { analytics.logTotalConvertCount(count = it + 1) }
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,10 @@ import kotlinx.coroutines.flow.asStateFlow
import kotlinx.coroutines.flow.update
import kotlinx.coroutines.launch
import ksnd.hiraganaconverter.core.analytics.Analytics
import ksnd.hiraganaconverter.core.analytics.ChangeHiraKanaType
import ksnd.hiraganaconverter.core.analytics.ClearAllText
import ksnd.hiraganaconverter.core.analytics.Convert
import ksnd.hiraganaconverter.core.analytics.ConvertError
import ksnd.hiraganaconverter.core.domain.NavKey
import ksnd.hiraganaconverter.core.domain.usecase.ConvertTextUseCase
import ksnd.hiraganaconverter.core.domain.usecase.toConvertErrorType
Expand All @@ -36,7 +40,7 @@ class ConvertViewModel @Inject constructor(
fun convert() {
// If input has not changed since the last time, it will not be converted.
if (uiState.value.isChangedInputText().not()) return
analytics.logConvert(hiraKanaType = uiState.value.selectedTextType.name, inputTextLength = uiState.value.inputText.length)
analytics.logEvent(Convert(hiraKanaType = uiState.value.selectedTextType.name, inputTextLength = uiState.value.inputText.length))
viewModelScope.launch {
_uiState.update { it.copy(isConverting = true) }
runCatching {
Expand All @@ -53,7 +57,7 @@ class ConvertViewModel @Inject constructor(
}.onFailure { throwable ->
val convertErrorType = throwable.toConvertErrorType()
Timber.d("convert error type: $convertErrorType")
analytics.logConvertError(error = convertErrorType.name)
analytics.logEvent(ConvertError(error = convertErrorType.name))
_uiState.update { it.copy(convertErrorType = convertErrorType, isConverting = false) }
}
}
Expand All @@ -72,7 +76,7 @@ class ConvertViewModel @Inject constructor(
}

fun clearAllText() {
if (uiState.value.inputText.isNotEmpty() || uiState.value.outputText.isNotEmpty()) analytics.logClearAllText()
if (uiState.value.inputText.isNotEmpty() || uiState.value.outputText.isNotEmpty()) analytics.logEvent(ClearAllText())
_uiState.update {
it.copy(
inputText = "",
Expand All @@ -83,7 +87,7 @@ class ConvertViewModel @Inject constructor(
}

fun changeHiraKanaType(type: HiraKanaType) {
analytics.logChangeHiraKanaType(hiraKanaType = type.name)
analytics.logEvent(ChangeHiraKanaType(hiraKanaType = type.name))
_uiState.update {
it.copy(
selectedTextType = type,
Expand Down
Loading

0 comments on commit 28809a4

Please sign in to comment.