1
0
Fork 0

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

master
Jeff Boek 3 years ago committed by Mihai Adrian
parent 9f3faa43fe
commit 952c48c8ce
  1. 7
      app/src/main/java/org/mozilla/fenix/FenixApplication.kt
  2. 3
      app/src/main/java/org/mozilla/fenix/components/Analytics.kt
  3. 4
      app/src/main/java/org/mozilla/fenix/components/metrics/AdjustMetricsService.kt
  4. 2
      app/src/main/java/org/mozilla/fenix/components/metrics/GleanMetricsService.kt
  5. 1
      app/src/main/java/org/mozilla/fenix/components/metrics/LeanplumMetricsService.kt
  6. 76
      app/src/main/java/org/mozilla/fenix/components/metrics/Metrics.kt
  7. 18
      app/src/main/java/org/mozilla/fenix/settings/DataChoicesFragment.kt

@ -34,6 +34,7 @@ import mozilla.components.support.rusthttp.RustHttpConfig
import mozilla.components.support.rustlog.RustLog
import mozilla.components.support.webextensions.WebExtensionSupport
import org.mozilla.fenix.components.Components
import org.mozilla.fenix.components.metrics.MetricServiceType
import org.mozilla.fenix.ext.settings
import org.mozilla.fenix.session.NotificationSessionObserver
import org.mozilla.fenix.session.VisibilityLifecycleCallback
@ -132,7 +133,11 @@ open class FenixApplication : LocaleAwareApplication() {
setupLeakCanary()
if (settings().isTelemetryEnabled) {
components.analytics.metrics.start()
components.analytics.metrics.start(MetricServiceType.Data)
}
if (settings().isMarketingTelemetryEnabled) {
components.analytics.metrics.start(MetricServiceType.Marketing)
}
setupPush()

@ -86,7 +86,8 @@ class Analytics(
LeanplumMetricsService(context as Application),
AdjustMetricsService(context)
),
isTelemetryEnabled = { context.settings().isTelemetryEnabled }
isDataTelemetryEnabled = { context.settings().isTelemetryEnabled },
isMarketingDataTelemetryEnabled = { context.settings().isMarketingTelemetryEnabled }
)
}
}

@ -17,9 +17,9 @@ import org.mozilla.fenix.ReleaseChannel
import org.mozilla.fenix.ext.settings
class AdjustMetricsService(private val application: Application) : MetricsService {
override fun start() {
if (!application.settings().isMarketingTelemetryEnabled) return
override val type = MetricServiceType.Marketing
override fun start() {
if ((BuildConfig.ADJUST_TOKEN.isNullOrBlank())) {
Log.i(LOGTAG, "No adjust token defined")

@ -497,6 +497,8 @@ private val Event.wrapper: EventWrapper<*>?
}
class GleanMetricsService(private val context: Context) : MetricsService {
override val type = MetricServiceType.Data
private val logger = Logger("GleanMetricsService")
private var initialized = false
/*

@ -53,6 +53,7 @@ class LeanplumMetricsService(private val application: Application) : MetricsServ
}
}
override val type = MetricServiceType.Marketing
private val token = Token(LeanplumId, LeanplumToken)
override fun start() {

@ -408,7 +408,13 @@ private fun Fact.toEvent(): Event? = when (Pair(component, item)) {
else -> null
}
enum class MetricServiceType {
Data, Marketing;
}
interface MetricsService {
val type: MetricServiceType
fun start()
fun stop()
fun track(event: Event)
@ -416,24 +422,32 @@ interface MetricsService {
}
interface MetricController {
fun start()
fun stop()
fun start(type: MetricServiceType)
fun stop(type: MetricServiceType)
fun track(event: Event)
companion object {
fun create(services: List<MetricsService>, isTelemetryEnabled: () -> Boolean): MetricController {
return if (BuildConfig.TELEMETRY) return ReleaseMetricController(services, isTelemetryEnabled)
else DebugMetricController()
fun create(
services: List<MetricsService>,
isDataTelemetryEnabled: () -> Boolean,
isMarketingDataTelemetryEnabled: () -> Boolean
): MetricController {
return if (BuildConfig.TELEMETRY) {
ReleaseMetricController(
services,
isDataTelemetryEnabled,
isMarketingDataTelemetryEnabled)
} else DebugMetricController()
}
}
}
private class DebugMetricController : MetricController {
override fun start() {
override fun start(type: MetricServiceType) {
Logger.debug("DebugMetricController: start")
}
override fun stop() {
override fun stop(type: MetricServiceType) {
Logger.debug("DebugMetricController: stop")
}
@ -444,9 +458,10 @@ private class DebugMetricController : MetricController {
private class ReleaseMetricController(
private val services: List<MetricsService>,
private val isTelemetryEnabled: () -> Boolean
private val isDataTelemetryEnabled: () -> Boolean,
private val isMarketingDataTelemetryEnabled: () -> Boolean
) : MetricController {
private var initialized = false
private var initialized = mutableSetOf<MetricServiceType>()
init {
Facts.registerProcessor(object : FactProcessor {
@ -458,25 +473,46 @@ private class ReleaseMetricController(
})
}
override fun start() {
if (!isTelemetryEnabled.invoke() || initialized) { return }
override fun start(type: MetricServiceType) {
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 = true
initialized.add(type)
}
override fun stop() {
if (!initialized) { return }
override fun stop(type: MetricServiceType) {
val isEnabled = isTelemetryEnabled(type)
val isInitialized = isInitialized(type)
if (isEnabled || !isInitialized) { return }
services.forEach { it.stop() }
initialized = false
services
.filter { it.type == type }
.forEach { it.stop() }
initialized.remove(type)
}
override fun track(event: Event) {
if (!isTelemetryEnabled.invoke() && !initialized) { return }
services
.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
import androidx.preference.SwitchPreference
import org.mozilla.fenix.Config
import org.mozilla.fenix.R
import org.mozilla.fenix.components.metrics.MetricServiceType
import org.mozilla.fenix.ext.components
import org.mozilla.fenix.ext.getPreferenceKey
import org.mozilla.fenix.ext.settings
@ -23,12 +24,19 @@ class DataChoicesFragment : PreferenceFragmentCompat() {
super.onCreate(savedInstanceState)
val context = requireContext()
preferenceManager.sharedPreferences.registerOnSharedPreferenceChangeListener(this) { sharedPreferences, key ->
preferenceManager.sharedPreferences.registerOnSharedPreferenceChangeListener(this) {
_, key ->
if (key == getPreferenceKey(R.string.pref_key_telemetry)) {
if (sharedPreferences.getBoolean(key, context.settings().isTelemetryEnabled)) {
context.components.analytics.metrics.start()
if (context.settings().isTelemetryEnabled) {
context.components.analytics.metrics.start(MetricServiceType.Data)
} 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() {
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
val appName = context.getString(R.string.app_name)

Loading…
Cancel
Save