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);
Note: If you already have an
/api/events API endpoint, add data-api-url to the DataFast script tag to send events to your own API endpoint. For example, data-api-url="/datafast-events" will send events to /datafast-events instead of /api/events. Read more hereImportant: If you notice all visitors showing from the same location in your analytics, use the managed proxy for automatic visitor IP handling, or review your proxy provider's forwarding settings.
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="dfid_******"
data-domain="your_domain.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
Troubleshooting
All visitors showing from the same location
If your analytics show all visitors from a single location (usually your server's location), your proxy isn't forwarding visitor IPs.
To fix:
- Use the managed proxy for automatic visitor IP handling, or review your proxy provider's forwarding settings.