> ## 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 Django site

> Install CrispStats in a Django project by adding the script tag to the head of base.html, the template your pages extend, then send server-side events.

To add CrispStats to a Django site, paste the script tag inside `<head>` in `base.html`, the template your other templates extend with `{% extends "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 base.html**

```django templates/base.html theme={"theme":{"light":"github-light","dark":"github-dark"}}
<!doctype html>
<html lang="en">
  <head>
    <meta charset="utf-8" />
    <title>{% block title %}My site{% endblock %}</title>
    <script
      defer
      data-site="cs_pk_xxxxxxxx"
      data-domains="example.com"
      src="https://in.crispstats.com/d.js"
    ></script>
  </head>
  <body>
    {% block content %}{% endblock %}
  </body>
</html>
```

Replace the key and the domain with yours. Templates that do not extend `base.html`, such as a standalone landing page, need the tag too.

**2. Deploy and open your site**

`python manage.py runserver` serves on `127.0.0.1`, which the script ignores. Deploy, then open your live site 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.
* If you use a Content Security Policy, allow `https://in.crispstats.com` for `script-src` and `connect-src`. See [Content Security Policy](https://docs.crispstats.com/help/content-security-policy.md).

## How page changes are counted

A Django site 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. The Django admin under `/admin/` is tracked only if its templates carry the tag, which they do not unless you add it. 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:

```django templates/pricing.html theme={"theme":{"light":"github-light","dark":"github-dark"}}
<a href="{% url '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 or your secrets store, never in a template:

```python billing/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": subscription.crispstats_visitor_id,
    },
    timeout=5,
)
```

A `202` means the event was accepted. See [server-side events](https://docs.crispstats.com/track/server-side-events.md) and, for payments from your own billing, the [payment API](https://docs.crispstats.com/revenue/payment-api.md).

## FAQ

> [accordion] Is there a Django app or middleware to install?
>
> No. The script tag and, for server events, one HTTP request are the whole integration.

> [accordion] Should I load the script only when DEBUG is off?
>
> It is not needed: the script ignores `localhost` and `127.0.0.1`. A condition in the template works too if you prefer it.

> [accordion] Can I serve the script from my own domain?
>
> Yes. On nginx, Caddy or Cloudflare in front of Django, 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 nginx](https://docs.crispstats.com/tracking/proxy/nginx.md)
>
> Serve the script from your own domain.

> [card] [Exclude traffic](https://docs.crispstats.com/tracking/exclude-traffic.md)
>
> Keep admin paths and your own visits out.

> [!NOTE]
>
> Django details (`base.html`, `{% extends %}` and `{% block %}`) checked against [https://docs.djangoproject.com/en/stable/ref/templates/language/](https://docs.djangoproject.com/en/stable/ref/templates/language/) 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.