Skip to content

Commit

Permalink
fix: add target text tracking for Android views
Browse files Browse the repository at this point in the history
  • Loading branch information
PouriaAmini committed Aug 13, 2024
1 parent a160e30 commit 05d331c
Show file tree
Hide file tree
Showing 6 changed files with 9 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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?,
) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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("_", " ")
Expand Down
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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<Float, Float>): Boolean {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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"
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down

0 comments on commit 05d331c

Please sign in to comment.