Install with Caddy
View as MarkdownAdd the Reveata snippet to sites Caddy serves, and proxy the script and events through your own domain with the visitor’s address and your proxy secret.
On this page
Caddy serves your pages, so the snippet goes in the HTML or templates it serves, before </head>. Caddy can also serve the Reveata script from your own domain and pass events on, so fewer visits are lost to ad blockers.
Copy the snippet from your Reveata dashboard (it contains your website’s tracking id):
<script defer data-website-id="ws_your_tracking_id" src="https://reveata.com/js/script.js"></script>Add it to your site
If you can edit the pages (a static site from file_server, or an app behind reverse_proxy), paste the snippet before </head> in the layout every page uses. The framework guides say where: Install the script, Frameworks and site builders.
If you can’t edit the pages, Caddy can add the snippet to every HTML response with the replace-response plugin, which isn’t part of the standard build. Build Caddy with it (xcaddy build --with github.com/caddyserver/replace-response), then in the Caddyfile:
{
order replace after encode
}
example.com {
replace {
</head> `<script defer data-website-id="ws_your_tracking_id" src="https://reveata.com/js/script.js"></script></head>`
}
reverse_proxy localhost:3000 {
header_up -Accept-Encoding
}
}header_up -Accept-Encoding asks your app for uncompressed pages, because the plugin can only change text it can read. Caddy still compresses responses for visitors if you use encode. Reload with caddy reload (or sudo systemctl reload caddy).
Check it works
Open a page in a normal browser window and view its source: the snippet is right before </head>. Then open Realtime in Reveata: your visit appears within a few seconds. If it doesn’t, follow Verify your installation.
Proxy through Caddy
Reveata only trusts the visitor’s address your proxy forwards when the request also carries your website’s proxy secret.
- Create a proxy secret. In Reveata, open your website’s Settings → General, find Proxy secret in Tracking code, and choose Create proxy secret. Copy it (it starts with
dt_proxy_and is shown once). - Give it to Caddy as the environment variable
REVEATA_PROXY_SECRET. With systemd, runsudo systemctl edit caddyand add:
[Service]
Environment=REVEATA_PROXY_SECRET=dt_proxy_your_secretWith Docker Compose, add it under the Caddy service’s environment:.
- Add the two routes to your site’s block in the Caddyfile:
handle /dt/script.js {
rewrite * /js/script.js
reverse_proxy https://reveata.com {
header_up Host {upstream_hostport}
header_up -Cookie
}
}
handle /dt/event {
rewrite * /api/collect
reverse_proxy https://reveata.com {
header_up Host {upstream_hostport}
header_up X-Reveata-Proxy-Secret {env.REVEATA_PROXY_SECRET}
header_up X-Reveata-Client-IP {client_ip}
header_up -Cookie
}
}- Point the snippet at them:
<script defer data-website-id="ws_your_tracking_id" data-api="/dt/event" src="/dt/script.js"></script>- Restart Caddy so it reads the new environment (
sudo systemctl restart caddy). Acaddy reloadis enough for later Caddyfile changes.
Caddy sets X-Forwarded-For itself and passes the browser’s other headers on. The paths can be anything; avoid words like analytics, which some blockers look for.
Behind a load balancer or CDN, {client_ip} is the load balancer’s address until you trust it. Add its ranges in the global options:
{
servers {
trusted_proxies static 173.245.48.0/20 103.21.244.0/22
}
}Use your load balancer’s or CDN’s published ranges (the two above are examples from Cloudflare’s list). With Cloudflare, also add client_ip_headers CF-Connecting-IP in the same servers block.
No server access? The managed proxy does the same with one DNS record. More platforms: Proxy through your own server.
Check the proxy works
- Open
https://your-site/dt/script.js: you should see the script. - Visit your site, open the developer tools’ Network tab, and find the request to
/dt/event. Its response has anx-reveata-proxyheader:trustedmeans the secret was accepted. - In Reveata’s Realtime, your visit shows your own city, not your server’s.
proxy_secret_invalid means the secret is missing, mistyped or rotated: check the environment with sudo systemctl show caddy -p Environment. No header at all means Caddy didn’t send it.
Revenue
Caddy only serves pages; payments are recorded by your provider or your app. Connect your provider in Reveata (Connect a payment provider), and pass the visitor id with each checkout from your app: see the guide for your framework, for example Django, Laravel, Express or PHP.
Common pitfalls
- Forgetting
header_up Host {upstream_hostport}. Without it, Caddy sends your own host name and Reveata can’t answer the request. - The secret in the Caddyfile. Keep it in the environment, so it never ends up in a repository.
- An app route with the same path. Caddy runs the most specific
handlefirst, so/dt/eventreaches Reveata even with a catch-allreverse_proxyto your app. If your app already uses/dt/…, pick other paths. - Caching
/dt/event. Never cache it. The script itself may be cached; Reveata sends the rightCache-Control.