Skip to content

Commit

Permalink
For mozilla-mobile#24211: Remove wrapper from search related metrics.
Browse files Browse the repository at this point in the history
Removes wrapper from search labeled counter and performed search event.
  • Loading branch information
mcarare committed Apr 7, 2022
1 parent 1b21790 commit 903c63e
Show file tree
Hide file tree
Showing 17 changed files with 275 additions and 248 deletions.
1 change: 1 addition & 0 deletions app/metrics.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,7 @@ events:
* default.suggestion
* shortcut.action
* shortcut.suggestion
type: string
bugs:
- https://github.com/mozilla-mobile/fenix/issues/959
- https://github.com/mozilla-mobile/fenix/issues/19923
Expand Down
66 changes: 0 additions & 66 deletions app/src/main/java/org/mozilla/fenix/components/metrics/Event.kt
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,11 @@

package org.mozilla.fenix.components.metrics

import mozilla.components.browser.state.search.SearchEngine
import mozilla.components.feature.top.sites.TopSite
import org.mozilla.fenix.GleanMetrics.Addons
import org.mozilla.fenix.GleanMetrics.AppTheme
import org.mozilla.fenix.GleanMetrics.Autoplay
import org.mozilla.fenix.GleanMetrics.ContextMenu
import org.mozilla.fenix.GleanMetrics.Events
import org.mozilla.fenix.GleanMetrics.Logins
import org.mozilla.fenix.GleanMetrics.Pocket
import org.mozilla.fenix.GleanMetrics.SearchTerms
Expand Down Expand Up @@ -206,70 +204,6 @@ sealed class Event {
get() = hashMapOf(Logins.saveLoginsSettingChangedKeys.setting to setting.name)
}

data class PerformedSearch(val eventSource: EventSource) : Event() {
sealed class EngineSource {
abstract val engine: SearchEngine
abstract val isCustom: Boolean

data class Default(override val engine: SearchEngine, override val isCustom: Boolean) :
EngineSource()

data class Shortcut(override val engine: SearchEngine, override val isCustom: Boolean) :
EngineSource()

// https://github.com/mozilla-mobile/fenix/issues/1607
// Sanitize identifiers for custom search engines.
val identifier: String
get() = if (isCustom) "custom" else engine.id

val searchEngine: SearchEngine
get() = when (this) {
is Default -> engine
is Shortcut -> engine
}

val descriptor: String
get() = when (this) {
is Default -> "default"
is Shortcut -> "shortcut"
}
}

sealed class EventSource(open val engineSource: EngineSource) {
data class Suggestion(override val engineSource: EngineSource) :
EventSource(engineSource)

data class Action(override val engineSource: EngineSource) : EventSource(engineSource)
data class Widget(override val engineSource: EngineSource) : EventSource(engineSource)
data class Shortcut(override val engineSource: EngineSource) : EventSource(engineSource)
data class TopSite(override val engineSource: EngineSource) : EventSource(engineSource)
data class Other(override val engineSource: EngineSource) : EventSource(engineSource)

private val label: String
get() = when (this) {
is Suggestion -> "suggestion"
is Action -> "action"
is Widget -> "widget"
is Shortcut -> "shortcut"
is TopSite -> "topsite"
is Other -> "other"
}

val countLabel: String
get() = "${engineSource.identifier.lowercase(Locale.getDefault())}.$label"

val sourceLabel: String
get() = "${engineSource.descriptor}.$label"
}

enum class SearchAccessPoint {
SUGGESTION, ACTION, WIDGET, SHORTCUT, TOPSITE, NONE
}

override val extras: Map<Events.performedSearchKeys, String>?
get() = mapOf(Events.performedSearchKeys.source to eventSource.sourceLabel)
}

