From Strings to Swift APIs

I built a Swift generator that turns analytics definitions into typed screen and action events. SwiftUI helpers and a provider-independent service connect those events to the app, replacing raw event names at call sites with discoverable Swift APIs.

  • Swift
  • SwiftUI
  • iOS

At a glance

The engineering problem

The luca iOS app defined analytics through screen names and paths, action categories, actions, and optional labels. As that taxonomy grew, raw strings made events harder to find and easier to mistype in Swift call sites.

I built a small Swift API at the point of use: the compiler checks generated symbols and argument types, while an interface keeps the event destination out of screen code. The generator still depends on the correctness of its source definitions.

Generating typed events

The existing build pipeline fetched .strings definitions from POEditor and used SwiftGen for localized copy. My separate generator read the analytics definitions and emitted Swift event objects during the build.

It split dot-separated keys into AnalyticsEvent.View and AnalyticsEvent.Logic members. A screen’s name and path pair became a ViewEvent; action definitions became LogicEvent values or functions when a typed label was required. Source comments could become documentation on the generated members.

This schematic input shows the shape of the generated API:

"logic.checkout.category" = "Checkout";
"logic.checkout.confirmed.action" = "Confirmed";
"view.checkout.name" = "Checkout";
"view.checkout.path" = "checkout";
AnalyticsEvent.Logic.Checkout.confirmed.send()
AnalyticsEvent.View.Checkout.event.send()

SwiftGen supplied localization accessors; the authored generator supplied analytics events. The two outputs served different call sites.

What the code looks like in a view

A view declares which generated screen event represents it. The helper adds the onAppear report, leaving the view body free of provider-specific calls. The sample view illustrates the call site:

struct AmountStep: View, AnalyticsTrackable {
    static let viewEvent = AnalyticsEvent.View.Payment.Flow.Amount.event

    var body: some View {
        Text("Amount")
            .handleAnalyticsOnAppearance(of: Self.self)
    }
}

The same layer includes handleAnalyticsBackProperties for a parent view that does not receive another onAppear when a bottom-up sheet closes. handleAnalyticsPop asks the analytics service to restore the previous view event when a navigation flag becomes false. These helpers model behavior observed in the app’s navigation; they do not guarantee exactly one event per visible screen, since SwiftUI appearance callbacks and navigation state can recur.

User actions can be reported directly through the generated API:

AnalyticsEvent.Logic.MyLuca.settingsClicked.send()

For an asynchronous operation, report(event:) on an Rx Observable, Completable, Maybe, or Single sends an event when the stream completes. A call site can therefore attach tracking to the operation’s completion instead of the tap that started it:

let trackedOperation = operation.report(
    event: AnalyticsEvent.Logic.MyLuca.Locations.sharingEnabled(locationId: id)
)

Delivery to analytics providers

AnalyticsEvent carries parameters such as category, action, name, path, and value. Its send() method resolves the registered analytics service through the app’s dependency injection context. AnalyticsService holds a list of implementations and forwards initialisation, session, event, and view-stack operations to them. The app initialiser registers Matomo and a logging implementation. The service also checks the app’s analytics preference; an explicit false suppresses sending.

This kept screens and business operations independent of a particular provider. The view-event stack in the implementations also let navigation helpers restore the previous screen context. Actual delivery depends on the runtime configuration and analytics preference.

Result and trade-offs

The delivered code gives engineers discoverable Swift members for analytics, a short screen-tracking call in SwiftUI, and one service boundary for multiple analytics implementations. After regeneration, removed or misspelled event members fail compilation; typed functions check their argument types. The generator and SwiftUI helpers fit the existing build pipeline.

Local generation depends on downloaded or cached .strings files, while some build environments skip the generation phases. The POEditor script reports refresh errors without failing the build, so a build can use previously generated sources after a refresh failure. The custom generator handles a defined set of analytics label types and fails on unsupported formats. Appearance-based reporting needs care around sheets and repeated SwiftUI lifecycle callbacks.