Skip to content

Commit

Permalink
feat: add screen name to elementInteractions properties for autocaptu…
Browse files Browse the repository at this point in the history
…re (#209)

* feat: add autocapture options and deprecate default tracking

* feat: track target hierarchy

* fix: fix lint

* fix: fix a bug that occurs when default tracking options are set after configuration is created

* fix: refactor default tracking deprecation

* fix: handle changes to defaultTrackingOptions after being assigned to Configuration

* fix: fix lint

* fix: attach autocapture option to defaultTracking to monitor changes to defaultTracking

* fix: remove redundant code

* refactor: refactor code to reduce object creation

* feat: set autocapture configs as a mutable set of options

* fix: fix lint

* refactor: refactor to use simple remove and add instead of augmented assignment

* refactor: remove redundant code

* feat: add experimental annotation to element interactions option

* fix: fix failing test

* fix: fix failing test

* fix: fix failing test

* feat: make autocapture options immutable and discard changes to defaultTracking options

* fix: fix lint

* test: add test for deprecated parameter.

* Revert "fix: fix lint"

This reverts commit afad034.

* feat: make changes to defaultTracking and trackingSessionEvents effective for autocapture

* fix: add a secondary constructor for Configuration to deprecate defaultTracking

* fix: fix the bug when a new DefaultTrackingOptions is passed to the Configuration

* test: add test for deprecation logic

* test: add test for deprecation logic

* fix: changes to the default tracking options replace the recent autocapture options entirely.

* fix: fix lint

* feat: add screen name to elementInteractions autocapture properties

* test: fix test

* fix: add exception error message

* style: fix error message style
  • Loading branch information
PouriaAmini authored Aug 7, 2024
1 parent dcb9393 commit 8044122
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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,
),
)
}
Expand Down

0 comments on commit 8044122

Please sign in to comment.