# Install on Django

> Add MetricRaven to the base template your Django pages extend.

Source: https://metricraven.com/docs/install-django  
Markdown: https://metricraven.com/docs/install-django.md  
Section: Installation guides

Add the tag once to the base template that your other templates extend with `{% extends "base.html" %}`.

1. **Copy your snippet**

   In MetricRaven, open your site's **Settings → Install** card and copy the script tag. It already contains your site ID.

2. **Paste it into the head of your base template**

   Replace `YOUR_SITE_ID` with the public site ID from your site's **Settings → Install** card. The site ID is public and safe in browser code; never put your secret key in a page.

   ```django title="templates/base.html"
   {# templates/base.html #}
   <!doctype html>
   <html lang="en">
     <head>
       <meta charset="utf-8">
       <title>{% block title %}My site{% endblock %}</title>
       <script
         defer
         src="https://metricraven.com/js/script.js"
         data-site-id="YOUR_SITE_ID"
       ></script>
       {% block extra_head %}{% endblock %}
     </head>
     <body>
       {% block content %}{% endblock %}
     </body>
   </html>
   ```

3. **Deploy and open your site**

   `runserver` on `localhost` is not tracked by default, so check your deployed site.

> **Verify it works**
>
> Open your live website in a normal browser window (not an automated or embedded one). In the browser's Network panel, look for `script.js` and then a request to `/api/v1/collect` whose response contains `ok: true`. Open your site's **Traffic** tab and choose **Refresh**; the selected dates must include your visit.
>
> Nothing showing? See [Troubleshooting](https://metricraven.com/docs/troubleshooting).

## Optional: read the site ID from settings

Useful if staging and production report to different MetricRaven sites. The site ID is public, so this is about convenience, not secrecy.

```python title="settings.py and context_processors.py"
# settings.py · optional: keep the site ID out of templates
METRICRAVEN_SITE_ID = os.environ.get("METRICRAVEN_SITE_ID", "")

# context_processors.py in one of your apps
from django.conf import settings

def metricraven(request):
    return {"METRICRAVEN_SITE_ID": settings.METRICRAVEN_SITE_ID}

# Then add "yourapp.context_processors.metricraven" to
# TEMPLATES[0]["OPTIONS"]["context_processors"] and in base.html use:
# data-site-id="{{ METRICRAVEN_SITE_ID }}"
```

## Notes

- Templates that don't extend the base template (for example a standalone landing page) need the tag too.
- If you set a Content Security Policy (for example with django-csp), allow `https://metricraven.com` in `script-src` and `connect-src`.
- Django admin pages use their own templates and are not tracked, which is usually what you want.
- To serve the script from your own domain, see the proxy recipes for [Nginx](https://metricraven.com/docs/proxy-nginx) or [Caddy](https://metricraven.com/docs/proxy-caddy), which usually sit in front of Django.

Optional script attributes are listed in [Install & verify](https://metricraven.com/docs/install). To exclude your own visits, see [Exclude your visits](https://metricraven.com/docs/exclude-visits).
