Skip to content

Commit

Permalink
Add pixels for autocomplete
Browse files Browse the repository at this point in the history
  • Loading branch information
CrisBarreiro committed Apr 23, 2024
1 parent e3836ef commit 041a68e
Show file tree
Hide file tree
Showing 4 changed files with 82 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ import com.duckduckgo.app.accessibility.data.AccessibilitySettingsDataStore
import com.duckduckgo.app.accessibility.data.AccessibilitySettingsSharedPreferences
import com.duckduckgo.app.autocomplete.api.AutoComplete.AutoCompleteResult
import com.duckduckgo.app.autocomplete.api.AutoComplete.AutoCompleteSuggestion.AutoCompleteBookmarkSuggestion
import com.duckduckgo.app.autocomplete.api.AutoComplete.AutoCompleteSuggestion.AutoCompleteHistorySearchSuggestion
import com.duckduckgo.app.autocomplete.api.AutoComplete.AutoCompleteSuggestion.AutoCompleteSearchSuggestion
import com.duckduckgo.app.autocomplete.api.AutoCompleteApi
import com.duckduckgo.app.autocomplete.api.AutoCompleteService
Expand Down Expand Up @@ -132,6 +133,7 @@ import com.duckduckgo.app.privacy.model.TestEntity
import com.duckduckgo.app.settings.db.SettingsDataStore
import com.duckduckgo.app.statistics.api.StatisticsUpdater
import com.duckduckgo.app.statistics.pixels.Pixel
import com.duckduckgo.app.statistics.pixels.Pixel.PixelParameter
import com.duckduckgo.app.statistics.pixels.Pixel.PixelType.COUNT
import com.duckduckgo.app.surrogates.SurrogateResponse
import com.duckduckgo.app.survey.api.SurveyRepository
Expand Down Expand Up @@ -2160,7 +2162,43 @@ class BrowserTabViewModelTest {
val suggestion = AutoCompleteBookmarkSuggestion("example", "Example", "https://example.com")
testee.autoCompleteViewState.value = autoCompleteViewState().copy(searchResults = AutoCompleteResult("", listOf(suggestion)))
testee.fireAutocompletePixel(suggestion)
verify(mockPixel).fire(AppPixelName.AUTOCOMPLETE_BOOKMARK_SELECTION, pixelParams(showedBookmarks = true, bookmarkCapable = true))
val argumentCaptor = argumentCaptor<Map<String, String>>()
verify(mockPixel).fire(eq(AppPixelName.AUTOCOMPLETE_BOOKMARK_SELECTION), argumentCaptor.capture(), any(), any())

assertEquals("true", argumentCaptor.firstValue[PixelParameter.SHOWED_BOOKMARKS])
assertEquals("true", argumentCaptor.firstValue[PixelParameter.BOOKMARK_CAPABLE])
}

@Test
fun whenBookmarkFavoriteSubmittedThenAutoCompleteFavoriteSelectionPixelSent() = runTest {
whenever(mockSavedSitesRepository.hasBookmarks()).thenReturn(true)
whenever(mockSavedSitesRepository.hasFavorites()).thenReturn(true)
val suggestion = AutoCompleteBookmarkSuggestion("example", "Example", "https://example.com", isFavorite = true)
testee.autoCompleteViewState.value = autoCompleteViewState().copy(searchResults = AutoCompleteResult("", listOf(suggestion)))
testee.fireAutocompletePixel(suggestion)

val argumentCaptor = argumentCaptor<Map<String, String>>()
verify(mockPixel).fire(eq(AppPixelName.AUTOCOMPLETE_FAVORITE_SELECTION), argumentCaptor.capture(), any(), any())

assertEquals("false", argumentCaptor.firstValue[PixelParameter.SHOWED_BOOKMARKS])
assertEquals("true", argumentCaptor.firstValue[PixelParameter.SHOWED_FAVORITES])
assertEquals("true", argumentCaptor.firstValue[PixelParameter.BOOKMARK_CAPABLE])
assertEquals("true", argumentCaptor.firstValue[PixelParameter.FAVORITE_CAPABLE])
}

