Dokumentaatio
Selaa dokumentaatiota

Add Privatus Analytics to Django

Add cookieless analytics to a Django project with one snippet in base.html and a small context processor that keeps your site id in settings.

Näytä Markdown-muodossa

Add the snippet to your base template, for example templates/base.html, inside <head>:

django
{% if PRIVATUS_SITE_ID %}
  <script defer src="https://privatusstats.com/js/pa.js" data-site="{{ PRIVATUS_SITE_ID }}" data-modules="engage,auto,vitals,clicks,ask"></script>
{% endif %}

Django's debug template variable is only set for addresses in INTERNAL_IPS, so use a setting instead. Add a context processor:

python
# myproject/context_processors.py
from django.conf import settings

def privatus(request):
    return {"PRIVATUS_SITE_ID": getattr(settings, "PRIVATUS_SITE_ID", "")}
python
# settings.py
PRIVATUS_SITE_ID = "" if DEBUG else "pa_YOURSITEID"

TEMPLATES = [{
    # ...
    "OPTIONS": {"context_processors": [
        # ...
        "myproject.context_processors.privatus",
    ]},
}]

Every template that extends base.html now gets the tag in production only. Django serves full pages, so each page view is counted once. Add other options as attributes.