OnyxMetric
Integration Guides

Django

Add OnyxMetric analytics to your Django app

Django pages extend a base template, so the tag goes in that template's <head>. Read the site ID from an environment variable in settings.py and hand it to templates with a context processor.

Copy your tracking snippet

In OnyxMetric, open your site's Site Settings and copy the tracking script. It looks like this:

<script src="https://app.onyxmetric.com/api/script.js?siteId=YOUR_SITE_ID" async></script>

YOUR_SITE_ID is the numeric ID of your site.

Add the snippet to Django

Configure the site ID

settings.py
import os

ONYXMETRIC_SITE_ID = os.environ.get("ONYXMETRIC_SITE_ID", "")

Set ONYXMETRIC_SITE_ID=YOUR_SITE_ID in production (or in your .env file if you load one).

Expose it to templates

myproject/context_processors.py
from django.conf import settings


def onyxmetric(request):
    return {"ONYXMETRIC_SITE_ID": settings.ONYXMETRIC_SITE_ID}

Register it under TEMPLATES in settings.py:

settings.py
"context_processors": [
    "django.template.context_processors.request",
    "django.contrib.auth.context_processors.auth",
    "django.contrib.messages.context_processors.messages",
    "myproject.context_processors.onyxmetric",
],

Edit the base template

In templates/base.html (or whichever template every page extends), add the tag in <head>:

templates/base.html
<head>
    <meta charset="utf-8">
    <title>{% block title %}My Site{% endblock %}</title>

    {% if ONYXMETRIC_SITE_ID %}
    <script src="https://app.onyxmetric.com/api/script.js?siteId={{ ONYXMETRIC_SITE_ID }}" async></script>
    {% endif %}
</head>

The {% if %} check drops the tag when the variable is empty, so local development stays untracked. Pages that do not extend the base template need the tag in their own <head>.

Check that it works

Load your live site and click through a few pages. Pageviews show up in the OnyxMetric dashboard within seconds.

If nothing arrives:

  • View the page source and search for script.js?siteId= to confirm the tag is present.
  • In the browser's Network tab, check that script.js returns 200 and that requests go to /api/track.
  • Turn off ad blockers, or proxy the script through your own domain.
  • Read script troubleshooting for other causes.

Troubleshooting

  • Tag missing: settings load at startup, so restart your WSGI or ASGI workers after changing the variable, and confirm the context processor path matches your project package.
  • No request in the template context: context processors run only for templates rendered with a request, as render() and class-based views do. A bare Template.render() skips them.
  • django-csp: allow app.onyxmetric.com in script-src and connect-src, or proxy the script.

Next steps

On this page