# Install on Django

> Add Reveata to the base template of a Django project.

Django pages usually extend one base template, so the snippet goes there once.

Copy the snippet from your Reveata dashboard (it contains your website’s tracking id):

```html
<script
  defer
  data-website-id="ws_your_tracking_id"
  src="https://reveata.com/js/script.js"
></script>
```

## Add it to your site

1. Open your base template, often `templates/base.html` (or `<app>/templates/<app>/base.html`). It is the template your other templates name in `{% extends "base.html" %}`.
2. Paste the snippet before `</head>`:

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

3. Deploy. Templates that don’t extend the base template (for example a custom error page) need the snippet too.

The Django admin uses its own templates, so it isn’t tracked.

## Check it works

Open your site in a private window, then check that the visit appears in your Reveata dashboard. If it doesn’t, follow [Verify your installation](https://reveata.com/docs/verify-your-installation). The script ignores `localhost` by default, so visits to the development server stay out of your numbers.

## Goals

Add `data-reveata-goal="signup"` to a button or link in your templates, or call `reveata('goal', 'signup')` from JavaScript. See [Custom goals](https://reveata.com/docs/custom-goals).

## Serve it from your own domain

Django apps usually run behind NGINX or Caddy, which can pass the script and events through your own domain with the visitor’s address and your proxy secret: see [Proxy through NGINX](https://reveata.com/docs/nginx#proxy-through-nginx) or [Proxy through Caddy](https://reveata.com/docs/caddy#proxy-through-caddy). Without access to the web server, use the [managed proxy](https://reveata.com/docs/managed-proxy): one DNS record, no code.

## Revenue

First connect your payment provider in Reveata: see [Connect a payment provider](https://reveata.com/docs/connect-a-payment-provider). Then pass the Reveata visitor id with each checkout, so the payment is credited to the visit that brought the buyer.

In the template with your buy button, add two hidden fields and fill them when the form is sent:

```html
<form method="post" action="/checkout">
  <input type="hidden" name="reveata_visitor_id">
  <input type="hidden" name="reveata_session_id">
  <button>Buy</button>
</form>
<script>
  // Fill the Reveata ids just before any form with these fields is sent.
  document.addEventListener('submit', function (e) {
    var ids = window.reveata && window.reveata.metadata ? window.reveata.metadata() : {};
    Object.keys(ids).forEach(function (name) {
      var input = e.target.querySelector('input[name="' + name + '"]');
      if (input) input.value = ids[name];
    });
  });
</script>
```

Add `{% csrf_token %}` inside the form.

In the view that creates a Stripe Checkout Session, copy the two fields into its metadata:

```python
# views.py
import stripe
from django.shortcuts import redirect
from django.views.decorators.http import require_POST

@require_POST
def checkout(request):
    ids = {k: request.POST[k] for k in ("reveata_visitor_id", "reveata_session_id") if request.POST.get(k)}
    session = stripe.checkout.Session.create(
        mode="payment",
        line_items=[{"price": "price_123", "quantity": 1}],
        success_url=request.build_absolute_uri("/thanks?session_id={CHECKOUT_SESSION_ID}"),
        metadata=ids,
    )
    return redirect(session.url, status=303)
```

For other providers, the same two ids go in their custom data: see the provider’s page under [Connect a payment provider](https://reveata.com/docs/connect-a-payment-provider).

**Payments from your own billing.** If Reveata doesn’t connect to your provider, send each payment from Python with the [Payments API](https://reveata.com/docs/payments-api):

```python
import os, requests

requests.post(
    "https://reveata.com/api/v1/payments",
    headers={"Authorization": f"Bearer {os.environ['REVEATA_SITE_KEY']}"},
    json={
        "transactionId": f"order_{order.id}",
        "amount": float(order.total_without_tax),  # major units, e.g. 49.00
        "currency": "USD",
        "email": order.email,
        "visitorId": order.reveata_visitor_id,  # saved from the hidden field
    },
    timeout=10,
)
```

## Common pitfalls

- **Templates that don’t extend the base.** Error pages and standalone templates need the snippet too.
- **The admin.** Django’s admin isn’t tracked, which is usually what you want.
- **Testing on localhost.** The script ignores `localhost` unless you add `data-allow-localhost="true"`, so test the deployed site.
- **A Content Security Policy.** If your app sends one, allow the Reveata script and its endpoint: see [Content Security Policy](https://reveata.com/docs/content-security-policy).
- **Counting payments twice.** If a provider is connected in Reveata, don’t also send its payments with the Payments API.
