Skip to content

Frameworks

Django analytics with CrispStats

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.

Updated

Set up CrispStats on Django

  1. 01

    Step 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. 02

    Step 2: Paste the tag into base.html

    Open the base template that your pages pull in with {% extends "base.html" %} (Django template inheritance) and paste the tag inside <head>. Templates that do not extend it, such as a standalone landing page, need the tag as well.

    Replace the highlighted values with your own.

    templates/base.html
    <!doctype html><html lang="en"><head><meta charset="utf-8" /><title>{% block title %}My site{% endblock %}</title><scriptdeferdata-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. 03

    Step 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. 04

    Step 4: Count clicks from a template (optional)

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

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

    Step 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.

    billing/crispstats.py
    import osimport requestsresponse = 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 the install check shows once the first events land, on the demo site, Quillbook, on generated traffic.
  • 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 and limits

    • 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).
    • 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) in front of Django: see the proxy guide.

The docs page covers the optional steps this guide leaves out.

Setup questions. Straight answers.

What people ask before they connect it.

  • 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.

  • 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.

  • 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.

  • 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.

Not answered here? Write to hello@crispstats.com

Go further

Related

Your traffic, crisp and clear

One script, no cookies, and every feature from the first day. Set up takes about as long as pasting a snippet.