Backend frameworks
Add Loogika to Django
Your Django pages extend a base template, and its head is where the Loogika tag belongs. The site ID lives in settings.py, which takes it from an environment variable. A small context processor passes the ID to every template, so no template holds a hard-coded value.
Last updated .
What do you need before you start?
Copy your script tag from Site Settings in Loogika, under the Tracking Script tab. The 12-character site ID in the tag is the value you set as an environment variable below.
<script src="https://app.loogika.com/api/script.js?siteId=YOUR_SITE_ID" defer></script>
Where do you store the site ID in Django?
Add one setting to settings.py. It takes the site ID from an environment variable:
import os
LOOGIKA_SITE_ID = os.environ.get("LOOGIKA_SITE_ID", "")
Your production environment needs the line LOOGIKA_SITE_ID=YOUR_SITE_ID. If you load a .env file with django-environ or python-dotenv, you can put the line there.
How do you pass the site ID to templates?
Create a context processor, which is a function that adds values to the context of every template. Save it as context_processors.py in your project package, shown here as myproject:
from django.conf import settings
def loogika(request):
return {
"LOOGIKA_SITE_ID": settings.LOOGIKA_SITE_ID,
}
Register the function under TEMPLATES in settings.py, as the last entry in the context_processors list:
TEMPLATES = [
{
"BACKEND": "django.template.backends.django.DjangoTemplates",
"DIRS": [BASE_DIR / "templates"],
"APP_DIRS": True,
"OPTIONS": {
"context_processors": [
"django.template.context_processors.request",
"django.contrib.auth.context_processors.auth",
"django.contrib.messages.context_processors.messages",
"myproject.context_processors.loogika",
],
},
},
]
How do you add the tag to the base template?
Every page extends one template, which in most projects is templates/base.html. Put the tag inside its head element:
<head>
<meta charset="utf-8">
<title>{% block title %}My Site{% endblock %}</title>
{% if LOOGIKA_SITE_ID %}
<script src="https://app.loogika.com/api/script.js?siteId={{ LOOGIKA_SITE_ID }}" defer></script>
{% endif %}
</head>
With an empty LOOGIKA_SITE_ID, the {% if %} block leaves the tag out. Local development is then not tracked unless you set the variable on your own machine.
A page that does not extend base.html needs the same block in its own head element. A separate landing page template is a typical example.
How do you check that Loogika is installed?
Open the production site in another tab and visit a few pages. The pageviews show up in the Loogika dashboard within a few seconds.
If nothing arrives, open the page source and look for script.js?siteId=. When the tag is on the page, check the Network tab of your browser for requests to /api/track with status 200. The script troubleshooting page lists further causes.
Why does Django not send data to Loogika?
| Cause | Fix |
|---|---|
| The server still runs with the old settings | Django reads settings at startup. Restart the server, or your WSGI or ASGI workers, after you change LOOGIKA_SITE_ID |
| The context processor path is wrong | The path in TEMPLATES must match the name of your project package |
| A template is rendered without a request | A context processor needs a RequestContext to run. render() and class-based views supply one. A call to Template.render() with no request leaves LOOGIKA_SITE_ID unset, so the tag is hidden |
| A Content Security Policy blocks the script | If you use django-csp, allow the Loogika domain with the directives below, or load the script through a proxy on your own domain |
A Content Security Policy must allow Loogika in two directives:
script-src 'self' https://app.loogika.com; connect-src 'self' https://app.loogika.com;