<head> in the base Jinja template your other templates extend, often templates/layout.html or templates/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 the base template
Put it inside Replace the key and the domain with yours. A child template that overrides
{% block head %} if your template has one, so child templates that call {{ super() }} keep it:templates/layout.html
head without {{ super() }} drops the tag, so check those templates. Placing the tag outside the block, directly in <head>, avoids that.2
Deploy and open your app
flask run serves on 127.0.0.1, which the script ignores. Deploy, then open your live app 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.
flask run, add data-debug and localhost in data-domains while you develop. Every event is logged in the console and nothing is sent. See localhost and testing.
How page changes are counted
A Flask app 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. 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, never in a template:
app/crispstats.py
202 means the event was accepted. Send it from a background job if you do not want a request to wait for it. See server-side events and the payment API.
FAQ
Is there a Flask extension?
Is there a Flask extension?
No. The script tag and, for server events, one HTTP request are the whole integration.
How do I pass the visitor id to my checkout route?
How do I pass the visitor id to my checkout route?
In the browser, call
await window.crispstats?.id() when the buyer starts checkout and send it with the request. Store it with the order. See visitor id at checkout.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 Flask, add one forwarding rule. See proxy by host.
Next steps
Server-side events
Send renewals and other backend actions.
Proxy with Caddy
Serve the script from your own domain.
Visitor id at checkout
Attribute payments to the visit that led to them.
Flask details (a base
layout.html template with {% block head %} and {{ super() }}) checked against https://flask.palletsprojects.com/en/stable/patterns/templateinheritance/ on 2026-11-23. The Python sample uses the requests library, as in server-side events.