Disabling Google FLoC with a Symfony event subscriber

Published on 2021-05-08 • Modified on 2021-05-08

In this snippet, we see how to disable Google FLoC with a Symfony event subscriber. In a previous snippet, we modified an Apache configuration to deactivate Google Floc globally. In this one, we see how to do it on the fly in a Symfony project with an event subscriber that adds the needed header. Fabpot originally proposed this snippet as a gist (check the "more on the web" button).


<?php

declare(strict_types=1);

namespace App\Subscriber;

use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Symfony\Component\HttpKernel\Event\ResponseEvent;

class PermissionsPolicySubscriber implements EventSubscriberInterface
{
    public static function getSubscribedEvents(): array
    {
        return [
            ResponseEvent::class => 'addPermissionsPolicyHeader',
        ];
    }

    public function addPermissionsPolicyHeader(ResponseEvent $event): void
    {
        $event->getResponse()->headers->set('permissions-policy', 'interest-cohort=()');
    }
}

 More on Stackoverflow   Read the doc  More on the web  Random snippet

  Work with me!