From 05d331cdac97d851daa39cf26e8b3a732cbe565b Mon Sep 17 00:00:00 2001 From: "pouria.amini" Date: Tue, 13 Aug 2024 07:55:27 -0700 Subject: [PATCH] fix: add target text tracking for Android views --- .../main/java/com/amplitude/android/internal/ViewTarget.kt | 1 + .../android/internal/gestures/AutocaptureGestureListener.kt | 2 ++ .../android/internal/locators/AndroidViewTargetLocator.kt | 4 +++- .../android/internal/locators/ComposeViewTargetLocator.java | 2 +- .../java/com/amplitude/android/utilities/DefaultEventUtils.kt | 1 + .../internal/gestures/AutocaptureGestureListenerClickTest.kt | 1 + 6 files changed, 9 insertions(+), 2 deletions(-) diff --git a/android/src/main/java/com/amplitude/android/internal/ViewTarget.kt b/android/src/main/java/com/amplitude/android/internal/ViewTarget.kt index 1029b237..ce2eb2f7 100644 --- a/android/src/main/java/com/amplitude/android/internal/ViewTarget.kt +++ b/android/src/main/java/com/amplitude/android/internal/ViewTarget.kt @@ -14,6 +14,7 @@ data class ViewTarget( val className: String?, val resourceName: String?, val tag: String?, + val text: String?, val source: String, val hierarchy: String?, ) { 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 4ccc1701..b2917ba3 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 @@ -15,6 +15,7 @@ import com.amplitude.android.utilities.DefaultEventUtils.EventProperties.TARGET_ import com.amplitude.android.utilities.DefaultEventUtils.EventProperties.TARGET_RESOURCE import com.amplitude.android.utilities.DefaultEventUtils.EventProperties.TARGET_SOURCE import com.amplitude.android.utilities.DefaultEventUtils.EventProperties.TARGET_TAG +import com.amplitude.android.utilities.DefaultEventUtils.EventProperties.TARGET_TEXT import com.amplitude.android.utilities.DefaultEventUtils.EventTypes.ELEMENT_INTERACTED import com.amplitude.common.Logger import java.lang.ref.WeakReference @@ -51,6 +52,7 @@ class AutocaptureGestureListener( TARGET_CLASS to target.className, TARGET_RESOURCE to target.resourceName, TARGET_TAG to target.tag, + TARGET_TEXT to target.text, TARGET_SOURCE to target.source .replace("_", " ") diff --git a/android/src/main/java/com/amplitude/android/internal/locators/AndroidViewTargetLocator.kt b/android/src/main/java/com/amplitude/android/internal/locators/AndroidViewTargetLocator.kt index 460504e8..86733b0e 100644 --- a/android/src/main/java/com/amplitude/android/internal/locators/AndroidViewTargetLocator.kt +++ b/android/src/main/java/com/amplitude/android/internal/locators/AndroidViewTargetLocator.kt @@ -1,6 +1,7 @@ package com.amplitude.android.internal.locators import android.view.View +import android.widget.TextView import com.amplitude.android.internal.ViewResourceUtils.resourceIdWithFallback import com.amplitude.android.internal.ViewTarget import com.amplitude.android.internal.ViewTarget.Type @@ -27,7 +28,8 @@ internal class AndroidViewTargetLocator : ViewTargetLocator { val className = javaClass.canonicalName ?: javaClass.simpleName ?: null val resourceName = resourceIdWithFallback val hierarchy = hierarchy - return ViewTarget(this, className, resourceName, null, SOURCE, hierarchy) + val text = (this as? TextView)?.text?.toString() + return ViewTarget(this, className, resourceName, tag as? String, text, SOURCE, hierarchy) } private fun View.touchWithinBounds(position: Pair): Boolean { diff --git a/android/src/main/java/com/amplitude/android/internal/locators/ComposeViewTargetLocator.java b/android/src/main/java/com/amplitude/android/internal/locators/ComposeViewTargetLocator.java index 750f6be7..1796d109 100644 --- a/android/src/main/java/com/amplitude/android/internal/locators/ComposeViewTargetLocator.java +++ b/android/src/main/java/com/amplitude/android/internal/locators/ComposeViewTargetLocator.java @@ -107,7 +107,7 @@ public ViewTarget locate( if (targetTag == null) { return null; } else { - return new ViewTarget(null, null, null, targetTag, SOURCE, null); + return new ViewTarget(null, null, null, targetTag, null, SOURCE, null); } } 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 efc756a7..04650913 100644 --- a/android/src/main/java/com/amplitude/android/utilities/DefaultEventUtils.kt +++ b/android/src/main/java/com/amplitude/android/utilities/DefaultEventUtils.kt @@ -38,6 +38,7 @@ class DefaultEventUtils(private val amplitude: Amplitude) { const val TARGET_CLASS = "[Amplitude] Target Class" const val TARGET_RESOURCE = "[Amplitude] Target Resource" const val TARGET_TAG = "[Amplitude] Target Tag" + const val TARGET_TEXT = "[Amplitude] Target Text" const val TARGET_SOURCE = "[Amplitude] Target Source" const val HIERARCHY = "[Amplitude] Hierarchy" } 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 0ba9949f..e0c91c97 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 @@ -146,6 +146,7 @@ class AutocaptureGestureListenerClickTest { "[Amplitude] Target Class" to "android.view.View", "[Amplitude] Target Resource" to "test_button", "[Amplitude] Target Tag" to null, + "[Amplitude] Target Text" to null, "[Amplitude] Target Source" to "Android View", "[Amplitude] Hierarchy" to "View", "[Amplitude] Screen Name" to null,