> ## 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 Android app

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

The SDK is one Kotlin file, `CrispStats.kt`. It uses only the Android SDK and the Java standard library, so there is no extra dependency. It works from Android 5.0 (API 21). It keeps a random install id on the device, queues events while the network is down, and sends them to `https://in.crispstats.com/e`. It does not read the advertising id.

## Add the file

From the app module, download the file into the package `com.crispstats.sdk`:

```bash theme={"theme":{"light":"github-light","dark":"github-dark"}}
mkdir -p app/src/main/java/com/crispstats/sdk
cd app/src/main/java/com/crispstats/sdk
curl -O https://crispstats.com/sdk/android/CrispStats.kt
```

The app needs the internet permission. Most apps have it already. If yours does not, add it to `AndroidManifest.xml`:

```xml theme={"theme":{"light":"github-light","dark":"github-dark"}}
<uses-permission android:name="android.permission.INTERNET" />
```

## Create the client

Create one client in your `Application` class and keep it for the life of the app.

```kotlin theme={"theme":{"light":"github-light","dark":"github-dark"}}
package com.acme.app

import android.app.Application
import android.os.Build
import com.crispstats.sdk.CrispStats

class AcmeApp : Application() {
    lateinit var analytics: CrispStats

    override fun onCreate() {
        super.onCreate()
        analytics = CrispStats(
            context = this,
            site = "cs_pk_xxxxxxxx",
            appId = "com.acme.app",
            appVersion = "1.4.0",
            osVersion = Build.VERSION.RELEASE,
            appBuild = "42",
        )
    }
}
```

Register the class in `AndroidManifest.xml` with `android:name=".AcmeApp"` on the `<application>` element.

Replace `cs_pk_xxxxxxxx` with the site's public key from **Settings**, **Tracking code**, and `com.acme.app` with the application id. `appVersion` is the version name. `appBuild` is the version code and can be left out. `osVersion` defaults to `Build.VERSION.RELEASE`, so you can leave it out too.

If the key does not start with `cs_pk_`, the application id has characters other than letters, digits, `.`, `_` and `-`, or the version is empty, the client sends nothing and says why once in Logcat in a debug build. It never throws.

## Count screens

Call `screen` when the visible destination changes:

```kotlin theme={"theme":{"light":"github-light","dark":"github-dark"}}
val analytics = (application as AcmeApp).analytics

analytics.screen("Home")
analytics.screen("Settings/Account")
```

A screen name is one to eight segments separated by `/`. Each segment starts with a letter or a digit and then uses letters, digits, spaces, `.`, `_` and `-`, up to 80 characters. The whole name is at most 200 characters. `Settings/Account` is stored as the page `/Settings/Account`. A name that breaks these rules sends nothing.

`screen` sends the time spent on the previous screen before it counts the new one. While at least one activity is started, the current screen's timer runs. When the last activity stops, the timer pauses and the time so far is sent. When the app comes back within 30 minutes, the same screen keeps adding up. After 30 minutes or more in the background, the screen is counted again, because a new session has started.

## Events, users and revenue

```kotlin theme={"theme":{"light":"github-light","dark":"github-dark"}}
analytics.track("signup", mapOf("plan" to "pro"))
analytics.identify("user_42", mapOf("plan" to "pro"))
analytics.revenue(4900, "USD")
```

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

## Campaigns from a deep link

Pass the campaign tags of the link that opened the app to the first `screen` call. `CrispStats.campaignFrom` reads them from the link:

```kotlin theme={"theme":{"light":"github-light","dark":"github-dark"}}
import android.app.Activity
import android.os.Bundle
import com.crispstats.sdk.CrispStats

class PricingActivity : Activity() {
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        val analytics = (application as AcmeApp).analytics
        val link = intent?.data?.toString()
        if (link != null) {
            analytics.screen("Pricing", campaign = CrispStats.campaignFrom(link))
        } else {
            analytics.screen("Pricing")
        }
    }
}
```

A link such as `acme://pricing?utm_source=newsletter&utm_medium=email` sets the channel to email. You can also pass the pairs yourself, and a referrer URL:

```kotlin theme={"theme":{"light":"github-light","dark":"github-dark"}}
analytics.screen(
    "Pricing",
    campaign = mapOf("utm_source" to "newsletter", "utm_campaign" to "spring"),
    referrer = "https://news.example.com/issue-12",
)
```

Keys are lower-cased and keep letters, digits and `_`, up to 32 characters. Values are trimmed and kept when they are 1 to 200 characters. At most 10 pairs are sent. The referrer is sent when it is 1 to 2,000 characters. Both apply to that one screen view only.

## Tablets

The device is detected from the smallest screen width: 600 dp or more counts as a tablet. To set it yourself, pass `device` in `onCreate`:

```kotlin theme={"theme":{"light":"github-light","dark":"github-dark"}}
analytics = CrispStats(
    context = this,
    site = "cs_pk_xxxxxxxx",
    appId = "com.acme.app",
    appVersion = "1.4.0",
    device = CrispStats.Device.TABLET,
)
```

## Opt out

```kotlin 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.

## Errors

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

The message is grouped on the Errors page.

## Offline and retries

Each event is one request. Events wait in a queue of up to 100, kept in `SharedPreferences`, and the oldest is dropped when it is full. The SDK sends the queue when the client is created, when the app returns to the foreground and after each new event. After a failure it tries again after 1 second, then 2, 4 and so on, up to once a minute. A request that takes longer than 10 seconds counts as a failure. Storage and network run on one background thread, so no call blocks the main thread.

## What gets stored

The screen is a pageview. The browser breakdown shows **Android app**, the OS shows **Android**, and the device shows **mobile** or **tablet**. The app version, build, OS version and SDK name are properties on the event. The language and time zone come from the device settings. The install id itself is not stored. See [mobile apps](https://docs.crispstats.com/install/mobile.md) for Play Store revenue through RevenueCat.


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