diff --git a/android/src/main/java/com/amplitude/android/internal/gestures/AutocaptureGestureListener.kt b/android/src/main/java/com/amplitude/android/internal/gestures/AutocaptureGestureListener.kt index ee8f2b2f..4ccc1701 100644 --- a/android/src/main/java/com/amplitude/android/internal/gestures/AutocaptureGestureListener.kt +++ b/android/src/main/java/com/amplitude/android/internal/gestures/AutocaptureGestureListener.kt @@ -7,8 +7,10 @@ import androidx.annotation.VisibleForTesting import com.amplitude.android.internal.ViewHierarchyScanner.findTarget import com.amplitude.android.internal.ViewTarget import com.amplitude.android.internal.locators.ViewTargetLocator +import com.amplitude.android.utilities.DefaultEventUtils.Companion.screenName import com.amplitude.android.utilities.DefaultEventUtils.EventProperties.ACTION import com.amplitude.android.utilities.DefaultEventUtils.EventProperties.HIERARCHY +import com.amplitude.android.utilities.DefaultEventUtils.EventProperties.SCREEN_NAME import com.amplitude.android.utilities.DefaultEventUtils.EventProperties.TARGET_CLASS import com.amplitude.android.utilities.DefaultEventUtils.EventProperties.TARGET_RESOURCE import com.amplitude.android.utilities.DefaultEventUtils.EventProperties.TARGET_SOURCE @@ -55,6 +57,12 @@ class AutocaptureGestureListener( .split(" ") .joinToString(" ") { it.replaceFirstChar { c -> c.uppercase() } }, HIERARCHY to target.hierarchy, + SCREEN_NAME to try { + activityRef.get()?.screenName + } catch (e: Exception) { + logger.error("Error getting screen name: $e") + null + } ).let { track(ELEMENT_INTERACTED, it) } return false diff --git a/android/src/main/java/com/amplitude/android/utilities/DefaultEventUtils.kt b/android/src/main/java/com/amplitude/android/utilities/DefaultEventUtils.kt index f2222cf4..efc756a7 100644 --- a/android/src/main/java/com/amplitude/android/utilities/DefaultEventUtils.kt +++ b/android/src/main/java/com/amplitude/android/utilities/DefaultEventUtils.kt @@ -119,19 +119,12 @@ class DefaultEventUtils(private val amplitude: Amplitude) { fun trackScreenViewedEvent(activity: Activity) { try { - val packageManager = activity.packageManager - val info = - packageManager?.getActivityInfo( - activity.componentName, - PackageManager.GET_META_DATA, + amplitude.track( + EventTypes.SCREEN_VIEWED, + mapOf( + EventProperties.SCREEN_NAME to activity.screenName ) - /* Get the label metadata in following order - 1. activity label - 2. if 1 is missing, fallback to parent application label - 3. if 2 is missing, use the activity name - */ - val activityLabel = info?.loadLabel(packageManager)?.toString() ?: info?.name - amplitude.track(EventTypes.SCREEN_VIEWED, mapOf(EventProperties.SCREEN_NAME to activityLabel)) + ) } catch (e: PackageManager.NameNotFoundException) { amplitude.logger.error("Failed to get activity info: $e") } catch (e: Exception) { @@ -162,6 +155,25 @@ class DefaultEventUtils(private val amplitude: Amplitude) { } ?: amplitude.logger.error("Failed to stop user interaction event tracking: Activity window is null") } + companion object { + internal val Activity.screenName: String? + @Throws(PackageManager.NameNotFoundException::class, Exception::class) + get() { + val packageManager = packageManager + val info = + packageManager?.getActivityInfo( + componentName, + PackageManager.GET_META_DATA, + ) + /* Get the label metadata in following order + 1. activity label + 2. if 1 is missing, fallback to parent application label + 3. if 2 is missing, use the activity name + */ + return info?.loadLabel(packageManager)?.toString() ?: info?.name + } + } + private fun getReferrer(activity: Activity): Uri? { if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP_MR1) { return activity.referrer diff --git a/android/src/test/java/com/amplitude/android/internal/gestures/AutocaptureGestureListenerClickTest.kt b/android/src/test/java/com/amplitude/android/internal/gestures/AutocaptureGestureListenerClickTest.kt index e327eb74..0ba9949f 100644 --- a/android/src/test/java/com/amplitude/android/internal/gestures/AutocaptureGestureListenerClickTest.kt +++ b/android/src/test/java/com/amplitude/android/internal/gestures/AutocaptureGestureListenerClickTest.kt @@ -148,6 +148,7 @@ class AutocaptureGestureListenerClickTest { "[Amplitude] Target Tag" to null, "[Amplitude] Target Source" to "Android View", "[Amplitude] Hierarchy" to "View", + "[Amplitude] Screen Name" to null, ), ) }