Proxying Flowsery Analytics with PHP

Route Flowsery Analytics through your PHP server to prevent adblocker interference and capture more accurate visitor data.

1. Create the Proxy Endpoints

Set up two PHP files that handle proxied requests:

script.php

<?php
header('Content-Type: application/javascript');
header('Cache-Control: public, max-age=31536000');

$ch = curl_init('https://analytics.flowsery.com/js/script.js');
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$script = curl_exec($ch);
curl_close($ch);

echo $script;

events.php

<?php
header('Content-Type: application/json');

// Resolve the real visitor IP
function getClientIp() {
    if (!empty($_SERVER['HTTP_X_REAL_IP'])) {
        return $_SERVER['HTTP_X_REAL_IP'];
    }
    if (!empty($_SERVER['HTTP_X_FORWARDED_FOR'])) {
        $ips = explode(',', $_SERVER['HTTP_X_FORWARDED_FOR']);
        return trim($ips[0]);
    }
    return $_SERVER['REMOTE_ADDR'] ?? '';
}

// Determine the origin from request headers or build it from server variables
$origin = $_SERVER['HTTP_ORIGIN'] ??
    ($_SERVER['HTTPS'] === 'on' ? 'https://' : 'http://') . $_SERVER['HTTP_HOST'];

$clientIp = getClientIp();

$ch = curl_init('https://analytics.flowsery.com/events');
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, file_get_contents('php://input'));
curl_setopt($ch, CURLOPT_HTTPHEADER, [
    'Content-Type: application/json',
    'User-Agent: ' . $_SERVER['HTTP_USER_AGENT'],
    'Origin: ' . $origin,
    'x-flowsery-ip: ' . $clientIp
]);
$response = curl_exec($ch);
curl_close($ch);

echo $response;

Note: If your application already has an /api/events endpoint, use the data-api attribute on the Flowsery Analytics script tag to point events to a different path. For example, data-api="/flowsery-events" routes data to /flowsery-events instead.

Important: If every visitor appears to be in the same location in your dashboard, confirm that the x-flowsery-ip header is being set to the real visitor IP (not the proxy server IP) when forwarding requests to the Flowsery Analytics /events endpoint.

2. Set Up Web Server Routing

Apache (.htaccess)

RewriteEngine On
RewriteRule ^js/script\.js$ script.php [L]
RewriteRule ^api/events$ events.php [L]

Nginx

location /js/script.js {
    try_files $uri $uri/ /script.php?$query_string;
}

location /api/events {
    try_files $uri $uri/ /events.php?$query_string;
}

3. Modify the Script Tag

Replace the original Flowsery Analytics snippet with the proxied version:

<script defer data-fl-website-id="flid_******" data-domain="your_domain.com" src="/js/script.js"></script>

4. Deploy

After deploying your changes, the proxy configuration activates automatically.

Confirming It Works

To validate that the proxy is functioning correctly:

  1. Navigate to your website
  2. Open your browser's developer tools and switch to the Network tab
  3. Verify that analytics requests are served from your domain rather than analytics.flowsery.com

Troubleshooting

Every visitor appears from the same location

When all visitors show a single geographic location (typically your server's region), the proxy is not forwarding real visitor IPs correctly.

Resolution:

  1. Ensure your proxy includes the x-flowsery-ip header containing the actual visitor IP address (not the server IP) when forwarding requests to the Flowsery Analytics /events endpoint