Skip to content

Commit

Permalink
For mozilla-mobile#18851 [Telemetry] Send an event when users click o…
Browse files Browse the repository at this point in the history
…n the "set as default browser" entry in the toolbar menu
  • Loading branch information
Amejia481 committed Apr 13, 2021
1 parent e8d405b commit e40ac27
Show file tree
Hide file tree
Showing 8 changed files with 134 additions and 3 deletions.
36 changes: 35 additions & 1 deletion app/metrics.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -209,7 +209,8 @@ events:
Settings, Help, Desktop Site toggle on/off, Find in Page,
New Tab, Private Tab, Share, Report Site Issue, Back/Forward button,
Reload Button, Quit, Reader Mode On, Reader Mode Off, Open In app,
Add To Top Sites, Add-ons Manager, Bookmarks, History
Add To Top Sites, Add-ons Manager, Bookmarks, History,
Set Default Browser
bugs:
- https://github.com/mozilla-mobile/fenix/issues/1024
data_reviews:
Expand All @@ -223,6 +224,25 @@ events:
notification_emails:
- fenix-core@mozilla.com
expires: "2021-07-01"
set_default_browser_experiment:
type: event
description: |
Indicates which branch let to user to set Firefox as a default browser
extra_keys:
item:
description: |
A string containing the experiment that let users to set their default
browser to Firefox. These items include ToolbarMenu, SettingMenu
and NewTabBanner
bugs:
- https://github.com/mozilla-mobile/fenix/issues/18851
data_reviews:
- https://github.com/mozilla-mobile/fenix/pull/18895
data_sensitivity:
- interaction
notification_emails:
- fenix-core@mozilla.com
expires: "2021-10-01"
total_uri_count:
type: counter
description: |
Expand Down Expand Up @@ -561,6 +581,20 @@ search_shortcuts:
- fenix-core@mozilla.com
expires: "2021-08-01"

experiments_default_browser:
toolbar_menu_clicked:
type: event
description: |
Set default browser was clicked from toolbar menu
bugs:
- https://github.com/mozilla-mobile/fenix/issues/18851
data_reviews:
- https://github.com/mozilla-mobile/fenix/pull/18895
data_sensitivity:
- interaction
notification_emails:
- fenix-core@mozilla.com
expires: "2021-10-01"
toolbar_settings:
changed_position:
type: event
Expand Down
24 changes: 24 additions & 0 deletions app/src/main/java/org/mozilla/fenix/HomeActivity.kt
Original file line number Diff line number Diff line change
Expand Up @@ -76,13 +76,15 @@ import org.mozilla.fenix.browser.browsingmode.DefaultBrowsingModeManager
import org.mozilla.fenix.components.metrics.BreadcrumbsRecorder
import org.mozilla.fenix.components.metrics.Event
import org.mozilla.fenix.exceptions.trackingprotection.TrackingProtectionExceptionsFragmentDirections
import org.mozilla.fenix.experiments.Experiments
import org.mozilla.fenix.ext.alreadyOnDestination
import org.mozilla.fenix.ext.breadcrumb
import org.mozilla.fenix.ext.components
import org.mozilla.fenix.ext.measureNoInline
import org.mozilla.fenix.ext.metrics
import org.mozilla.fenix.ext.nav
import org.mozilla.fenix.ext.settings
import org.mozilla.fenix.ext.withExperiment
import org.mozilla.fenix.home.HomeFragmentDirections
import org.mozilla.fenix.home.intent.CrashReporterIntentProcessor
import org.mozilla.fenix.home.intent.DeepLinkIntentProcessor
Expand Down Expand Up @@ -333,6 +335,28 @@ open class HomeActivity : LocaleAwareAppCompatActivity(), NavHostActivity {

settings().wasDefaultBrowserOnLastResume = settings().isDefaultBrowser()
}

reportDefaultBrowserExperimentDataIfNeeded()
}

