Skip to content

Commit

Permalink
for mozilla-mobile#11830 added new metric for collecting startup method
Browse files Browse the repository at this point in the history
for mozilla-mobile#11830 collecting startup metric inaside onNewIntent in HomeActivity

clean up, added comments

nitpicks cleanup

move all source startup telemetry into its own logic and added an UNKOWN state

nit cleanup

minor changes
  • Loading branch information
sraturi committed Jun 26, 2020
1 parent f8cb1d6 commit 742f061
Show file tree
Hide file tree
Showing 5 changed files with 92 additions and 0 deletions.
17 changes: 17 additions & 0 deletions app/metrics.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,23 @@ no_lint:
- CATEGORY_GENERIC

events:
app_opened_all_startup:
type: event
description: |
A user opened the app from any one of the following flows:
hot, warm or cold start to the HomeActivity.
extra_keys:
source:
description: |
The method used to open Fenix. Possible values are `app_icon`,
`custom_tab` or `link` or `unknown`
bugs:
- https://github.com/mozilla-mobile/fenix/issues/11830
data_reviews:
- https://github.com/mozilla-mobile/fenix/pull/11940/
notification_emails:
- [email protected]
expires: "2020-12-01"
app_opened:
type: event
description: |
Expand Down
13 changes: 13 additions & 0 deletions app/src/main/java/org/mozilla/fenix/HomeActivity.kt
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ import org.mozilla.fenix.browser.UriOpenedObserver
import org.mozilla.fenix.browser.browsingmode.BrowsingMode
import org.mozilla.fenix.browser.browsingmode.BrowsingModeManager
import org.mozilla.fenix.browser.browsingmode.DefaultBrowsingModeManager
import org.mozilla.fenix.components.metrics.AllSourceStartupTelemetry
import org.mozilla.fenix.components.metrics.BreadcrumbsRecorder
import org.mozilla.fenix.components.metrics.Event
import org.mozilla.fenix.exceptions.ExceptionsFragmentDirections
Expand Down Expand Up @@ -133,6 +134,7 @@ open class HomeActivity : LocaleAwareAppCompatActivity() {
}

private lateinit var navigationToolbar: Toolbar
private lateinit var allSourceStartupTelemetry: AllSourceStartupTelemetry

final override fun onCreate(savedInstanceState: Bundle?) {
StrictModeManager.changeStrictModePolicies(supportFragmentManager)
Expand Down Expand Up @@ -193,6 +195,16 @@ open class HomeActivity : LocaleAwareAppCompatActivity() {
}

captureSnapshotTelemetryMetrics()
allSourceStartupTelemetry =
AllSourceStartupTelemetry(
intent,
components.analytics.metrics
)
}

override fun onStart() {
super.onStart()
allSourceStartupTelemetry.onStart()
}

@CallSuper
Expand Down Expand Up @@ -250,6 +262,7 @@ open class HomeActivity : LocaleAwareAppCompatActivity() {
?.let { it as? TabTrayDialogFragment }
?.also { it.dismissAllowingStateLoss() }
}
allSourceStartupTelemetry.onNewIntent(intent)
}

/**
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
/* 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 mozilla.components.support.utils.SafeIntent
import mozilla.components.support.utils.toSafeIntent
import org.mozilla.fenix.customtabs.ExternalAppBrowserActivity

/**
* Tracks how the application was opened through [Event.OpenedAppAllStart]
* Since OnNewIntent() is always called before onStart() (in a warm/hot startup), we update
* our current intent with the new intent. However we only track the event during onStart()
* Because we dont want random intents with [Intent.ACTION_VIEW] to be recorded as app_opened
* For example, Start app through APP_ICON -> run adb intent command to open a link.
* We want to ignore these adb commands since app is already on foreground.
*/
open class AllSourceStartupTelemetry(private var currentIntent: Intent?, val metrics: MetricController) {

/**
* Called from activity's onStart() function. records the OpenedAppALlStart telemetry.
*/
fun onStart() {
currentIntent?.toSafeIntent()
?.let(::getIntentAllStartSource)
?.also {
metrics.track(Event.OpenedAppAllStart(it))
}
}

/**
* Called from Activity's onNewIntent() function. sets our currentIntent with the latest one
* received by activity's onNewIntent() function.
*/
fun onNewIntent(intent: Intent?) {
currentIntent = intent
}

open fun getIntentAllStartSource(intent: SafeIntent): Event.OpenedAppAllStart.Source? {
val callingActivityName: String = intent.unsafe.component?.className.toString()
return when {
callingActivityName == ExternalAppBrowserActivity::class.java.name
-> Event.OpenedAppAllStart.Source.CUSTOM_TAB
intent.isLauncherIntent -> Event.OpenedAppAllStart.Source.APP_ICON
intent.action == Intent.ACTION_VIEW -> Event.OpenedAppAllStart.Source.LINK
else -> Event.OpenedAppAllStart.Source.UNKNOWN
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,10 @@ private val Event.wrapper: EventWrapper<*>?
{ Events.appOpened.record(it) },
{ Events.appOpenedKeys.valueOf(it) }
)
is Event.OpenedAppAllStart -> EventWrapper(
{ Events.appOpenedAllStartup.record(it) },
{ Events.appOpenedAllStartupKeys.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 @@ -296,6 +296,13 @@ sealed class Event {
get() = hashMapOf(Events.appOpenedKeys.source to source.name)
}

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

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

0 comments on commit 742f061

Please sign in to comment.