Skip to main content

Basic Usage (iOS)

This guide covers the most common operations you'll perform with the OpenCDP iOS SDK.

Initialization​

Initialize the SDK once, typically in your AppDelegate or at the entry point of your SwiftUI app.

SwiftUI​

import SwiftUI
import OpenCDP

@main
struct MyApp: App {
init() {
let config = OpenCDPConfig(
cdpApiKey: "YOUR_API_KEY",
debug: true,
autoTrackScreens: true,
trackApplicationLifecycleEvents: true
)
OpenCDP.shared.initialize(config: config)
}

var body: some Scene {
WindowGroup {
ContentView()
}
}
}

Identifying Users​

Use identify to create or update a user profile.

func onUserLogin(userId: String, email: String) {
let properties: [String: Any] = [
"email": email,
"name": "Jane Doe",
"plan": "enterprise"
]

// Identify the user with a unique ID and optional traits
OpenCDP.shared.identify(identifier: userId, properties: properties)
}

Tracking Events​

Use track to record user actions.

func onPurchaseComplete(orderId: String, amount: Double) {
let properties: [String: Any] = [
"order_id": orderId,
"amount": amount,
"currency": "USD"
]

OpenCDP.shared.track(eventName: "purchase_completed", properties: properties)
}

Tracking Screens​

If autoTrackScreens is enabled, ViewControllers/Views are tracked automatically. You can also track screens manually:

func viewProductDetails(productId: String) {
OpenCDP.shared.trackScreenView(title: "Product Details", properties: ["product_id": productId])
}

Clearing Identity​

When a user logs out, clear their identity to reset the SDK state.

func logout() {
OpenCDP.shared.clearIdentity()
}