data class DarkThemeSelected(val source: Source) : Event() {
enum class Source { SETTINGS }

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,14 +18,12 @@ import org.mozilla.fenix.GleanMetrics.ContextMenu
import org.mozilla.fenix.GleanMetrics.ContextualMenu
import org.mozilla.fenix.GleanMetrics.CreditCards
import org.mozilla.fenix.GleanMetrics.CustomTab
import org.mozilla.fenix.GleanMetrics.Events
import org.mozilla.fenix.GleanMetrics.ExperimentsDefaultBrowser
import org.mozilla.fenix.GleanMetrics.HomeMenu
import org.mozilla.fenix.GleanMetrics.HomeScreen
import org.mozilla.fenix.GleanMetrics.Logins
import org.mozilla.fenix.GleanMetrics.MediaNotification
import org.mozilla.fenix.GleanMetrics.MediaState
import org.mozilla.fenix.GleanMetrics.Metrics
import org.mozilla.fenix.GleanMetrics.Pings
import org.mozilla.fenix.GleanMetrics.Pocket
import org.mozilla.fenix.GleanMetrics.ProgressiveWebApp
Expand Down Expand Up @@ -88,13 +86,6 @@ private class EventWrapper<T : Enum<T>>(
// FIXME(#19967): Migrate to non-deprecated API.
private val Event.wrapper: EventWrapper<*>?
get() = when (this) {
is Event.PerformedSearch -> EventWrapper(
{
Metrics.searchCount[this.eventSource.countLabel].add(1)
Events.performedSearch.record(it)
},
{ Events.performedSearchKeys.valueOf(it) }
)
is Event.SearchWithAds -> EventWrapper<NoExtraKeys>(
{
BrowserSearch.withAds[label].add(1)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,64 +13,44 @@ import com.google.android.gms.common.GooglePlayServicesRepairableException
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.withContext
import mozilla.components.browser.state.search.SearchEngine
import mozilla.components.browser.state.state.selectedOrDefaultSearchEngine
import mozilla.components.browser.state.store.BrowserStore
import mozilla.components.support.base.log.logger.Logger
import org.mozilla.fenix.components.metrics.Event.PerformedSearch.SearchAccessPoint
import org.mozilla.fenix.GleanMetrics.Events
import org.mozilla.fenix.GleanMetrics.Metrics
import java.io.IOException
import java.security.NoSuchAlgorithmException
import java.security.spec.InvalidKeySpecException
import javax.crypto.SecretKeyFactory
import javax.crypto.spec.PBEKeySpec

object MetricsUtils {
fun createSearchEvent(

/**
* Possible sources for a performed search.
*/
enum class Source {
ACTION, SHORTCUT, SUGGESTION, TOPSITE, WIDGET, NONE
}

/**
* Records the appropriate metric for performed searches.
*/
fun recordSearchMetrics(
engine: SearchEngine,
store: BrowserStore,
searchAccessPoint: SearchAccessPoint
): Event.PerformedSearch? {
val isShortcut = engine != store.state.search.selectedOrDefaultSearchEngine
val isCustom = engine.type == SearchEngine.Type.CUSTOM
isDefault: Boolean,
searchAccessPoint: Source
) {
val identifier = if (engine.type == SearchEngine.Type.CUSTOM) "custom" else engine.id
val source = searchAccessPoint.name.lowercase()

val engineSource =
if (isShortcut) {
Event.PerformedSearch.EngineSource.Shortcut(engine, isCustom)
} else {
Event.PerformedSearch.EngineSource.Default(engine, isCustom)
}
Metrics.searchCount["$identifier.$source"].add()

return when (searchAccessPoint) {
SearchAccessPoint.SUGGESTION -> Event.PerformedSearch(
Event.PerformedSearch.EventSource.Suggestion(
engineSource
)
)
SearchAccessPoint.ACTION -> Event.PerformedSearch(
Event.PerformedSearch.EventSource.Action(
engineSource
)
)
SearchAccessPoint.WIDGET -> Event.PerformedSearch(
Event.PerformedSearch.EventSource.Widget(
engineSource
)
)
SearchAccessPoint.SHORTCUT -> Event.PerformedSearch(
Event.PerformedSearch.EventSource.Shortcut(
engineSource
)
)
SearchAccessPoint.TOPSITE -> Event.PerformedSearch(
Event.PerformedSearch.EventSource.TopSite(
engineSource
)
)
SearchAccessPoint.NONE -> Event.PerformedSearch(
Event.PerformedSearch.EventSource.Other(
engineSource
)
)
val performedSearchExtra = if (isDefault) {
"default.$source"
} else {
"shortcut.$source"
}

Events.performedSearch.record(Events.PerformedSearchExtra(performedSearchExtra))
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,11 @@ import android.content.Intent
import android.os.StrictMode
import androidx.navigation.NavController
import mozilla.components.browser.state.search.SearchEngine
import mozilla.components.browser.state.state.selectedOrDefaultSearchEngine
import mozilla.components.browser.state.store.BrowserStore
import mozilla.components.feature.search.ext.waitForSelectedOrDefaultSearchEngine
import org.mozilla.fenix.BrowserDirection
import org.mozilla.fenix.HomeActivity
import org.mozilla.fenix.components.metrics.Event
import org.mozilla.fenix.components.metrics.MetricController
import org.mozilla.fenix.components.metrics.MetricsUtils
import org.mozilla.fenix.ext.components
Expand Down Expand Up @@ -52,12 +52,11 @@ class SpeechProcessingIntentProcessor(

private fun launchToBrowser(searchEngine: SearchEngine, text: String) {
activity.components.strictMode.resetAfter(StrictMode.allowThreadDiskReads()) {
val searchEvent = MetricsUtils.createSearchEvent(
MetricsUtils.recordSearchMetrics(
searchEngine,
store,
Event.PerformedSearch.SearchAccessPoint.WIDGET
searchEngine == store.state.search.selectedOrDefaultSearchEngine,
MetricsUtils.Source.WIDGET
)
searchEvent?.let { metrics.track(it) }
}

activity.openToBrowserAndLoad(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@ import org.mozilla.fenix.GleanMetrics.SearchWidget
import org.mozilla.fenix.HomeActivity
import org.mozilla.fenix.NavGraphDirections
import org.mozilla.fenix.R
import org.mozilla.fenix.components.metrics.Event
import org.mozilla.fenix.components.metrics.MetricController
import org.mozilla.fenix.components.metrics.MetricsUtils
import org.mozilla.fenix.ext.nav

/**
Expand All @@ -30,12 +30,12 @@ class StartSearchIntentProcessor(
val source = when (event) {
SEARCH_WIDGET -> {
SearchWidget.newTabButton.record(NoExtras())
Event.PerformedSearch.SearchAccessPoint.WIDGET
MetricsUtils.Source.WIDGET
}
STATIC_SHORTCUT_NEW_TAB,
STATIC_SHORTCUT_NEW_PRIVATE_TAB,
PRIVATE_BROWSING_PINNED_SHORTCUT -> {
Event.PerformedSearch.SearchAccessPoint.SHORTCUT
MetricsUtils.Source.SHORTCUT
}
else -> null
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import androidx.navigation.NavController
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.launch
import mozilla.components.browser.state.search.SearchEngine
import mozilla.components.browser.state.selector.getNormalOrPrivateTabs
import mozilla.components.browser.state.state.availableSearchEngines
import mozilla.components.browser.state.state.searchEngines
Expand Down Expand Up @@ -47,7 +48,6 @@ import org.mozilla.fenix.components.metrics.Event
import org.mozilla.fenix.components.metrics.MetricController
import org.mozilla.fenix.components.metrics.MetricsUtils
import org.mozilla.fenix.ext.components
import org.mozilla.fenix.ext.metrics
import org.mozilla.fenix.ext.nav
import org.mozilla.fenix.ext.settings
import org.mozilla.fenix.gleanplumb.Message
Expand Down Expand Up @@ -420,17 +420,18 @@ class DefaultSessionControlController(
SupportUtils.POCKET_TRENDING_URL -> metrics.track(Event.PocketTopSiteClicked)
}

val availableEngines = getAvailableSearchEngines()
val searchAccessPoint = Event.PerformedSearch.SearchAccessPoint.TOPSITE
val event =
availableEngines.firstOrNull { engine ->
engine.resultUrls.firstOrNull { it.contains(topSite.url) } != null
}?.let { searchEngine ->
searchAccessPoint.let { sap ->
MetricsUtils.createSearchEvent(searchEngine, store, sap)
}
}
event?.let { activity.metrics.track(it) }
val availableEngines: List<SearchEngine> = getAvailableSearchEngines()
val searchAccessPoint = MetricsUtils.Source.TOPSITE

availableEngines.firstOrNull { engine ->
engine.resultUrls.firstOrNull { it.contains(topSite.url) } != null
}?.let { searchEngine ->
MetricsUtils.recordSearchMetrics(
searchEngine,
searchEngine == store.state.search.selectedOrDefaultSearchEngine,
searchAccessPoint
)
}

val tabId = addTabUseCase.invoke(
url = appendSearchAttributionToUrlIfNeeded(topSite.url),
Expand Down Expand Up @@ -596,15 +597,12 @@ class DefaultSessionControlController(
if (clipboardText.isUrl() || searchEngine == null) {
Events.enteredUrl.record(Events.EnteredUrlExtra(autocomplete = false))
} else {
val searchAccessPoint = Event.PerformedSearch.SearchAccessPoint.ACTION
val event = searchAccessPoint.let { sap ->
MetricsUtils.createSearchEvent(
searchEngine,
store,
sap
)
}
event?.let { activity.metrics.track(it) }
val searchAccessPoint = MetricsUtils.Source.ACTION
MetricsUtils.recordSearchMetrics(
searchEngine,
searchEngine == store.state.search.selectedOrDefaultSearchEngine,
searchAccessPoint
)
}
}

Expand Down
Loading

0 comments on commit 903c63e

Please sign in to comment.