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__)

@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():
    response = requests.post(
        'https://datafa.st/api/events',
        json=request.get_json(),
        headers={
            'Content-Type': 'application/json',
            'User-Agent': request.headers.get('User-Agent')
        }
    )
    return Response(
        response.content,
        content_type='application/json',
        status=response.status_code
    )

if __name__ == '__main__':
    app.run()

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:

  1. Visit your website
  2. Open the network tab in your browser's developer tools
  3. Check that analytics requests are going through your domain instead of datafa.st
Something missing? Suggest features ✍️