From f8cb1d6b485deb40261ac5e1c1de3259b201ce9e Mon Sep 17 00:00:00 2001 From: Grisha Kruglov Date: Thu, 25 Jun 2020 17:35:52 -0700 Subject: [PATCH] Closes #11909: Metrics for recording number of recently used PWAs --- app/metrics.yaml | 37 +++++++++++++++++++ .../java/org/mozilla/fenix/HomeActivity.kt | 26 +++++++++++++ docs/metrics.md | 2 + 3 files changed, 65 insertions(+) diff --git a/app/metrics.yaml b/app/metrics.yaml index a56e2fba1..952ab58c3 100644 --- a/app/metrics.yaml +++ b/app/metrics.yaml @@ -497,6 +497,43 @@ metrics: notification_emails: - fenix-core@mozilla.com expires: "2020-09-01" + recently_used_pwa_count: + type: counter + lifetime: application + description: | + A counter that indicates how many PWAs a user has recently used. + Threshold for "recency" set in HomeActivity#PWA_RECENTLY_USED_THRESHOLD. + Currently we are not told by the OS when a PWA is removed by the user, + so we use the "recently used" heuristic to judge how many PWAs are still + active, as a proxy for "installed". This value will only be set if the + user has at least *one* recently used PWA. If they have 0, this metric + will not be sent, resulting in a null value during analysis on the + server-side. To disambiguate between a failed `recently_used_pwa_count` + metric and 0 recent PWAs, please see `has_recent_pwas`. + send_in_pings: + - metrics + bugs: + - https://github.com/mozilla-mobile/fenix/issues/11909 + data_reviews: + - https://github.com/mozilla-mobile/fenix/pull/11982#pullrequestreview-437963817 + notification_emails: + - fenix-core@mozilla.com + expires: "2020-12-01" + has_recent_pwas: + type: boolean + lifetime: application + description: | + A boolean that indicates if the user has recently used PWAs. + See recently_used_pwa_count for the actual count. + send_in_pings: + - metrics + bugs: + - https://github.com/mozilla-mobile/fenix/issues/11909 + data_reviews: + - https://github.com/mozilla-mobile/fenix/pull/11982#pullrequestreview-437963817 + notification_emails: + - fenix-core@mozilla.com + expires: "2020-12-01" search_count: type: labeled_counter description: | diff --git a/app/src/main/java/org/mozilla/fenix/HomeActivity.kt b/app/src/main/java/org/mozilla/fenix/HomeActivity.kt index 0a9b5178c..a80def5b0 100644 --- a/app/src/main/java/org/mozilla/fenix/HomeActivity.kt +++ b/app/src/main/java/org/mozilla/fenix/HomeActivity.kt @@ -8,6 +8,7 @@ import android.content.Context import android.content.Intent import android.os.Bundle import android.os.StrictMode +import android.text.format.DateUtils import android.util.AttributeSet import android.view.View import android.view.WindowManager @@ -27,6 +28,7 @@ import androidx.navigation.ui.NavigationUI import androidx.recyclerview.widget.LinearLayoutManager import kotlinx.android.synthetic.main.activity_home.* import kotlinx.coroutines.CoroutineScope +import kotlinx.coroutines.Dispatchers import kotlinx.coroutines.launch import mozilla.components.browser.search.SearchEngine import mozilla.components.browser.session.Session @@ -50,6 +52,7 @@ import mozilla.components.support.locale.LocaleAwareAppCompatActivity import mozilla.components.support.utils.SafeIntent import mozilla.components.support.utils.toSafeIntent import mozilla.components.support.webextensions.WebExtensionPopupFeature +import org.mozilla.fenix.GleanMetrics.Metrics import org.mozilla.fenix.browser.UriOpenedObserver import org.mozilla.fenix.browser.browsingmode.BrowsingMode import org.mozilla.fenix.browser.browsingmode.BrowsingModeManager @@ -188,6 +191,8 @@ open class HomeActivity : LocaleAwareAppCompatActivity() { intent.removeExtra(START_IN_RECENTS_SCREEN) moveTaskToBack(true) } + + captureSnapshotTelemetryMetrics() } @CallSuper @@ -523,6 +528,23 @@ open class HomeActivity : LocaleAwareAppCompatActivity() { this.visualCompletenessQueue = visualCompletenessQueue } + private fun captureSnapshotTelemetryMetrics() = CoroutineScope(Dispatchers.IO).launch { + // PWA + val recentlyUsedPwaCount = components.core.webAppShortcutManager.recentlyUsedWebAppsCount( + activeThresholdMs = PWA_RECENTLY_USED_THRESHOLD + ) + if (recentlyUsedPwaCount == 0) { + Metrics.hasRecentPwas.set(false) + } else { + Metrics.hasRecentPwas.set(true) + // This metric's lifecycle is set to 'application', meaning that it gets reset upon + // application restart. Combined with the behaviour of the metric type itself (a growing counter), + // it's important that this metric is only set once per application's lifetime. + // Otherwise, we're going to over-count. + Metrics.recentlyUsedPwaCount.add(recentlyUsedPwaCount) + } + } + @VisibleForTesting internal fun isActivityColdStarted(startingIntent: Intent, activityIcicle: Bundle?): Boolean = // First time opening this activity in the task. @@ -541,5 +563,9 @@ open class HomeActivity : LocaleAwareAppCompatActivity() { const val EXTRA_OPENED_FROM_NOTIFICATION = "notification_open" const val delay = 5000L const val START_IN_RECENTS_SCREEN = "start_in_recents_screen" + + // PWA must have been used within last 30 days to be considered "recently used" for the + // telemetry purposes. + const val PWA_RECENTLY_USED_THRESHOLD = DateUtils.DAY_IN_MILLIS * 30L } } diff --git a/docs/metrics.md b/docs/metrics.md index 69aa677cf..68ebc7c3a 100644 --- a/docs/metrics.md +++ b/docs/metrics.md @@ -251,8 +251,10 @@ The following metrics are added to the ping: | metrics.adjust_network |[string](https://mozilla.github.io/glean/book/user/metrics/string.html) |A string containing the Adjust network ID from which the user installed Fenix. This will not send on the first session the user runs. If the install is organic, this will be empty. |[1](https://github.com/mozilla-mobile/fenix/pull/9253)||2020-09-01 | | metrics.default_browser |[boolean](https://mozilla.github.io/glean/book/user/metrics/boolean.html) |Is Fenix the default browser? |[1](https://github.com/mozilla-mobile/fenix/pull/1067#issuecomment-474598673)||2020-09-01 | | metrics.default_moz_browser |[string](https://mozilla.github.io/glean/book/user/metrics/string.html) |The name of the default browser on device if and only if it's a Mozilla owned product |[1](https://github.com/mozilla-mobile/fenix/pull/1953/), [2](https://github.com/mozilla-mobile/fenix/pull/5216)||2020-09-01 | +| metrics.has_recent_pwas |[boolean](https://mozilla.github.io/glean/book/user/metrics/boolean.html) |A boolean that indicates if the user has recently used PWAs. See recently_used_pwa_count for the actual count. |[1](https://github.com/mozilla-mobile/fenix/pull/11982#pullrequestreview-437963817)||2020-12-01 | | metrics.has_top_sites |[boolean](https://mozilla.github.io/glean/book/user/metrics/boolean.html) |A boolean that indicates if the user has top sites |[1](https://github.com/mozilla-mobile/fenix/pull/9556)||2020-09-01 | | metrics.mozilla_products |[string_list](https://mozilla.github.io/glean/book/user/metrics/string_list.html) |A list of all the Mozilla products installed on device. We currently scan for: Firefox, Firefox Beta, Firefox Aurora, Firefox Nightly, Firefox Fdroid, Firefox Lite, Reference Browser, Reference Browser Debug, Fenix, Focus, and Lockwise. |[1](https://github.com/mozilla-mobile/fenix/pull/1953/), [2](https://github.com/mozilla-mobile/fenix/pull/5216)||2020-09-01 | +| metrics.recently_used_pwa_count |[counter](https://mozilla.github.io/glean/book/user/metrics/counter.html) |A counter that indicates how many PWAs a user has recently used. Threshold for "recency" set in HomeActivity#PWA_RECENTLY_USED_THRESHOLD. Currently we are not told by the OS when a PWA is removed by the user, so we use the "recently used" heuristic to judge how many PWAs are still active, as a proxy for "installed". This value will only be set if the user has at least *one* recently used PWA. If they have 0, this metric will not be sent, resulting in a null value during analysis on the server-side. To disambiguate between a failed `recently_used_pwa_count` metric and 0 recent PWAs, please see `has_recent_pwas`. |[1](https://github.com/mozilla-mobile/fenix/pull/11982#pullrequestreview-437963817)||2020-12-01 | | metrics.search_count |[labeled_counter](https://mozilla.github.io/glean/book/user/metrics/labeled_counters.html) |The labels for this counter are `.`. If the search engine is bundled with Fenix `search-engine-name` will be the name of the search engine. If it's a custom search engine (defined: https://github.com/mozilla-mobile/fenix/issues/1607) the value will be `custom`. `source` will be: `action`, `suggestion`, `widget` or `shortcut` (depending on the source from which the search started). Also added the `other` option for the source but it should never enter on this case. |[1](https://github.com/mozilla-mobile/fenix/pull/1677), [2](https://github.com/mozilla-mobile/fenix/pull/5216), [3](https://github.com/mozilla-mobile/fenix/pull/7310)||2020-09-01 | | metrics.search_widget_installed |[boolean](https://mozilla.github.io/glean/book/user/metrics/boolean.html) |Whether or not the search widget is installed |[1](https://github.com/mozilla-mobile/fenix/pull/10958)||2020-09-01 | | metrics.toolbar_position |[string](https://mozilla.github.io/glean/book/user/metrics/string.html) |A string that indicates the new position of the toolbar TOP or BOTTOM |[1](https://github.com/mozilla-mobile/fenix/pull/6608)||2020-09-01 |