> ## 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 web analytics product. It has no self-hosted edition, no session replay, no heatmaps, no A/B testing, no mobile SDKs and no data import except a site's history from a Plausible export (Settings, Import and export).
> 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.
> There is no npm package to install. 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.

# Add CrispStats to a Flask app

> Install CrispStats in a Flask app by adding the script tag to the head block of your base Jinja template, then send server-side events with Python.

To add CrispStats to a Flask app, paste the script tag inside `<head>` in the base Jinja template your other templates extend, often `templates/layout.html` or `templates/base.html`. Every page rendered from it is tracked. For actions that happen on the server, send an event with one HTTP request from Python.

You need your site's public key, which starts with `cs_pk_`. Find it in your site's settings under **Tracking code**, on the **HTML** tab. The key is safe to publish. There is no pip or npm package to install.

## Add the script

**1. Paste the tag into the base template**

Put it inside `{% block head %}` if your template has one, so child templates that call `{{ super() }}` keep it:

```jinja templates/layout.html theme={"theme":{"light":"github-light","dark":"github-dark"}}
<!doctype html>
<html>
  <head>
    {% block head %}
    <title>{% block title %}{% endblock %} - My app</title>
    <script
      defer
      data-site="cs_pk_xxxxxxxx"
      data-domains="example.com"
      src="https://in.crispstats.com/d.js"
    ></script>
    {% endblock %}
  </head>
  <body>
    {% block content %}{% endblock %}
  </body>
</html>
```

Replace the key and the domain with yours. A child template that overrides `head` without `{{ super() }}` drops the tag, so check those templates. Placing the tag outside the block, directly in `<head>`, avoids that.

**2. Deploy and open your app**

`flask run` serves on `127.0.0.1`, which the script ignores. Deploy, then open your live app in a normal browser window.

## Check that it works

* In your browser's developer tools, the **Network** tab shows `d.js` loading from `in.crispstats.com` and a `POST` to `https://in.crispstats.com/e` that gets `202`.
* In CrispStats, **Settings**, **Tracking code** shows **Receiving events.**, and the visit appears in **Realtime** within seconds.

To see events on `flask run`, add `data-debug` and `localhost` in `data-domains` while you develop. Every event is logged in the console and nothing is sent. See [localhost and testing](https://docs.crispstats.com/tracking/localhost-and-testing.md#debug-mode).

## How page changes are counted

A Flask app that renders each page on the server does a full page load per page, so each load is one pageview. If you add a library that swaps page content and updates the URL with `history.pushState`, the script counts each new URL too. See [single-page apps](https://docs.crispstats.com/tracking/single-page-apps.md).

## Send custom events

A `data-cs-event` attribute counts clicks with no JavaScript:

```jinja templates/pricing.html theme={"theme":{"light":"github-light","dark":"github-dark"}}
<a href="{{ url_for('signup') }}" data-cs-event="signup click" data-cs-prop-plan="pro">Start free</a>
```

From your own JavaScript, call `window.crispstats?.("signup", { plan: "pro" })`. See [declarative events](https://docs.crispstats.com/track/declarative-events.md).

## Send events from your server

Server-side events need a secret API key (`cs_sk_…`) with the **Events** scope, kept in an environment variable, never in a template:

```python app/crispstats.py theme={"theme":{"light":"github-light","dark":"github-dark"}}
import os
import requests

response = requests.post(
    "https://in.crispstats.com/e",
    headers={"Authorization": f"Bearer {os.environ['CRISPSTATS_API_KEY']}"},
    json={
        "k": "cs_pk_xxxxxxxx",
        "t": "event",
        "n": "subscription_renewed",
        "u": "",
        "p": {"plan": "pro"},
        "vid": visitor_id,  # from crispstats.id(), saved at checkout
    },
    timeout=5,
)
```

A `202` means the event was accepted. Send it from a background job if you do not want a request to wait for it. See [server-side events](https://docs.crispstats.com/track/server-side-events.md) and the [payment API](https://docs.crispstats.com/revenue/payment-api.md).

## FAQ

> [accordion] Is there a Flask extension?
>
> No. The script tag and, for server events, one HTTP request are the whole integration.

> [accordion] How do I pass the visitor id to my checkout route?
>
> In the browser, call `await window.crispstats?.id()` when the buyer starts checkout and send it with the request. Store it with the order. See [visitor id at checkout](https://docs.crispstats.com/revenue/visitor-id-at-checkout.md).

> [accordion] Can I serve the script from my own domain?
>
> Yes. On nginx, Caddy or Cloudflare in front of Flask, add one forwarding rule. See [proxy by host](https://docs.crispstats.com/tracking/proxy.md).

## Next steps

> [card] [Server-side events](https://docs.crispstats.com/track/server-side-events.md)
>
> Send renewals and other backend actions.

> [card] [Proxy with Caddy](https://docs.crispstats.com/tracking/proxy/caddy.md)
>
> Serve the script from your own domain.

> [card] [Visitor id at checkout](https://docs.crispstats.com/revenue/visitor-id-at-checkout.md)
>
> Attribute payments to the visit that led to them.

> [!NOTE]
>
> Flask details (a base `layout.html` template with `{% block head %}` and `{{ super() }}`) checked against [https://flask.palletsprojects.com/en/stable/patterns/templateinheritance/](https://flask.palletsprojects.com/en/stable/patterns/templateinheritance/) on 2026-11-23. The Python sample uses the `requests` library, as in [server-side events](https://docs.crispstats.com/track/server-side-events.md).


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