> ## Documentation Index
> Fetch the complete documentation index at: https://help.trfyx.com/llms.txt
> Use this file to discover all available pages before exploring further.

# Proxy Trfyx with Firebase Hosting

> Proxy Trfyx analytics from Firebase Hosting with a Cloud Function.

Firebase Hosting does not directly reverse-proxy arbitrary external destinations. Use a Cloud Function as the proxy and route your hosting paths to it.

<Steps>
  <Step title="Initialize Functions">
    ```bash Terminal theme={"dark"}
    firebase init functions
    cd functions
    npm install express express-http-proxy
    ```
  </Step>

  <Step title="Create the proxy">
    ```js functions/index.js theme={"dark"}
    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);

    app.use(
      "/script.js",
      proxy("https://app.trfyx.com", {
        proxyReqPathResolver: () => "/script.js",
      }),
    );

    app.use(
      "/api/events",
      proxy("https://app.trfyx.com", {
        proxyReqPathResolver: () => "/api/events",
      }),
    );

    exports.trfyxProxy = onRequest(app);
    ```
  </Step>

  <Step title="Configure Hosting rewrites">
    Point `/script.js` and `/api/events` to the deployed function in `firebase.json`:

    ```json firebase.json theme={"dark"}
    {
      "hosting": {
        "rewrites": [
          { "source": "/script.js", "function": "trfyxProxy" },
          { "source": "/api/events", "function": "trfyxProxy" }
        ]
      }
    }
    ```
  </Step>

  <Step title="Update the tracker">
    Replace the default Trfyx script with the proxied path:

    ```html index.html theme={"dark"}
    <script
      defer
      data-website-id="YOUR_TRFYX_PROJECT_ID"
      data-domain="yourdomain.com"
      src="/script.js"
    ></script>
    ```
  </Step>

  <Step title="Verify installation">
    Deploy and verify the two requests in your browser Network panel. Confirm the visit appears in Trfyx.
  </Step>
</Steps>

## Next steps

<CardGroup cols={2}>
  <Card title="Proxy setup" icon="signs-post" href="/proxy-guide">
    Read the full proxy setup guide
  </Card>

  <Card title="Managed Proxy" icon="shield-halved" href="/managed-proxy">
    Let Trfyx manage the proxy for you
  </Card>

  <Card title="Goals" icon="bullseye" href="/goals">
    Start tracking goals with Trfyx
  </Card>

  <Card title="Troubleshooting" icon="life-ring" href="/troubleshooting">
    Fix common proxy issues
  </Card>
</CardGroup>


## Related topics

- [Proxy Trfyx through your domain](/proxy-guide.md)
- [Proxy Trfyx through Vue.js](/vuejs-proxy.md)
- [Proxy Trfyx through Next.js](/nextjs-proxy.md)
- [Proxy Trfyx with Laravel](/laravel-proxy.md)


This documentation is built and hosted on [Mintlify](https://mintlify.com), a developer documentation platform.