diff --git a/app/src/main/java/org/mozilla/fenix/components/metrics/Event.kt b/app/src/main/java/org/mozilla/fenix/components/metrics/Event.kt index 3fa6b65897..a6e43ea03a 100644 --- a/app/src/main/java/org/mozilla/fenix/components/metrics/Event.kt +++ b/app/src/main/java/org/mozilla/fenix/components/metrics/Event.kt @@ -205,6 +205,9 @@ sealed class Event { object ContextMenuSelectAllTapped : Event() object ContextMenuShareTapped : Event() + object HaveTopSites : Event() + object HaveNoTopSites : Event() + // Interaction events with extras data class TopSiteSwipeCarousel(val page: Int) : Event() { 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 00fc6bc338..3b8da8e117 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 @@ -726,6 +726,12 @@ private val Event.wrapper: EventWrapper<*>? Event.HaveNoOpenTabs -> EventWrapper( { Metrics.hasOpenTabs.set(false) } ) + Event.HaveTopSites -> EventWrapper( + { Metrics.hasTopSites.set(true) } + ) + Event.HaveNoTopSites -> EventWrapper( + { Metrics.hasTopSites.set(false) } + ) // Don't record other events in Glean: is Event.AddBookmark -> null diff --git a/app/src/main/java/org/mozilla/fenix/components/metrics/MetricController.kt b/app/src/main/java/org/mozilla/fenix/components/metrics/MetricController.kt index 3b31385c21..013fd24222 100644 --- a/app/src/main/java/org/mozilla/fenix/components/metrics/MetricController.kt +++ b/app/src/main/java/org/mozilla/fenix/components/metrics/MetricController.kt @@ -22,6 +22,7 @@ import mozilla.components.feature.findinpage.facts.FindInPageFacts import mozilla.components.feature.media.facts.MediaFacts import mozilla.components.feature.prompts.dialog.LoginDialogFacts import mozilla.components.feature.pwa.ProgressiveWebAppFacts +import mozilla.components.feature.top.sites.facts.TopSitesFacts import mozilla.components.support.base.Component import mozilla.components.support.base.facts.Action import mozilla.components.support.base.facts.Fact @@ -134,6 +135,13 @@ internal class ReleaseMetricController( } } + @VisibleForTesting + internal fun factToEvent( + fact: Fact + ): Event? { + return fact.toEvent() + } + private fun isInitialized(type: MetricServiceType): Boolean = initialized.contains(type) private fun isTelemetryEnabled(type: MetricServiceType): Boolean = when (type) { @@ -242,6 +250,23 @@ internal class ReleaseMetricController( Component.FEATURE_PWA to ProgressiveWebAppFacts.Items.INSTALL_SHORTCUT -> { Event.ProgressiveWebAppInstallAsShortcut } + Component.FEATURE_TOP_SITES to TopSitesFacts.Items.COUNT -> { + value?.let { + var count = 0 + try { + count = it.toInt() + } catch (e: NumberFormatException) { + // Do nothing + } + + return if (count > 0) { + Event.HaveTopSites + } else { + Event.HaveNoTopSites + } + } + null + } else -> null } diff --git a/app/src/test/java/org/mozilla/fenix/components/metrics/MetricControllerTest.kt b/app/src/test/java/org/mozilla/fenix/components/metrics/MetricControllerTest.kt index 97934c5414..be97fd8a34 100644 --- a/app/src/test/java/org/mozilla/fenix/components/metrics/MetricControllerTest.kt +++ b/app/src/test/java/org/mozilla/fenix/components/metrics/MetricControllerTest.kt @@ -10,7 +10,12 @@ import io.mockk.impl.annotations.MockK import io.mockk.mockk import io.mockk.verify import io.mockk.verifyAll +import mozilla.components.feature.top.sites.facts.TopSitesFacts +import mozilla.components.support.base.Component +import mozilla.components.support.base.facts.Action +import mozilla.components.support.base.facts.Fact import mozilla.components.support.base.log.logger.Logger +import org.junit.Assert.assertEquals import org.junit.Before import org.junit.Test @@ -167,4 +172,59 @@ class MetricControllerTest { controller.track(Event.TabMediaPause) verify { marketingService1.track(Event.TabMediaPause) } } + + @Test + fun `topsites fact should convert to the right events`() { + var enabled = true + val controller = ReleaseMetricController( + services = listOf(dataService1), + isDataTelemetryEnabled = { enabled }, + isMarketingDataTelemetryEnabled = { enabled } + ) + + var fact = Fact( + Component.FEATURE_TOP_SITES, + Action.INTERACTION, + TopSitesFacts.Items.COUNT, + "1" + ) + + assertEquals(controller.factToEvent(fact), Event.HaveTopSites) + + fact = Fact( + Component.FEATURE_TOP_SITES, + Action.INTERACTION, + TopSitesFacts.Items.COUNT, + "0" + ) + + assertEquals(controller.factToEvent(fact), Event.HaveNoTopSites) + + fact = Fact( + Component.FEATURE_TOP_SITES, + Action.INTERACTION, + TopSitesFacts.Items.COUNT, + "10" + ) + + assertEquals(controller.factToEvent(fact), Event.HaveTopSites) + + fact = Fact( + Component.FEATURE_TOP_SITES, + Action.INTERACTION, + TopSitesFacts.Items.COUNT, + "-4" + ) + + assertEquals(controller.factToEvent(fact), Event.HaveNoTopSites) + + fact = Fact( + Component.FEATURE_TOP_SITES, + Action.INTERACTION, + TopSitesFacts.Items.COUNT, + "test" + ) + + assertEquals(controller.factToEvent(fact), Event.HaveNoTopSites) + } }