1
0
Fork 0

for #11830 created class containing the logic for sending AllStartup telemetry logic

lint check

renamed the intentReceived telemetry to appOpenedAllSource

added comments

removed unused code

moved lifecycle process to AppAllSourceStartTelemetry

moved tracking event out of init function

lint fix

moved appAllStartTelemetry to components

added bit more info about the metrics

added the  onReceivedIntent metric back

minor fix

change discriptions based on the comments frm MR

wrote test cases for AppAllSourceStartTelemetry.kt

lint fix

test case to mock application going background

post rebase:

post rebase:

fixed nit from comments

fixed nit from comments

fixed nit from comments

lint fix

lint fix
master
sraturi 2020-06-29 15:15:12 -07:00 committed by blallo
parent 11df2ff5b5
commit 7e35c37862
Signed by: blallo
GPG Key ID: 0CBE577C9B72DC3F
9 changed files with 223 additions and 12 deletions

View File

@ -8,6 +8,36 @@ no_lint:
- CATEGORY_GENERIC
events:
app_opened_all_startup:
type: event
description: |
A user opened the app to the HomeActivity. The HomeActivity
encompasses the home screen, browser screen, settings screen,
collections and other screens in the nav_graph.
This differs from the app_opened probe because it measures all
startups, not just cold startup. Note: There is a short gap
between the time application goes into background and the time
android reports the application going into the background.
Note: This metric does not cover the following cases:
Case # 1 -> a). open a link(for example, gmail) with in-app
Browser (metric report custom_tab startup) b). press home button
c). open gmail again (which brings us back to in app browser).
Step c will not report startup metric. Case # 2 -> a). open fenix
b). press home button c). launch fenix through app switcher/recent
apps. step c will not report startup type.
extra_keys:
source:
description: |
The method used to open Fenix. Possible values are `app_icon`,
`custom_tab`, `link` or `unknown`
bugs:
- https://github.com/mozilla-mobile/fenix/issues/11830
data_reviews:
- https://github.com/mozilla-mobile/fenix/pull/12114#pullrequestreview-445245341
notification_emails:
- esmyth@mozilla.com
- perf-android-fe@mozilla.com
expires: "2020-12-01"
app_received_intent:
type: event
description: |

View File

@ -201,7 +201,7 @@ open class HomeActivity : LocaleAwareAppCompatActivity(), NavHostActivity {
// record on cold startup
safeIntent
?.let(::getIntentAllSource)
?.also { components.analytics.metrics.track(Event.AppRecievedIntent(it)) }
?.also { components.analytics.metrics.track(Event.AppReceivedIntent(it)) }
}
supportActionBar?.hide()
@ -217,9 +217,15 @@ open class HomeActivity : LocaleAwareAppCompatActivity(), NavHostActivity {
captureSnapshotTelemetryMetrics()
setAppAllStartTelemetry(intent.toSafeIntent())
StartupTimeline.onActivityCreateEndHome(this) // DO NOT MOVE ANYTHING BELOW HERE.
}
protected open fun setAppAllStartTelemetry(safeIntent: SafeIntent) {
components.appAllSourceStartTelemetry.receivedIntentInHomeActivity(safeIntent)
}
@CallSuper
override fun onResume() {
super.onResume()
@ -281,14 +287,15 @@ open class HomeActivity : LocaleAwareAppCompatActivity(), NavHostActivity {
?.also { it.dismissAllowingStateLoss() }
}
// If there is a warm or hot startup, onNewIntent method is always called first.
// Note: This does not work in case of an user sending an intent with ACTION_VIEW
// for example, launch the application, and than use adb to send an intent with
// ACTION_VIEW to open a link. In this case, we will get multiple telemetry events.
intent
.toSafeIntent()
.let(::getIntentAllSource)
?.also { components.analytics.metrics.track(Event.AppRecievedIntent(it)) }
?.also { components.analytics.metrics.track(Event.AppReceivedIntent(it)) }
setAppAllStartTelemetry(intent.toSafeIntent())
}
/**
@ -412,11 +419,11 @@ open class HomeActivity : LocaleAwareAppCompatActivity(), NavHostActivity {
}
}
protected open fun getIntentAllSource(intent: SafeIntent): Event.AppRecievedIntent.Source? {
protected open fun getIntentAllSource(intent: SafeIntent): Event.AppReceivedIntent.Source? {
return when {
intent.isLauncherIntent -> Event.AppRecievedIntent.Source.APP_ICON
intent.action == Intent.ACTION_VIEW -> Event.AppRecievedIntent.Source.LINK
else -> Event.AppRecievedIntent.Source.UNKNOWN
intent.isLauncherIntent -> Event.AppReceivedIntent.Source.APP_ICON
intent.action == Intent.ACTION_VIEW -> Event.AppReceivedIntent.Source.LINK
else -> Event.AppReceivedIntent.Source.UNKNOWN
}
}

View File

@ -18,6 +18,7 @@ import mozilla.components.lib.publicsuffixlist.PublicSuffixList
import mozilla.components.support.migration.state.MigrationStore
import org.mozilla.fenix.BuildConfig
import org.mozilla.fenix.HomeActivity
import org.mozilla.fenix.components.metrics.AppAllSourceStartTelemetry
import org.mozilla.fenix.utils.ClipboardHandler
import org.mozilla.fenix.utils.Mockable
import org.mozilla.fenix.utils.Settings
@ -81,6 +82,8 @@ class Components(private val context: Context) {
}
}
val appAllSourceStartTelemetry by lazy { AppAllSourceStartTelemetry(analytics.metrics) }
@Suppress("MagicNumber")
val addonUpdater by lazy {
DefaultAddonUpdater(context, AddonUpdater.Frequency(12, TimeUnit.HOURS))

View File

@ -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
import android.content.Intent
import androidx.annotation.VisibleForTesting
import androidx.annotation.VisibleForTesting.PRIVATE
import androidx.lifecycle.Lifecycle
import androidx.lifecycle.LifecycleObserver
import androidx.lifecycle.OnLifecycleEvent
import androidx.lifecycle.ProcessLifecycleOwner
import mozilla.components.support.utils.SafeIntent
/**
* Tracks how the application was opened through [Event.AppOpenedAllSourceStartup].
* We only considered to be "opened" if it received an intent and the app was in the background.
*/
class AppAllSourceStartTelemetry(private val metrics: MetricController) : LifecycleObserver {
// default value is true to capture the first launch of the application
private var wasApplicationInBackground = true
init {
ProcessLifecycleOwner.get().lifecycle.addObserver(this)
}
fun receivedIntentInExternalAppBrowserActivity(safeIntent: SafeIntent) {
setAppOpenedAllSourceFromIntent(safeIntent, true)
}
fun receivedIntentInHomeActivity(safeIntent: SafeIntent) {
setAppOpenedAllSourceFromIntent(safeIntent, false)
}
private fun setAppOpenedAllSourceFromIntent(intent: SafeIntent, isExternalAppBrowserActivity: Boolean) {
if (!wasApplicationInBackground) {
return
}
val source = when {
isExternalAppBrowserActivity -> Event.AppOpenedAllSourceStartup.Source.CUSTOM_TAB
intent.isLauncherIntent -> Event.AppOpenedAllSourceStartup.Source.APP_ICON
intent.action == Intent.ACTION_VIEW -> Event.AppOpenedAllSourceStartup.Source.LINK
else -> Event.AppOpenedAllSourceStartup.Source.UNKNOWN
}
metrics.track(Event.AppOpenedAllSourceStartup(source))
wasApplicationInBackground = false
}
@OnLifecycleEvent(Lifecycle.Event.ON_STOP)
@VisibleForTesting(otherwise = PRIVATE)
fun onApplicationOnStop() {
wasApplicationInBackground = true
}
}

View File

@ -98,10 +98,14 @@ private val Event.wrapper: EventWrapper<*>?
{ Events.appOpened.record(it) },
{ Events.appOpenedKeys.valueOf(it) }
)
is Event.AppRecievedIntent -> EventWrapper(
is Event.AppReceivedIntent -> EventWrapper(
{ Events.appReceivedIntent.record(it) },
{ Events.appReceivedIntentKeys.valueOf(it) }
)
is Event.AppOpenedAllSourceStartup -> EventWrapper(
{ Events.appOpenedAllStartup.record(it) },
{ Events.appOpenedAllStartupKeys.valueOf(it) }
)
is Event.SearchBarTapped -> EventWrapper(
{ Events.searchBarTapped.record(it) },
{ Events.searchBarTappedKeys.valueOf(it) }

View File

@ -312,11 +312,19 @@ sealed class Event {
get() = hashMapOf(Events.appOpenedKeys.source to source.name)
}
data class AppRecievedIntent(val source: Source) : Event() {
data class AppReceivedIntent(val source: Source) : Event() {
enum class Source { APP_ICON, LINK, CUSTOM_TAB, UNKNOWN }
override val extras: Map<Events.appReceivedIntentKeys, String>?
get() = hashMapOf(Events.appReceivedIntentKeys.source to source.name)
override val extras: Map<Events.appOpenedAllStartupKeys, String>?
get() = hashMapOf(Events.appOpenedAllStartupKeys.source to source.name)
}
data class AppOpenedAllSourceStartup(val source: Source) : Event() {
enum class Source { APP_ICON, LINK, CUSTOM_TAB, UNKNOWN }
override val extras: Map<Events.appOpenedAllStartupKeys, String>?
get() = hashMapOf(Events.appOpenedAllStartupKeys.source to source.name)
}
data class CollectionSaveButtonPressed(val fromScreen: String) : Event() {

View File

@ -41,10 +41,14 @@ open class ExternalAppBrowserActivity : HomeActivity() {
final override fun getIntentSource(intent: SafeIntent) = Event.OpenedApp.Source.CUSTOM_TAB
final override fun getIntentAllSource(intent: SafeIntent) = Event.AppRecievedIntent.Source.CUSTOM_TAB
final override fun getIntentAllSource(intent: SafeIntent) = Event.AppReceivedIntent.Source.CUSTOM_TAB
final override fun getIntentSessionId(intent: SafeIntent) = intent.getSessionId()
override fun setAppAllStartTelemetry(safeIntent: SafeIntent) {
components.appAllSourceStartTelemetry.receivedIntentInExternalAppBrowserActivity(safeIntent)
}
override fun getNavDirections(
from: BrowserDirection,
customTabSessionId: String?

View File

@ -0,0 +1,95 @@
/* 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.Intent
import io.mockk.MockKAnnotations
import io.mockk.every
import io.mockk.impl.annotations.RelaxedMockK
import io.mockk.verify
import mozilla.components.support.utils.toSafeIntent
import org.junit.Before
import org.junit.Test
class AppAllSourceStartTelemetryTest {
@RelaxedMockK
private lateinit var metricController: MetricController
@RelaxedMockK
private lateinit var intent: Intent
private lateinit var appAllSourceStartTelemetry: AppAllSourceStartTelemetry
@Before
fun setup() {
MockKAnnotations.init(this)
appAllSourceStartTelemetry = AppAllSourceStartTelemetry(metricController)
}
@Test
fun `WHEN a main launcher intent is received in HomeActivity THEN an app start metric is recorded from app_icon`() {
every { intent.action } returns Intent.ACTION_MAIN
every { intent.categories.contains(Intent.CATEGORY_LAUNCHER) } returns true
appAllSourceStartTelemetry.receivedIntentInHomeActivity(intent.toSafeIntent())
val validSource = Event.AppOpenedAllSourceStartup.Source.APP_ICON
verify(exactly = 1) { metricController.track(Event.AppOpenedAllSourceStartup(validSource)) }
}
@Test
fun `WHEN a VIEW intent is received in HomeActivity THEN an app start metric is recorded from link`() {
every { intent.action } returns Intent.ACTION_VIEW
appAllSourceStartTelemetry.receivedIntentInHomeActivity(intent.toSafeIntent())
val validSource = Event.AppOpenedAllSourceStartup.Source.LINK
verify(exactly = 1) { metricController.track(Event.AppOpenedAllSourceStartup(validSource)) }
}
@Test
fun `WHEN a intent is received in ExternalAppBrowserActivity THEN an app start metric is recorded from custom_tab`() {
val intent = Intent()
appAllSourceStartTelemetry.receivedIntentInExternalAppBrowserActivity(intent.toSafeIntent())
val validSource = Event.AppOpenedAllSourceStartup.Source.CUSTOM_TAB
verify(exactly = 1) { metricController.track(Event.AppOpenedAllSourceStartup(validSource)) }
}
@Test
fun `GIVEN an app is in the foreground WHEN an intent is received THEN no startup metric is recorded`() {
appAllSourceStartTelemetry.receivedIntentInHomeActivity(intent.toSafeIntent())
appAllSourceStartTelemetry.receivedIntentInHomeActivity(intent.toSafeIntent())
verify(exactly = 1) { metricController.track(any()) }
}
@Test
fun `WHEN application goes in background and comes foreground, THEN an app start metric is recorded`() {
// first startup
appAllSourceStartTelemetry.receivedIntentInHomeActivity(intent.toSafeIntent())
// mock application going in the background
appAllSourceStartTelemetry.onApplicationOnStop()
appAllSourceStartTelemetry.receivedIntentInHomeActivity(intent.toSafeIntent())
verify(exactly = 2) { metricController.track(any()) }
}
@Test
fun `WHEN an intent received in HomeActivity is not launcher or does not have VIEW action, THEN an app start is recorded from unknown`() {
every { intent.action } returns Intent.ACTION_MAIN
appAllSourceStartTelemetry.receivedIntentInHomeActivity(intent.toSafeIntent())
val validSource = Event.AppOpenedAllSourceStartup.Source.UNKNOWN
verify(exactly = 1) { metricController.track(Event.AppOpenedAllSourceStartup(validSource)) }
}
}

File diff suppressed because one or more lines are too long