🐍

Django Integration

Add theBizness.ai analytics to your Django application with template integration and robust configuration.

Setup Time

5 minutes

Difficulty

Medium

Performance

Pythonic

Choose Your Implementation Method

Base Template Method
Add analytics to your base template with context processors

Step 1: Environment Variables

Add to your .env file:

# .env
ANALYTICS_ENABLED=True
ANALYTICS_WEBSITE_ID=your-website-id
ANALYTICS_DOMAIN=yourdomain.com

Step 2: Update Settings

# settings.py

import os
from decouple import config  # pip install python-decouple

# Analytics Configuration
ANALYTICS_ENABLED = config('ANALYTICS_ENABLED', default=False, cast=bool)
ANALYTICS_WEBSITE_ID = config('ANALYTICS_WEBSITE_ID', default='')
ANALYTICS_DOMAIN = config('ANALYTICS_DOMAIN', default='')
ANALYTICS_SCRIPT_URL = config('ANALYTICS_SCRIPT_URL', default='https://thebizness.ai/js/script.js')

# Context processors (add to TEMPLATES settings)
TEMPLATES = [
    {
        'BACKEND': 'django.template.backends.django.DjangoTemplates',
        'DIRS': [BASE_DIR / 'templates'],
        'APP_DIRS': True,
        'OPTIONS': {
            'context_processors': [
                'django.template.context_processors.debug',
                'django.template.context_processors.request',
                'django.contrib.auth.context_processors.auth',
                'django.contrib.messages.context_processors.messages',
                'myapp.context_processors.analytics',  # Add this line
            ],
        },
    },
]

Step 3: Create Context Processor

# myapp/context_processors.py

from django.conf import settings

def analytics(request):
    """
    Add analytics configuration to template context
    """
    return {
        'ANALYTICS_ENABLED': getattr(settings, 'ANALYTICS_ENABLED', False),
        'ANALYTICS_WEBSITE_ID': getattr(settings, 'ANALYTICS_WEBSITE_ID', ''),
        'ANALYTICS_DOMAIN': getattr(settings, 'ANALYTICS_DOMAIN', ''),
        'ANALYTICS_SCRIPT_URL': getattr(settings, 'ANALYTICS_SCRIPT_URL', 'https://thebizness.ai/js/script.js'),
    }

Step 4: Update Base Template

<!-- templates/base.html -->
{% load static %}
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>{% block title %}My Django App{% endblock %}</title>
    
    {% block extra_css %}{% endblock %}
    
    <!-- theBizness Analytics -->
    {% if ANALYTICS_ENABLED and ANALYTICS_WEBSITE_ID %}
        <script 
            async 
            defer 
            data-website-id="{{ ANALYTICS_WEBSITE_ID }}" 
            data-domain="{{ ANALYTICS_DOMAIN|default:request.get_host }}" 
            src="https://thebizness.ai/js/script.js">
        </script>
    {% endif %}
</head>
<body>
    {% block content %}{% endblock %}
    
    {% block extra_js %}{% endblock %}
</body>
</html>
Django Best Practices
  • • Environment-based configuration
  • • Context processor for template availability
  • • Auto-detection of domain from request
  • • Easy to disable for development
Verify Installation
Test your Django analytics integration

✅ Development Check

  1. 1. Run python manage.py runserver
  2. 2. Visit your Django app
  3. 3. View page source to check script
  4. 4. Check browser DevTools Network tab

📊 Django Specific

  1. 1. Test with different views/URLs
  2. 2. Check admin interface (should NOT have script)
  3. 3. Verify environment variables
  4. 4. Test with DEBUG=False
Django-Specific Tips
  • • Use different settings for development/production
  • • Consider disabling analytics in DEBUG mode
  • • Test with both authenticated and anonymous users
  • • Make sure ALLOWED_HOSTS includes your domain

🎉 Django Integration Complete!

Your Django application is now tracking analytics with Python elegance. Here's what to explore next:

🛒 Django Commerce

Track e-commerce with Django models

📊 Custom Events

Server-side event tracking with Python

🔗 Django REST

API integration with Django REST framework