Firebase Hosting proxy
Proxy attribu.tech analytics through Firebase Hosting using Cloud Functions.
This is a server-specific guide. See Proxy the tracking script for an overview of why and how proxying works.
Firebase Hosting doesn't support reverse proxy natively. This guide uses Cloud Functions as a workaround to proxy requests to attribu.tech.
1. Set up Firebase Functions
If you haven't already, initialize Firebase Functions in your project:
firebase init functions2. Install dependencies
cd functions && npm install express express-http-proxy3. Create the Cloud Function
Replace or update functions/index.js with an Express app that proxies script and event requests to attribu.tech:
const functions = require("firebase-functions");
const express = require("express");
const proxy = require("express-http-proxy");
const app = express();
// Proxy the attribu.tech tracking script
app.get("/js/attribu.js", proxy("attribu.tech", {
https: true,
proxyReqPathResolver: () => "/js/script.js",
}));
// Proxy the attribu.tech event API
app.post("/api/proxy/events", proxy("attribu.tech", {
https: true,
proxyReqPathResolver: () => "/api/events",
proxyReqOptDecorator: (proxyReqOpts, srcReq) => {
proxyReqOpts.headers["X-Forwarded-For"] =
srcReq.headers["x-forwarded-for"] || srcReq.ip;
return proxyReqOpts;
},
}));
exports.reverseProxy = functions.https.onRequest(app);4. Configure Firebase Hosting rewrites
In your firebase.json, add rewrites so Firebase Hosting routes the proxy paths to your Cloud Function:
{
"hosting": {
"public": "public",
"rewrites": [
{
"source": "/js/attribu.js",
"function": "reverseProxy"
},
{
"source": "/api/proxy/events",
"function": "reverseProxy"
}
]
}
}5. Update your script tag
Point the script src to your proxied path and set data-api-url to your proxied API endpoint:
<script
defer
data-website-id="YOUR_SITE_ID"
data-domain="yourdomain.com"
data-api-url="https://yourdomain.com/api/proxy/events"
src="/js/attribu.js"
></script>Replace
YOUR_SITE_ID with your actual Website ID from attribu.tech. Replace yourdomain.com with your website's root domain.6. Deploy
firebase deploy --only hosting,functionsVerify installation
- Visit your site in an incognito/private window.
- Open your browser's Developer Tools and go to the Network tab.
- Confirm the script loads from
/js/attribu.json your domain. - Confirm the pageview POST goes to
/api/proxy/eventson your domain. - Check your attribu.tech dashboard. A new pageview should appear within a few seconds.
Troubleshooting
- Function not found - Make sure the function name in
firebase.jsonrewrites matches the export name infunctions/index.js(both should bereverseProxy). - Cold start delays - Cloud Functions may take a few seconds on the first request. Subsequent requests will be faster.
- Script loads but no pageviews - Verify the
/api/proxy/eventsrewrite is in yourfirebase.jsonand that the function handles POST requests. - CORS errors - The proxy should eliminate CORS issues since both the script and API are served from your domain. If you still see errors, make sure the proxy is not adding extra CORS headers that conflict.
What's next
- Back to proxy overview
- Nginx proxy setup
- Connect Stripe to attribute revenue to traffic sources.