Install on Django
View as MarkdownAdd Reveata to the base template of a Django project.
On this page
Django pages usually extend one base template, so the snippet goes there once.
Copy the snippet from your Reveata dashboard (it contains your website’s tracking id):
<script
defer
data-website-id="ws_your_tracking_id"
src="https://reveata.com/js/script.js"
></script>Add it to your site
- Open your base template, often
templates/base.html(or<app>/templates/<app>/base.html). It is the template your other templates name in{% extends "base.html" %}. - Paste the snippet before
</head>:
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>{% block title %}My site{% endblock %}</title>
<script defer data-website-id="ws_your_tracking_id" src="https://reveata.com/js/script.js"></script>
</head>
<body>
{% block content %}{% endblock %}
</body>
</html>- Deploy. Templates that don’t extend the base template (for example a custom error page) need the snippet too.
The Django admin uses its own templates, so it isn’t tracked.
Check it works
Open your site in a private window, then check that the visit appears in your Reveata dashboard. If it doesn’t, follow Verify your installation. The script ignores localhost by default, so visits to the development server stay out of your numbers.
Goals
Add data-reveata-goal="signup" to a button or link in your templates, or call reveata('goal', 'signup') from JavaScript. See Custom goals.
Serve it from your own domain
Django apps usually run behind NGINX or Caddy, which can pass the script and events through your own domain with the visitor’s address and your proxy secret: see Proxy through NGINX or Proxy through Caddy. Without access to the web server, use the managed proxy: one DNS record, no code.
Revenue
First connect your payment provider in Reveata: see Connect a payment provider. Then pass the Reveata visitor id with each checkout, so the payment is credited to the visit that brought the buyer.
In the template with your buy button, add two hidden fields and fill them when the form is sent:
<form method="post" action="/checkout">
<input type="hidden" name="reveata_visitor_id">
<input type="hidden" name="reveata_session_id">
<button>Buy</button>
</form>
<script>
// Fill the Reveata ids just before any form with these fields is sent.
document.addEventListener('submit', function (e) {
var ids = window.reveata && window.reveata.metadata ? window.reveata.metadata() : {};
Object.keys(ids).forEach(function (name) {
var input = e.target.querySelector('input[name="' + name + '"]');
if (input) input.value = ids[name];
});
});
</script>Add {% csrf_token %} inside the form.
In the view that creates a Stripe Checkout Session, copy the two fields into its metadata:
# views.py
import stripe
from django.shortcuts import redirect
from django.views.decorators.http import require_POST
@require_POST
def checkout(request):
ids = {k: request.POST[k] for k in ("reveata_visitor_id", "reveata_session_id") if request.POST.get(k)}
session = stripe.checkout.Session.create(
mode="payment",
line_items=[{"price": "price_123", "quantity": 1}],
success_url=request.build_absolute_uri("/thanks?session_id={CHECKOUT_SESSION_ID}"),
metadata=ids,
)
return redirect(session.url, status=303)For other providers, the same two ids go in their custom data: see the provider’s page under Connect a payment provider.
Payments from your own billing. If Reveata doesn’t connect to your provider, send each payment from Python with the Payments API:
import os, requests
requests.post(
"https://reveata.com/api/v1/payments",
headers={"Authorization": f"Bearer {os.environ['REVEATA_SITE_KEY']}"},
json={
"transactionId": f"order_{order.id}",
"amount": float(order.total_without_tax), # major units, e.g. 49.00
"currency": "USD",
"email": order.email,
"visitorId": order.reveata_visitor_id, # saved from the hidden field
},
timeout=10,
)Common pitfalls
- Templates that don’t extend the base. Error pages and standalone templates need the snippet too.
- The admin. Django’s admin isn’t tracked, which is usually what you want.
- Testing on localhost. The script ignores
localhostunless you adddata-allow-localhost="true", so test the deployed site. - A Content Security Policy. If your app sends one, allow the Reveata script and its endpoint: see Content Security Policy.
- Counting payments twice. If a provider is connected in Reveata, don’t also send its payments with the Payments API.