ActionLab Analytics for Django
Add privacy-first, AI-powered analytics to your Django site in under 60 seconds. No cookies, no consent banners, no complex setup.
Setup at a Glance
Add ActionLab to Django in 4 simple steps. Copy one script tag, paste it into your project, and start collecting privacy-friendly analytics immediately.
About Django
Django is Python's premier web framework, used for everything from content sites to complex web applications. Its template system, ORM, admin interface, and "batteries-included" philosophy make it a go-to choice for Python developers. With HTMX gaining popularity in the Django ecosystem for dynamic interactions, Django applications increasingly use partial page updates that resemble SPA navigation. ActionLab integrates through Django templates with context processor configuration, and automatically tracks HTMX-driven page transitions.
Why Add Analytics to Django
Django developers value the "batteries-included" philosophy of having everything you need with minimal external dependencies. ActionLab follows this spirit by providing complete analytics through a single template inclusion — no pip packages, no middleware, no management commands, and no Django app registration. For Django applications using HTMX for dynamic interactions, ActionLab automatic History API tracking captures HTMX page transitions without any Django-specific hooks. The zero-dependency approach means no compatibility concerns with Django version upgrades.
Installation Code
{# templates/base.html #}
<head>
<script
src="https://cdn.actionlabanalytics.com/actionlab.js"
data-site="{{ ACTIONLAB_SITE_ID }}"
defer
></script>
</head>Step-by-Step Setup
- 1
Add the ActionLab script to your base template (templates/base.html) within the <head> section.
- 2
Create a context processor that passes ACTIONLAB_SITE_ID from Django settings to all templates, or use settings directly if your template configuration allows.
- 3
Set the ACTIONLAB_SITE_ID in your Django settings module, reading from environment variables: ACTIONLAB_SITE_ID = os.environ.get("ACTIONLAB_SITE_ID").
- 4
HTMX-driven page navigation is tracked automatically when HTMX uses hx-push-url to update the browser URL.
Why Use ActionLab with Django
- Template variable configuration via Django context processors follows Django standard pattern for passing settings to templates.
- Works with Django templates and HTMX dynamic page transitions without any Django middleware or signal handlers.
- No pip package needed — the template inclusion is the complete integration, keeping your requirements.txt focused on application dependencies.
- Compatible with Django 4+ and works with both synchronous and async Django views.
Frequently Asked Questions
Does ActionLab work with HTMX?
Yes. When HTMX uses hx-push-url to update the browser URL during partial page updates, ActionLab detects the URL change via the History API and records it as a pageview. This means HTMX-driven navigation in Django applications is tracked automatically without any Django middleware, HTMX extensions, or JavaScript event listeners. For HTMX requests that do not change the URL (in-place updates), no pageview is recorded, which is the correct behavior.
How do I pass the site ID to Django templates?
The cleanest approach is a context processor: create a function that returns {"ACTIONLAB_SITE_ID": settings.ACTIONLAB_SITE_ID} and add it to your TEMPLATES context_processors list. This makes the variable available in all templates. Alternatively, if you use django.template.context_processors.debug, settings variables are available via {{ settings.ACTIONLAB_SITE_ID }} in debug mode.
Does ActionLab work with Django REST Framework?
Django REST Framework serves API responses, not HTML pages. ActionLab is designed for HTML websites. If your DRF API has a separate frontend (React, Vue, etc.), add ActionLab to the frontend. If your Django project has both DRF API routes and template-rendered pages, ActionLab tracks the template-rendered pages while DRF endpoints are unaffected.
Can I conditionally load ActionLab in Django?
Yes. Use Django template conditionals: {% if not debug %} ... {% endif %} to load ActionLab only in production. Or check a custom setting: {% if ACTIONLAB_ENABLED %} to have more granular control over analytics loading.
Does ActionLab work with Django Channels?
ActionLab tracks HTTP page views and client-side navigation. Django Channels WebSocket connections and server-sent events are not tracked because they are not page navigations. If a WebSocket message triggers a client-side page change, ActionLab detects the URL change and records the navigation.