Flowsery Analytics mit FastAPI proxyen
Leiten Sie Flowsery Analytics über Ihren FastAPI-Server weiter, um Störungen durch Adblocker zu vermeiden und genauere Besucherdaten zu erfassen.
1. Abhängigkeiten installieren
pip install fastapi uvicorn httpx2. Proxy einrichten
Fügen Sie Folgendes zu Ihrer FastAPI-Anwendung hinzu:
from fastapi import FastAPI, Request
from fastapi.responses import Response
import httpx
app = FastAPI()
def get_client_ip(request: Request) -> str:
"""Resolve the actual visitor IP address"""
# Prefer Cloudflare's CF-Connecting-IP if available
if cf_connecting_ip := request.headers.get("cf-connecting-ip"):
return cf_connecting_ip
# Then X-Real-IP
if x_real_ip := request.headers.get("x-real-ip"):
return x_real_ip
# Fall back to X-Forwarded-For (first entry)
if x_forwarded_for := request.headers.get("x-forwarded-for"):
return x_forwarded_for.split(",")[0].strip()
# Last resort: direct connection host
return request.client.host if request.client else ""
@app.get("/js/main.js")
async def proxy_script():
async with httpx.AsyncClient() as client:
response = await client.get('https://cdn.flowsery.com/main.js')
return Response(
content=response.content,
media_type='application/javascript',
headers={
'Cache-Control': 'public, max-age=31536000'
}
)
@app.post("/api/track")
async def proxy_events(request: Request):
body = await request.json()
# Determine the origin from the request or construct it from the base URL
origin = request.headers.get('origin') or str(request.base_url).rstrip('/')
# Resolve visitor IP for accurate geolocation
client_ip = get_client_ip(request)
async with httpx.AsyncClient() as client:
headers = {
'Content-Type': 'application/json',
'User-Agent': request.headers.get('user-agent'),
'Origin': origin,
'x-forwarded-for': client_ip
}
if client_ip:
# CRITICAL: Flowsery reads this as the authoritative visitor IP. Without it, every visitor resolves to your server's region.
headers['x-flowsery-real-ip'] = client_ip
response = await client.post(
'https://analytics.flowsery.com/analytics/events',
json=body,
headers=headers
)
return Response(
content=response.content,
media_type='application/json',
status_code=response.status_code
)
if __name__ == "__main__":
import uvicorn
uvicorn.run(app, host="0.0.0.0", port=8000)Hinweis: Wenn Ihre Anwendung bereits einen Endpunkt /api/track hat, verwenden Sie das Attribut data-api im Flowsery-Analytics-Skript, um Events an einen anderen Pfad zu senden. Zum Beispiel leitet data-api="/flowsery-events" Daten stattdessen an /flowsery-events.
Wichtig: Wenn im Dashboard jeder Besucher aus demselben Ort zu kommen scheint, prüfen Sie, ob x-flowsery-real-ip beim Weiterleiten an den Flowsery-Analytics-Endpunkt /events auf die echte Besucher-IP gesetzt wird und nicht auf die IP des Proxy-Servers. Wenn FastAPI hinter einem weiteren Proxy läuft (Cloudflare, Vercel usw.), ist request.client.host die IP dieses Upstream-Proxys - lesen Sie stattdessen direkt cf-connecting-ip oder x-real-ip.
3. Skript-Tag anpassen
Ersetzen Sie das ursprüngliche Flowsery-Analytics-Snippet durch die geproxyte Variante:
<script defer data-fl-website-id="flid_******" src="/js/main.js"></script>4. Deploy
Nach dem Deploy Ihres Servers wird die Proxy-Konfiguration automatisch aktiv.
Prüfen, ob es funktioniert
So validieren Sie das Proxy-Setup:
- Öffnen Sie Ihre Website.
- Öffnen Sie die Entwicklerwerkzeuge Ihres Browsers und wechseln Sie zum Tab "Network".
- Prüfen Sie, ob Analytics-Requests über Ihre Domain statt über
analytics.flowsery.comausgeliefert werden.