fix: send pageviews to the analytics API correctly
- Use the correct endpoint (analytics.mattitja.se instead of the non-existent .cloud domain) - Authenticate with the site ingest secret as a Bearer token so the API trusts the forwarded visitor IP and user agent - Forward the visitor Accept-Language header (required by bot detection) - Only track successful GET requests rendering HTML - Short timeouts and a file-based circuit breaker that pauses sending after connection failures or 5xx responses, so no queue, worker or cache is needed and a down API never slows the host site - Log rejected requests instead of failing silently
This commit is contained in:
11 files changed
+298
-26
No files matched your search
@@ -48,7 +48,7 @@ it('captures analytics data for web requests', function () {
|
||||
Http::assertSent(function ($request) {
|
||||
$data = $request->data();
|
||||
|
||||
return $request->url() === 'https://analytics.mattitja.cloud/api/send' &&
|
||||
return $request->url() === 'https://analytics.mattitja.se/api/send' &&
|
||||
$data['k'] === 'testsiteky' &&
|
||||
$data['u'] === 'https://example.com/test' &&
|
||||
$data['r'] === 'https://google.com' &&
|
||||
@@ -140,3 +140,50 @@ it('handles empty user agent gracefully', function () {
|
||||
return array_key_exists('ua', $data) && $data['ua'] === '';
|
||||
});
|
||||
});
|
||||
|
||||
it('does not capture analytics without an ingest secret', function () {
|
||||
config(['mattitja-analytics.ingest_secret' => null]);
|
||||
|
||||
handleRequest(createMiddleware(), createTestRequest());
|
||||
|
||||
Http::assertNothingSent();
|
||||
});
|
||||
|
||||
it('forwards the visitor Accept-Language header', function () {
|
||||
$request = createTestRequest();
|
||||
$request->headers->set('Accept-Language', 'sv-SE,sv;q=0.9');
|
||||
|
||||
handleRequest(createMiddleware(), $request);
|
||||
|
||||
Http::assertSent(fn ($request) => $request->hasHeader('Accept-Language', 'sv-SE,sv;q=0.9'));
|
||||
});
|
||||
|
||||
it('ignores non-GET requests', function () {
|
||||
handleRequest(createMiddleware(), createTestRequest(method: 'POST'));
|
||||
|
||||
Http::assertNothingSent();
|
||||
});
|
||||
|
||||
it('ignores JSON responses and redirects', function () {
|
||||
$middleware = createMiddleware();
|
||||
$request = createTestRequest();
|
||||
|
||||
foreach ([
|
||||
new \Illuminate\Http\JsonResponse(['ok' => true]),
|
||||
new \Illuminate\Http\RedirectResponse('https://example.com/elsewhere'),
|
||||
] as $response) {
|
||||
$result = $middleware->handle($request, fn () => $response);
|
||||
$middleware->terminate($request, $result);
|
||||
}
|
||||
|
||||
Http::assertNothingSent();
|
||||
});
|
||||
|
||||
it('ignores requests expecting JSON', function () {
|
||||
$request = createTestRequest();
|
||||
$request->headers->set('Accept', 'application/json');
|
||||
|
||||
handleRequest(createMiddleware(), $request);
|
||||
|
||||
Http::assertNothingSent();
|
||||
});
|
||||
Reference in new issue
Block a user