Flask proxy
Proxy attribu.tech analytics through your Flask server to bypass ad blockers.
This is a server-specific guide. See Proxy the tracking script for an overview of why and how proxying works.
1. Install dependencies
pip install flask requests2. Add proxy routes
from flask import Flask, request, Response
import requests
app = Flask(__name__)
ATTRIBU_BASE = "https://attribu.tech"
@app.route("/js/attribu.js")
def proxy_script():
resp = requests.get(f"{ATTRIBU_BASE}/js/script.js")
return Response(
resp.content,
status=resp.status_code,
content_type="application/javascript",
headers={"Cache-Control": "public, max-age=31536000"},
)
@app.route("/api/proxy/events", methods=["POST"])
def proxy_events():
origin = request.headers.get("Origin") or request.host_url.rstrip("/")
resp = requests.post(
f"{ATTRIBU_BASE}/api/events",
data=request.get_data(),
headers={
"Content-Type": "application/json",
"User-Agent": request.headers.get("User-Agent", ""),
"Origin": origin,
"X-Forwarded-For": request.remote_addr,
},
timeout=5,
)
return Response(
resp.content,
status=resp.status_code,
content_type="application/json",
)If you already have an
/api/events endpoint, use a different path like /api/proxy/events and set data-api-url on your script tag.3. Update your script tag
<script
defer
data-website-id="YOUR_SITE_ID"
data-domain="yourdomain.com"
data-api-url="https://yourdomain.com/api/proxy/events"
src="/js/attribu.js"
></script>4. Run your server
flask runVerify installation
- Visit your site in an incognito/private window.
- Open your browser's Developer Tools and go to the Network tab.
- Confirm the script loads from
/js/attribu.json your domain. - Confirm the pageview POST goes to
/api/proxy/eventson your domain. - Check your attribu.tech dashboard. A new pageview should appear within a few seconds.
Troubleshooting
All visitors showing from the same location
Your proxy isn't forwarding visitor IPs. Make sure X-Forwarded-For is set to request.remote_addr in the proxy headers.
What's next
- FastAPI proxy setup
- Back to proxy overview
- Connect Stripe to attribute revenue to traffic sources.