Trackthemetric + Django
Here's how you can set up Trackthemetric analytics in a Django project.
Option 1Use Django settings and context processors
If you prefer a more dynamic setup (useful across multiple environments), you can load the IDs from Django settings and inject them into templates.
1. Add your Trackthemetric IDs to yoursettings.pyfile:
TRACKTHEMETRIC_WEBSITE_ID = 'your_website_id' TRACKTHEMETRIC_DOMAIN = 'yourdomain.com'
2. Create a context processor (e.g. myapp/context_processors.py):
from django.conf import settings
def trackthemetric(request):
return {
'TRACKTHEMETRIC_WEBSITE_ID': settings.TRACKTHEMETRIC_WEBSITE_ID,
'TRACKTHEMETRIC_DOMAIN': settings.TRACKTHEMETRIC_DOMAIN,
}
3. Register the context processor in settings.py):
TEMPLATES = [
{
# ...
'OPTIONS': {
'context_processors': [
# default context processors...
'myapp.context_processors.trackthemetric',
],
},
},
]
4. Use the variables in your base template:
<script
defer
id="TrackTheMetric"
data-website-id="{{ TRACKTHEMETRIC_WEBSITE_ID }}"
data-domain="{{ TRACKTHEMETRIC_DOMAIN }}"
src="https://app.trackthemetric.com/tracker.js"
></script>
Option 2Add the tracking script in your base template
To make sure the tracking script appears on every page, include it in your base HTML template (e.g. templates/base.html):
1. Open your base template.templates/base.html).
2. Add the following script into the <head> section:
<script defer id="TrackTheMetric" data-website-id="YOUR_WEBSITE_ID" data-domain="YOUR_DOMAIN.COM" src="https://app.trackthemetric.com/tracker.js" ></script>
Make sure to replace the YOUR_WEBSITE_ID and YOUR_DOMAIN.COM with your actual website ID and domain.
3Verifying the setup
Once you've deployed your project:
- Open your production site in the browser.
- Head over to your Trackthemetric dashboard.
- Within a few minutes, you should see traffic starting to appear.