Skip to main content

Documentation Index

Fetch the complete documentation index at: https://docs.prism.byescaleira.com/llms.txt

Use this file to discover all available pages before exploring further.

Prism’s analytics layer is provider-agnostic: you implement PrismAnalyticsProvider once for your chosen backend, inject it into your view hierarchy with .prismAnalytics(_:), and every Prism component automatically fires structured PrismAnalyticsEvent values as users interact with your app.

Implementing PrismAnalyticsProvider

The protocol has a single requirement: a track(_:) method that receives a PrismAnalyticsEvent and forwards it to your backend.
public protocol PrismAnalyticsProvider: Sendable {
    func track(_ event: PrismAnalyticsEvent)
}
Here is the Firebase example from the source documentation:
import FirebaseAnalytics

struct FirebaseAnalyticsProvider: PrismAnalyticsProvider {
    func track(_ event: PrismAnalyticsEvent) {
        Analytics.logEvent(
            event.name,
            parameters: event.parameters
        )
    }
}
You can use the same pattern with any other backend:
import Mixpanel

struct MixpanelAnalyticsProvider: PrismAnalyticsProvider {
    func track(_ event: PrismAnalyticsEvent) {
        Mixpanel.mainInstance().track(
            event: event.name,
            properties: event.parameters
        )
    }
}
PrismAnalyticsEvent carries three fields:
FieldTypeDescription
nameStringThe event name, e.g. "button_tap".
parameters[String: String]Key-value metadata.
timestampDateWhen the event was created (defaults to Date.now).

Registering the provider

Call .prismAnalytics(_:) on any view to inject the provider into the SwiftUI environment. All descendant views in the hierarchy inherit it.
@main
struct MyApp: App {
    var body: some Scene {
        WindowGroup {
            ContentView()
                .prismAnalytics(FirebaseAnalyticsProvider())
        }
    }
}
Register the provider at the root of your view hierarchy so that every screen and component has access to it automatically.

Tracking screen views

Apply .prismTrackScreen(_:) to a view to fire a screen_view event when the view appears:
HomeView()
    .prismTrackScreen("Home")

ProfileView()
    .prismTrackScreen("Profile")
This is equivalent to manually calling .prismTrack(.screenView(name: "Home")).

Tracking any event manually

Use .prismTrack(_:) to fire a specific PrismAnalyticsEvent when a view appears:
PurchaseConfirmationView()
    .prismTrack(.screenView(name: "Purchase Confirmation", route: "/checkout/confirm"))

Built-in automatic events

Prism components fire these events automatically when the provider is registered. You do not need to add any extra code.

button_tap

Fired when a Prism button is tapped.
public static func buttonTap(
    label: String,
    testID: String = ""
) -> PrismAnalyticsEvent
Parameters: label, test_id.

screen_view

Fired on view appearance via .prismTrackScreen(_:).
public static func screenView(
    name: String,
    route: String = ""
) -> PrismAnalyticsEvent
Parameters: screen_name, route.

field_interaction

Fired when a text field receives or loses focus.
public static func fieldInteraction(
    testID: String,
    action: String
) -> PrismAnalyticsEvent
Parameters: test_id, action (e.g., "focus" or "blur"). Fired when a carousel advances to a new item.
public static func carouselScroll(
    testID: String,
    index: Int
) -> PrismAnalyticsEvent
Parameters: test_id, index.

tab_select

Fired when a tab is selected in a Prism tab view.
public static func tabSelect(
    testID: String,
    tab: String
) -> PrismAnalyticsEvent
Parameters: test_id, tab. Fired when a menu item is activated.
public static func menuAction(
    testID: String,
    action: String
) -> PrismAnalyticsEvent
Parameters: test_id, action.

Custom events

Use PrismAnalyticsEvent.custom(_:parameters:) to define events that don’t map to any built-in category:
public static func custom(
    _ name: String,
    parameters: [String: String] = [:]
) -> PrismAnalyticsEvent
// Fire a custom event directly
provider.track(.custom("onboarding_step_completed", parameters: [
    "step": "email_verified",
    "method": "magic_link",
]))

// Attach to a view
PaywallView()
    .prismTrack(.custom("paywall_shown", parameters: [
        "source": "settings",
        "variant": "annual_discount",
    ]))
All parameter values are String. Convert numeric or boolean values with String(value) before passing them as parameters.

Testing analytics events

Inject a test provider during unit or UI tests to capture events without hitting a real backend:
final class CapturingAnalyticsProvider: PrismAnalyticsProvider {
    private(set) var events: [PrismAnalyticsEvent] = []

    func track(_ event: PrismAnalyticsEvent) {
        events.append(event)
    }
}

// In your test
let provider = CapturingAnalyticsProvider()
let view = HomeView()
    .prismAnalytics(provider)

// … interact with the view …

XCTAssertTrue(provider.events.contains { $0.name == "screen_view" })