From 9dc0b4781a364f886bb7d5201734cd741629d282 Mon Sep 17 00:00:00 2001 From: Jeff Boek Date: Tue, 12 Mar 2019 13:09:04 -0700 Subject: [PATCH] Fixes #965 - Adds a metrics wrapper for telemetry --- .../org/mozilla/fenix/FenixApplication.kt | 12 +--- .../java/org/mozilla/fenix/LeanplumHelper.kt | 22 ------ .../org/mozilla/fenix/components/Analytics.kt | 13 ++++ .../components/metrics/GleanMetricsService.kt | 24 +++++++ .../metrics/LeanplumMetricsService.kt | 69 +++++++++++++++++++ .../fenix/components/metrics/Metrics.kt | 59 ++++++++++++++++ 6 files changed, 167 insertions(+), 32 deletions(-) delete mode 100644 app/src/main/java/org/mozilla/fenix/LeanplumHelper.kt create mode 100644 app/src/main/java/org/mozilla/fenix/components/metrics/GleanMetricsService.kt create mode 100644 app/src/main/java/org/mozilla/fenix/components/metrics/LeanplumMetricsService.kt create mode 100644 app/src/main/java/org/mozilla/fenix/components/metrics/Metrics.kt diff --git a/app/src/main/java/org/mozilla/fenix/FenixApplication.kt b/app/src/main/java/org/mozilla/fenix/FenixApplication.kt index 868f5f2d1..f684fcae9 100644 --- a/app/src/main/java/org/mozilla/fenix/FenixApplication.kt +++ b/app/src/main/java/org/mozilla/fenix/FenixApplication.kt @@ -15,13 +15,11 @@ import kotlinx.coroutines.launch import mozilla.components.service.fretboard.Fretboard import mozilla.components.service.fretboard.source.kinto.KintoExperimentSource import mozilla.components.service.fretboard.storage.flatfile.FlatFileExperimentStorage -import mozilla.components.service.glean.Glean import mozilla.components.support.base.log.Log import mozilla.components.support.base.log.logger.Logger import mozilla.components.support.base.log.sink.AndroidLogSink import mozilla.components.support.rustlog.RustLog import org.mozilla.fenix.AdjustHelper.setupAdjustIfNeeded -import org.mozilla.fenix.LeanplumHelper.setupLeanplumIfNeeded import org.mozilla.fenix.components.Components import java.io.File @@ -33,7 +31,6 @@ open class FenixApplication : Application() { override fun onCreate() { super.onCreate() - val megazordEnabled = setupMegazord() setupLogging(megazordEnabled) setupCrashReporting() @@ -47,10 +44,10 @@ open class FenixApplication : Application() { } setupLeakCanary() - setupGlean(this) + loadExperiments() setupAdjustIfNeeded(this) - setupLeanplumIfNeeded(this) + components.analytics.metrics.start() } protected open fun setupLeakCanary() { @@ -73,11 +70,6 @@ open class FenixApplication : Application() { } } - private fun setupGlean(context: Context) { - Glean.initialize(context) - Glean.setUploadEnabled(BuildConfig.TELEMETRY) - } - private fun loadExperiments() { val experimentsFile = File(filesDir, EXPERIMENTS_JSON_FILENAME) val experimentSource = KintoExperimentSource( diff --git a/app/src/main/java/org/mozilla/fenix/LeanplumHelper.kt b/app/src/main/java/org/mozilla/fenix/LeanplumHelper.kt deleted file mode 100644 index ee47f516a..000000000 --- a/app/src/main/java/org/mozilla/fenix/LeanplumHelper.kt +++ /dev/null @@ -1,22 +0,0 @@ -/* This Source Code Form is subject to the terms of the Mozilla Public - * License, v. 2.0. If a copy of the MPL was not distributed with this - * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ - -package org.mozilla.fenix - -import com.leanplum.Leanplum -import com.leanplum.LeanplumActivityHelper -import com.leanplum.annotations.Parser -import org.mozilla.fenix.utils.Settings - -object LeanplumHelper { - fun setupLeanplumIfNeeded(application: FenixApplication) { - if (!Settings.getInstance(application).isTelemetryEnabled) { return } - - Leanplum.setApplicationContext(application) - Parser.parseVariables(application) - LeanplumActivityHelper.enableLifecycleCallbacks(application) - Leanplum.setAppIdForProductionMode(BuildConfig.LEANPLUM_ID, BuildConfig.LEANPLUM_TOKEN) - Leanplum.start(application) - } -} diff --git a/app/src/main/java/org/mozilla/fenix/components/Analytics.kt b/app/src/main/java/org/mozilla/fenix/components/Analytics.kt index 6357169b0..e711f7461 100644 --- a/app/src/main/java/org/mozilla/fenix/components/Analytics.kt +++ b/app/src/main/java/org/mozilla/fenix/components/Analytics.kt @@ -4,12 +4,16 @@ package org.mozilla.fenix.components +import android.app.Application import android.content.Context import mozilla.components.lib.crash.CrashReporter import mozilla.components.lib.crash.service.MozillaSocorroService import mozilla.components.lib.crash.service.SentryService import org.mozilla.fenix.BuildConfig import org.mozilla.fenix.R +import org.mozilla.fenix.components.metrics.GleanMetricsService +import org.mozilla.fenix.components.metrics.LeanplumMetricsService +import org.mozilla.fenix.components.metrics.Metrics import org.mozilla.geckoview.BuildConfig.MOZ_APP_BUILDID import org.mozilla.geckoview.BuildConfig.MOZ_APP_VERSION @@ -39,4 +43,13 @@ class Analytics( enabled = true ) } + + val metrics: Metrics by lazy { + Metrics( + listOf( + GleanMetricsService(context), + LeanplumMetricsService(context as Application) + ) + ) + } } 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 new file mode 100644 index 000000000..bc0996560 --- /dev/null +++ b/app/src/main/java/org/mozilla/fenix/components/metrics/GleanMetricsService.kt @@ -0,0 +1,24 @@ +/* This Source Code Form is subject to the terms of the Mozilla Public + * License, v. 2.0. If a copy of the MPL was not distributed with this + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ +package org.mozilla.fenix.components.metrics + +import android.content.Context +import mozilla.components.service.glean.Glean +import org.mozilla.fenix.BuildConfig +import org.mozilla.fenix.utils.Settings + +class GleanMetricsService(private val context: Context) : MetricsService { + override fun start() { + Glean.initialize(context) + Glean.setUploadEnabled(IsGleanEnabled) + } + + override fun track(event: Event) { } + + override fun shouldTrack(event: Event): Boolean = Settings.getInstance(context).isTelemetryEnabled + + companion object { + private const val IsGleanEnabled = BuildConfig.TELEMETRY + } +} 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 new file mode 100644 index 000000000..099f4aca4 --- /dev/null +++ b/app/src/main/java/org/mozilla/fenix/components/metrics/LeanplumMetricsService.kt @@ -0,0 +1,69 @@ +/* This Source Code Form is subject to the terms of the Mozilla Public + * License, v. 2.0. If a copy of the MPL was not distributed with this + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ +package org.mozilla.fenix.components.metrics + +import android.app.Application +import com.leanplum.Leanplum +import com.leanplum.LeanplumActivityHelper +import com.leanplum.annotations.Parser +import org.mozilla.fenix.BuildConfig +import org.mozilla.fenix.utils.Settings + +private val Event.name: String + get() = when (this) { + is Event.AddBookmark -> "E_Add_Bookmark" + is Event.RemoveBookmark -> "E_Remove_Bookmark" + is Event.OpenedBookmark -> "E_Opened_Bookmark" + is Event.OpenedApp -> "E_Opened_App" + is Event.OpenedAppFirstRun -> "E_Opened_App_FirstRun" + is Event.InteractWithSearchURLArea -> "E_Interact_With_Search_URL_Area" + is Event.SavedLoginandPassword -> "E_Saved_Login_and_Password" + is Event.FXANewSignup -> "E_FXA_New_Signup" + is Event.UserSignedInToFxA -> "E_User_Signed_In_To_FxA" + is Event.UserDownloadedFocus -> "E_User_Downloaded_Focus" + is Event.UserDownloadedLockbox -> "E_User_Downloaded_Lockbox" + is Event.UserDownloadedFennec -> "E_User_Downloaded_Fennec" + is Event.TrackingProtectionSettingsChanged -> "E_Tracking_Protection_Settings_Changed" + is Event.FXASyncedNewDevice -> "E_FXA_Synced_New_Device" + is Event.DismissedOnboarding -> "E_Dismissed_Onboarding" + is Event.Uninstall -> "E_Uninstall" + is Event.OpenNewNormalTab -> "E_Open_New_Normal_Tab" + is Event.OpenNewPrivateTab -> "E_Open_New_Private_Tab" + is Event.ShareStarted -> "E_Share_Started" + is Event.ShareCanceled -> "E_Share_Canceled" + is Event.ShareCompleted -> "E_Share_Completed" + is Event.ClosePrivateTabs -> "E_Close_Private_Tabs" + is Event.ClearedPrivateData -> "E_Cleared_Private_Data" + is Event.OpenedLoginManager -> "E_Opened_Login_Manager" + is Event.OpenedMailtoLink -> "E_Opened_Mailto_Link" + is Event.DownloadMediaSavedImage -> "E_Download_Media_Saved_Image" + is Event.UserUsedReaderView -> "E_User_Used_Reader_View" + is Event.UserDownloadedPocket -> "E_User_Downloaded_Pocket" + is Event.UserDownloadedSend -> "E_User_Downloaded_Send" + is Event.OpenedPocketStory -> "E_Opened_Pocket_Story" + is Event.DarkModeEnabled -> "E_Dark_Mode_Enabled" +} + +class LeanplumMetricsService(private val application: Application) : MetricsService { + override fun start() { + Leanplum.setApplicationContext(application) + Parser.parseVariables(application) + LeanplumActivityHelper.enableLifecycleCallbacks(application) + Leanplum.setAppIdForProductionMode(LeanplumId, LeanplumToken) + Leanplum.start(application) + } + + override fun track(event: Event) { + Leanplum.track(event.name, event.extras) + } + + override fun shouldTrack(event: Event): Boolean = Settings.getInstance(application).isTelemetryEnabled + + companion object { + private val LeanplumId: String + get() = BuildConfig.LEANPLUM_ID + private val LeanplumToken: String + get() = BuildConfig.LEANPLUM_TOKEN + } +} 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 new file mode 100644 index 000000000..1f8186eb3 --- /dev/null +++ b/app/src/main/java/org/mozilla/fenix/components/metrics/Metrics.kt @@ -0,0 +1,59 @@ +/* This Source Code Form is subject to the terms of the Mozilla Public + * License, v. 2.0. If a copy of the MPL was not distributed with this + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ +package org.mozilla.fenix.components.metrics + +sealed class Event { + object AddBookmark : Event() + object RemoveBookmark : Event() + object OpenedBookmark : Event() + object OpenedApp : Event() + object OpenedAppFirstRun : Event() + object InteractWithSearchURLArea : Event() + object SavedLoginandPassword : Event() + object FXANewSignup : Event() + object UserSignedInToFxA : Event() + object UserDownloadedFocus : Event() + object UserDownloadedLockbox : Event() + object UserDownloadedFennec : Event() + object TrackingProtectionSettingsChanged : Event() + object FXASyncedNewDevice : Event() + object DismissedOnboarding : Event() + object Uninstall : Event() + object OpenNewNormalTab : Event() + object OpenNewPrivateTab : Event() + object ShareStarted : Event() + object ShareCanceled : Event() + object ShareCompleted : Event() + object ClosePrivateTabs : Event() + object ClearedPrivateData : Event() + object OpenedLoginManager : Event() + object OpenedMailtoLink : Event() + object DownloadMediaSavedImage : Event() + object UserUsedReaderView : Event() + object UserDownloadedPocket : Event() + object UserDownloadedSend : Event() + object OpenedPocketStory : Event() + object DarkModeEnabled : Event() + + val extras: Map? + get() = null +} + +interface MetricsService { + fun start() + fun track(event: Event) + fun shouldTrack(event: Event): Boolean +} + +class Metrics(private val services: List) { + fun start() { + services.forEach { it.start() } + } + + fun track(event: Event) { + services + .filter { it.shouldTrack(event) } + .forEach { it.track(event) } + } +}