Fixes #965 - Adds a metrics wrapper for telemetry
parent
f66bae0801
commit
9dc0b4781a
|
@ -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(
|
||||
|
|
|
@ -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)
|
||||
}
|
||||
}
|
|
@ -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)
|
||||
)
|
||||
)
|
||||
}
|
||||
}
|
||||
|
|
|
@ -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
|
||||
}
|
||||
}
|
|
@ -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
|
||||
}
|
||||
}
|
|
@ -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<String, Any>?
|
||||
get() = null
|
||||
}
|
||||
|
||||
interface MetricsService {
|
||||
fun start()
|
||||
fun track(event: Event)
|
||||
fun shouldTrack(event: Event): Boolean
|
||||
}
|
||||
|
||||
class Metrics(private val services: List<MetricsService>) {
|
||||
fun start() {
|
||||
services.forEach { it.start() }
|
||||
}
|
||||
|
||||
fun track(event: Event) {
|
||||
services
|
||||
.filter { it.shouldTrack(event) }
|
||||
.forEach { it.track(event) }
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue