📦 SwiftyLaunch Modules
📈 AnalyticsKit
Capture View Activity

Capture View Activity

AnalyticsKit (SwiftyLaunch Module) - Track View Activity in your App

PostHog SDK has an option to track when a view is shown on screen. The problem with that is that it only works for apps using UIViewController. As we are using SwiftUI to build our apps, we needed to find another way to track when a view is shown on screen.

View Modifiers to the Rescue

To combat this, we created a simple view modifier that you should attach to your navigation views.

View Modifier Definition

captureViewActivity.swift
extension View {
	public func captureViewActivity(as viewName: String) -> some View { }
}
  • viewName: The name of the view that is shown on screen. (Example: HomeFeedView)

Example Usage

Here we have a simple SwiftUI app with two views. The HomeView has a button that navigates to the ProfileView. Both views have the .captureViewActivity() view modifier attached to them

import SwiftUI
import AnalyticsKit
 
struct HomeView: View {
    var body: some View {
        NavigationStack {
            VStack {
 
                Text("Home View")
 
                NavigationLink {
                    ProfileView()
                } label: {
                    Text("Open Profile View")
                }
            }
            .captureViewActivity(as: "HomeView")
        }
    }
 
    struct ProfileView: View {
        var body: some View {
            Text("Profile View")
                .captureViewActivity(as: "ProfileView")
        }
    }
}

Example in a Mockup GIF

Captured View Activity in PostHog Dashboard:

PostHog Dashboard showing captured view activity

For a more thorough tracking of user navigation, you could try using newly introduced Session Recordings. Note that PostHog session recordings for iOS are currently in beta.