Skip to main content
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

templates/base.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.

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.

Send custom events

A data-cs-event attribute counts clicks with no JavaScript:
templates/pricing.html
From your own JavaScript, call window.crispstats?.("signup", { plan: "pro" }). See declarative events.

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:
billing/crispstats.py
A 202 means the event was accepted. See server-side events and, for payments from your own billing, the payment API.

FAQ

No. The script tag and, for server events, one HTTP request are the whole integration.
It is not needed: the script ignores localhost and 127.0.0.1. A condition in the template works too if you prefer it.
Yes. On nginx, Caddy or Cloudflare in front of Django, add one forwarding rule. See proxy by host.

Next steps

Server-side events

Send renewals and other backend actions.

Proxy with nginx

Serve the script from your own domain.

Exclude traffic

Keep admin paths and your own visits out.
Django details (base.html, {% extends %} and {% block %}) checked against 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.
Last modified on November 23, 2026