Skip to content
Frameworks

Django analytics with revenue attribution

2 min read · Updated · By the Reveata team

Short answerPaste the Reveata snippet before the closing head tag of the base template your Django templates extend. Every page is tracked, the Django admin is not, and two hidden form fields let your checkout view put the visitor id into the payment's metadata.

Django projects tend to own their whole stack: templates, views, billing. That makes revenue attribution straightforward, because your own view creates the checkout and can say which visitor it belongs to.

What you get

  • Every page that extends your base template, tracked with its source, campaign, country and device.
  • The admin left out. Django's admin uses its own templates, so it isn't tracked, which is usually what you want.
  • Revenue credited to visits from checkouts your views create.
  • Payments from your own billing sent from Python with the Payments API.

How to install it

  1. Open the base template your other templates name in {% extends "base.html" %}, often templates/base.html.
  2. Paste the snippet before </head> and deploy.
  3. Templates that don't extend the base, such as a custom error page, need the snippet too.

Template inheritance is described in Django's template language docs (opens in a new tab). If your app sends a Content Security Policy, allow the Reveata script and endpoint (Content Security Policy).

Linking payments to visits

  1. Connect your payment provider in Reveata.
  2. In the template with your buy button, add two hidden fields, reveata_visitor_id and reveata_session_id, filled from window.reveata.metadata() when the form is sent. Keep {% csrf_token %} inside the form.
  3. In the view that creates the Stripe Checkout Session, copy them into metadata:
python
session = stripe.checkout.Session.create(
    mode="subscription",
    line_items=[{"price": price_id, "quantity": 1}],
    success_url="https://example.com/thanks",
    metadata={k: v for k, v in {
        "reveata_visitor_id": request.POST.get("reveata_visitor_id"),
        "reveata_session_id": request.POST.get("reveata_session_id"),
    }.items() if v},
)

The view can also read Reveata's _dt_v and _dt_s cookies from request.COOKIES. Other providers take the same keys in their custom data (Stripe Checkout attribution). With your own billing, post each payment to the Payments API instead, never both for the same provider.

Serving the script from your own domain

Django usually runs behind NGINX or Caddy, which can proxy the script and events with the visitor's address and your proxy secret (NGINX, Caddy). Without server access, the managed proxy needs one DNS record.

Frequently asked questions

Do I need a Django package?

No. A script tag in the base template is all the tracking needs. Payments use your existing Stripe or HTTP library.

Why are my local visits missing?

The script ignores localhost by default, so development visits stay out of your numbers. Test the deployed site.

Do I need the session id as well as the visitor id?

No. The visitor id alone matches the payment; the session id pins the exact visit.

See it with your own numbersReveata ties every visit and sale to the source that brought it. Add your website in a minute.
Get started
Was this page useful?
IntegrationStripe Checkout analytics and attributionAdd the Reveata visitor and session ids to the metadata of each Stripe Checkout Session your server creates. When Stripe reports the payment, Reveata reads the ids and credits the money to the exact visit that started the checkout.IntegrationNGINX analytics proxy and snippet injectionNGINX can insert the Reveata snippet into every HTML page with sub_filter, and proxy the script and events through two locations on your own domain. With your website's proxy secret, Reveata trusts the visitor's address NGINX forwards, so locations stay accurate.IntegrationAngular analytics with ReveataPaste the Reveata snippet before the closing head tag of src/index.html. ng build keeps it in the built page, server-side rendering included, and the Angular Router's History API navigation is counted automatically. HashLocationStrategy needs data-hash="true".IntegrationAstro analytics with revenue attributionPaste the Reveata snippet with is:inline into the Astro layout that renders the head of every page. Astro then leaves the tag as written, view transitions are tracked, and payment links or checkouts started from your pages are credited to the visit behind them.IntegrationDocusaurus analytics for documentation sitesAdd the Reveata script to the scripts array in docusaurus.config.js, or build the entry with @reveata/next/snippet. Page changes inside the docs are tracked by the script, so every page a reader opens is counted.IntegrationExpress.js analytics with a first-party proxyPaste the Reveata snippet into the layout, static HTML or front-end index.html your Express app serves. Two Express routes can serve the script from your own domain with your proxy secret, and your checkout route can copy the visitor id into the checkout metadata.