[fenix] Fixes https://github.com/mozilla-mobile/fenix/issues/965 - Adds a metrics wrapper for telemetry
parent
7916052eae
commit
2a3b9dac0c
@ -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)
|
||||
}
|
||||
}
|
@ -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