# Django analytics with CrispStats

Updated 2026-11-23

Django analytics with CrispStats is one script tag inside the head of base.html, the template your other templates extend, and a deploy. Every page rendered from it is tracked, and actions that happen on the server are sent with one HTTP request from Python.

## Steps

### 1. Copy the snippet with your key

In the dashboard, open your site's **Settings**, select **Tracking code** and choose the **HTML** tab. It shows the tag with your public key (it starts with `cs_pk_`) and your domain filled in. There is no pip package, Django app or middleware to install.

### 2. Paste the tag into base.html

Open the base template that your pages pull in with `{% extends "base.html" %}` ([Django template inheritance](https://docs.djangoproject.com/en/stable/ref/templates/language/)) and paste the tag inside `<head>`. Templates that do not extend it, such as a standalone landing page, need the tag as well.

`templates/base.html`

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

### 3. Deploy and open your live site

`python manage.py runserver` serves on `127.0.0.1`, which the script ignores, so you do not need a `DEBUG` condition around the tag. Deploy, then open your live site in a normal browser window.

### 4. Count clicks from a template (optional)

A `data-cs-event` attribute turns a link or button into a [custom event](/glossary/custom-event), with properties from `data-cs-prop-` attributes. From your own JavaScript, call `window.crispstats?.("signup", { plan: "pro" })`.

`templates/pricing.html`

```html
<a href="{% url 'signup' %}" data-cs-event="signup click" data-cs-prop-plan="pro">Start free</a>
```

### 5. Send events from your server (optional)

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. Pass the visitor id you saved from `crispstats.id()` so the event joins that visitor's history. Payments from your own billing go through the [payment API](/docs/revenue/payment-api).

`billing/crispstats.py`

```python
import os
import requests

response = requests.post(
    "https://in.crispstats.com/e",
    headers={"Authorization": f"Bearer {os.environ['CRISPSTATS_API_KEY']}"},  # cs_sk_…
    json={
        "k": "cs_pk_your_site_key",
        "t": "event",
        "n": "subscription_renewed",
        "u": "",
        "p": {"plan": "pro"},
        "vid": subscription.crispstats_visitor_id,
    },
    timeout=5,
)
# 202 means the event was accepted.
```

## Check that it works

Open your live site with the developer tools on the **Network** tab. You should see `d.js` load from `in.crispstats.com` and a `POST` to `/e` that answers `202`. In the dashboard, **Settings**, **Tracking code** shows **Receiving events.** and the visit appears on **Realtime** within seconds. If nothing loads, check your Content Security Policy first.

## What you get

- One pageview per server-rendered page, with visitors, sessions, entry and exit pages, and no cookies set on the visitor's device.
- Sources and channels, including search, UTM campaigns and AI assistants such as ChatGPT and Perplexity, with countries, devices and browsers.
- Pages that swap content with a front-end library and update the URL with `history.pushState`, counted as new pageviews too.
- Server events such as renewals in the same reports as browser events, tied to the visitor through `crispstats_visitor_id`.
- A core script of about 2.2 KB gzipped, with optional web vitals and browser errors switched on by `data-vitals` and `data-errors`.

## Notes for Django

- If you send a Content Security Policy header, allow `https://in.crispstats.com` for `script-src` and `connect-src`.
- The Django admin under `/admin/` is not tracked: its templates do not carry the tag unless you add it. Leave other private paths out with `data-exclude` ([exclude traffic](/docs/tracking/exclude-traffic)).
- CrispStats records browser errors only. Python exceptions are not captured, so keep your server error tracker.
- To count visitors whose ad blocker stops the collector host, add one forwarding rule on nginx, Caddy or Cloudflare ([Worker guide](/integrations/cloudflare)) in front of Django: see [the proxy guide](/docs/tracking/proxy).

Full reference: [Django in the docs](https://docs.crispstats.com/install/django)

## Frequently asked questions

### Is there a Django app or middleware for this?

No. The script tag in base.html and, for server events, one HTTP request with requests are the whole integration. Nothing goes in INSTALLED_APPS or MIDDLEWARE.

### Should I load the script only when DEBUG is off?

It is not needed, because the script ignores localhost and 127.0.0.1. A condition in the template works too if you prefer to keep the tag out of development pages.

### Does Django analytics need a cookie banner with this?

CrispStats sets no cookies and stores nothing on the device by default, and IP addresses are never stored. Django's own session and CSRF cookies are a separate question. This is not legal advice.

### Do events sent from Python count toward my plan?

Yes. An event your server sends counts the same as one from the browser, and pageviews count one each. Every account starts with a 14-day free trial with every feature and no card; then Starter is $5 a month for 250,000 events.

## Related

- [Web analytics for developers](/for/developers)
- [Server-side tracking explained](/glossary/server-side-tracking)
- [Error tracking in your analytics](/features/error-tracking)
- [Do you need a cookie banner for analytics?](/blog/do-you-need-a-cookie-banner-for-analytics)
- [Rails analytics](/integrations/rails)
- [Laravel analytics](/integrations/laravel)
