Backends

Django
Django analytics with Stickybeak starts by adding its script to your rendered pages; this guide shows you where it belongs and how to track page views, goals and purchase revenue. It is a lighter alternative to Google Analytics for Django, with live visitors, goals and revenue attribution in one script.
Add the script
Add a setting
Set
STICKYBEAK_SITE_IDin your production environment only. Copy the ID from Settings in the dashboard.import os # Empty outside production, so dev and staging aren't counted. STICKYBEAK_SITE_ID = os.environ.get("STICKYBEAK_SITE_ID", "")Write a context processor
It adds the site ID to every template, and moves a pending goal out of the session (more on that below).
from django.conf import settings def stickybeak(request): session = getattr(request, "session", None) return { "STICKYBEAK_SITE_ID": settings.STICKYBEAK_SITE_ID, "stickybeak_goal": session.pop("stickybeak_goal", None) if session is not None else None, }Register it in
TEMPLATES:TEMPLATES = [ { "BACKEND": "django.template.backends.django.DjangoTemplates", # ... "OPTIONS": { "context_processors": [ # ...the defaults... "mysite.context_processors.stickybeak", ], }, }, ]Add Django visitor tracking to your base template
Put this before
</head>:<script>window.stickybeak = window.stickybeak || function () { (window.stickybeak.q = window.stickybeak.q || []).push(arguments); };</script> {% if STICKYBEAK_SITE_ID %} <script defer data-site="{{ STICKYBEAK_SITE_ID }}" data-domain="example.com" src="https://app.usestickybeak.com/_cfw/s.js" ></script> {% endif %}
Check it's working
Publish first, then follow Verify your install.
Track goals
Give any button or link a data-goal attribute. Extra data-goal-* attributes become properties. See Custom goals.
<a href="{% url 'pricing' %}" data-goal="view_pricing" data-goal-from="nav">Pricing</a>
Fire a goal after a redirect
When a form saves and your view redirects, store the goal in the session:
from django.shortcuts import redirect, render
def signup(request):
form = SignupForm(request.POST or None)
if request.method == "POST" and form.is_valid():
user = form.save()
request.session["stickybeak_goal"] = {"name": "signup", "props": {"plan": user.plan}}
return redirect("welcome")
return render(request, "accounts/signup.html", {"form": form})
{% if stickybeak_goal %}
{{ stickybeak_goal|json_script:"stickybeak-goal" }}
<script>
(function () {
var goal = JSON.parse(document.getElementById("stickybeak-goal").textContent);
window.stickybeak(goal.name, goal.props);
})();
</script>
{% endif %}
json_script is Django's safe way to hand data to JavaScript: it escapes everything that could end the tag.
Track purchases
request.session["stickybeak_goal"] = {
"name": "purchase",
"props": {"revenue": str(order.total), "currency": order.currency, "order_id": str(order.pk)},
}
return redirect("order_complete", pk=order.pk)
See Revenue.
If it isn't working
- Confirm the environment variable reaches Gunicorn or uWSGI and the context processor is registered.
See Troubleshooting.