Commit 6eedf876 authored by MozLando's avatar MozLando
Browse files

Merge #7493

7493: mozilla-mobile/fenix#10261: Add facts for PWAs r=NotWoods a=eliserichards

Add PWA Facts for https://github.com/mozilla-mobile/fenix/issues/10261


  * An event ping when putting a PWA to the foreground with a time interval from start to finish use.
    * `ProgressiveWebAppFacts.Items.DURATION_IN_BACKGROUND`
  * An event ping when tapping PWA homescreen icons.
    * `ProgressiveWebAppFacts.Items.HOMESCREEN_ICON_TAP`
Co-authored-by: default avatarElise Richards <erichards@mozilla.com>
parents 6eb059b9 1b5daa9c
......@@ -59,6 +59,22 @@ When `"display": "fullscreen"` is set in the web app manifest, the web app will
`WebAppSiteControlsFeature` will display a silent notification whenever a web app is open. This notification contains controls to interact with the web app, such as a refresh button and a shortcut to copy the URL.
## Facts
This component emits the following [Facts](../../support/base/README.md#Facts):
| Action | Item | Extras | Description |
|--------|---------|----------------|------------------------------------|
| CLICK | homescreen_icon_tap | | The user tapped the PWA icon on the homescreen. |
| INTERACTION | enter_background | `itemExtras` | The current system time when the app is backgrounded. |
| INTERACTION | enter_foreground | `itemExtras` | The current system time when the app is foregrounded. |
#### `itemExtras`
| Key | Type | Value |
|--------------|---------|-----------------------------------|
| timingNs | Long | The current system time when a foreground or background action is taken. |
## License
This Source Code Form is subject to the terms of the Mozilla Public
......
/* 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 mozilla.components.feature.pwa
import mozilla.components.feature.pwa.ProgressiveWebAppFacts.Companion.MS_PRECISION
import mozilla.components.support.base.Component
import mozilla.components.support.base.facts.Action
import mozilla.components.support.base.facts.Fact
import mozilla.components.support.base.facts.collect
/**
* Facts emitted for telemetry related to [PwaFeature]
*/
class ProgressiveWebAppFacts {
/**
* Items that specify which portion of the [PwaFeature] was interacted with
*/
object Items {
const val HOMESCREEN_ICON_TAP = "homescreen_icon_tap"
const val ENTER_BACKGROUND = "enter_background"
const val ENTER_FOREGROUND = "enter_foreground"
}
/**
* Keys used to record metadata about [Items].
*/
object MetadataKeys {
const val BACKGROUND_TIME = "background_time"
const val FOREGROUND_TIME = "foreground_time"
}
companion object {
// We only care about millisecond precision in this context
internal const val MS_PRECISION = 1_000_000L
}
}
private fun emitPwaFact(
action: Action,
item: String,
value: String? = null,
metadata: Map<String, Any>? = null
) {
Fact(
Component.FEATURE_PWA,
action,
item,
value,
metadata
).collect()
}
internal fun emitHomescreenIconTapFact() =
emitPwaFact(
Action.CLICK,
ProgressiveWebAppFacts.Items.HOMESCREEN_ICON_TAP
)
internal fun emitForegroundTimingFact(timingNs: Long) =
emitPwaFact(
Action.INTERACTION,
ProgressiveWebAppFacts.Items.ENTER_FOREGROUND,
metadata = mapOf(
ProgressiveWebAppFacts.MetadataKeys.FOREGROUND_TIME to (timingNs / MS_PRECISION)
)
)
internal fun emitBackgroundTimingFact(timingNs: Long) =
emitPwaFact(
Action.INTERACTION,
ProgressiveWebAppFacts.Items.ENTER_BACKGROUND,
metadata = mapOf(
ProgressiveWebAppFacts.MetadataKeys.BACKGROUND_TIME to (timingNs / MS_PRECISION)
)
)
......@@ -8,6 +8,7 @@ import android.content.ActivityNotFoundException
import android.content.Intent
import android.net.Uri
import android.os.Bundle
import android.os.SystemClock
import androidx.annotation.VisibleForTesting
import androidx.annotation.VisibleForTesting.PRIVATE
import androidx.appcompat.app.AppCompatActivity
......@@ -21,8 +22,8 @@ import mozilla.components.support.base.log.logger.Logger
/**
* This activity is launched by Web App shortcuts on the home screen.
*
* Based on the Web App Manifest (display) it will decide whether the app is launched in the browser or in a
* standalone activity.
* Based on the Web App Manifest (display) it will decide whether the app is launched in the
* browser or in a standalone activity.
*/
class WebAppLauncherActivity : AppCompatActivity() {
......@@ -44,6 +45,18 @@ class WebAppLauncherActivity : AppCompatActivity() {
}
}
override fun onResume() {
super.onResume()
val currTime = SystemClock.elapsedRealtimeNanos()
emitForegroundTimingFact(currTime)
}
override fun onPause() {
super.onPause()
val currTime = SystemClock.elapsedRealtimeNanos()
emitBackgroundTimingFact(currTime)
}
override fun onDestroy() {
super.onDestroy()
scope.cancel()
......@@ -83,6 +96,7 @@ class WebAppLauncherActivity : AppCompatActivity() {
try {
startActivity(intent)
emitHomescreenIconTapFact()
} catch (e: ActivityNotFoundException) {
logger.error("Packages does not handle ACTION_VIEW_PWA intent. Can't launch as web app.", e)
// Fall back to normal browser
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment