Skip to content

Commit

Permalink
For mozilla-mobile#24210: Remove wrapper from "app opened" event.
Browse files Browse the repository at this point in the history
  • Loading branch information
mcarare committed Mar 30, 2022
1 parent a1243a8 commit 5ce2c65
Show file tree
Hide file tree
Showing 8 changed files with 12 additions and 48 deletions.
1 change: 1 addition & 0 deletions app/metrics.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ events:
description: |
The method used to open Fenix. Possible values are: `app_icon`,
`custom_tab` or `link`
type: string
bugs:
- https://github.com/mozilla-mobile/fenix/issues/968
- https://github.com/mozilla-mobile/fenix/issues/10616
Expand Down
9 changes: 5 additions & 4 deletions app/src/main/java/org/mozilla/fenix/HomeActivity.kt
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,7 @@ import mozilla.components.support.locale.LocaleAwareAppCompatActivity
import mozilla.components.support.utils.SafeIntent
import mozilla.components.support.utils.toSafeIntent
import mozilla.components.support.webextensions.WebExtensionPopupFeature
import org.mozilla.fenix.GleanMetrics.Events
import org.mozilla.fenix.GleanMetrics.Metrics
import org.mozilla.fenix.addons.AddonDetailsFragmentDirections
import org.mozilla.fenix.addons.AddonPermissionsDetailsFragmentDirections
Expand Down Expand Up @@ -258,7 +259,7 @@ open class HomeActivity : LocaleAwareAppCompatActivity(), NavHostActivity {
val safeIntent = intent?.toSafeIntent()
safeIntent
?.let(::getIntentSource)
?.also { components.analytics.metrics.track(Event.OpenedApp(it)) }
?.also { Events.appOpened.record(Events.AppOpenedExtra(it)) }
}
supportActionBar?.hide()

Expand Down Expand Up @@ -667,10 +668,10 @@ open class HomeActivity : LocaleAwareAppCompatActivity(), NavHostActivity {
}

