# Install on Flask

> Add Reveata to the base Jinja template of a Flask app.

Flask renders pages with Jinja templates that 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 in the `templates` folder, often `templates/base.html` or `templates/layout.html`. It is the template your other templates name in `{% extends "base.html" %}`.
2. Paste the snippet before `</head>`:

```jinja
<!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. Pages rendered from templates that don’t extend the base template need the snippet too.

## 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

Flask can pass the script and events through two routes of its own, with the visitor’s address and your proxy secret: see [Proxy with Flask](https://reveata.com/docs/proxy-flask). If NGINX or Caddy sits in front of your app, it can do it instead: [Proxy through NGINX](https://reveata.com/docs/nginx#proxy-through-nginx), [Proxy through Caddy](https://reveata.com/docs/caddy#proxy-through-caddy). Or 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>
```

In the route that creates a Stripe Checkout Session, copy them into its metadata:

```python
import stripe
from flask import redirect, request, url_for

@app.post("/checkout")
def checkout():
    ids = {k: request.form[k] for k in ("reveata_visitor_id", "reveata_session_id") if request.form.get(k)}
    session = stripe.checkout.Session.create(
        mode="payment",
        line_items=[{"price": "price_123", "quantity": 1}],
        success_url=url_for("thanks", _external=True) + "?session_id={CHECKOUT_SESSION_ID}",
        metadata=ids,
    )
    return redirect(session.url, code=303)
```

If Reveata doesn’t connect to your provider, send each payment from your code with the [Payments API](https://reveata.com/docs/payments-api) (`POST https://reveata.com/api/v1/payments` with a website API key, the `transactionId`, `amount`, `currency`, `email` and the saved `visitorId`).

## Common pitfalls

- **Templates that don’t extend the base.** Pages rendered from them need the snippet too.
- **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.
