posthog-ios icon indicating copy to clipboard operation
posthog-ios copied to clipboard

Support SwiftUI screen names through accessibilityIdentifier and friends

Open marandaneto opened this issue 1 year ago • 2 comments

Problem Statement

Screen names are auto generated, so autocapture does not work well nor the detection of the current screen for session replay

Solution Brainstorm

fallback to accessibilityIdentifier or friends if available

https://github.com/PostHog/posthog-ios/blob/e35ec56b8e25d759e285d9a7d831eed070bf3cd3/PostHog/UIViewController.swift#L55 https://github.com/PostHog/posthog-ios/blob/e35ec56b8e25d759e285d9a7d831eed070bf3cd3/PostHog/Replay/PostHogReplayIntegration.swift#L476

marandaneto avatar Aug 21 '24 06:08 marandaneto

afaik accessibilityIdentifier isn't available at runtime for production builds, what we could do is wrap the view:

var body: some View {
    PostHogView("Name"){
        List {
            ...
        }
    }
}

in this case, we can read the Name at runtime but it's not much different than calling PostHog.screen("Name"), all screens need to be wrapped.

marandaneto avatar Aug 21 '24 12:08 marandaneto

Another option would be to use the objc_getAssociatedObject and objc_setAssociatedObject as an extension method to UIKit and SwiftUI views

Edit: not possible either since the associated objects are different when scanning the view tree

marandaneto avatar Aug 26 '24 12:08 marandaneto

closed by https://posthog.com/docs/libraries/ios#capturing-screen-views-in-swiftui

marandaneto avatar Nov 13 '24 12:11 marandaneto