From 786592e8d09de140ef84749ecc1ba1a3bfaa235d Mon Sep 17 00:00:00 2001 From: Jeff Boek Date: Mon, 18 Mar 2019 14:54:36 -0700 Subject: [PATCH] For #959 - Adds telemetry for when the search bar is tapped --- app/metrics.yaml | 15 ++++++++++++++- .../main/java/org/mozilla/fenix/HomeActivity.kt | 2 +- .../org/mozilla/fenix/browser/BrowserFragment.kt | 16 +++++++++++----- .../components/metrics/GleanMetricsService.kt | 16 +++++++++------- .../components/metrics/LeanplumMetricsService.kt | 11 ++++++++--- .../mozilla/fenix/components/metrics/Metrics.kt | 14 ++++++++++---- .../java/org/mozilla/fenix/home/HomeFragment.kt | 3 +++ 7 files changed, 56 insertions(+), 21 deletions(-) diff --git a/app/metrics.yaml b/app/metrics.yaml index 2c974162b..df3388595 100644 --- a/app/metrics.yaml +++ b/app/metrics.yaml @@ -8,7 +8,7 @@ events: app_opened: type: event description: > - A User opened the app + A user opened the app extra_keys: source: "The source from which the app was opened" bugs: @@ -18,6 +18,19 @@ events: notification_emails: - telemetry-client-dev@mozilla.com expires: never + search_bar_tapped: + type: event + description: > + A user tapped the search bar + extra_keys: + source: "The source from which the search bar was tapped" + bugs: + - 123456789 + data_reviews: + - N/A + notification_emails: + - telemetry-client-dev@mozilla.com + expires: never metrics: default_browser: diff --git a/app/src/main/java/org/mozilla/fenix/HomeActivity.kt b/app/src/main/java/org/mozilla/fenix/HomeActivity.kt index 933104a26..bcca742e5 100644 --- a/app/src/main/java/org/mozilla/fenix/HomeActivity.kt +++ b/app/src/main/java/org/mozilla/fenix/HomeActivity.kt @@ -65,7 +65,7 @@ open class HomeActivity : AppCompatActivity() { val safeIntent = intent?.let { SafeIntent(it) } if (safeIntent?.isLauncherIntent == true) { - val source = if (isCustomTab) Event.OpenedAppSource.CUSTOM_TAB else Event.OpenedAppSource.APP_ICON + val source = if (isCustomTab) Event.OpenedApp.Source.CUSTOM_TAB else Event.OpenedApp.Source.APP_ICON components.analytics.metrics.track(Event.OpenedApp(source)) } diff --git a/app/src/main/java/org/mozilla/fenix/browser/BrowserFragment.kt b/app/src/main/java/org/mozilla/fenix/browser/BrowserFragment.kt index a21398933..bfdd02076 100644 --- a/app/src/main/java/org/mozilla/fenix/browser/BrowserFragment.kt +++ b/app/src/main/java/org/mozilla/fenix/browser/BrowserFragment.kt @@ -42,6 +42,7 @@ import org.mozilla.fenix.IntentReceiverActivity import org.mozilla.fenix.utils.ItsNotBrokenSnack import org.mozilla.fenix.R import org.mozilla.fenix.components.FindInPageIntegration +import org.mozilla.fenix.components.metrics.Event import org.mozilla.fenix.components.toolbar.SearchAction import org.mozilla.fenix.components.toolbar.SearchState import org.mozilla.fenix.components.toolbar.ToolbarComponent @@ -242,11 +243,16 @@ class BrowserFragment : Fragment(), BackHandler { getAutoDisposeObservable() .subscribe { when (it) { - is SearchAction.ToolbarTapped -> Navigation.findNavController(toolbarComponent.getView()) - .navigate( - BrowserFragmentDirections.actionBrowserFragmentToSearchFragment( - requireComponents.core.sessionManager.selectedSession?.id) - ) + is SearchAction.ToolbarTapped -> { + Navigation + .findNavController(toolbarComponent.getView()) + .navigate( + BrowserFragmentDirections.actionBrowserFragmentToSearchFragment( + requireComponents.core.sessionManager.selectedSession?.id) + ) + + requireComponents.analytics.metrics.track(Event.SearchBarTapped(Event.SearchBarTapped.Source.BROWSER)) + } is SearchAction.ToolbarMenuItemTapped -> handleToolbarItemInteraction(it) } } diff --git a/app/src/main/java/org/mozilla/fenix/components/metrics/GleanMetricsService.kt b/app/src/main/java/org/mozilla/fenix/components/metrics/GleanMetricsService.kt index 80210a1e8..ab7e1f836 100644 --- a/app/src/main/java/org/mozilla/fenix/components/metrics/GleanMetricsService.kt +++ b/app/src/main/java/org/mozilla/fenix/components/metrics/GleanMetricsService.kt @@ -12,6 +12,13 @@ import org.mozilla.fenix.utils.Settings import org.mozilla.fenix.debug.GleanMetrics.Metrics import org.mozilla.fenix.debug.GleanMetrics.Events +private val Event.metricType: EventMetricType? + get() = when(this) { + is Event.OpenedApp -> Events.appOpened + is Event.SearchBarTapped -> Events.searchBarTapped + else -> null + } + class GleanMetricsService(private val context: Context) : MetricsService { override fun start() { Glean.initialize(context) @@ -22,17 +29,12 @@ class GleanMetricsService(private val context: Context) : MetricsService { } } - private fun mapEventToGlean(event: Event): EventMetricType? = when(event) { - is Event.OpenedApp -> Events.appOpened - else -> null - } - override fun track(event: Event) { - mapEventToGlean(event)?.record(event.extras) + event.metricType?.record(event.extras) } override fun shouldTrack(event: Event): Boolean { - return Settings.getInstance(context).isTelemetryEnabled + return Settings.getInstance(context).isTelemetryEnabled && event.metricType != null } companion object { diff --git a/app/src/main/java/org/mozilla/fenix/components/metrics/LeanplumMetricsService.kt b/app/src/main/java/org/mozilla/fenix/components/metrics/LeanplumMetricsService.kt index 9e6cb48c5..5e520f124 100644 --- a/app/src/main/java/org/mozilla/fenix/components/metrics/LeanplumMetricsService.kt +++ b/app/src/main/java/org/mozilla/fenix/components/metrics/LeanplumMetricsService.kt @@ -11,7 +11,7 @@ import com.leanplum.annotations.Parser import org.mozilla.fenix.BuildConfig import org.mozilla.fenix.utils.Settings -private val Event.name: String +private val Event.name: String? get() = when (this) { is Event.AddBookmark -> "E_Add_Bookmark" is Event.RemoveBookmark -> "E_Remove_Bookmark" @@ -44,6 +44,9 @@ private val Event.name: String is Event.UserDownloadedSend -> "E_User_Downloaded_Send" is Event.OpenedPocketStory -> "E_Opened_Pocket_Story" is Event.DarkModeEnabled -> "E_Dark_Mode_Enabled" + + // Do not track these events in Leanplum + is Event.SearchBarTapped -> "" } class LeanplumMetricsService(private val application: Application) : MetricsService { @@ -84,12 +87,14 @@ class LeanplumMetricsService(private val application: Application) : MetricsServ } override fun track(event: Event) { - Leanplum.track(event.name, event.extras) + event.name?.also { + Leanplum.track(it, event.extras) + } } override fun shouldTrack(event: Event): Boolean { return Settings.getInstance(application).isTelemetryEnabled && - token.type != Token.Type.Invalid + token.type != Token.Type.Invalid && !event.name.isNullOrEmpty() } companion object { diff --git a/app/src/main/java/org/mozilla/fenix/components/metrics/Metrics.kt b/app/src/main/java/org/mozilla/fenix/components/metrics/Metrics.kt index 915cba6f9..1582bfa8d 100644 --- a/app/src/main/java/org/mozilla/fenix/components/metrics/Metrics.kt +++ b/app/src/main/java/org/mozilla/fenix/components/metrics/Metrics.kt @@ -10,10 +10,9 @@ sealed class Event { object RemoveBookmark : Event() object OpenedBookmark : Event() - enum class OpenedAppSource { - APP_ICON, CUSTOM_TAB - } - data class OpenedApp(val source: OpenedAppSource) : Event() { + + data class OpenedApp(val source: Source) : Event() { + enum class Source { APP_ICON, CUSTOM_TAB } override val extras: Map? get() = hashMapOf("source" to source.name) } @@ -46,6 +45,13 @@ sealed class Event { object OpenedPocketStory : Event() object DarkModeEnabled : Event() + // Interaction Events + data class SearchBarTapped(val source: Source) : Event() { + enum class Source { HOME, BROWSER } + override val extras: Map? + get() = mapOf("source" to source.name) + } + open val extras: Map? get() = null } diff --git a/app/src/main/java/org/mozilla/fenix/home/HomeFragment.kt b/app/src/main/java/org/mozilla/fenix/home/HomeFragment.kt index 78426bb07..721ba8134 100644 --- a/app/src/main/java/org/mozilla/fenix/home/HomeFragment.kt +++ b/app/src/main/java/org/mozilla/fenix/home/HomeFragment.kt @@ -35,6 +35,7 @@ import org.mozilla.fenix.DefaultThemeManager import org.mozilla.fenix.HomeActivity import org.mozilla.fenix.utils.ItsNotBrokenSnack import org.mozilla.fenix.R +import org.mozilla.fenix.components.metrics.Event import org.mozilla.fenix.ext.archive import org.mozilla.fenix.ext.requireComponents import org.mozilla.fenix.home.sessions.ArchivedSession @@ -134,6 +135,8 @@ class HomeFragment : Fragment(), CoroutineScope { view.toolbar.setOnClickListener { val directions = HomeFragmentDirections.actionHomeFragmentToSearchFragment(null) Navigation.findNavController(it).navigate(directions) + + requireComponents.analytics.metrics.track(Event.SearchBarTapped(Event.SearchBarTapped.Source.HOME)) } // There is currently an issue with visibility changes in ConstraintLayout 2.0.0-alpha3