@Test
fun whenHistorySubmittedThenAutoCompleteHistorySelectionPixelSent() = runTest {
whenever(mockSavedSitesRepository.hasBookmarks()).thenReturn(true)
val suggestion = AutoCompleteHistorySearchSuggestion("example")
testee.autoCompleteViewState.value = autoCompleteViewState().copy(searchResults = AutoCompleteResult("", listOf(suggestion)))
testee.fireAutocompletePixel(suggestion)

val argumentCaptor = argumentCaptor<Map<String, String>>()
verify(mockPixel).fire(eq(AppPixelName.AUTOCOMPLETE_HISTORY_SELECTION), argumentCaptor.capture(), any(), any())

assertEquals("false", argumentCaptor.firstValue[PixelParameter.SHOWED_BOOKMARKS])
assertEquals("true", argumentCaptor.firstValue[PixelParameter.BOOKMARK_CAPABLE])
assertEquals("true", argumentCaptor.firstValue[PixelParameter.SHOWED_HISTORY])
}

@Test
Expand All @@ -2170,7 +2208,11 @@ class BrowserTabViewModelTest {
testee.autoCompleteViewState.value = autoCompleteViewState().copy(searchResults = AutoCompleteResult("", suggestions))
testee.fireAutocompletePixel(AutoCompleteSearchSuggestion("example", false))

verify(mockPixel).fire(AppPixelName.AUTOCOMPLETE_SEARCH_SELECTION, pixelParams(showedBookmarks = true, bookmarkCapable = true))
val argumentCaptor = argumentCaptor<Map<String, String>>()
verify(mockPixel).fire(eq(AppPixelName.AUTOCOMPLETE_SEARCH_SELECTION), argumentCaptor.capture(), any(), any())

assertEquals("true", argumentCaptor.firstValue[PixelParameter.SHOWED_BOOKMARKS])
assertEquals("true", argumentCaptor.firstValue[PixelParameter.BOOKMARK_CAPABLE])
}

@Test
Expand All @@ -2179,7 +2221,11 @@ class BrowserTabViewModelTest {
testee.autoCompleteViewState.value = autoCompleteViewState().copy(searchResults = AutoCompleteResult("", emptyList()))
testee.fireAutocompletePixel(AutoCompleteSearchSuggestion("example", false))

verify(mockPixel).fire(AppPixelName.AUTOCOMPLETE_SEARCH_SELECTION, pixelParams(showedBookmarks = false, bookmarkCapable = false))
val argumentCaptor = argumentCaptor<Map<String, String>>()
verify(mockPixel).fire(eq(AppPixelName.AUTOCOMPLETE_SEARCH_SELECTION), argumentCaptor.capture(), any(), any())

assertEquals("false", argumentCaptor.firstValue[PixelParameter.SHOWED_BOOKMARKS])
assertEquals("false", argumentCaptor.firstValue[PixelParameter.BOOKMARK_CAPABLE])
}

