Skip to content

Swift SDK

Official Swift SDK for Mitr Analytics — native iOS, macOS, tvOS, and watchOS. Requires iOS 15+ / macOS 12+ (async URLSession).

Install

Not yet published as a versioned package — add it as a local Swift Package dependency:

Xcode → File → Add Package Dependencies → point at wherever mitr-sdk-suite/mitr-swift lives

Get your Site ID and Secret Key

From the dashboard: Integration Hub → Add New App → iOS. You'll get a Site ID and a Secret Key — copy the secret key now, it's shown in full only once. iOS/macOS apps have no browser Origin to check, so the secret key is sent with every request instead.

Initialize

import MitrAnalytics

let mitr = MitrAnalytics(options: .init(
    siteId: "YOUR_SITE_ID",
    secretKey: "YOUR_SECRET_KEY"
))

MitrAnalytics is an actor — every call from outside it needs await, the same way you'd await a promise/future in the other SDKs.

Manual tracking

await mitr.track("purchase_completed", metadata: ["value": 49.99, "currency": "USD"])
await mitr.pageView("HomeScreen")

Identifying users (login/logout)

// After a successful login:
await mitr.identify(user.id)

// On logout:
await mitr.reset()

User ids are SHA-256 hashed on-device (via CryptoKit) before they're ever sent — the backend only ever sees user_hash.

Deep-link attribution

Swift has no SDK-level hook into app lifecycle, so feed deep links to the SDK yourself, from wherever your app already handles them:

// SwiftUI
.onOpenURL { url in
    Task { await mitr.captureDeepLink(url) }
}

First-touch semantics: only the first utm_*-bearing URL you pass in is kept.

Resilience

Events are batched (flushed every 5s or every 10 events, whichever comes first) and persisted to UserDefaults between flushes — an app kill doesn't lose queued events.

Have questions about integration or security? Drop us a line directly.