Proxy DataFast with Flask
Learn how to proxy DataFast analytics through your Flask server to bypass adblockers and improve accuracy.
1. Install Required Dependencies
pip install flask requests
2. Add Proxy Configuration
Add the following to your Flask application:
from flask import Flask, request, Response
import requests
app = Flask(__name__)
def get_client_ip():
"""
Return the real client IP address.
If X-Forwarded-For exists (e.g. behind proxy/CDN), use the first value.
Otherwise, fall back to Flask's request.remote_addr.
"""
xff = request.headers.get("X-Forwarded-For")
if xff:
# Can be a comma-separated list: "client, proxy1, proxy2"
parts = [ip.strip() for ip in xff.split(",")]
if parts:
return parts[0] # left-most is always the original client IP
return request.remote_addr
@app.route("/js/script.js")
def proxy_script():
response = requests.get("https://datafa.st/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 all incoming headers except those that must match the target
excluded = {"host", "content-length"}
headers = {
key: value
for key, value in request.headers.items()
if key.lower() not in excluded
}
# Add the real client IP if not already set
client_ip = get_client_ip()
headers.setdefault("X-Forwarded-For", client_ip)
# Forward the request body and headers to DataFast
response = requests.post(
"https://datafa.st/api/events",
data=request.get_data(),
headers=headers,
timeout=5,
)
# Return DataFast's response back to the browser
return Response(
response.content,
status=response.status_code,
content_type=response.headers.get("Content-Type", "application/json"),
)
3. Update Your Script Tag
Replace your existing DataFast script with the proxied version:
<script
defer
data-website-id="yourwebsiteid"
data-domain="yourdomain.com"
src="/js/script.js"
></script>
4. Deploy your server
The proxy configuration will take effect automatically after deployment.
Verification
To verify the proxy is working:
- Visit your website
- Open the network tab in your browser's developer tools
- Check that analytics requests are going through your domain instead of datafa.st