Proxy with Firebase Hosting
View as MarkdownServe the Reveata script and forward events through a Cloud Function behind Firebase Hosting rewrites, with your proxy secret.
On this page
Firebase Hosting rewrites can only point to files, Cloud Functions or Cloud Run, not to another website, so a small function does the forwarding. Cloud Functions need the pay-as-you-go (Blaze) plan.
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. - Store it in Secret Manager for your function, which asks for the value:
firebase functions:secrets:set REVEATA_PROXY_SECRETThe function
In functions/index.js (2nd generation functions, Node.js 18 or later):
const { onRequest } = require('firebase-functions/v2/https');
const { defineSecret } = require('firebase-functions/params');
const proxySecret = defineSecret('REVEATA_PROXY_SECRET');
const UPSTREAM = 'https://reveata.com';
const PASS = [
'user-agent',
'accept-language',
'origin',
'referer',
'sec-fetch-dest',
'sec-fetch-mode',
'sec-fetch-site',
];
exports.reveata = onRequest({ secrets: [proxySecret] }, async (req, res) => {
if (req.method === 'GET' && req.path === '/dt/script.js') {
const r = await fetch(`${UPSTREAM}/js/script.js`);
res.status(r.status);
res.set('content-type', 'text/javascript; charset=utf-8');
res.set('cache-control', r.headers.get('cache-control') ?? 'public, max-age=86400');
res.send(Buffer.from(await r.arrayBuffer()));
return;
}
if (req.method === 'POST' && req.path === '/dt/event') {
const body = req.rawBody ?? Buffer.alloc(0);
if (body.length > 16 * 1024) {
res.status(413).end();
return;
}
const headers = {
'content-type': 'text/plain',
'x-reveata-proxy-secret': proxySecret.value(),
};
const ip = (req.get('x-forwarded-for') ?? '').split(',')[0].trim();
if (ip) headers['x-reveata-client-ip'] = ip;
for (const name of PASS) if (req.get(name)) headers[name] = req.get(name);
const r = await fetch(`${UPSTREAM}/api/collect`, { method: 'POST', headers, body });
res.set('cache-control', 'no-store');
const diagnostic = r.headers.get('x-reveata-proxy');
if (diagnostic) res.set('x-reveata-proxy', diagnostic);
res.status(r.status).end();
return;
}
res.status(404).end();
});It forwards the body exactly as received (req.rawBody), with the visitor's browser headers, and never your cookies.
The rewrites
In firebase.json, send both paths to the function, before any catch-all rewrite of a single-page app:
{
"hosting": {
"public": "dist",
"rewrites": [
{ "source": "/dt/script.js", "function": { "functionId": "reveata" } },
{ "source": "/dt/event", "function": { "functionId": "reveata" } },
{ "source": "**", "destination": "/index.html" }
]
}
}Deploy with firebase deploy --only functions,hosting, then point the snippet at the two paths:
<script defer data-website-id="ws_your_tracking_id" data-api="/dt/event" src="/dt/script.js"></script>Hosting caches function responses only when they say so: the script response allows it, and events answer no-store.
The visitor's address
A request that passes through Hosting and reaches the function carries the visitor's address as the first entry of X-Forwarded-For, which is what the function forwards. The browser can put its own value at the front of that header, so treat the location of a single visit as a hint: Reveata uses this address only to locate the visit, apply your IP and country exclusions, tell cookieless visitors apart and limit events per visitor, and never stores it. If you see your function's region instead of your city in the check below, log req.get('x-forwarded-for') once to see what your setup receives.
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 not, read the x-reveata-proxy response header of /dt/event in your browser's network tab (the function passes it on), and see Check that it works for what each value means.