Skip to content
LogZuno

Swift event tracking

Send events to LogZuno from Swift, with code that is tested against the API.

This example sends one event to LogZuno's ingestion API from Swift. It is the exact file our snippet tests send to the API before the docs are published.

Requirements

Swift 5.5 or later (async/await) with Foundation, on macOS or Linux.

Send an event

// Swift 5.5 or later (async/await), Foundation only.
import Foundation

let env = ProcessInfo.processInfo.environment
let endpoint = env["LOGZUNO_ENDPOINT"] ?? "https://api.logzuno.app/v1"

var request = URLRequest(url: URL(string: "\(endpoint)/log")!)
request.httpMethod = "POST"
request.timeoutInterval = 10
request.setValue("Bearer \(env["LOGZUNO_API_KEY"] ?? "")", forHTTPHeaderField: "Authorization")
request.setValue("application/json", forHTTPHeaderField: "Content-Type")
request.httpBody = try JSONSerialization.data(withJSONObject: [
    "channel": "signups",
    "event": "User signed up",
    "user_id": "user-123",
    "icon": "🎉",
    "notify": true,
    "tags": ["plan": "growth", "source": "website"],
])

let (data, response) = try await URLSession.shared.data(for: request)
print((response as! HTTPURLResponse).statusCode, String(decoding: data, as: UTF8.self))

Export LOGZUNO_API_KEY and run swift logzuno.swift. LogZuno answers 202 and the event appears in your project's live feed within seconds. Keep the key in an environment variable or your secrets manager, not in source control.

Notes for Swift

Keys in apps. An ingest key can be shipped in an iOS or macOS app because it can only send data, but anyone can extract it. Turn off its New channels and New groups switches so a copied key can't create junk.

Batch on device. Queue events locally and send them in batches of up to 100 to /v1/batch when the app moves to the background, rather than one request per tap.

Responses. Check statusCode: 202 accepted, 422 invalid fields, 429 too many requests (wait for Retry-After).

What the request contains

The body is the same in every language: a channel and an event, and optionally user_id, group_id, description, icon, notify and tags. All fields, limits and response codes are in sending events and the ingestion API reference.