iOS SDK
Coming soon
Native iOS SDK for Swift applications with automatic lifecycle tracking and session management.
Interim integration
Send events via the Connect RPC API until the native SDK ships.
Track events
import Foundation
class PugClient {
let projectId: String
let apiKey: String
let endpoint: String
init(projectId: String, apiKey: String, endpoint: String = "https://api.pug.sh") {
self.projectId = projectId
self.apiKey = apiKey
self.endpoint = endpoint
}
func track(_ eventName: String, properties: [String: Any] = [:]) {
let payload: [String: Any] = [
"events": [[
"name": eventName,
"properties": properties,
"timestamp": ISO8601DateFormatter().string(from: Date())
]]
]
guard let url = URL(string: "\(endpoint)/events.v1.EventsService/BatchCreate"),
let body = try? JSONSerialization.data(withJSONObject: payload) else { return }
var request = URLRequest(url: url)
request.httpMethod = "POST"
request.setValue("Bearer \(apiKey)", forHTTPHeaderField: "Authorization")
request.setValue(projectId, forHTTPHeaderField: "x-project-id")
request.setValue("application/json", forHTTPHeaderField: "Content-Type")
request.setValue("1", forHTTPHeaderField: "Connect-Protocol-Version")
request.httpBody = body
URLSession.shared.dataTask(with: request) { _, _, _ in
// Handle silently — analytics should not crash the app
}.resume()
}
}
// Usage
let pug = PugClient(projectId: "YOUR_PROJECT_ID", apiKey: "YOUR_SDK_API_KEY")
pug.track("app_open")
pug.track("purchase", properties: [
"revenue": 29.99,
"currency": "USD",
"order_id": "ord-123"
])Identify users
func identify(externalId: String, traits: [String: Any]) {
let payload: [String: Any] = [
"externalId": externalId,
"traits": traits
]
// POST to /profiles.v1.ProfilesSDKService/Identify
}Planned features
- Swift Package Manager distribution
- Automatic UIApplication lifecycle and session tracking
- Background batch flush
- Swift protobuf client generated from proto
- App Tracking Transparency (ATT) integration guidance
Further reading
Last updated on