Add Millimetry to an app
The Swift SDK is a Swift package with no dependencies. It runs on iOS and iPadOS 15, macOS 12, tvOS 15, watchOS 8 and visionOS 1 or later. It is written in Swift 6 with strict concurrency, and every call is safe from any thread.
1. Create the app
Section titled “1. Create the app”In the dashboard, choose Add an app or site, name the app, and copy its key (ak_…). The key identifies your app; it is not a secret, so it is fine in your binary.
2. Add the package
Section titled “2. Add the package”In Xcode, choose File › Add Package Dependencies… and add the Millimetry package. In a Package.swift:
.package(url: "https://github.com/Pineapple-Eng/millimetry-swift", from: "0.1.0")3. Configure at launch
Section titled “3. Configure at launch”Call configure once, as early as possible: in your App’s initializer, or in application(_:didFinishLaunchingWithOptions:).
import Millimetry
@mainstruct SlicksApp: App { init() { Millimetry.configure( appKey: "ak_…", host: URL(string: "https://e.millimetry.app")! ) } var body: some Scene { WindowGroup { ContentView() } }}By default the SDK sends app_open at launch and whenever the app returns to the foreground. Nothing else is tracked automatically. Turn it off with options: .init(trackAppOpen: false).
4. Track events
Section titled “4. Track events”Millimetry.track("screen_view", ["screen": "settings"])Millimetry.track("purchase", ["plan": "pro", "price": 4.99, "trial": false])- Names are 1 to 64 characters. Names starting with
$are reserved. - Up to 25 properties per event. Values are strings, whole numbers, decimals or Booleans, written as plain literals.
- Strings longer than 256 characters are cut.
See naming events for names that stay useful. Never put personal data in properties: no names, email addresses, phone numbers or account IDs. The server drops properties that look like those, but that is a safety net, not a guarantee.
When events are sent
Section titled “When events are sent”Events wait in a small queue on disk and go out in batches:
- every 30 seconds while the app is in the foreground (every 2 seconds in debug builds);
- as soon as 50 events are waiting;
- when the app moves to the background, inside a background task so the upload can finish;
- when you call
await Millimetry.flush().
Offline, or when the server is busy, a batch stays queued and is retried with a growing delay, up to 5 minutes. A batch keeps its ID across retries and relaunches, so it is stored once however many times it arrives. The queue keeps at most 1,000 events and drops events older than 7 days.
Debug builds
Section titled “Debug builds”Builds with the Debug configuration mark their events as debug data. The dashboard keeps that data apart and deletes it after 30 days. Override with Options(isDebug:).
Letting people opt out
Section titled “Letting people opt out”Millimetry.setEnabled(false) stops tracking and deletes everything queued. The SDK does not remember the choice, because remembering it would mean storing something on the device. Keep your own setting and apply it on every launch, before configuring:
Millimetry.setEnabled(Settings.shared.analyticsAllowed)Millimetry.configure(appKey: "ak_…", host: URL(string: "https://e.millimetry.app")!)