Proxying Flowsery Analytics with Flask

Route Flowsery Analytics through your Flask application to prevent adblocker interference and capture more accurate visitor data.

1. Install Dependencies

pip install flask requests

2. Set Up the Proxy

Add the following routes and helper function to your Flask app:

from flask import Flask, request, Response
import requests

app = Flask(__name__)


def get_client_ip():
    """
    Determine the actual visitor IP address.
    When behind a reverse proxy or CDN, prefer X-Forwarded-For.
    Otherwise, use Flask's request.remote_addr as a fallback.
    """
    xff = request.headers.get("X-Forwarded-For")
    if xff:
        # Comma-separated list: "client, proxy1, proxy2"
        parts = [ip.strip() for ip in xff.split(",")]
        if parts:
            return parts[0]  # the leftmost entry is the original client
    return request.remote_addr


@app.route("/js/script.js")
def proxy_script():
    response = requests.get("https://analytics.flowsery.com/js/script.js")
    return Response(
        response.content,
        content_type="application/javascript",
        headers={"Cache-Control": "public, max-age=31536000"},
    )


@app.route("/api/events", methods=["POST"])
def proxy_events():
    # Copy incoming headers but omit those that should match the target
    excluded = {"host", "content-length"}
    headers = {
        key: value
        for key, value in request.headers.items()
        if key.lower() not in excluded
    }

    # The Origin header is required by the Flowsery Analytics API
    if "Origin" not in headers:
        scheme = request.scheme
        host = request.host
        headers["Origin"] = f"{scheme}://{host}"

    # Attach the real visitor IP for accurate geolocation
    client_ip = get_client_ip()
    headers["x-flowsery-ip"] = client_ip

    # Forward the request body and headers to Flowsery Analytics
    response = requests.post(
        "https://analytics.flowsery.com/events",
        data=request.get_data(),
        headers=headers,
        timeout=5,
    )

    # Return the upstream response to the browser
    return Response(
        response.content,
        status=response.status_code,
        content_type=response.headers.get("Content-Type", "application/json"),
    )

Note: If your application already has an /api/events endpoint, use the data-api attribute on the Flowsery Analytics script tag to send events to a different path. For example, data-api="/flowsery-events" routes data to /flowsery-events instead.

Important: If every visitor appears to be in the same location in your dashboard, confirm that the x-flowsery-ip header is being set to the real visitor IP (not the proxy server IP) when forwarding requests to the Flowsery Analytics /events endpoint.

3. Modify the Script Tag

Replace the original Flowsery Analytics snippet with the proxied version:

<script defer data-fl-website-id="flid_******" data-domain="your_domain.com" src="/js/script.js"></script>

4. Deploy

After deploying your server, the proxy configuration activates automatically.

Confirming It Works

To validate that the proxy is functioning correctly:

  1. Navigate to your website
  2. Open your browser's developer tools and switch to the Network tab
  3. Verify that analytics requests are served from your domain rather than analytics.flowsery.com

Troubleshooting

Every visitor appears from the same location

When all visitors show a single geographic location (typically your server's region), the proxy is not forwarding real visitor IPs correctly.

Resolution:

  1. Ensure your proxy includes the x-flowsery-ip header containing the actual visitor IP address (not the server IP) when forwarding requests to the Flowsery Analytics /events endpoint