setono / client-bundle
Integrate the client library into your Symfony application
Installs: 4 094
Dependents: 4
Suggesters: 0
Security: 0
Stars: 1
Watchers: 1
Forks: 0
Open Issues: 1
Type:symfony-bundle
Requires
- php: >=8.1
- doctrine/dbal: ^2.13 || ^3.0 || ^4.0
- doctrine/doctrine-bundle: ^2.7
- doctrine/orm: ^2.0 || ^3.0
- doctrine/persistence: ^2.5 || ^3.0
- psr/event-dispatcher: ^1.0
- psr/log: ^1.1 || ^2.0 || ^3.0
- setono/client: ^1.1.1
- setono/doctrine-orm-trait: ^1.1
- symfony/config: ^6.4 || ^7.0
- symfony/dependency-injection: ^6.4 || ^7.0
- symfony/event-dispatcher: ^6.4 || ^7.0
- symfony/http-foundation: ^6.4 || ^7.0
- symfony/http-kernel: ^6.4 || ^7.0
- symfony/var-exporter: ^6.4 || ^7.0
Requires (Dev)
- matthiasnoback/symfony-dependency-injection-test: ^4.3.1 || ^5.1
- phpspec/prophecy-phpunit: ^2.2
- phpunit/phpunit: ^10.5
- psalm/plugin-phpunit: ^0.19
- psalm/plugin-symfony: ^5.1
- setono/code-quality-pack: ^2.7
This package is auto-updated.
Last update: 2024-11-21 10:27:49 UTC
README
This bundle allows you to track your users between visits and add custom metadata to each user.
Out of the box, the bundle will store a cookie named setono_client_id
which contains the client id, a created timestamp and a last seen timestamp.
It will also create a new table with metadata for each client id. The metadata functionality is lazy loaded, so if you don't use it, it will not query the database.
Installation
To install this bundle, simply run:
composer require setono/client-bundle
Migrate your database
php bin/console doctrine:migrations:diff php bin/console doctrine:migrations:migrate
Usage
Access the Client
object along with some metadata
use Setono\Client\Client; final class YourController extends AbstractController { public function index(Client $client): Response { return $this->render('your_template.html.twig', [ 'id' => $client->id, 'some_metadata' => $client->metadata->get('some_metadata_key'), // this call will initialize the metadata object from the database ]); } }
Set some metadata in an event subscriber
use Setono\Client\Client; use Symfony\Component\EventDispatcher\EventSubscriberInterface; use Symfony\Component\HttpKernel\KernelEvents; use Symfony\Component\HttpKernel\Event\RequestEvent; use Setono\ClientBundle\Context\ClientContextInterface; final class YourEventSubscriber implements EventSubscriberInterface { public function __construct(private readonly ClientContextInterface $clientContext) {} public static function getSubscribedEvents(): array { return [ KernelEvents::REQUEST => 'setMetadata', ]; } public function setMetadata(RequestEvent $event): void { if (!$event->isMainRequest() || !$event->getRequest()->query->has('gclid')) { return; } $this->clientContext->getClient()->metadata->set('google_click_id', $event->getRequest()->query->get('gclid')); } }
Access the cookie
The client id is saved in a cookie named setono_client_id
(by default). The cookie also holds other information, like
the first and last time the client was seen. You can access the cookie like this:
use Setono\ClientBundle\CookieProvider\CookieProviderInterface; final class YourService { public function __construct(private readonly CookieProviderInterface $cookieProvider) {} public function call(): void { $cookie = $this->cookieProvider->getCookie(); if(null === $cookie) { return; // no cookie found } $clientId = $cookie->clientId; // the client id $created = $cookie->firstSeenAt; // the timestamp when the client was first seen $lastSeen = $cookie->lastSeenAt; // the timestamp when the client was last seen } }
Configuration
Here's the configuration you are able to do:
setono_client: cookie: # The name of the cookie that holds the client id. NOTICE that if you change this value, all clients with a cookie with the old name will be considered new clients name: setono_client_id # The expiration of the cookie. This is a string that can be parsed by strtotime expiration: '+365 days' # If you want to use a custom metadata class, you can specify it here metadata_class: Setono\ClientBundle\Entity\Metadata