Publish Score iOS SDK 0.1.0 (SPM package + podspec + docs)

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
This commit is contained in:
Blake
2026-08-06 01:27:14 -04:00
commit ca717e4656
7 changed files with 393 additions and 0 deletions

View File

@@ -0,0 +1,114 @@
import Foundation
import ScoreTrackerKit
/// Idiomatic Swift facade over the Score Tracker KMP framework (#177). Wraps the
/// generated `ScoreTrackerKit` Obj-C API with Swift types, sensible defaults, and
/// `async`/`await`. Mirrors the web tracker's surface. The durable offline queue
/// and persistent `anonymousId` are handled natively (NSUserDefaults) see #176.
public final class ScoreClient {
private let tracker: ScoreTracker
private var autoFlushTask: Task<Void, Never>?
/// - Parameters:
/// - endpoint: middleware base URL; events POST to `<endpoint>/api/tracking/website`.
/// - initialConsent: defaults to "none" nothing fires until `setConsent(...)`.
public init(
publishableKey: String,
siteId: String,
endpoint: String,
sdkVersion: String,
appVersion: String? = nil,
os: String? = nil,
device: String? = nil,
locale: String? = nil,
initialConsent: ConsentState = ConsentState.companion.NONE
) {
let config = TrackerConfig(
publishableKey: publishableKey,
siteId: siteId,
endpoint: endpoint,
sdk: SdkInfo(name: "@3halves-labs/score-tracker-ios", version: sdkVersion),
device: DeviceDescriptor(os: os, device: device, appVersion: appVersion, locale: locale)
)
tracker = IosPlatformKt.doNewScoreTracker(config: config, initialConsent: initialConsent)
}
// MARK: - Consent (wire to your CMP / OneTrust)
public func setConsent(analytics: Bool, personalization: Bool, advertising: Bool) {
tracker.setConsent(state: ConsentState(analytics: analytics, personalization: personalization, advertising: advertising))
}
public func grantAllConsent() { tracker.setConsent(state: ConsentState.companion.ALL) }
public func revokeConsent() { tracker.setConsent(state: ConsentState.companion.NONE) }
// MARK: - Identity / Auth0
public func identify(userId: String, token: String? = nil) {
tracker.identify(userId: userId, token: token)
}
public func setToken(_ token: String?) { tracker.setToken(token: token) }
/// Provider is consulted before each flush wire to your Auth0 access token.
public func setTokenProvider(_ provider: @escaping () -> String?) {
tracker.setTokenProvider(provider: provider)
}
public var anonymousId: String { tracker.anonymousId }
public var pending: Int { Int(tracker.pending()) }
// MARK: - Tracking
/// Track an app event. Returns false if dropped by the consent gate.
@discardableResult
public func track(_ name: String, props: [String: Any] = [:]) -> Bool {
tracker.track(name: name, props: props, type: .track, requires: [], delivery: .bestEffort)
}
/// Track an event gated on advertising consent (dropped until granted).
@discardableResult
public func trackAdvertising(_ name: String, props: [String: Any] = [:]) -> Bool {
tracker.track(name: name, props: props, type: .track, requires: [ConsentCategory.advertising], delivery: .bestEffort)
}
/// Record an app-screen view.
@discardableResult
public func screen(_ name: String, props: [String: Any] = [:]) -> Bool {
tracker.screen(name: name, props: props)
}
// MARK: - Flush
/// Send the queued batch. Returns true on a 2xx ack.
@discardableResult
public func flush() async throws -> Bool {
try await tracker.flush().boolValue
}
/// Call when the app returns to the foreground.
public func onForeground() async throws { _ = try await tracker.onForeground() }
/// Call when network connectivity is regained.
public func onNetworkAvailable() async throws { _ = try await tracker.onNetworkAvailable() }
public func shutdown() async throws {
stopAutoFlush()
try await tracker.shutdown()
}
// MARK: - Auto-flush (Swift-native; avoids passing a Kotlin CoroutineScope)
public func startAutoFlush(everySeconds: Double = 30) {
stopAutoFlush()
autoFlushTask = Task { [weak self] in
while !Task.isCancelled {
try? await Task.sleep(nanoseconds: UInt64(everySeconds * 1_000_000_000))
_ = try? await self?.flush()
}
}
}
public func stopAutoFlush() {
autoFlushTask?.cancel()
autoFlushTask = nil
}
}