Proxy DataFast with Caddy

Learn how to proxy DataFast analytics through Caddy to bypass adblockers and improve accuracy. Caddy's simple configuration makes it easy to set up.

1. Basic Caddy Configuration

Add the following to your Caddyfile:

yourdomain.com {
    # Proxy the analytics script
    handle /js/script.js {
        reverse_proxy https://datafa.st {
            header_up Host {upstream_hostport}
            header_up X-Real-IP {remote_host}
            header_up X-Forwarded-For {remote_host}
            header_up X-Forwarded-Proto {scheme}
        }
        
        # Cache the script for 1 year
        header Cache-Control "public, max-age=31536000"
        header Expires "1y"
    }

    # Proxy the events endpoint
    handle /api/events {
        reverse_proxy https://datafa.st {
            header_up Host {upstream_hostport}
            header_up X-Real-IP {remote_host}
            header_up X-Forwarded-For {remote_host}
            header_up X-Forwarded-Proto {scheme}
        }
    }

    # Your other site configurations...
}

Note: If you already have an /api/events API endpoint, add data-api-url to the DataFast script tag to send events to your own API endpoint. For example, data-api-url="/datafast-events" will send events to /datafast-events instead of /api/events. Read more here

2. Optional: Add Caching Configuration

For better performance, you can add caching:

yourdomain.com {
    # Cache configuration
    cache {
        path /var/cache/caddy
        ttl 1y
        capacity 10GB
    }

    handle /js/script.js {
        cache
        reverse_proxy https://datafa.st {
            header_up Host {upstream_hostport}
            header_up X-Real-IP {remote_host}
            header_up X-Forwarded-For {remote_host}
            header_up X-Forwarded-Proto {scheme}
        }
        
        header Cache-Control "public, max-age=31536000"
        header Expires "1y"
    }

    # ... rest of the configuration
}

3. Update Your Script Tag

Replace your existing DataFast script with the proxied version:

<script
  defer
  data-website-id="dfid_******""
  data-domain="yourdomain.com"
  src="/js/script.js"
></script>

4. Reload Caddy

# Test the configuration
caddy validate

# If the test is successful, reload Caddy
caddy reload

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
Caddy automatically handles HTTPS certificates and HTTP/2, making it a great choice for proxying analytics. Make sure your Caddy server has enough disk space allocated for caching if you're using the cache configuration.

Troubleshooting

All visitors showing from the same location

If your analytics show all visitors from a single location (usually your server's location), your proxy isn't forwarding visitor IPs.

To fix:

  1. Verify your proxy sends x-real-ip or x-forwarded-for headers with the client IP
  2. If the issue persists, contact support with your proxy configuration
Something missing? Suggest features ✍️