@Test
Expand Down Expand Up @@ -5330,9 +5376,11 @@ class BrowserTabViewModelTest {
private fun pixelParams(
showedBookmarks: Boolean,
bookmarkCapable: Boolean,
showedHistory: Boolean = false,
) = mapOf(
Pixel.PixelParameter.SHOWED_BOOKMARKS to showedBookmarks.toString(),
Pixel.PixelParameter.BOOKMARK_CAPABLE to bookmarkCapable.toString(),
Pixel.PixelParameter.SHOWED_HISTORY to showedHistory.toString(),
)

private fun givenExpectedCtaAddWidgetInstructions() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,7 @@ import com.duckduckgo.app.location.data.LocationPermissionType
import com.duckduckgo.app.location.data.LocationPermissionsRepository
import com.duckduckgo.app.onboarding.ui.page.experiment.ExtendedOnboardingExperimentVariantManager
import com.duckduckgo.app.pixels.AppPixelName
import com.duckduckgo.app.pixels.AppPixelName.AUTOCOMPLETE_HISTORY_SELECTION
import com.duckduckgo.app.pixels.remoteconfig.AndroidBrowserConfigFeature
import com.duckduckgo.app.privacy.db.NetworkLeaderboardDao
import com.duckduckgo.app.privacy.db.UserAllowListRepository
Expand Down Expand Up @@ -671,20 +672,33 @@ class BrowserTabViewModel @Inject constructor(
val hasBookmarks = withContext(dispatchers.io()) {
savedSitesRepository.hasBookmarks()
}
val hasBookmarkResults = currentViewState.searchResults.suggestions.any { it is AutoCompleteBookmarkSuggestion }
val hasFavorites = withContext(dispatchers.io()) {
savedSitesRepository.hasFavorites()
}
val hasBookmarkResults = currentViewState.searchResults.suggestions.any { it is AutoCompleteBookmarkSuggestion && !it.isFavorite }
val hasFavoriteResults = currentViewState.searchResults.suggestions.any { it is AutoCompleteBookmarkSuggestion && it.isFavorite }
val hasHistoryResults =
currentViewState.searchResults.suggestions.any { it is AutoCompleteHistorySuggestion || it is AutoCompleteHistorySearchSuggestion }
val params = mapOf(
PixelParameter.SHOWED_BOOKMARKS to hasBookmarkResults.toString(),
PixelParameter.SHOWED_FAVORITES to hasFavoriteResults.toString(),
PixelParameter.BOOKMARK_CAPABLE to hasBookmarks.toString(),
PixelParameter.FAVORITE_CAPABLE to hasFavorites.toString(),
PixelParameter.SHOWED_HISTORY to hasHistoryResults.toString(),
)
val pixelName = when (suggestion) {
is AutoCompleteBookmarkSuggestion -> AppPixelName.AUTOCOMPLETE_BOOKMARK_SELECTION
is AutoCompleteBookmarkSuggestion -> {
if (suggestion.isFavorite) {
AppPixelName.AUTOCOMPLETE_FAVORITE_SELECTION
} else {
AppPixelName.AUTOCOMPLETE_BOOKMARK_SELECTION
}
}
is AutoCompleteSearchSuggestion -> AppPixelName.AUTOCOMPLETE_SEARCH_SELECTION
is AutoCompleteHistorySuggestion, is AutoCompleteHistorySearchSuggestion -> null
is AutoCompleteHistorySuggestion, is AutoCompleteHistorySearchSuggestion -> AUTOCOMPLETE_HISTORY_SELECTION
}

pixelName?.let {
pixel.fire(pixelName, params)
}
pixel.fire(pixelName, params)
}

fun onUserLongPressedBack() {
Expand Down
8 changes: 8 additions & 0 deletions app/src/main/java/com/duckduckgo/app/pixels/AppPixelName.kt
Original file line number Diff line number Diff line change
Expand Up @@ -179,7 +179,15 @@ enum class AppPixelName(override val pixelName: String) : Pixel.PixelName {
FEEDBACK_NEGATIVE_SUBMISSION("mfbs_%s_%s_%s"),

AUTOCOMPLETE_BOOKMARK_SELECTION("m_aut_s_b"),
AUTOCOMPLETE_FAVORITE_SELECTION("m_autocomplete_favorite_selection"),
AUTOCOMPLETE_SEARCH_SELECTION("m_aut_s_s"),
AUTOCOMPLETE_HISTORY_SELECTION("m_autocomplete_history_selection"),

AUTOCOMPLETE_HISTORY_TOGGLED_OFF("m_autocomplete_history_toggled_off"),
AUTOCOMPLETE_HISTORY_TOGGLED_ON("m_autocomplete_history_toggled_on"),

AUTOCOMPLETE_BANNER_SHOWN("m_autocomplete_banner_shown"),
AUTOCOMPLETE_BANNER_DISMISSED("m_autocomplete_banner_dismissed"),

SERP_REQUERY("rq_%s"),

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,10 @@ interface Pixel {
const val APP_VERSION = "appVersion"
const val URL = "url"
const val BOOKMARK_CAPABLE = "bc"
const val FAVORITE_CAPABLE = "fc"
const val SHOWED_BOOKMARKS = "sb"
const val SHOWED_FAVORITES = "sf"
const val SHOWED_HISTORY = "sh"
const val DEFAULT_BROWSER_BEHAVIOUR_TRIGGERED = "bt"
const val DEFAULT_BROWSER_SET_FROM_ONBOARDING = "fo"
const val DEFAULT_BROWSER_SET_ORIGIN = "dbo"
Expand Down

0 comments on commit 041a68e

Please sign in to comment.