> ## Documentation Index
> Fetch the complete documentation index at: https://docs.datalyr.com/llms.txt
> Use this file to discover all available pages before exploring further.

# iOS SDK

> Track native iOS apps with DatalyrSDK.

The iOS SDK supports iOS 13 and later.

## Install with Swift Package Manager

In Xcode, select **File → Add Package Dependencies** and add:

```text theme={null}
https://github.com/datalyr/swift
```

Add the `DatalyrSDK` product to your app target.

## Initialize

```swift theme={null}
import DatalyrSDK

Task {
    try await DatalyrSDK.configure(
        apiKey: "dk_your_api_key"
    )
}
```

## Track an event

```swift theme={null}
await datalyrTrack("signup_completed", properties: [
    "plan": "pro"
])
```

## Identify a user

```swift theme={null}
await datalyrIdentify("user_123", properties: [
    "email": "person@example.com"
])
```

## Track a screen

<Tabs>
  <Tab title="SwiftUI">
    ```swift theme={null}
    PricingView()
        .datalyrScreen("Pricing")
    ```
  </Tab>

  <Tab title="Swift">
    ```swift theme={null}
    await datalyrScreen("Pricing")
    ```
  </Tab>
</Tabs>

## Core methods

| Method                          | Use it to                         |
| ------------------------------- | --------------------------------- |
| `DatalyrSDK.configure(...)`     | Initialize the shared SDK.        |
| `datalyrTrack(...)`             | Track an event.                   |
| `datalyrScreen(...)`            | Track a screen view.              |
| `datalyrIdentify(...)`          | Identify a user.                  |
| `datalyrAlias(...)`             | Link two IDs for the same user.   |
| `datalyrReset()`                | Clear identity on logout.         |
| `datalyrFlush()`                | Send queued events immediately.   |
| `datalyrGetAnonymousId()`       | Read the persistent anonymous ID. |
| `datalyrTrackPurchase(...)`     | Track purchase revenue.           |
| `datalyrTrackSubscription(...)` | Track subscription revenue.       |

Call `datalyrReset()` when a user signs out.

## CocoaPods

```ruby theme={null}
pod 'DatalyrSDK', '~> 2.1.8'
```

Then run `pod install` and open the generated workspace.

## Configuration

```swift theme={null}
let config = DatalyrConfig(
    apiKey: "dk_your_api_key",
    debug: false,
    maxRetries: 3,
    timeout: 15,
    batchSize: 10,
    flushInterval: 10,
    maxQueueSize: 1000,
    enableAutoEvents: true,
    enableAttribution: true
)

try await DatalyrSDK.shared.initialize(config: config)
```

| Option              | Default  | What it controls                            |
| ------------------- | -------- | ------------------------------------------- |
| `apiKey`            | Required | Workspace API key.                          |
| `debug`             | `false`  | Development logging.                        |
| `maxRetries`        | `3`      | Delivery retries.                           |
| `retryDelay`        | `1`      | Delay between retries in seconds.           |
| `timeout`           | `15`     | Request timeout in seconds.                 |
| `batchSize`         | `10`     | Events sent in each batch.                  |
| `flushInterval`     | `10`     | Automatic flush interval in seconds.        |
| `maxQueueSize`      | `1000`   | Maximum queued events.                      |
| `enableAutoEvents`  | `true`   | App lifecycle and session events.           |
| `enableAttribution` | `true`   | Deep-link and campaign attribution.         |
| `autoEventConfig`   | —        | Fine-grained automatic event settings.      |
| `skadTemplate`      | —        | SKAdNetwork template for conversion values. |

## Automatic events

```swift theme={null}
let autoEvents = AutoEventConfig(
    trackSessions: true,
    trackScreenViews: true,
    trackAppUpdates: true,
    trackPerformance: false,
    autoTrackScreenViews: false
)
```

Automatic UIKit screen tracking is opt-in. For SwiftUI, use the `.datalyrScreen()` modifier.

## Identity

```swift theme={null}
await datalyrIdentify("user_123", properties: [
    "email": "person@example.com",
    "plan": "pro"
])

let anonymousId = datalyrGetAnonymousId()
```

Use `datalyrAlias` only to join two IDs for the same person. Call `datalyrReset()` on logout or account switching.

## Revenue and subscriptions

```swift theme={null}
await datalyrTrackPurchase(
    value: 99.99,
    currency: "USD",
    productId: "pro_yearly"
)

await datalyrTrackSubscription(
    value: 19.99,
    currency: "USD",
    plan: "pro_monthly"
)
```

The SDK also includes helpers for add to cart, content views, checkout, registration, search, leads, and payment information.

## Attribution and deep links

Pass incoming links to the SDK:

```swift theme={null}
await DatalyrSDK.shared.handleDeepLink(url)
```

Read the current state when you need it:

```swift theme={null}
let attribution = DatalyrSDK.shared.getAttributionData()
let journey = DatalyrSDK.shared.getJourney()
let summary = DatalyrSDK.shared.getJourneySummary()
```

## App Tracking Transparency

```swift theme={null}
let status = await DatalyrSDK.shared.requestTrackingAuthorization()
let idfa = DatalyrSDK.shared.getIDFA()
```

ATT controls access to advertising identifiers. It is not a complete analytics-consent system. Your app remains responsible for consent handling and accurate App Store privacy disclosures.

## RevenueCat and Superwall

```swift theme={null}
let revenueCat = DatalyrSDK.shared.getRevenueCatAttributes()
let superwall = DatalyrSDK.shared.getSuperwallAttributes()
```

Pass these attributes to the matching SDK when you need Datalyr identity available in subscription events.

## Diagnostics

```swift theme={null}
let status = DatalyrSDK.shared.getStatus()
await datalyrFlush()
```

Use `getStatus()` during setup verification and `datalyrFlush()` before a controlled shutdown or background transition when immediate delivery matters.
