📦 SwiftyLaunch Modules
📈 AnalyticsKit
Track Taps

Track Taps on a SwiftUI View

AnalyticsKit (SwiftyLaunch Module) - Track Taps in your App

To track taps in your SwiftUI app, we have added a convenient view modifier that does just that. You can attach it to any SwiftUI view and assign a name to the view that will be used as the event name in PostHog.

View Modifier Definiton

captureTaps.swift
extension View {
	public func captureTaps(
		_ tapTargetId: String,
		fromView: String? = nil,
		relevancy: EventRelevancy = .medium
	) -> some View { }
}
  • tapTargetId: The name of the tapped view. (Example: like_button)
  • fromView (Optional): The name of the screen where the tap happened. (Example: HomeFeedView)
  • relevancy (Optional): The relevancy of the event. Example: .low, .medium, .high. Defaults to .medium.

The view modifier will automatically prefix the tapTargetId with user_tapped_on_ and send it as the event name to PostHog.

Example: tapTargetId is set to like_button, so the event name will be user_tapped_on_like_button.

Example Usage

import SwiftUI
import AnalyticsKit
 
struct PostView: View {
    var body: some View {
        VStack {
            Image( /* image */ )
 
            LikeButton( /* likes */ )
                .captureTaps("like_button", fromView: "PostView")
 
            CommentSection( /* comments */ )
        }
    }
}

In the PostHog Dashboard

Capture Taps in PostHog Dashboard

Fallback function if SwiftUI doesn't register the tap

Some views in SwiftUI ignore the Tap Gesture modifier, which we use under the hood to power the .captureTaps() view modifier. If that's the case, you can use the following function to manually capture taps on those views:

Function Definition

Analytics.swift
public class Analytics {
    static public func captureTap(
		_ tapTargetId: String,
		fromView screenName: String?,
		relevancy: EventRelevancy = .medium
	) { }
}
  • tapTargetId: The name of the tapped view. (Example: like_button)
  • fromView: The name of the screen where the tap happened. (Example: HomeFeedView)
  • relevancy: The relevancy of the event. Example: .low, .medium, .high. Defaults to .medium.

Simply call it via Analytics.captureTap("like_button", fromView: "HomeFeedView") to manually capture a tap event.