<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
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.jsloading fromin.crispstats.comand aPOSTtohttps://in.crispstats.com/ethat gets202. - 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.comforscript-srcandconnect-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 withhistory.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
Adata-cs-event attribute counts clicks with no JavaScript:
templates/pricing.html
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
202 means the event was accepted. See server-side events and, for payments from your own billing, the payment API.
FAQ
Is there a Django app or middleware to install?
Is there a Django app or middleware to install?
No. The script tag and, for server events, one HTTP request are the whole integration.
Should I load the script only when DEBUG is off?
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.Can I serve the script from my own domain?
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.
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.