From 0e40e26f22e261aa048cb8798c758cb17a5bfbcc Mon Sep 17 00:00:00 2001 From: Don Turner Date: Tue, 9 May 2023 19:04:46 +0100 Subject: [PATCH 1/3] Add analytics for search queries Change-Id: If5290a903afb928fc1c3aaa66a52d2f182aba118 --- .../nowinandroid/core/analytics/AnalyticsEvent.kt | 2 ++ .../nowinandroid/feature/search/SearchViewModel.kt | 11 +++++++++++ 2 files changed, 13 insertions(+) diff --git a/core/analytics/src/main/java/com/google/samples/apps/nowinandroid/core/analytics/AnalyticsEvent.kt b/core/analytics/src/main/java/com/google/samples/apps/nowinandroid/core/analytics/AnalyticsEvent.kt index 97ae76b56..3e0650eed 100644 --- a/core/analytics/src/main/java/com/google/samples/apps/nowinandroid/core/analytics/AnalyticsEvent.kt +++ b/core/analytics/src/main/java/com/google/samples/apps/nowinandroid/core/analytics/AnalyticsEvent.kt @@ -34,6 +34,7 @@ data class AnalyticsEvent( class Types { companion object { const val SCREEN_VIEW = "screen_view" // (extras: SCREEN_NAME) + const val VIEW_SEARCH_RESULTS = "view_search_results" // (extras: SEARCH_TERM) } } @@ -53,6 +54,7 @@ data class AnalyticsEvent( class ParamKeys { companion object { const val SCREEN_NAME = "screen_name" + const val SEARCH_TERM = "search_term" } } } diff --git a/feature/search/src/main/java/com/google/samples/apps/nowinandroid/feature/search/SearchViewModel.kt b/feature/search/src/main/java/com/google/samples/apps/nowinandroid/feature/search/SearchViewModel.kt index f4b4485bc..b1b0614dc 100644 --- a/feature/search/src/main/java/com/google/samples/apps/nowinandroid/feature/search/SearchViewModel.kt +++ b/feature/search/src/main/java/com/google/samples/apps/nowinandroid/feature/search/SearchViewModel.kt @@ -19,6 +19,9 @@ package com.google.samples.apps.nowinandroid.feature.search import androidx.lifecycle.SavedStateHandle import androidx.lifecycle.ViewModel import androidx.lifecycle.viewModelScope +import com.google.samples.apps.nowinandroid.core.analytics.AnalyticsEvent +import com.google.samples.apps.nowinandroid.core.analytics.AnalyticsEvent.Param +import com.google.samples.apps.nowinandroid.core.analytics.AnalyticsHelper import com.google.samples.apps.nowinandroid.core.data.repository.RecentSearchRepository import com.google.samples.apps.nowinandroid.core.domain.GetRecentSearchQueriesUseCase import com.google.samples.apps.nowinandroid.core.domain.GetSearchContentsCountUseCase @@ -42,6 +45,7 @@ class SearchViewModel @Inject constructor( recentSearchQueriesUseCase: GetRecentSearchQueriesUseCase, private val recentSearchRepository: RecentSearchRepository, private val savedStateHandle: SavedStateHandle, + private val analyticsHelper: AnalyticsHelper, ) : ViewModel() { val searchQuery = savedStateHandle.getStateFlow(SEARCH_QUERY, "") @@ -105,6 +109,13 @@ class SearchViewModel @Inject constructor( viewModelScope.launch { recentSearchRepository.insertOrReplaceRecentSearch(query) } + analyticsHelper.logEvent( + AnalyticsEvent( + type = SEARCH_QUERY, + extras = listOf(Param(SEARCH_QUERY, query) + ) + ) + ) } fun clearRecentSearches() { From 6343b831b92a28f5ec66d1cfc72ce3e9439c21da Mon Sep 17 00:00:00 2001 From: Don Turner Date: Wed, 10 May 2023 08:54:40 +0100 Subject: [PATCH 2/3] Use NoOpAnalyticsHelper for tests Change-Id: I44657037f8f7ec6e38bcc3901e235ceab8c71237 --- .../apps/nowinandroid/feature/search/SearchViewModelTest.kt | 3 +++ 1 file changed, 3 insertions(+) diff --git a/feature/search/src/test/java/com/google/samples/apps/nowinandroid/feature/search/SearchViewModelTest.kt b/feature/search/src/test/java/com/google/samples/apps/nowinandroid/feature/search/SearchViewModelTest.kt index 1d50b75fd..01763ba09 100644 --- a/feature/search/src/test/java/com/google/samples/apps/nowinandroid/feature/search/SearchViewModelTest.kt +++ b/feature/search/src/test/java/com/google/samples/apps/nowinandroid/feature/search/SearchViewModelTest.kt @@ -17,6 +17,8 @@ package com.google.samples.apps.nowinandroid.feature.search import androidx.lifecycle.SavedStateHandle +import com.google.samples.apps.nowinandroid.core.analytics.AnalyticsHelper +import com.google.samples.apps.nowinandroid.core.analytics.NoOpAnalyticsHelper import com.google.samples.apps.nowinandroid.core.domain.GetRecentSearchQueriesUseCase import com.google.samples.apps.nowinandroid.core.domain.GetSearchContentsCountUseCase import com.google.samples.apps.nowinandroid.core.domain.GetSearchContentsUseCase @@ -68,6 +70,7 @@ class SearchViewModelTest { recentSearchQueriesUseCase = getRecentQueryUseCase, savedStateHandle = SavedStateHandle(), recentSearchRepository = recentSearchRepository, + analyticsHelper = NoOpAnalyticsHelper(), ) } From aae307ee086a529fe24b697444942d292214dc11 Mon Sep 17 00:00:00 2001 From: Don Turner Date: Wed, 10 May 2023 08:56:21 +0100 Subject: [PATCH 3/3] Fix spotless Change-Id: I84da36ba957cb74f9fb4479e81d714c2d8928a01 --- .../apps/nowinandroid/feature/search/SearchViewModel.kt | 7 ++++--- .../nowinandroid/feature/search/SearchViewModelTest.kt | 1 - 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/feature/search/src/main/java/com/google/samples/apps/nowinandroid/feature/search/SearchViewModel.kt b/feature/search/src/main/java/com/google/samples/apps/nowinandroid/feature/search/SearchViewModel.kt index b1b0614dc..f5b409edf 100644 --- a/feature/search/src/main/java/com/google/samples/apps/nowinandroid/feature/search/SearchViewModel.kt +++ b/feature/search/src/main/java/com/google/samples/apps/nowinandroid/feature/search/SearchViewModel.kt @@ -112,9 +112,10 @@ class SearchViewModel @Inject constructor( analyticsHelper.logEvent( AnalyticsEvent( type = SEARCH_QUERY, - extras = listOf(Param(SEARCH_QUERY, query) - ) - ) + extras = listOf( + Param(SEARCH_QUERY, query), + ), + ), ) } diff --git a/feature/search/src/test/java/com/google/samples/apps/nowinandroid/feature/search/SearchViewModelTest.kt b/feature/search/src/test/java/com/google/samples/apps/nowinandroid/feature/search/SearchViewModelTest.kt index 01763ba09..fc9c20549 100644 --- a/feature/search/src/test/java/com/google/samples/apps/nowinandroid/feature/search/SearchViewModelTest.kt +++ b/feature/search/src/test/java/com/google/samples/apps/nowinandroid/feature/search/SearchViewModelTest.kt @@ -17,7 +17,6 @@ package com.google.samples.apps.nowinandroid.feature.search import androidx.lifecycle.SavedStateHandle -import com.google.samples.apps.nowinandroid.core.analytics.AnalyticsHelper import com.google.samples.apps.nowinandroid.core.analytics.NoOpAnalyticsHelper import com.google.samples.apps.nowinandroid.core.domain.GetRecentSearchQueriesUseCase import com.google.samples.apps.nowinandroid.core.domain.GetSearchContentsCountUseCase