API reference

The public API on the Sophonz class — lifecycle, users and sessions, logs and crashes, spans, breadcrumbs, push notifications, experiments, and SwiftUI.

Starting is the static Sophonz.start(options:); every call after that goes through Sophonz.shared. Where automatic instrumentation is not enough, these methods record telemetry directly or add context to what was captured.

Lifecycle

APIDescription
Sophonz.start(options:)Starts the SDK. Static and throwing
Sophonz.shared.stop()Stops it
Sophonz.shared.state.notInitialized, .started, .stopped
Sophonz.shared.isSDKEnabledWhether it is collecting
Sophonz.shared.logLevelThe SDK's console log level. Defaults to .error
Sophonz.sdkVersionSDK version

start must be called on the main thread; otherwise it throws SophonzSetupError.invalidThread.

Users and sessions

APIDescription
deviceIdDevice identifier
currentUserSessionIdCurrent session ID
endUserSession()Ends the session explicitly
userIdentifierUser identifier, readable and writable
setProperty(key:value:lifespan:)Adds a property; a nil value removes it
removeAllProperties(lifespans:)Removes properties in bulk
addPersona(_:lifespan:), removePersona(_:lifespan:)User classification tags
removeAllPersonas(lifespans:)Removes tags in bulk
getCurrentPersonas(completion:)Reads current tags, asynchronously

lifespan decides how long a value survives — this session only, the life of the process, or permanently.

CAUTION — Choosing an identifier

Whatever you assign to userIdentifier is stored as given and displayed in the dashboard. An internal identifier is safer than an email address or a real name.

Logs and crashes

Sophonz.shared.log(
  "Checkout failed",
  severity: .error,
  attributes: ["order.id": orderId]
)
APIDescription
log(_:severity:timestamp:attachment:attributes:stackTraceBehavior:)Records a log
appendCrashInfo(key:value:)A value to include with the next crash report
lastRunEndState()How the previous run ended

stackTraceBehavior controls whether a stack trace is included, and attachment sends a file alongside the log.

Check lastRunEndState() early in launch to find out whether the previous run ended in a crash.

Spans

let span = Sophonz.shared.createSpan(name: "checkout", type: .performance)
defer { span?.end() }
APIDescription
createSpan(name:parentSpan:type:status:startTime:endTime:events:links:attributes:autoTerminationCode:)Creates a span
end(errorCode:endTime:)Ends a span
createStartupChildSpan(name:startTime:endTime:attributes:)Adds a child span to the startup trace
addAttributesToStartupTrace(_:)Adds attributes to the startup trace

type defaults to .performance and takes the spz.type values listed in Instrumentation.

autoTerminationCode closes a still-open span with that code when the session ends, which keeps unfinished spans from outliving the session boundary.

APIDescription
addBreadcrumb(_:timestamp:attributes:)A breadcrumb, shown on the session timeline
addSessionEvent(name:type:timestamp:attributes:)Adds an event to the session span
addSessionEvent(_:)Adds an event you built yourself

Push notifications

APIDescription
addPushNotificationEvent(notification:timestamp:attributes:captureData:)Records from a UNNotification
addPushNotificationEvent(userInfo:timestamp:attributes:captureData:)Records from a payload dictionary

Use these when automatic capture is off, or when you want to control where notifications are recorded. Note that captureData defaults to true here, unlike the automatic capture service, where it defaults to false.

Experiments and feature flags

APIDescription
trackExperiment(id:variant:startedAt:), untrackExperiment(id:endedAt:)Records experiment enrolment
trackExperiments(_:), untrackExperiments(ids:endedAt:)Several at once
trackFeatureFlag(id:variant:startedAt:), untrackFeatureFlag(id:endedAt:)Feature flags
trackFeatureFlags(_:), untrackFeatureFlags(ids:endedAt:)Several at once

SwiftUI

Screen capture names a screen after the view controller that appeared. In UIKit a screen is a view controller and that is right; in SwiftUI a NavigationStack is one view controller — UIKitNavigationController — for the whole app, so without help every span carries that one name.

A SwiftUI screen names itself:

struct GalleryView: View {
  var body: some View {
    List { /* ... */ }
      .sophonzScreen("Gallery")
  }
}
APIDescription
sophonzScreen(_:attributes:)Declares that this view is a screen. Sets the screen name and traces the view
sophonzTrace(_:attributes:)Records a view interval that is part of a screen
sophonzTrace(_:attributes:contentComplete:)Treats the time until contentComplete settles as loading
@SophonzTrace macroThe same instrumentation as a macro

Spans read the current screen when they start, so a request that outlives the screen still belongs to the screen that made it — which matters most for the slow ones.

app.screen.name reading UIKitNavigationController in your telemetry means a screen is missing its sophonzScreen call.

OpenTelemetry extension

Exporters and processors can only be supplied through Sophonz.OTelOptions at start. There is no API to add them afterwards.

let otel = Sophonz.OTelOptions(spanExporters: [myExporter])
try Sophonz.start(options: .withCollector(
  url: "https://in.sophonz.ai",
  appKey: "sk_…",
  otel: otel
))

NOTE — Network and WebView have no direct API

Both are captured only by capture-service swizzling. There is no method to record a request yourself and no bridge API to attach to a web view. Adjust them through the service options in Configuration.