Commit 952c48c8 authored by Jeff Boek's avatar Jeff Boek Committed by Mihai Adrian
Browse files

For #4674 - Adds ability to toggle metric services by type

parent 9f3faa43
...@@ -34,6 +34,7 @@ import mozilla.components.support.rusthttp.RustHttpConfig ...@@ -34,6 +34,7 @@ import mozilla.components.support.rusthttp.RustHttpConfig
import mozilla.components.support.rustlog.RustLog import mozilla.components.support.rustlog.RustLog
import mozilla.components.support.webextensions.WebExtensionSupport import mozilla.components.support.webextensions.WebExtensionSupport
import org.mozilla.fenix.components.Components import org.mozilla.fenix.components.Components
import org.mozilla.fenix.components.metrics.MetricServiceType
import org.mozilla.fenix.ext.settings import org.mozilla.fenix.ext.settings
import org.mozilla.fenix.session.NotificationSessionObserver import org.mozilla.fenix.session.NotificationSessionObserver
import org.mozilla.fenix.session.VisibilityLifecycleCallback import org.mozilla.fenix.session.VisibilityLifecycleCallback
...@@ -132,7 +133,11 @@ open class FenixApplication : LocaleAwareApplication() { ...@@ -132,7 +133,11 @@ open class FenixApplication : LocaleAwareApplication() {
setupLeakCanary() setupLeakCanary()
if (settings().isTelemetryEnabled) { if (settings().isTelemetryEnabled) {
components.analytics.metrics.start() components.analytics.metrics.start(MetricServiceType.Data)
}
if (settings().isMarketingTelemetryEnabled) {
components.analytics.metrics.start(MetricServiceType.Marketing)
} }
setupPush() setupPush()
......
...@@ -86,7 +86,8 @@ class Analytics( ...@@ -86,7 +86,8 @@ class Analytics(
LeanplumMetricsService(context as Application), LeanplumMetricsService(context as Application),
AdjustMetricsService(context) AdjustMetricsService(context)
), ),
isTelemetryEnabled = { context.settings().isTelemetryEnabled } isDataTelemetryEnabled = { context.settings().isTelemetryEnabled },
isMarketingDataTelemetryEnabled = { context.settings().isMarketingTelemetryEnabled }
) )
} }
} }
......
...@@ -17,9 +17,9 @@ import org.mozilla.fenix.ReleaseChannel ...@@ -17,9 +17,9 @@ import org.mozilla.fenix.ReleaseChannel
import org.mozilla.fenix.ext.settings import org.mozilla.fenix.ext.settings
class AdjustMetricsService(private val application: Application) : MetricsService { class AdjustMetricsService(private val application: Application) : MetricsService {
override fun start() { override val type = MetricServiceType.Marketing
if (!application.settings().isMarketingTelemetryEnabled) return
override fun start() {
if ((BuildConfig.ADJUST_TOKEN.isNullOrBlank())) { if ((BuildConfig.ADJUST_TOKEN.isNullOrBlank())) {
Log.i(LOGTAG, "No adjust token defined") Log.i(LOGTAG, "No adjust token defined")
......
...@@ -497,6 +497,8 @@ private val Event.wrapper: EventWrapper<*>? ...@@ -497,6 +497,8 @@ private val Event.wrapper: EventWrapper<*>?
} }
class GleanMetricsService(private val context: Context) : MetricsService { class GleanMetricsService(private val context: Context) : MetricsService {
override val type = MetricServiceType.Data
private val logger = Logger("GleanMetricsService") private val logger = Logger("GleanMetricsService")
private var initialized = false private var initialized = false
/* /*
......
...@@ -53,6 +53,7 @@ class LeanplumMetricsService(private val application: Application) : MetricsServ ...@@ -53,6 +53,7 @@ class LeanplumMetricsService(private val application: Application) : MetricsServ
} }
} }
override val type = MetricServiceType.Marketing
private val token = Token(LeanplumId, LeanplumToken) private val token = Token(LeanplumId, LeanplumToken)
override fun start() { override fun start() {
......
...@@ -408,7 +408,13 @@ private fun Fact.toEvent(): Event? = when (Pair(component, item)) { ...@@ -408,7 +408,13 @@ private fun Fact.toEvent(): Event? = when (Pair(component, item)) {
else -> null else -> null
} }
enum class MetricServiceType {
Data, Marketing;
}
interface MetricsService { interface MetricsService {
val type: MetricServiceType
fun start() fun start()
fun stop() fun stop()
fun track(event: Event) fun track(event: Event)
...@@ -416,24 +422,32 @@ interface MetricsService { ...@@ -416,24 +422,32 @@ interface MetricsService {
} }
interface MetricController { interface MetricController {
fun start() fun start(type: MetricServiceType)
fun stop() fun stop(type: MetricServiceType)
fun track(event: Event) fun track(event: Event)
companion object { companion object {
fun create(services: List<MetricsService>, isTelemetryEnabled: () -> Boolean): MetricController { fun create(
return if (BuildConfig.TELEMETRY) return ReleaseMetricController(services, isTelemetryEnabled) services: List<MetricsService>,
else DebugMetricController() isDataTelemetryEnabled: () -> Boolean,
isMarketingDataTelemetryEnabled: () -> Boolean
): MetricController {
return if (BuildConfig.TELEMETRY) {
ReleaseMetricController(
services,
isDataTelemetryEnabled,
isMarketingDataTelemetryEnabled)
} else DebugMetricController()
} }
} }
} }
private class DebugMetricController : MetricController { private class DebugMetricController : MetricController {
override fun start() { override fun start(type: MetricServiceType) {
Logger.debug("DebugMetricController: start") Logger.debug("DebugMetricController: start")
} }
override fun stop() { override fun stop(type: MetricServiceType) {
Logger.debug("DebugMetricController: stop") Logger.debug("DebugMetricController: stop")
} }
...@@ -444,9 +458,10 @@ private class DebugMetricController : MetricController { ...@@ -444,9 +458,10 @@ private class DebugMetricController : MetricController {
private class ReleaseMetricController( private class ReleaseMetricController(
private val services: List<MetricsService>, private val services: List<MetricsService>,
private val isTelemetryEnabled: () -> Boolean private val isDataTelemetryEnabled: () -> Boolean,
private val isMarketingDataTelemetryEnabled: () -> Boolean
) : MetricController { ) : MetricController {
private var initialized = false private var initialized = mutableSetOf<MetricServiceType>()
init { init {
Facts.registerProcessor(object : FactProcessor { Facts.registerProcessor(object : FactProcessor {
...@@ -458,25 +473,46 @@ private class ReleaseMetricController( ...@@ -458,25 +473,46 @@ private class ReleaseMetricController(
}) })
} }
override fun start() { override fun start(type: MetricServiceType) {
if (!isTelemetryEnabled.invoke() || initialized) { return } val isEnabled = isTelemetryEnabled(type)
val isInitialized = isInitialized(type)
if (!isEnabled || isInitialized) { return }
services
.filter { it.type == type }
.forEach { it.start() }
services.forEach { it.start() } initialized.add(type)
initialized = true
} }
override fun stop() { override fun stop(type: MetricServiceType) {
if (!initialized) { return } val isEnabled = isTelemetryEnabled(type)
val isInitialized = isInitialized(type)
if (isEnabled || !isInitialized) { return }
services.forEach { it.stop() } services
initialized = false .filter { it.type == type }
.forEach { it.stop() }
initialized.remove(type)
} }
override fun track(event: Event) { override fun track(event: Event) {
if (!isTelemetryEnabled.invoke() && !initialized) { return }
services services
.filter { it.shouldTrack(event) } .filter { it.shouldTrack(event) }
.forEach { it.track(event) } .forEach {
val isEnabled = isTelemetryEnabled(it.type)
val isInitialized = isInitialized(it.type)
if (!isEnabled || !isInitialized) { return }
it.track(event)
}
}
private fun isInitialized(type: MetricServiceType): Boolean = initialized.contains(type)
private fun isTelemetryEnabled(type: MetricServiceType): Boolean = when (type) {
MetricServiceType.Data -> isDataTelemetryEnabled()
MetricServiceType.Marketing -> isMarketingDataTelemetryEnabled()
} }
} }
...@@ -9,6 +9,7 @@ import androidx.preference.PreferenceFragmentCompat ...@@ -9,6 +9,7 @@ import androidx.preference.PreferenceFragmentCompat
import androidx.preference.SwitchPreference import androidx.preference.SwitchPreference
import org.mozilla.fenix.Config import org.mozilla.fenix.Config
import org.mozilla.fenix.R import org.mozilla.fenix.R
import org.mozilla.fenix.components.metrics.MetricServiceType
import org.mozilla.fenix.ext.components import org.mozilla.fenix.ext.components
import org.mozilla.fenix.ext.getPreferenceKey import org.mozilla.fenix.ext.getPreferenceKey
import org.mozilla.fenix.ext.settings import org.mozilla.fenix.ext.settings
...@@ -23,12 +24,19 @@ class DataChoicesFragment : PreferenceFragmentCompat() { ...@@ -23,12 +24,19 @@ class DataChoicesFragment : PreferenceFragmentCompat() {
super.onCreate(savedInstanceState) super.onCreate(savedInstanceState)
val context = requireContext() val context = requireContext()
preferenceManager.sharedPreferences.registerOnSharedPreferenceChangeListener(this) { sharedPreferences, key -> preferenceManager.sharedPreferences.registerOnSharedPreferenceChangeListener(this) {
_, key ->
if (key == getPreferenceKey(R.string.pref_key_telemetry)) { if (key == getPreferenceKey(R.string.pref_key_telemetry)) {
if (sharedPreferences.getBoolean(key, context.settings().isTelemetryEnabled)) { if (context.settings().isTelemetryEnabled) {
context.components.analytics.metrics.start() context.components.analytics.metrics.start(MetricServiceType.Data)
} else { } else {
context.components.analytics.metrics.stop() context.components.analytics.metrics.stop(MetricServiceType.Data)
}
} else if (key == getPreferenceKey(R.string.pref_key_marketing_telemetry)) {
if (context.settings().isMarketingTelemetryEnabled) {
context.components.analytics.metrics.start(MetricServiceType.Marketing)
} else {
context.components.analytics.metrics.stop(MetricServiceType.Marketing)
} }
} }
} }
...@@ -51,7 +59,7 @@ class DataChoicesFragment : PreferenceFragmentCompat() { ...@@ -51,7 +59,7 @@ class DataChoicesFragment : PreferenceFragmentCompat() {
onPreferenceChangeListener = SharedPreferenceUpdater() onPreferenceChangeListener = SharedPreferenceUpdater()
} }
findPreference<SwitchPreference>(getPreferenceKey(R.string.pref_key_telemetry))?.apply { findPreference<SwitchPreference>(getPreferenceKey(R.string.pref_key_marketing_telemetry))?.apply {
isChecked = context.settings().isMarketingTelemetryEnabled isChecked = context.settings().isMarketingTelemetryEnabled
val appName = context.getString(R.string.app_name) val appName = context.getString(R.string.app_name)
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment