This repository has been archived by the owner on Feb 20, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
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
- Loading branch information
Showing
9 changed files
with
223 additions
and
12 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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: | ||
- [email protected] | ||
- [email protected] | ||
expires: "2020-12-01" | ||
app_received_intent: | ||
type: event | ||
description: | | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
59 changes: 59 additions & 0 deletions
59
app/src/main/java/org/mozilla/fenix/components/metrics/AppAllSourceStartTelemetry.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
95 changes: 95 additions & 0 deletions
95
app/src/test/java/org/mozilla/fenix/components/metrics/AppAllSourceStartTelemetryTest.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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)) } | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters