Skip to content

Commit

Permalink
For mozilla-mobile#24210: Remove wrapper from preference toggled events.
Browse files Browse the repository at this point in the history
  • Loading branch information
mcarare committed Apr 6, 2022
1 parent 08cae3b commit aca93c7
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 46 deletions.
2 changes: 2 additions & 0 deletions app/metrics.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -258,8 +258,10 @@ events:
pref_key_sync_logins, pref_key_sync_bookmarks, pref_key_sync_history,
pref_key_show_voice_search,
and pref_key_show_search_suggestions_in_private.
type: string
enabled:
description: "Whether or not the preference is *now* enabled"
type: boolean
bugs:
- https://github.com/mozilla-mobile/fenix/issues/975
- https://github.com/mozilla-mobile/fenix/issues/5094
Expand Down
36 changes: 0 additions & 36 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,7 +4,6 @@

package org.mozilla.fenix.components.metrics

import android.content.Context
import mozilla.components.browser.state.search.SearchEngine
import mozilla.components.feature.top.sites.TopSite
import org.mozilla.fenix.GleanMetrics.Addons
Expand All @@ -17,7 +16,6 @@ import org.mozilla.fenix.GleanMetrics.Logins
import org.mozilla.fenix.GleanMetrics.Pocket
import org.mozilla.fenix.GleanMetrics.SearchTerms
import org.mozilla.fenix.GleanMetrics.TopSites
import org.mozilla.fenix.R
import org.mozilla.fenix.ext.name
import java.util.Locale

Expand Down Expand Up @@ -213,40 +211,6 @@ sealed class Event {
enum class Source { NEWTAB }
}

data class PreferenceToggled(
val preferenceKey: String,
val enabled: Boolean,
val context: Context
) : Event() {
private val booleanPreferenceTelemetryAllowList = listOf(
context.getString(R.string.pref_key_show_search_suggestions),
context.getString(R.string.pref_key_remote_debugging),
context.getString(R.string.pref_key_telemetry),
context.getString(R.string.pref_key_tracking_protection),
context.getString(R.string.pref_key_search_bookmarks),
context.getString(R.string.pref_key_search_browsing_history),
context.getString(R.string.pref_key_show_clipboard_suggestions),
context.getString(R.string.pref_key_show_search_engine_shortcuts),
context.getString(R.string.pref_key_open_links_in_a_private_tab),
context.getString(R.string.pref_key_sync_logins),
context.getString(R.string.pref_key_sync_bookmarks),
context.getString(R.string.pref_key_sync_history),
context.getString(R.string.pref_key_show_voice_search),
context.getString(R.string.pref_key_show_search_suggestions_in_private)
)

override val extras: Map<Events.preferenceToggledKeys, String>?
get() = mapOf(
Events.preferenceToggledKeys.preferenceKey to preferenceKey,
Events.preferenceToggledKeys.enabled to enabled.toString()
)

init {
// If the event is not in the allow list, we don't want to track it
require(booleanPreferenceTelemetryAllowList.contains(preferenceKey))
}
}

data class AddonsOpenInToolbarMenu(val addonId: String) : Event() {
override val extras: Map<Addons.openAddonInToolbarMenuKeys, String>?
get() = hashMapOf(Addons.openAddonInToolbarMenuKeys.addonId to addonId)
Expand Down
31 changes: 21 additions & 10 deletions app/src/main/java/org/mozilla/fenix/settings/SettingsFragment.kt
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ import mozilla.components.support.ktx.android.view.showKeyboard
import mozilla.telemetry.glean.private.NoExtras
import org.mozilla.fenix.BrowserDirection
import org.mozilla.fenix.Config
import org.mozilla.fenix.GleanMetrics.Events
import org.mozilla.fenix.GleanMetrics.TrackingProtection
import org.mozilla.fenix.HomeActivity
import org.mozilla.fenix.R
Expand Down Expand Up @@ -118,20 +119,30 @@ class SettingsFragment : PreferenceFragmentCompat() {
requireComponents.backgroundServices.accountManager.accountProfile()
)

val booleanPreferenceTelemetryAllowList = listOf(
requireContext().getString(R.string.pref_key_show_search_suggestions),
requireContext().getString(R.string.pref_key_remote_debugging),
requireContext().getString(R.string.pref_key_telemetry),
requireContext().getString(R.string.pref_key_tracking_protection),
requireContext().getString(R.string.pref_key_search_bookmarks),
requireContext().getString(R.string.pref_key_search_browsing_history),
requireContext().getString(R.string.pref_key_show_clipboard_suggestions),
requireContext().getString(R.string.pref_key_show_search_engine_shortcuts),
requireContext().getString(R.string.pref_key_open_links_in_a_private_tab),
requireContext().getString(R.string.pref_key_sync_logins),
requireContext().getString(R.string.pref_key_sync_bookmarks),
requireContext().getString(R.string.pref_key_sync_history),
requireContext().getString(R.string.pref_key_show_voice_search),
requireContext().getString(R.string.pref_key_show_search_suggestions_in_private)
)

preferenceManager.sharedPreferences
.registerOnSharedPreferenceChangeListener(this) { sharedPreferences, key ->
try {
context?.let { context ->
context.components.analytics.metrics.track(
Event.PreferenceToggled(
key,
sharedPreferences.getBoolean(key, false),
context
)
)
if (key in booleanPreferenceTelemetryAllowList) {
val enabled = sharedPreferences.getBoolean(key, false)
Events.preferenceToggled.record(Events.PreferenceToggledExtra(enabled, key))
}
} catch (e: IllegalArgumentException) {
// The event is not tracked
} catch (e: ClassCastException) {
// The setting is not a boolean, not tracked
}
Expand Down

0 comments on commit aca93c7

Please sign in to comment.