Giter VIP home page Giter VIP logo

client-bundle's Introduction

Track users between visits in Symfony

Latest Version Software License Build Status Code Coverage

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

client-bundle's People

Contributors

loevgaard avatar

Stargazers

 avatar

Watchers

 avatar

client-bundle's Issues

Ability to disable per request

Hi!

It would be very nice to have an ability to selectively disable the setting of the client cookie via f.x. route parameters:

app_some_route:
    path: /some/very/good/path
    defaults:
        _controller: App\Controller\SomeAction
        _disable_setono_client_id: true

and then in SaveClientIdSubscriber:

        if ($event->getRequest()->attributes->getBoolean('_disable_setono_client_id')) {
            return;
        }

Handle race conditions

In the DoctrineOrmBasedMetadataPersister there is a race condition, both when inserting and updating.

Right now the inserting is handled by catching the exception, which doesn't really handle the problem.

When updating the metadata, it's the one that updates the last that wins. This could be handled with optimistic locking.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.