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 requests

2. 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 run

Verify installation

  1. Visit your site in an incognito/private window.
  2. Open your browser's Developer Tools and go to the Network tab.
  3. Confirm the script loads from /js/attribu.js on your domain.
  4. Confirm the pageview POST goes to /api/proxy/events on your domain.
  5. 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

Copied