2019-03-12 21:09:04 +01:00
|
|
|
/* 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
|
2019-04-09 17:57:50 +02:00
|
|
|
import mozilla.components.service.glean.private.NoExtraKeys
|
2019-03-18 21:30:25 +01:00
|
|
|
import mozilla.components.support.utils.Browsers
|
2019-03-12 21:09:04 +01:00
|
|
|
import org.mozilla.fenix.BuildConfig
|
2019-03-27 03:08:16 +01:00
|
|
|
import org.mozilla.fenix.GleanMetrics.CrashReporter
|
|
|
|
import org.mozilla.fenix.GleanMetrics.Events
|
|
|
|
import org.mozilla.fenix.GleanMetrics.FindInPage
|
|
|
|
import org.mozilla.fenix.GleanMetrics.ContextMenu
|
2019-04-06 00:08:55 +02:00
|
|
|
import org.mozilla.fenix.GleanMetrics.QuickActionSheet
|
2019-03-25 17:35:58 +01:00
|
|
|
import org.mozilla.fenix.GleanMetrics.Metrics
|
2019-03-27 02:36:58 +01:00
|
|
|
import org.mozilla.fenix.utils.Settings
|
2019-03-12 21:09:04 +01:00
|
|
|
|
2019-03-22 07:05:28 +01:00
|
|
|
private class EventWrapper<T : Enum<T>>(
|
2019-04-02 14:31:30 +02:00
|
|
|
private val recorder: ((Map<T, String>?) -> Unit),
|
2019-04-03 01:47:11 +02:00
|
|
|
private val keyMapper: ((String) -> T)? = null
|
2019-03-22 07:05:28 +01:00
|
|
|
) {
|
2019-03-26 17:13:59 +01:00
|
|
|
private val String.asCamelCase: String
|
|
|
|
get() = this.split("_").reduceIndexed { index, acc, s ->
|
|
|
|
if (index == 0) acc + s
|
|
|
|
else acc + s.capitalize()
|
|
|
|
}
|
|
|
|
|
2019-03-22 07:05:28 +01:00
|
|
|
fun track(event: Event) {
|
|
|
|
val extras = if (keyMapper != null) {
|
2019-03-26 17:13:59 +01:00
|
|
|
event.extras?.mapKeys { keyMapper.invoke(it.key.asCamelCase) }
|
2019-03-22 07:05:28 +01:00
|
|
|
} else {
|
|
|
|
null
|
|
|
|
}
|
|
|
|
|
2019-04-02 14:31:30 +02:00
|
|
|
this.recorder(extras)
|
2019-03-22 07:05:28 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private val Event.wrapper
|
2019-03-19 04:49:17 +01:00
|
|
|
get() = when (this) {
|
2019-04-02 14:31:30 +02:00
|
|
|
is Event.OpenedApp -> EventWrapper(
|
|
|
|
{ Events.appOpened.record(it) },
|
|
|
|
{ Events.appOpenedKeys.valueOf(it) }
|
|
|
|
)
|
|
|
|
is Event.SearchBarTapped -> EventWrapper(
|
|
|
|
{ Events.searchBarTapped.record(it) },
|
|
|
|
{ Events.searchBarTappedKeys.valueOf(it) }
|
|
|
|
)
|
|
|
|
is Event.EnteredUrl -> EventWrapper(
|
|
|
|
{ Events.enteredUrl.record(it) },
|
|
|
|
{ Events.enteredUrlKeys.valueOf(it) }
|
|
|
|
)
|
|
|
|
is Event.PerformedSearch -> EventWrapper(
|
|
|
|
{ Events.performedSearch.record(it) },
|
|
|
|
{ Events.performedSearchKeys.valueOf(it) }
|
|
|
|
)
|
2019-04-03 01:47:11 +02:00
|
|
|
is Event.FindInPageOpened -> EventWrapper<NoExtraKeys>(
|
|
|
|
{ FindInPage.opened.record(it) }
|
|
|
|
)
|
|
|
|
is Event.FindInPageClosed -> EventWrapper<NoExtraKeys>(
|
|
|
|
{ FindInPage.closed.record(it) }
|
|
|
|
)
|
|
|
|
is Event.FindInPageNext -> EventWrapper<NoExtraKeys>(
|
|
|
|
{ FindInPage.nextResult.record(it) }
|
|
|
|
)
|
2019-04-03 03:18:14 +02:00
|
|
|
is Event.FindInPagePrevious -> EventWrapper<NoExtraKeys>(
|
2019-04-03 01:47:11 +02:00
|
|
|
{ FindInPage.previousResult.record(it) }
|
|
|
|
)
|
|
|
|
is Event.FindInPageSearchCommitted -> EventWrapper<NoExtraKeys>(
|
|
|
|
{ FindInPage.searchedPage.record(it) }
|
|
|
|
)
|
2019-04-03 03:18:14 +02:00
|
|
|
is Event.ContextMenuItemTapped -> EventWrapper(
|
|
|
|
{ ContextMenu.itemTapped.record(it) },
|
|
|
|
{ ContextMenu.itemTappedKeys.valueOf(it) }
|
|
|
|
)
|
2019-03-27 02:36:58 +01:00
|
|
|
is Event.CrashReporterOpened -> EventWrapper<NoExtraKeys>(
|
|
|
|
{ CrashReporter.opened }
|
|
|
|
)
|
|
|
|
is Event.CrashReporterClosed -> EventWrapper(
|
|
|
|
{ CrashReporter.closed },
|
|
|
|
{ CrashReporter.closedKeys.valueOf(it) }
|
|
|
|
)
|
2019-03-27 03:08:16 +01:00
|
|
|
is Event.BrowserMenuItemTapped -> EventWrapper(
|
|
|
|
{ Events.browserMenuAction },
|
|
|
|
{ Events.browserMenuActionKeys.valueOf(it) }
|
|
|
|
)
|
2019-04-06 00:08:55 +02:00
|
|
|
is Event.QuickActionSheetOpened -> EventWrapper<NoExtraKeys>(
|
|
|
|
{ QuickActionSheet.opened.record(it) }
|
|
|
|
)
|
|
|
|
is Event.QuickActionSheetClosed -> EventWrapper<NoExtraKeys>(
|
|
|
|
{ QuickActionSheet.closed.record(it) }
|
|
|
|
)
|
|
|
|
is Event.QuickActionSheetShareTapped -> EventWrapper<NoExtraKeys>(
|
|
|
|
{ QuickActionSheet.shareTapped.record(it) }
|
|
|
|
)
|
|
|
|
is Event.QuickActionSheetBookmarkTapped -> EventWrapper<NoExtraKeys>(
|
|
|
|
{ QuickActionSheet.bookmarkTapped.record(it) }
|
|
|
|
)
|
|
|
|
is Event.QuickActionSheetDownloadTapped -> EventWrapper<NoExtraKeys>(
|
|
|
|
{ QuickActionSheet.downloadTapped.record(it) }
|
|
|
|
)
|
|
|
|
is Event.QuickActionSheetReadTapped -> EventWrapper<NoExtraKeys>(
|
|
|
|
{ QuickActionSheet.readTapped.record(it) }
|
|
|
|
)
|
2019-03-27 02:36:58 +01:00
|
|
|
|
2019-04-03 01:47:11 +02:00
|
|
|
// Don't track other events with Glean
|
2019-03-18 22:54:36 +01:00
|
|
|
else -> null
|
|
|
|
}
|
|
|
|
|
2019-03-12 21:09:04 +01:00
|
|
|
class GleanMetricsService(private val context: Context) : MetricsService {
|
|
|
|
override fun start() {
|
|
|
|
Glean.initialize(context)
|
|
|
|
Glean.setUploadEnabled(IsGleanEnabled)
|
2019-03-18 21:30:25 +01:00
|
|
|
|
|
|
|
Metrics.apply {
|
|
|
|
defaultBrowser.set(Browsers.all(context).isDefaultBrowser)
|
|
|
|
}
|
2019-03-12 21:09:04 +01:00
|
|
|
}
|
|
|
|
|
2019-04-03 20:59:08 +02:00
|
|
|
override fun stop() {
|
|
|
|
Glean.setUploadEnabled(false)
|
|
|
|
}
|
|
|
|
|
2019-03-18 22:32:03 +01:00
|
|
|
override fun track(event: Event) {
|
2019-03-22 07:05:28 +01:00
|
|
|
event.wrapper?.track(event)
|
2019-03-18 22:32:03 +01:00
|
|
|
}
|
2019-03-12 21:09:04 +01:00
|
|
|
|
2019-03-18 22:32:03 +01:00
|
|
|
override fun shouldTrack(event: Event): Boolean {
|
2019-03-22 07:05:28 +01:00
|
|
|
return Settings.getInstance(context).isTelemetryEnabled && event.wrapper != null
|
2019-03-18 22:32:03 +01:00
|
|
|
}
|
2019-03-12 21:09:04 +01:00
|
|
|
|
|
|
|
companion object {
|
|
|
|
private const val IsGleanEnabled = BuildConfig.TELEMETRY
|
|
|
|
}
|
|
|
|
}
|