setono/client-bundle

将客户端库集成到您的 Symfony 应用程序中

安装数量: 2,930

依赖项: 3

建议者: 0

安全性: 0

星标: 1

关注者: 1

分支: 0

开放问题: 1

类型:symfony-bundle

v1.0.0-beta.4 2024-05-21 09:02 UTC

This package is auto-updated.

Last update: 2024-09-21 09:55:40 UTC


README

Latest Version Software License Build Status Code Coverage

此包允许您在访问之间跟踪您的用户,并为每个用户添加自定义元数据。

默认情况下,该包将存储一个名为 setono_client_id 的cookie,其中包含客户端ID、创建时间戳和最后看到的时间戳。

它还会为每个客户端ID创建一个新的包含元数据的表。元数据功能是延迟加载的,所以如果您不使用它,它将不会查询数据库。

安装

要安装此包,只需运行

composer require setono/client-bundle

迁移您的数据库

php bin/console doctrine:migrations:diff
php bin/console doctrine:migrations:migrate

使用方法

访问带有一些元数据的 Client 对象

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
        ]);
    }
}

在事件订阅者中设置一些元数据

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'));
    }
}

访问cookie

客户端ID存储在一个名为 setono_client_id 的cookie中(默认情况下)。该cookie还包含其他信息,如客户端首次和最后一次被看到的时间。您可以像这样访问cookie

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
    }
}

配置

以下是您可以进行的配置

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