Skip to content
This repository has been archived by the owner on Feb 20, 2023. It is now read-only.

Commit

Permalink
For #1158 - Add search counts
Browse files Browse the repository at this point in the history
  • Loading branch information
boek committed Apr 16, 2019
1 parent 3b3d4c3 commit d59532d
Show file tree
Hide file tree
Showing 4 changed files with 91 additions and 13 deletions.
32 changes: 27 additions & 5 deletions app/metrics.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -54,14 +54,19 @@ events:
description: >
A user performed a search
extra_keys:
search_suggestion:
description: "A boolean that tells us whether or not the search term was suggested by the Awesomebar"
search_shortcut:
description: "A boolean that tells us whether or not the search was conducted through a search shortcut"
source:
description: >
A string that tells us how the user performed the search. Possible values are:
* default.action
* default.suggestion
* shortcut.action
* shortcut.suggestion
bugs:
- 959
data_reviews:
- https://github.com/mozilla-mobile/fenix/pull/1067#issuecomment-474598673
- https://github.com/mozilla-mobile/fenix/pull/1677
notification_emails:
- fenix-core@mozilla.com
expires: "2020-03-01"
Expand Down Expand Up @@ -307,6 +312,23 @@ metrics:
notification_emails:
- fenix-core@mozilla.com
expires: "2020-03-01"
search_count:
type: labeled_counter
description: >
If the search engine is pre-loaded with Fenix this value
will be he base URL we use to build the search query for the search engine.
For example: https://mysearchengine.com/?query=%s. If it's a custom search engine
(defined: https://github.com/mozilla-mobile/fenix/issues/1607) the
value will be "custom"
send_in_pings:
- metrics
bugs:
- 1158
data_reviews:
- https://github.com/mozilla-mobile/fenix/pull/1677
notification_emails:
- fenix-core@mozilla.com
expires: "2019-09-01"

search.default_engine:
code:
Expand Down Expand Up @@ -357,4 +379,4 @@ search.default_engine:
- https://github.com/mozilla-mobile/fenix/issues/1607
notification_emails:
- fenix-core@mozilla.com
expires: "2019-09-01"
expires: "2019-09-01"
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,10 @@ private val Event.wrapper
{ Events.enteredUrlKeys.valueOf(it) }
)
is Event.PerformedSearch -> EventWrapper(
{ Events.performedSearch.record(it) },
{
Metrics.searchCount[this.eventSource.countLabel].add(1)
Events.performedSearch.record(it)
},
{ Events.performedSearchKeys.valueOf(it) }
)
is Event.FindInPageOpened -> EventWrapper<NoExtraKeys>(
Expand Down
48 changes: 45 additions & 3 deletions app/src/main/java/org/mozilla/fenix/components/metrics/Metrics.kt
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
package org.mozilla.fenix.components.metrics

import mozilla.components.browser.search.SearchEngine
import mozilla.components.support.base.Component
import mozilla.components.support.base.facts.Fact
import mozilla.components.support.base.facts.FactProcessor
Expand Down Expand Up @@ -69,10 +70,49 @@ sealed class Event {
get() = mapOf("autocomplete" to autoCompleted.toString())
}

data class PerformedSearch(val fromSearchSuggestion: Boolean, val fromSearchShortcut: Boolean) : Event() {
data class PerformedSearch(val eventSource: EventSource) : Event() {
sealed class EngineSource {
data class Default(val engine: SearchEngine): EngineSource()
data class Shortcut(val engine: SearchEngine): EngineSource()

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 {
data class Suggestion(val engineSource: EngineSource) : EventSource()
data class Action(val engineSource: EngineSource) : EventSource()

private val source: EngineSource
get() = when (this) {
is Suggestion -> engineSource
is Action -> engineSource
}

private val label: String
get() = when (this) {
is Suggestion -> "suggestion"
is Action -> "action"
}

val countLabel: String
get() = "${source.searchEngine.name}.$label"

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

override val extras: Map<String, String>?
get() = mapOf("search_suggestion" to fromSearchSuggestion.toString(),
"search_shortcut" to fromSearchShortcut.toString())
get() = mapOf("source" to eventSource.sourceLabel)
}

// Track only built-in engine selection. Do not track user-added engines!
Expand Down Expand Up @@ -122,6 +162,8 @@ sealed class Event {
get() = mapOf("item" to item.toString().toLowerCase())
}

sealed class Search

open val extras: Map<String, String>?
get() = null
}
Expand Down
19 changes: 15 additions & 4 deletions app/src/main/java/org/mozilla/fenix/search/SearchFragment.kt
Original file line number Diff line number Diff line change
Expand Up @@ -119,9 +119,15 @@ class SearchFragment : Fragment() {
val event = if (it.url.isUrl()) {
Event.EnteredUrl(false)
} else {
val isSearchShortcut = it.engine !=
if (it.engine == null) { return@subscribe }

val isShortcut = it.engine !=
requireComponents.search.searchEngineManager.defaultSearchEngine
Event.PerformedSearch(false, isSearchShortcut)
val engineSource =
if (isShortcut) Event.PerformedSearch.EngineSource.Shortcut(it.engine)
else Event.PerformedSearch.EngineSource.Default(it.engine)

Event.PerformedSearch(Event.PerformedSearch.EventSource.Action(engineSource))
}

requireComponents.analytics.metrics.track(event)
Expand Down Expand Up @@ -151,11 +157,16 @@ class SearchFragment : Fragment() {
.invoke(it.searchTerms, it.engine)
(activity as HomeActivity).openToBrowser(sessionId, BrowserDirection.FromSearch)

val isSearchShortcut = it.engine !=
if (it.engine == null) { return@subscribe }

val isShortcut = it.engine !=
requireComponents.search.searchEngineManager.defaultSearchEngine
val engineSource =
if (isShortcut) Event.PerformedSearch.EngineSource.Shortcut(it.engine)
else Event.PerformedSearch.EngineSource.Default(it.engine)

requireComponents.analytics.metrics
.track(Event.PerformedSearch(true, isSearchShortcut))
.track(Event.PerformedSearch(Event.PerformedSearch.EventSource.Suggestion(engineSource)))
}
is AwesomeBarAction.SearchShortcutEngineSelected -> {
getManagedEmitter<AwesomeBarChange>()
Expand Down

0 comments on commit d59532d

Please sign in to comment.