Skip to content

Improving UIKitScreenViews Support for Multiple Child View Controller #282

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
VitorFerraz opened this issue Apr 30, 2025 · 0 comments
Open
Labels
enhancement New feature or request

Comments

@VitorFerraz
Copy link

Expected Behavior

When a view controller contains multiple embedded child view controllers, UIKitScreenViews should not return the first child; instead, it should return nil. In cases where there is only one child, it should return that single child view controller as it is done currently.

Why this should be the correct Behavior?
If a view controller has two or more child view controllers, they should not be considered the correct screen name. Instead, they should be treated as "subviews" within a "parent view". In this case, the parent view controller that contains the child controllers should be considered the correct source for the screen name.

Current Behavior

Context: Screen view tracking can be enabled by including AutocaptureOptions.screenViews in the autocapture configuration from Amplitude. When you enable this setting, Amplitude tracks the [Amplitude] Screen Viewed event and sets the screen name property of this event to the name of the top-most view controller's class using the UIKitScreenViews class.

Problem: Issue with Embedded Child View Controllers
We found multiple cases where a view controller contains two or more embedded child view controllers.
Amplitude’s automatic tracking does not handle these situations correctly. It tracks only the first embedded child view controller, resulting in incorrect screen name tracking.

Possible Solution

UIKitScreenViews.swift, around line 112, in the amp_topViewController method.
Current validation:

...
if rootViewController.children.count > 0 {
    if let firstChildViewController = rootViewController.children.first {
        return firstChildViewController
    }
}

return nil

Fixed validation:

...
if rootViewController.children.count == 1 {
    return rootViewController.children.first
}
return nil

Steps to Reproduce

  1. Create a ViewController to act as a container
  2. Add two or more Child View Controller in the container controller
  3. Enable AutocaptureOptions.screenViews
  4. [Amplitude] Screen Viewed event it will be sent with the first child view controller as the screen name

Environment

  • SDK Version:
  • Found on version 1.11.3 and the current version (1.12.0) still have the same problem
  • OS Info:
  • Tested on an iPhone
@VitorFerraz VitorFerraz added the bug Something isn't working label Apr 30, 2025
@crleona crleona added enhancement New feature or request and removed bug Something isn't working labels May 13, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

2 participants