> ## 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 a Flutter app

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

The SDK is one Dart file, `crispstats.dart`. It uses only the Dart SDK and Flutter, so there is no extra dependency. It needs Dart 3.0 and Flutter 3.10 or later, and runs on iOS and Android. On any other platform it does nothing. It keeps a random install id, queues events while the network is down, and sends them to `https://in.crispstats.com/e`. It does not read an advertising id.

## Add the file

From the project root:

```bash theme={"theme":{"light":"github-light","dark":"github-dark"}}
curl -o lib/crispstats.dart https://crispstats.com/sdk/flutter/crispstats.dart
```

Then import it from `lib/main.dart` with `import "crispstats.dart";`.

## Create the client

Without storage, the install id and the queue live in memory and reset when the process ends. Pass `read` and `write` backed by `shared_preferences`, or any other store, so they survive a restart. Add the package with `flutter pub add shared_preferences`.

```dart theme={"theme":{"light":"github-light","dark":"github-dark"}}
import "package:flutter/widgets.dart";
import "package:shared_preferences/shared_preferences.dart";

import "crispstats.dart";

late final CrispStats analytics;

Future<void> main() async {
  WidgetsFlutterBinding.ensureInitialized();
  final SharedPreferences prefs = await SharedPreferences.getInstance();
  analytics = CrispStats(
    site: "cs_pk_xxxxxxxx",
    appId: "com.acme.app",
    appVersion: "1.4.0",
    appBuild: "42",
    read: (String key) async => prefs.getString(key),
    write: (String key, String value) async {
      await prefs.setString(key, value);
    },
  );
  analytics.start();
  runApp(const AcmeApp());
}

class AcmeApp extends StatelessWidget {
  const AcmeApp({super.key});

  @override
  Widget build(BuildContext context) {
    return const Directionality(
      textDirection: TextDirection.ltr,
      child: Center(child: Text("Home")),
    );
  }
}
```

Replace `cs_pk_xxxxxxxx` with the site's public key from **Settings**, **Tracking code**, and `com.acme.app` with the bundle id or application id. `appBuild` is the build number and can be left out.

`start` listens for background and foreground, detects the device, and sends anything a previous launch left queued. Call it once, after `WidgetsFlutterBinding.ensureInitialized()`. Call `dispose` if you ever tear the client down.

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

## OS version

`Platform.operatingSystemVersion` is a long string, for example `Version 17.4 (Build 21E213)` on iOS and the Linux kernel version on Android. On iOS the SDK takes `17.4` from it when you leave `osVersion` out. On Android pass the short value yourself, for example `info.version.release` from the `device_info_plus` package. Without it the OS version shows as `unknown`. In `main`, in place of the call above:

```dart theme={"theme":{"light":"github-light","dark":"github-dark"}}
analytics = CrispStats(
  site: "cs_pk_xxxxxxxx",
  appId: "com.acme.app",
  appVersion: "1.4.0",
  osVersion: "14",
);
```

## Count screens

Call `screen` when the visible screen changes:

```dart theme={"theme":{"light":"github-light","dark":"github-dark"}}
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. Time on screen pauses while the app is not resumed. 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

```dart theme={"theme":{"light":"github-light","dark":"github-dark"}}
analytics.track("signup", {"plan": "pro"});
analytics.identify("user_42", {"plan": "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, whichever deep link package gives it to you:

```dart theme={"theme":{"light":"github-light","dark":"github-dark"}}
void openFromLink(Uri link) {
  analytics.screen("Pricing", campaign: CrispStats.campaignFrom(link.toString()));
}
```

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:

```dart theme={"theme":{"light":"github-light","dark":"github-dark"}}
analytics.screen(
  "Pricing",
  campaign: {"utm_source": "newsletter", "utm_campaign": "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

When `start` runs, the SDK reads the shortest side of the screen in logical pixels: 600 or more counts as a tablet. To set it yourself, pass `device: CrispStatsDevice.tablet` or `device: CrispStatsDevice.phone`.

## Time zone

Flutter has no built-in way to read the device's IANA time zone name, so the SDK sends no time zone unless you pass one, for example from a time zone package. In `main`, in place of the call above:

```dart theme={"theme":{"light":"github-light","dark":"github-dark"}}
analytics = CrispStats(
  site: "cs_pk_xxxxxxxx",
  appId: "com.acme.app",
  appVersion: "1.4.0",
  timezone: "Europe/Berlin",
);
```

The time zone is a fallback for the country. Without it the country comes from the request as usual.

## Opt out

```dart 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 when `read` and `write` are set.

## Errors

```dart 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 through `write` when it is set, and the oldest is dropped when it is full. The SDK sends the queue when `start` runs, 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. No call waits for the network.

## What gets stored

The screen is a pageview. The browser breakdown shows **iOS app** or **Android app**, and the device shows **mobile** or **tablet**. The SDK property is `flutter`. The language comes from the device settings. The install id itself is not stored. See [mobile apps](https://docs.crispstats.com/install/mobile.md) for the rest of the mapping.


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