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

> ## Agent Instructions
> CrispStats is a hosted, privacy-first analytics product. It has no self-hosted edition, no session replay, no heatmaps, no A/B testing and no data import except a site's history from a Plausible export (Settings, Import and export). Websites use the script. iOS, Android, React Native and Flutter use the mobile SDKs in the install guides. Those SDKs are source files, not a published package, and they do not read an advertising id.
> The script global is crispstats(...), site keys start with cs_pk_ and secret API keys start with cs_sk_. The script is served from https://in.crispstats.com/d.js and the REST API lives at https://crispstats.com/api/v1.
> The script tag is the main install; the npm package @crispstats/tracker does the same from code, never alongside the tag. Integrations copy the script tag, the queue stub, crispstats.d.ts and proxy code from these docs or from Settings, Tracking code in the dashboard. A first-party proxy forwards one path of the site to https://in.crispstats.com/p/ and forwards only content-type, user-agent, accept-language, referer, origin, if-none-match and x-forwarded-for, never cookies.

# Install CrispStats in an iOS app

> Add the CrispStats Swift file to an iOS app and send screen views, campaigns, custom events, identify and revenue with the site's public key.

The SDK is one Swift file with no dependencies. It keeps a random install id in `UserDefaults`, queues events while the network is down, and sends them to `https://in.crispstats.com/e`. It does not read the advertising identifier.

## Add the file

Download the file:

```bash theme={"theme":{"light":"github-light","dark":"github-dark"}}
curl -O https://crispstats.com/sdk/ios/CrispStats.swift
```

Drag `CrispStats.swift` into your Xcode project and tick your app target. The file is part of your app's own module, so there is nothing to import. It needs iOS 15 or later and works in Swift 5 and Swift 6 language modes.

## Count screens

Create one client and keep it for the life of the app. A global constant is fine, and every method can be called from any thread.

```swift theme={"theme":{"light":"github-light","dark":"github-dark"}}
let analytics = CrispStats(
    site: "cs_pk_xxxxxxxx",
    appId: "com.acme.app",
    appVersion: "1.4.0",
    appBuild: "42"
)
```

Replace `cs_pk_xxxxxxxx` with the site's public key from **Settings**, **Tracking code**, and `com.acme.app` with the bundle id. `appVersion` is the marketing version. `appBuild` is the build number and can be left out. The iOS version is read for you.

Call `screen` on each screen the visitor sees:

```swift theme={"theme":{"light":"github-light","dark":"github-dark"}}
analytics.screen("Home")
analytics.track("signup", props: ["plan": "pro"])
analytics.identify("user_42", traits: ["plan": "pro"])
analytics.revenue(4900, currency: "USD")
```

`revenue` takes minor units: `4900` with `USD` is \$49.00. A negative amount is ignored.

`screen` sends the time spent on the previous screen before it counts the new one. Time is counted from the moment you call `screen`, and it stops while the app is in the background. When the app comes back after 30 minutes or more, the current screen is counted again, because a new visit has started. All of this runs from the app's background and foreground notifications.

A screen name is one to eight segments of letters, numbers, spaces, `.`, `_` and `-`, separated by `/`. Each segment starts with a letter or a number. `Settings/Account` is stored as the page `/Settings/Account`. A name with `..`, a question mark or an accented letter is rejected and nothing is sent.

## SwiftUI

Call `screen` when a screen appears:

```swift theme={"theme":{"light":"github-light","dark":"github-dark"}}
struct HomeScreen: View {
    var body: some View {
        Text("Home")
            .onAppear { analytics.screen("Home") }
    }
}
```

## Campaigns from a deep link

When the app opens from a link such as `acme://promo?utm_source=newsletter&utm_campaign=spring`, pass the link's campaign to the screen it opens. `CrispStats.campaign(from:)` reads the query for you.

```swift theme={"theme":{"light":"github-light","dark":"github-dark"}}
struct AcmeApp: App {
    var body: some Scene {
        WindowGroup {
            ContentView()
                .onOpenURL { url in
                    analytics.screen(
                        "Promo",
                        campaign: CrispStats.campaign(from: url),
                        referrer: "https://newsletter.acme.com"
                    )
                }
        }
    }
}
```

The campaign and the referrer apply to that one screen view only. Keys are lower-cased and keep letters, numbers and `_` up to 32 characters. Values are trimmed and keep up to 200 characters. At most 10 pairs are sent. The referrer can be up to 2,000 characters.

## Tablets

An iPad is counted as a tablet and an iPhone as a phone. To set it yourself, pass `device`:

```swift theme={"theme":{"light":"github-light","dark":"github-dark"}}
let tabletAnalytics = CrispStats(
    site: "cs_pk_xxxxxxxx",
    appId: "com.acme.app",
    appVersion: "1.4.0",
    device: .tablet
)
```

## Opt out

```swift theme={"theme":{"light":"github-light","dark":"github-dark"}}
analytics.optOut()
analytics.optIn()
```

`optOut` drops anything still queued and sends nothing until `optIn`. The choice is kept across launches. If the visitor opts out before the first event, no install id is created until they opt in.

## Errors

```swift theme={"theme":{"light":"github-light","dark":"github-dark"}}
analytics.recordError("Checkout failed", source: "CheckoutScreen")
```

The message is grouped on the Errors page. The source is the screen or file name, and it is optional.

## Delivery

Each event is sent on its own. Up to 100 events wait on the device while the network is down, and the oldest is dropped first. The queue is sent when the app starts, when it returns to the foreground and after each new event. After a failure the SDK waits 1 second, then 2, then 4, up to a minute between attempts. When the app moves to the background it asks iOS for a few seconds to finish sending.

If the site key does not start with `cs_pk_`, the app id is not a valid bundle id, or the version is empty, the client sends nothing and prints the reason once in a debug build. It never crashes your app.

## What gets stored

The screen is a pageview. The browser breakdown shows **iOS app**, the OS shows **iOS**, and the device shows **mobile** or **tablet**. The language is sent as a tag such as `en-IN`, with the time zone. The app version, build, OS version and SDK name are properties on the event. The install id itself is not stored: the collector hashes it first. See [mobile apps](https://docs.crispstats.com/install/mobile.md) for the rest of the mapping, including revenue from the App Store.


This documentation is built and hosted on [Mintlify](https://mintlify.com), a developer documentation platform.