Skip to content

Consent

Automatic page views can wait for consent. Implement the resolver:

namespace App\Support;
use Fomvasss\Visits\Contracts\ConsentResolverInterface;
use Illuminate\Http\Request;
class CookieConsentResolver implements ConsentResolverInterface
{
public function hasConsent(Request $request): bool
{
return $request->cookie('cookie_consent') === 'accepted';
}
}
config/visits.php
'consent' => [
'require_consent' => true,
'resolver' => \App\Support\CookieConsentResolver::class,
],

The resolver is resolved from the container on every request TrackVisit would otherwise track. Without consent the request is passed through untouched: no job, no cookie.

A cookie read in hasConsent() must be readable by Laravel: a cookie set by a JavaScript consent banner is not encrypted, so add its name to the EncryptCookies exceptions:

bootstrap/app.php
->withMiddleware(function (Middleware $middleware) {
$middleware->encryptCookies(except: ['cookie_consent']);
})