Add analytics for search queries

Change-Id: If5290a903afb928fc1c3aaa66a52d2f182aba118
pull/719/head
Don Turner 2 years ago
parent 6a8736d4d0
commit 0e40e26f22

@ -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"
}
}
}

@ -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() {

Loading…
Cancel
Save