Proxy DataFast with Firebase Hosting
Learn how to proxy DataFast analytics through Firebase Hosting using Cloud Functions to bypass adblockers and improve accuracy.
Note: Firebase Hosting does not support reverse proxy and rewrite rules for external destinations natively. This guide uses Firebase Cloud Functions as a workaround.
1. Set up Firebase Functions (if not already done)
If you haven't yet, add support of Firebase Functions to your Firebase project:
firebase init functions
Follow the instructions according to your setup. At the end, you should have a /functions
folder in your project.
2. Install Dependencies
Make sure you're in the functions/
folder and install required dependencies:
cd functions/
npm i -s express express-http-proxy
3. Create ReverseProxy Firebase Function
Create or update your functions/index.js
file with the following code:
const { onRequest } = require("firebase-functions/v2/https");
const express = require("express");
const proxy = require("express-http-proxy");
const app = express();
app.set("trust proxy", true);
// Proxy the DataFast script
app.use(
"/js/script.js", proxy("https://datafa.st", {
proxyReqPathResolver: () => "/js/script.js",
}),
);
// Proxy the events endpoint
app.use(
"/api/events", proxy("https://datafa.st", {
proxyReqPathResolver: () => "/api/events",
}),
);
exports.reverseProxy = onRequest(app);
4. Configure Firebase Hosting Rewrites
Update your firebase.json
to point to the reverseProxy
function:
{
"hosting": {
"public": "public",
"ignore": [
"firebase.json",
"**/.*",
"**/node_modules/**"
],
"rewrites": [
{
"source": "/js/script.js",
"function": "reverseProxy"
},
{
"source": "/api/events",
"function": "reverseProxy"
}
]
},
"functions": [
{
"source": "functions",
"codebase": "default",
"ignore": ["node_modules", ".git", "firebase-debug.log", "firebase-debug.*.log", "*.local"]
}
]
}
5. Update Your Script Tag
Replace your existing DataFast script with the proxied version:
<script
defer
data-website-id="yourwebsiteid"
data-domain="yourdomain.com"
src="/js/script.js"
></script>
6. Deploy Firebase Hosting and Functions
Deploy both hosting and functions:
firebase deploy --only hosting,functions
Verification
To verify the proxy is working:
- Visit your website
- Open the network tab in your browser's developer tools
- Check that analytics requests are going through your domain instead of datafa.st