Proxying Flowsery Analytics with Laravel
Route Flowsery Analytics through your Laravel application to prevent adblocker interference and capture more accurate visitor data.
1. Generate the Proxy Controller
Create a dedicated controller for handling proxy requests:
php artisan make:controller FlowseryProxyController
2. Implement the Proxy Logic
Add the following to app/Http/Controllers/FlowseryProxyController.php:
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Http;
use Illuminate\Support\Facades\Cache;
class FlowseryProxyController extends Controller
{
public function script()
{
// Cache the tracking script for 1 day
return Cache::remember('flowsery_script', 86400, function () {
$response = Http::get('https://analytics.flowsery.com/js/script.js');
return response($response->body(), 200, [
'Content-Type' => 'application/javascript',
'Cache-Control' => 'public, max-age=86400'
]);
});
}
public function events(Request $request)
{
// Determine the origin from the request or build it from the URL
$origin = $request->header('Origin') ?? $request->getSchemeAndHttpHost();
// Resolve the real visitor IP for accurate geolocation
$clientIp = $request->header('X-Real-IP')
?? $request->header('X-Forwarded-For')
?? $request->ip();
if (str_contains($clientIp, ',')) {
$clientIp = trim(explode(',', $clientIp)[0]);
}
$response = Http::withHeaders([
'User-Agent' => $request->header('User-Agent'),
'Content-Type' => 'application/json',
'Origin' => $origin,
'x-flowsery-ip' => $clientIp
])->post('https://analytics.flowsery.com/events', $request->all());
return response($response->body(), $response->status());
}
}
Note: If your application already has an /api/events route, add data-api to the Flowsery Analytics script tag to direct events to a different path. For example, data-api="/flowsery-events" sends data to /flowsery-events instead.
3. Register Routes
Add the following to routes/web.php:
Route::get('/js/script.js', [FlowseryProxyController::class, 'script']);
Route::post('/api/events', [FlowseryProxyController::class, 'events']);
4. 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>
5. Deploy
After deploying your application, the proxy configuration activates automatically.
Confirming It Works
To validate that the proxy is functioning correctly:
- Navigate to your website
- Open your browser's developer tools and switch to the Network tab
- 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:
- Ensure your proxy includes the
x-flowsery-ipheader containing the actual visitor IP address (not the server IP) when forwarding requests to the Flowsery Analytics/eventsendpoint