Best analytics tool I've used in 14 years

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",
    proxyReqOptDecorator: (proxyReqOpts, srcReq) => {
      // Forward client IP for accurate location analytics
      const clientIp = srcReq.headers['x-real-ip'] || 
                        srcReq.headers['x-forwarded-for']?.split(',')[0]?.trim() || 
                        srcReq.ip;
      proxyReqOpts.headers['x-datafast-real-ip'] = clientIp;
      return proxyReqOpts;
    }
  }),
);

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 here

Important: If you notice all visitors showing from the same location in your analytics, make sure you're sending the x-datafast-real-ip header with the actual visitor IP address (not your proxy server IP) when forwarding events to DataFast's /api/events endpoint.

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="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:

  1. Visit your website
  2. Open the network tab in your browser's developer tools
  3. 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:

  1. Make sure your proxy sends the x-datafast-real-ip header with the actual visitor IP address (not your proxy server IP) when forwarding events to DataFast's /api/events endpoint
  2. If the issue persists, contact support with your proxy configuration
Something missing? Suggest features ✍️