Merge pull request #719 from android/add-search-analytics

Add analytics for search queries
pull/723/head
Don Turner 2 years ago committed by GitHub
commit a4eab289df
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

@ -34,6 +34,7 @@ data class AnalyticsEvent(
class Types { class Types {
companion object { companion object {
const val SCREEN_VIEW = "screen_view" // (extras: SCREEN_NAME) 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 { class ParamKeys {
companion object { companion object {
const val SCREEN_NAME = "screen_name" const val SCREEN_NAME = "screen_name"
const val SEARCH_TERM = "search_term"
} }
} }
} }

@ -19,6 +19,9 @@ package com.google.samples.apps.nowinandroid.feature.search
import androidx.lifecycle.SavedStateHandle import androidx.lifecycle.SavedStateHandle
import androidx.lifecycle.ViewModel import androidx.lifecycle.ViewModel
import androidx.lifecycle.viewModelScope 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.data.repository.RecentSearchRepository
import com.google.samples.apps.nowinandroid.core.domain.GetRecentSearchQueriesUseCase 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.GetSearchContentsCountUseCase
@ -42,6 +45,7 @@ class SearchViewModel @Inject constructor(
recentSearchQueriesUseCase: GetRecentSearchQueriesUseCase, recentSearchQueriesUseCase: GetRecentSearchQueriesUseCase,
private val recentSearchRepository: RecentSearchRepository, private val recentSearchRepository: RecentSearchRepository,
private val savedStateHandle: SavedStateHandle, private val savedStateHandle: SavedStateHandle,
private val analyticsHelper: AnalyticsHelper,
) : ViewModel() { ) : ViewModel() {
val searchQuery = savedStateHandle.getStateFlow(SEARCH_QUERY, "") val searchQuery = savedStateHandle.getStateFlow(SEARCH_QUERY, "")
@ -105,6 +109,14 @@ class SearchViewModel @Inject constructor(
viewModelScope.launch { viewModelScope.launch {
recentSearchRepository.insertOrReplaceRecentSearch(query) recentSearchRepository.insertOrReplaceRecentSearch(query)
} }
analyticsHelper.logEvent(
AnalyticsEvent(
type = SEARCH_QUERY,
extras = listOf(
Param(SEARCH_QUERY, query),
),
),
)
} }
fun clearRecentSearches() { fun clearRecentSearches() {

@ -17,6 +17,7 @@
package com.google.samples.apps.nowinandroid.feature.search package com.google.samples.apps.nowinandroid.feature.search
import androidx.lifecycle.SavedStateHandle import androidx.lifecycle.SavedStateHandle
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.GetRecentSearchQueriesUseCase
import com.google.samples.apps.nowinandroid.core.domain.GetSearchContentsCountUseCase import com.google.samples.apps.nowinandroid.core.domain.GetSearchContentsCountUseCase
import com.google.samples.apps.nowinandroid.core.domain.GetSearchContentsUseCase import com.google.samples.apps.nowinandroid.core.domain.GetSearchContentsUseCase
@ -68,6 +69,7 @@ class SearchViewModelTest {
recentSearchQueriesUseCase = getRecentQueryUseCase, recentSearchQueriesUseCase = getRecentQueryUseCase,
savedStateHandle = SavedStateHandle(), savedStateHandle = SavedStateHandle(),
recentSearchRepository = recentSearchRepository, recentSearchRepository = recentSearchRepository,
analyticsHelper = NoOpAnalyticsHelper(),
) )
} }

Loading…
Cancel
Save