From f91ab0b4602721823a923be9b5d292ed08bf61b5 Mon Sep 17 00:00:00 2001 From: Colin Lee Date: Fri, 26 Apr 2019 00:43:02 -0700 Subject: [PATCH] [fenix] Merge pull request https://github.com/mozilla-mobile/fenix/pull/1785 from colintheshots/fix1301 For https://github.com/mozilla-mobile/fenix/issues/1301: Add uri_count to metrics ping --- app/metrics.yaml | 13 +++++ .../java/org/mozilla/fenix/HomeActivity.kt | 50 +++++++++++++++++++ .../components/metrics/GleanMetricsService.kt | 3 ++ .../fenix/components/metrics/Metrics.kt | 1 + 4 files changed, 67 insertions(+) diff --git a/app/metrics.yaml b/app/metrics.yaml index d88c44acd6..c47ea9de84 100644 --- a/app/metrics.yaml +++ b/app/metrics.yaml @@ -123,6 +123,19 @@ events: notification_emails: - fenix-core@mozilla.com expires: "2020-03-01" + total_uri_count: + type: counter + description: > + A counter of URIs visited by the user in the current session, including page reloads. This does not include background page requests and URIs from embedded pages or private browsing. + send_in_pings: + - metrics + bugs: + - 1301 + data_reviews: + - https://github.com/mozilla-mobile/fenix/pull/1785 + notification_emails: + - fenix-core@mozilla.com + expires: "2020-03-01" crash_reporter: opened: diff --git a/app/src/main/java/org/mozilla/fenix/HomeActivity.kt b/app/src/main/java/org/mozilla/fenix/HomeActivity.kt index b2da3c9d98..852caad341 100644 --- a/app/src/main/java/org/mozilla/fenix/HomeActivity.kt +++ b/app/src/main/java/org/mozilla/fenix/HomeActivity.kt @@ -17,6 +17,7 @@ import androidx.navigation.ui.AppBarConfiguration import androidx.navigation.ui.NavigationUI import mozilla.components.browser.search.SearchEngine import mozilla.components.browser.session.Session +import mozilla.components.browser.session.SessionManager import mozilla.components.concept.engine.EngineView import mozilla.components.feature.intent.IntentProcessor import mozilla.components.lib.crash.Crash @@ -36,6 +37,7 @@ import org.mozilla.fenix.settings.SettingsFragmentDirections @SuppressWarnings("TooManyFunctions") open class HomeActivity : AppCompatActivity() { open val isCustomTab = false + private var sessionObserver: SessionManager.Observer? = null val themeManager = DefaultThemeManager().also { it.onThemeChange = { theme -> @@ -85,6 +87,11 @@ open class HomeActivity : AppCompatActivity() { handleOpenedFromExternalSourceIfNecessary(intent) } + override fun onDestroy() { + sessionObserver?.let { components.core.sessionManager.unregister(it) } + super.onDestroy() + } + override fun onNewIntent(intent: Intent?) { super.onNewIntent(intent) handleCrashIfNecessary(intent) @@ -168,6 +175,8 @@ open class HomeActivity : AppCompatActivity() { BrowserDirection.FromHistory -> HistoryFragmentDirections.actionHistoryFragmentToBrowserFragment(externalSessionId) } + if (sessionObserver == null) + sessionObserver = subscribeToSessions() navHost.navController.navigate(directions) } @@ -197,6 +206,47 @@ open class HomeActivity : AppCompatActivity() { } } + private val singleSessionObserver = object : Session.Observer { + var urlLoading: String? = null + + override fun onLoadingStateChanged(session: Session, loading: Boolean) { + super.onLoadingStateChanged(session, loading) + + if (loading) urlLoading = session.url + else if (urlLoading != null && !session.private) + components.analytics.metrics.track(Event.UriOpened) + } + } + + private fun subscribeToSessions(): SessionManager.Observer { + + return object : SessionManager.Observer { + override fun onAllSessionsRemoved() { + super.onAllSessionsRemoved() + components.core.sessionManager.sessions.forEach { + it.unregister(singleSessionObserver) + } + } + + override fun onSessionAdded(session: Session) { + super.onSessionAdded(session) + session.register(singleSessionObserver) + } + + override fun onSessionRemoved(session: Session) { + super.onSessionRemoved(session) + session.unregister(singleSessionObserver) + } + + override fun onSessionsRestored() { + super.onSessionsRestored() + components.core.sessionManager.sessions.forEach { + it.register(singleSessionObserver) + } + } + }.also { components.core.sessionManager.register(it) } + } + companion object { const val OPEN_TO_BROWSER = "open_to_browser" } 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 2a11f30090..07b4eb9d18 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 @@ -150,6 +150,9 @@ private val Event.wrapper is Event.CustomTabsClosed -> EventWrapper( { CustomTab.closed.record(it) } ) + is Event.UriOpened -> EventWrapper( + { Events.totalUriCount.add(1) } + ) // Don't track other events with Glean else -> null 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 80a2798aa0..30b364fe39 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 @@ -70,6 +70,7 @@ sealed class Event { object CustomTabsClosed : Event() object CustomTabsActionTapped : Event() object CustomTabsMenuOpened : Event() + object UriOpened : Event() // Interaction Events data class SearchBarTapped(val source: Source) : Event() {