/*
* Report when need if any of the default browser experiments resulted in users changing their
* default browser to Firefox.
* */
private fun reportDefaultBrowserExperimentDataIfNeeded() {
val analytics = components.analytics
analytics.experiments.withExperiment(Experiments.DEFAULT_BROWSER) {

val experimentTypes = Event.ExperimentDefaultBrowser.Type.values()

experimentTypes.forEach forEach@{ type ->
if (settings().wasFirefoxSetAsDefaultByExperiment(type)) {
analytics.metrics.track(Event.ExperimentDefaultBrowser(type))
return@forEach
}
}
settings().clearDefaultBrowserExperimentValues()
}
}

override fun onStart() = PerfStartup.homeActivityOnStart.measureNoInline {
Expand Down
11 changes: 11 additions & 0 deletions app/src/main/java/org/mozilla/fenix/components/metrics/Event.kt
Original file line number Diff line number Diff line change
Expand Up @@ -224,6 +224,8 @@ sealed class Event {
object SearchSuggestionClicked : Event()
object OpenedTabSuggestionClicked : Event()

object SetDefaultBrowserToolbarMenuClicked : Event()

// Interaction events with extras

data class TopSiteSwipeCarousel(val page: Int) : Event() {
Expand Down Expand Up @@ -589,6 +591,15 @@ sealed class Event {
get() = mapOf(Events.browserMenuActionKeys.item to item.toString().toLowerCase(Locale.ROOT))
}

data class ExperimentDefaultBrowser(val type: Type) : Event() {
enum class Type {
TOOLBAR_MENU, SETTINGS_MENU, NEW_TAB_BANNER
}

override val extras: Map<Events.setDefaultBrowserExperimentKeys, String>?
get() = mapOf(Events.setDefaultBrowserExperimentKeys.item to type.toString().toLowerCase(Locale.ROOT))
}

data class TabCounterMenuItemTapped(val item: Item) : Event() {
enum class Item {
NEW_TAB, NEW_PRIVATE_TAB, CLOSE_TAB
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,10 +28,11 @@ import org.mozilla.fenix.GleanMetrics.ContextualMenu
import org.mozilla.fenix.GleanMetrics.CrashReporter
import org.mozilla.fenix.GleanMetrics.CustomTab
import org.mozilla.fenix.GleanMetrics.DownloadNotification
import org.mozilla.fenix.GleanMetrics.DownloadsMisc
import org.mozilla.fenix.GleanMetrics.DownloadsManagement
import org.mozilla.fenix.GleanMetrics.DownloadsMisc
import org.mozilla.fenix.GleanMetrics.ErrorPage
import org.mozilla.fenix.GleanMetrics.Events
import org.mozilla.fenix.GleanMetrics.ExperimentsDefaultBrowser
import org.mozilla.fenix.GleanMetrics.FindInPage
import org.mozilla.fenix.GleanMetrics.History
import org.mozilla.fenix.GleanMetrics.LoginDialog
Expand Down Expand Up @@ -192,6 +193,13 @@ private val Event.wrapper: EventWrapper<*>?
{ Events.browserMenuAction.record(it) },
{ Events.browserMenuActionKeys.valueOf(it) }
)
is Event.ExperimentDefaultBrowser -> EventWrapper(
{ Events.setDefaultBrowserExperiment.record(it) },
{ Events.setDefaultBrowserExperimentKeys.valueOf(it) }
)
is Event.SetDefaultBrowserToolbarMenuClicked -> EventWrapper<NoExtraKeys>(
{ ExperimentsDefaultBrowser.toolbarMenuClicked.record(it) }
)
is Event.OpenedBookmark -> EventWrapper<NoExtraKeys>(
{ BookmarksManagement.open.record(it) }
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -346,6 +346,8 @@ class DefaultBrowserToolbarMenuController(
)
}
is ToolbarMenu.Item.SetDefaultBrowser -> {
metrics.track(Event.SetDefaultBrowserToolbarMenuClicked)
settings.recordIntentOfSettingDefaultBrowserExperiment(Event.ExperimentDefaultBrowser.Type.TOOLBAR_MENU)
activity.openSetDefaultBrowserOption()
}
}
Expand Down
46 changes: 46 additions & 0 deletions app/src/main/java/org/mozilla/fenix/utils/Settings.kt
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ import org.mozilla.fenix.Config
import org.mozilla.fenix.FeatureFlags
import org.mozilla.fenix.R
import org.mozilla.fenix.browser.browsingmode.BrowsingMode
import org.mozilla.fenix.components.metrics.Event
import org.mozilla.fenix.components.metrics.MozillaProductDetector
import org.mozilla.fenix.components.settings.counterPreference
import org.mozilla.fenix.components.settings.featureFlagPreference
Expand All @@ -42,6 +43,7 @@ import org.mozilla.fenix.settings.logins.SortingStrategy
import org.mozilla.fenix.settings.registerOnSharedPreferenceChangeListener
import org.mozilla.fenix.settings.sitepermissions.AUTOPLAY_BLOCK_ALL
import java.security.InvalidParameterException
import java.util.Date

private const val AUTOPLAY_USER_SETTING = "AUTOPLAY_USER_SETTING"

Expand Down Expand Up @@ -111,6 +113,50 @@ class Settings(private val appContext: Context) : PreferencesHolder {
default = 0
)

private var experimentDefaultBrowserLastTappedExperiment by stringPreference(
appContext.getPreferenceKey(R.string.pref_key_experiment_default_last_tapped_experiment),
default = ""
)

private var experimentDefaultBrowserLastTappedTimeInMillis by longPreference(
appContext.getPreferenceKey(R.string.pref_key_experiment_default_last_tapped_time_in_millis),
default = -1
)

/*
* As there is not a consistent way to across OS versions to determine that the default browser
* was updated as result of tapping of one of the experiment, we are using some heuristics,
* to determine it.
* 1) The user has previously tapped one of the experiments.
* 2) The actual default browser it's Firefox.
* 4) The default browser was updated in an interval of 5 minutes of the last tap on
* an experiment.
* */
@Suppress("MagicNumber")
fun wasFirefoxSetAsDefaultByExperiment(experiment: Event.ExperimentDefaultBrowser.Type): Boolean {
val minuteInMS = 60 * 1000
val maxAllowedTime = 5 * 1000 // Max time between the last intent of setting a default browser
val browsers = BrowsersCache.all(appContext)
val wasSameExperiment = experimentDefaultBrowserLastTappedExperiment == experiment.name
val wasRecentlyUpdated =
experimentDefaultBrowserLastTappedTimeInMillis < Date().time - maxAllowedTime * minuteInMS

// We want to determine if the previous intent of setting a default browser was successful
// and was produced by this experiment.

return browsers.isDefaultBrowser && wasSameExperiment && wasRecentlyUpdated
}

fun recordIntentOfSettingDefaultBrowserExperiment(experiment: Event.ExperimentDefaultBrowser.Type) {
experimentDefaultBrowserLastTappedExperiment = experiment.name
experimentDefaultBrowserLastTappedTimeInMillis = Date().time
}

fun clearDefaultBrowserExperimentValues() {
experimentDefaultBrowserLastTappedExperiment = ""
experimentDefaultBrowserLastTappedTimeInMillis = -1
}

var lastReviewPromptTimeInMillis by longPreference(
appContext.getPreferenceKey(R.string.pref_key_last_review_prompt_shown_time),
default = 0L
Expand Down
4 changes: 4 additions & 0 deletions app/src/main/res/values/preference_keys.xml
Original file line number Diff line number Diff line change
Expand Up @@ -236,6 +236,10 @@
<string name="pref_key_enabled_addons_count" translatable="false">pref_key_enabled_addons_count</string>
<string name="pref_key_enabled_addons_list" translatable="false">pref_key_enabled_addons_list</string>

<!-- Experiment default browser -->
<string name="pref_key_experiment_default_last_tapped_experiment" translatable="false">pref_key_experiment_default_last_tapped_experiment</string>
<string name="pref_key_experiment_default_last_tapped_time_in_millis" translatable="false">pref_key_experiment_default_last_tapped_time_in_millis</string>

<string name="pref_key_search_count" translatable="false">pref_key_search_count</string>

<string name="pref_key_mobile_bookmarks_size" translatable="false">pref_key_mobile_bookmarks_size</string>
Expand Down
Loading

0 comments on commit e40ac27

Please sign in to comment.