Proxy with Flask
View as MarkdownServe the Reveata script and forward events through two Flask routes, with the visitor's IP and your proxy secret.
On this page
Two routes in your Flask app stand in for Reveata's addresses, so the browser only talks to your domain. The code uses requests (pip install requests).
Before you start
- In Reveata, open your website, then Settings → General → Tracking code, and choose Create proxy secret under Proxy secret (admins and the owner can). Copy it: it starts with
dt_proxy_and is shown once. - Set it as the environment variable
REVEATA_PROXY_SECRETwhere the app runs (your host's environment settings, a systemdEnvironment=line, or a.envfile that is never committed). Keep it out of templates and static files.
The routes
import os
import requests
from flask import Flask, Response, request
app = Flask(__name__)
UPSTREAM = "https://reveata.com"
PASS = [
"User-Agent",
"Accept-Language",
"Origin",
"Referer",
"Sec-Fetch-Dest",
"Sec-Fetch-Mode",
"Sec-Fetch-Site",
]
@app.get("/dt/script.js")
def reveata_script():
r = requests.get(f"{UPSTREAM}/js/script.js", timeout=10)
resp = Response(r.content, status=r.status_code, mimetype="text/javascript")
resp.headers["Cache-Control"] = r.headers.get("Cache-Control", "public, max-age=86400")
if "ETag" in r.headers:
resp.headers["ETag"] = r.headers["ETag"]
return resp
@app.post("/dt/event")
def reveata_event():
body = request.get_data(cache=False)
if len(body) > 16 * 1024:
return Response(status=413)
headers = {
"Content-Type": "text/plain",
"X-Reveata-Proxy-Secret": os.environ.get("REVEATA_PROXY_SECRET", ""),
"X-Reveata-Client-IP": request.remote_addr or "",
}
for name in PASS:
value = request.headers.get(name)
if value:
headers[name] = value
r = requests.post(f"{UPSTREAM}/api/collect", data=body, headers=headers, timeout=10)
resp = Response(status=r.status_code)
resp.headers["Cache-Control"] = "no-store"
if "x-reveata-proxy" in r.headers:
resp.headers["x-reveata-proxy"] = r.headers["x-reveata-proxy"]
return respThe event route forwards the body unchanged, with the visitor's browser headers, and never your cookies. With Flask-WTF's CSRFProtect, exempt it (@csrf.exempt): the tracker sends no CSRF token.
Then point the snippet at the two routes:
<script defer data-website-id="ws_your_tracking_id" data-api="/dt/event" src="/dt/script.js"></script>Behind a load balancer or reverse proxy
request.remote_addr is the address that connected to your app. Behind Nginx, a load balancer or a platform router, that is their address. Tell Flask how many proxies sit in front, so it reads the visitor's address from X-Forwarded-For:
from werkzeug.middleware.proxy_fix import ProxyFix
app.wsgi_app = ProxyFix(app.wsgi_app, x_for=1) # one proxy in frontCount the proxies exactly: with too high a number, a visitor can choose the address you forward.
Check it works
Open /dt/script.js on your site: you should see the script. Visit a page, then open Realtime in Reveata: your visit should appear within seconds, in your own city. If it shows your server's location, read the x-reveata-proxy response header of /dt/event in your browser's network tab (the route passes it on), and see Check that it works for what each value means.