Skip to main content
iOSSwiftMixpanel

Type-safe Mixpanel events in Swift

iOS apps ship through the App Store, so every tracking bug lives in the wild until the next release. Define the plan in Ordaze, generate native Swift methods for each event, and run the scanner on every pull request so no mistyped Mixpanel.mainInstance().track ever reaches TestFlight.

Mixpanel's Swift SDK exposes a generic track method: Mixpanel.mainInstance().track(event: String, properties: Properties?). Properties is effectively [String: MixpanelType], which means any dictionary of supported types compiles, including ones that have nothing to do with your tracking plan.

On iOS this matters more than on the web. A bad event name in JavaScript gets hot-patched and deployed in minutes. A bad event name in Swift sits in the App Store for days or weeks while the next release goes through review. Ordaze's Swift codegen turns every event in your tracking plan into a method with typed parameters, so the compiler refuses to build the app when a call drifts.

The Mixpanel SDK

This guide assumes you are installing the official Mixpanel SDK. The typed wrapper Ordaze generates sits on top of it and calls into it directly. You are not replacing your analytics provider, just adding a type-safe layer.

// Swift Package Manager
// Add via Xcode: File > Add Packages
// Mixpanel-swift

Setup in 5 steps

  1. 1

    Add the Mixpanel Swift SDK via Swift Package Manager

    In Xcode: File > Add Packages > paste the Mixpanel URL. Add the product to your app target.

    // URL for Swift Package Manager:
    https://github.com/mixpanel/mixpanel-swift
  2. 2

    Initialize Mixpanel on app launch

    In a SwiftUI app, call Mixpanel.initialize inside your @main App init. For UIKit, do it in application(_:didFinishLaunchingWithOptions:).

    import Mixpanel
    import SwiftUI
    
    @main
    struct MyApp: App {
        init() {
            Mixpanel.initialize(
                token: "YOUR_PROJECT_TOKEN",
                trackAutomaticEvents: false
            )
        }
        var body: some Scene {
            WindowGroup { ContentView() }
        }
    }
  3. 3

    Define events in Ordaze and generate the Swift file

    In the Ordaze dashboard, open your tracking plan and export the Swift codegen for Mixpanel. The output is a single .swift file containing an enum-like namespace with one method per event.

  4. 4

    Drop the generated file into your Xcode project

    Copy Analytics.swift into your app target. Import wherever you need to track, typically inside a view model or SwiftUI View action handler.

    import SwiftUI
    
    struct SignUpView: View {
        var body: some View {
            Button("Sign up") {
                Analytics.track.signUpCompleted(plan: .pro)
            }
        }
    }
  5. 5

    Run the Ordaze scanner on every pull request

    The scanner parses Swift source, finds every call into Mixpanel, and fails the build if any call does not match a method generated from the plan. This catches cases where a developer bypassed the Analytics namespace and called Mixpanel.mainInstance().track directly.

Before and after

Before

Strings and dictionaries, compiles with any typo

Button("Sign up") {
    Mixpanel.mainInstance().track(
        event: "Sign Up Complete", // typo: missing 'd'
        properties: ["Plan": "pro"]  // 'Plan' vs 'plan', new property in Mixpanel
    )
}
After

Enum parameters, compiler-checked at the call site

Button("Sign up") {
    Analytics.track.signUpCompleted(plan: .pro)
    // .pro, .free, .enterprise are the only valid values
    // Missing 'plan' fails to compile
}

Swift + Mixpanel gotchas

Turn off trackAutomaticEvents

Mixpanel's Swift SDK ships with automatic session and app-open tracking enabled. On a real tracking plan, these default events often collide with ones you track explicitly. Pass trackAutomaticEvents: false to Mixpanel.initialize and rely on the plan.

Do not track inside init of a SwiftUI View

SwiftUI rebuilds Views frequently, so init can fire many times per screen. Track inside onAppear or in a view model's init that lives for the screen's lifetime.

Properties are serialized at call time

Mixpanel's Swift SDK turns properties into JSON on the calling thread. If a property is expensive to compute (a long list, a big struct), wrap the track call in Task.detached or move the computation outside the SwiftUI Button closure.

Enum cases in properties need a rawValue

Mixpanel's Properties type accepts String, Int, Double, Bool, Date, URL, Array, and Dictionary of those. An enum case must be converted to its rawValue before it is passed. The Ordaze codegen handles this automatically; if you hand-write a call, you will hit a compile error here.

Why Ordaze for this combo

Compile-time guarantee, not a lint

Because the generated file uses method overloads and enums, a typo or a missing required property is a compile error, not a warning or a CI failure. The app will not build.

Works with Swift Concurrency

Generated methods are regular Swift functions, safe to call from @MainActor, async contexts, or inside a Task. No runtime or queueing is injected by Ordaze.

Scanner sees every Mixpanel call site

Even if someone imports Mixpanel directly and bypasses Analytics.track, the scanner finds the call, compares event name and properties to the plan, and fails CI. Typed wrapper + static scan is a defense-in-depth setup.

Frequently asked questions

Yes. The generated Swift file is UI-framework-agnostic. Call Analytics.track from any IBAction, view controller method, or callback. Only the initialization call site changes between SwiftUI and UIKit.
The Swift codegen produces Swift methods. To call them from Objective-C code, annotate the generated methods with @objc (there is a flag in the codegen settings), or wrap them in an @objc-annotated helper.
Every time the tracking plan changes in a way that affects event shapes. In practice this is rare, typically when you add events or add required properties. You can wire a GitHub Action to regenerate the file on main and open a PR.
Yes, if Mixpanel's Swift SDK supports that platform (it supports iOS, macOS, tvOS, and watchOS as of 2026). The generated Swift file works anywhere Mixpanel does.
The codegen focuses on event tracking. Super properties and group analytics are called through the standard Mixpanel SDK APIs; they do not need a typed wrapper because their shape is fixed.

Keep reading

Ship iOS tracking that the compiler verifies

Free plan includes 100 events, unlimited team members, and full Swift codegen. No credit card.

Get Started Free