amplitude / Amplitude-Kotlin

Amplitude Kotlin SDK
MIT License
27 stars 10 forks source link

`trackScreenViewedEvent` would return the application label for most situation #145

Open zhchen-wish opened 1 year ago

zhchen-wish commented 1 year ago

Summary

fun trackScreenViewedEvent(activity: Activity) {
        try {
            val packageManager = activity.packageManager
            val info = packageManager?.getActivityInfo(
                activity.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
             */
            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) {
            amplitude.logger.error("Failed to track screen viewed event: $e")
        }
    }

Most of our activity has not set a label and our application has a label. Then it would fallback to parent application label, and never goes to #3 to return a activity name. Which means all the [Amplitude] Screen Viewed event can not tell the difference of which screen the user is viewing.

Do we have a plan to use the activity name as fallback when there is no activity label? We can use the info?labelRes == 0 to find whether #1 is matched, instead of info?.loadLabel

liuyang1520 commented 10 months ago

Thanks for the feedback! I agree we can probably adjust the order to be activity label > activity name (> application label). @amplitude/dx what do you think?