Flowsery Analytics'i FastAPI ile proxy'leme
Reklam engelleyicilerin müdahalesini önlemek ve daha doğru ziyaretçi verisi toplamak için Flowsery Analytics'i FastAPI sunucunuz üzerinden yönlendirin.
1. Bağımlılıkları kurun
pip install fastapi uvicorn httpx2. Proxy'yi kurun
Aşağıdakini FastAPI uygulamanıza ekleyin:
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)Not: Uygulamanızda zaten bir /api/track uç noktası varsa, olayları farklı bir yola yönlendirmek için Flowsery Analytics betik etiketinde data-api özniteliğini kullanın. Örneğin data-api="/flowsery-events", verileri /flowsery-events adresine gönderir.
Önemli: Panelinizde tüm ziyaretçiler aynı konumda görünüyorsa, istekleri Flowsery Analytics /events uç noktasına iletirken x-flowsery-real-ip başlığının proxy sunucusunun IP'sine değil gerçek ziyaretçi IP'sine ayarlandığını doğrulayın. FastAPI başka bir proxy'nin (Cloudflare, Vercel vb.) arkasındaysa request.client.host o üst proxy olur; bunun yerine cf-connecting-ip veya x-real-ip değerini doğrudan okuyun.
3. Betik etiketini değiştirin
Orijinal Flowsery Analytics kodunu proxy'lenmiş sürümle değiştirin:
<script defer data-fl-website-id="flid_******" src="/js/main.js"></script>4. Yayına alın
Sunucunuzu yayına aldıktan sonra proxy yapılandırması otomatik olarak etkinleşir.
Çalıştığını doğrulama
Proxy'nin doğru çalıştığını doğrulamak için:
- Web sitenize gidin
- Tarayıcınızın geliştirici araçlarını açın ve Network sekmesine geçin
- Analitik isteklerinin analytics.flowsery.com yerine kendi alan adınızdan sunulduğunu doğrulayın