@VisibleForTesting(otherwise = PROTECTED)
internal open fun getIntentSource(intent: SafeIntent): Event.OpenedApp.Source? {
internal open fun getIntentSource(intent: SafeIntent): String? {
return when {
intent.isLauncherIntent -> Event.OpenedApp.Source.APP_ICON
intent.action == Intent.ACTION_VIEW -> Event.OpenedApp.Source.LINK
intent.isLauncherIntent -> "APP_ICON"
intent.action == Intent.ACTION_VIEW -> "LINK"
else -> null
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -365,13 +365,6 @@ sealed class Event {
get() = hashMapOf(Logins.saveLoginsSettingChangedKeys.setting to setting.name)
}

data class OpenedApp(val source: Source) : Event() {
enum class Source { APP_ICON, LINK, CUSTOM_TAB }

override val extras: Map<Events.appOpenedKeys, String>?
get() = hashMapOf(Events.appOpenedKeys.source to source.name)
}

data class CollectionSaveButtonPressed(val fromScreen: String) : Event() {
override val extras: Map<Collections.saveButtonKeys, String>?
get() = mapOf(Collections.saveButtonKeys.fromScreen to fromScreen)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -96,10 +96,6 @@ private class EventWrapper<T : Enum<T>>(
// FIXME(#19967): Migrate to non-deprecated API.
private val Event.wrapper: EventWrapper<*>?
get() = when (this) {
is Event.OpenedApp -> EventWrapper(
{ Events.appOpened.record(it) },
{ Events.appOpenedKeys.valueOf(it) }
)
is Event.SearchBarTapped -> EventWrapper(
{ Events.searchBarTapped.record(it) },
{ Events.searchBarTappedKeys.valueOf(it) }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ import mozilla.components.support.utils.SafeIntent
import org.mozilla.fenix.BrowserDirection
import org.mozilla.fenix.HomeActivity
import org.mozilla.fenix.NavGraphDirections
import org.mozilla.fenix.components.metrics.Event
import org.mozilla.fenix.ext.components
import java.security.InvalidParameterException

Expand Down Expand Up @@ -45,7 +44,7 @@ open class ExternalAppBrowserActivity : HomeActivity() {
return "Changing to fragment $fragmentName, isCustomTab: true"
}

final override fun getIntentSource(intent: SafeIntent) = Event.OpenedApp.Source.CUSTOM_TAB
final override fun getIntentSource(intent: SafeIntent) = "CUSTOM_TAB"

final override fun getIntentSessionId(intent: SafeIntent) = intent.getSessionId()

Expand Down
5 changes: 2 additions & 3 deletions app/src/test/java/org/mozilla/fenix/HomeActivityTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@ import org.junit.runner.RunWith
import org.mozilla.fenix.HomeActivity.Companion.PRIVATE_BROWSING_MODE
import org.mozilla.fenix.browser.browsingmode.BrowsingMode
import org.mozilla.fenix.browser.browsingmode.BrowsingModeManager
import org.mozilla.fenix.components.metrics.Event
import org.mozilla.fenix.ext.components
import org.mozilla.fenix.ext.settings
import org.mozilla.fenix.helpers.FenixRobolectricTestRunner
Expand All @@ -50,10 +49,10 @@ class HomeActivityTest {
val launcherIntent = Intent(Intent.ACTION_MAIN).apply {
addCategory(Intent.CATEGORY_LAUNCHER)
}.toSafeIntent()
assertEquals(Event.OpenedApp.Source.APP_ICON, activity.getIntentSource(launcherIntent))
assertEquals("APP_ICON", activity.getIntentSource(launcherIntent))

val viewIntent = Intent(Intent.ACTION_VIEW).toSafeIntent()
assertEquals(Event.OpenedApp.Source.LINK, activity.getIntentSource(viewIntent))
assertEquals("LINK", activity.getIntentSource(viewIntent))

val otherIntent = Intent().toSafeIntent()
assertNull(activity.getIntentSource(otherIntent))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,30 +37,6 @@ class GleanMetricsServiceTest {
gleanService = GleanMetricsService(testContext)
}

@Test
fun `the app_opened event is correctly recorded`() {
// Build the event wrapper used by Fenix.
val event = Event.OpenedApp(Event.OpenedApp.Source.APP_ICON)

// Feed the wrapped event in the Glean service.
gleanService.track(event)

// Use the testing API to verify that it's correctly recorded.
assertTrue(Events.appOpened.testHasValue())

// Get all the recorded events. We only expect 1 to be recorded.
val events = Events.appOpened.testGetValue()
assertEquals(1, events.size)

// Verify that we get the expected content out.
assertEquals("events", events[0].category)
assertEquals("app_opened", events[0].name)

// We only expect 1 extra key.
assertEquals(1, events[0].extra!!.size)
assertEquals("APP_ICON", events[0].extra!!["source"])
}

@Test
fun `synced tab event is correctly recorded`() {
assertFalse(SyncedTabs.syncedTabsSuggestionClicked.testHasValue())
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,6 @@ import org.junit.runner.RunWith
import org.mozilla.fenix.BrowserDirection
import org.mozilla.fenix.browser.browsingmode.BrowsingMode
import org.mozilla.fenix.browser.browsingmode.BrowsingModeManager
import org.mozilla.fenix.components.metrics.Event
import org.mozilla.fenix.ext.components
import org.mozilla.fenix.helpers.FenixRobolectricTestRunner
import org.mozilla.fenix.utils.Settings
Expand All @@ -43,13 +42,13 @@ class ExternalAppBrowserActivityTest {
val launcherIntent = Intent(Intent.ACTION_MAIN).apply {
addCategory(Intent.CATEGORY_LAUNCHER)
}.toSafeIntent()
assertEquals(Event.OpenedApp.Source.CUSTOM_TAB, activity.getIntentSource(launcherIntent))
assertEquals("CUSTOM_TAB", activity.getIntentSource(launcherIntent))

val viewIntent = Intent(Intent.ACTION_VIEW).toSafeIntent()
assertEquals(Event.OpenedApp.Source.CUSTOM_TAB, activity.getIntentSource(viewIntent))
assertEquals("CUSTOM_TAB", activity.getIntentSource(viewIntent))

val otherIntent = Intent().toSafeIntent()
assertEquals(Event.OpenedApp.Source.CUSTOM_TAB, activity.getIntentSource(otherIntent))
assertEquals("CUSTOM_TAB", activity.getIntentSource(otherIntent))
}

@Test
Expand Down

0 comments on commit 5ce2c65

Please sign in to comment.