PHP code example of lexik / data-layer-bundle

1. Go to this page and download the library: Download lexik/data-layer-bundle library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

lexik / data-layer-bundle example snippets




namespace AppBundle\Listener;

use Lexik\Bundle\DataLayerBundle\Manager\DataLayerManager;

/**
 * UserEventListener
 */
class UserEventListener
{
    /**
     * @var DataLayerManager
     */
    protected $manager;

    /**
     * @param DataLayerManager $manager
     */
    public function __construct(DataLayerManager $manager)
    {
        $this->manager = $manager;
    }

    /**
     * onUserRegistration
     */
    public function onUserRegistration()
    {
        $this->manager->add(['registration' => true]);
    }
}



namespace Lexik\Bundle\DataLayerBundle\Collector;

use Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorageInterface;
use Symfony\Component\Security\Core\User\UserInterface;

/**
 * UserIdCollector
 */
class UserIdCollector implements CollectorInterface
{
    /**
     * @var TokenStorageInterface
     */
    protected $tokenStorage;

    /**
     * @param TokenStorageInterface $tokenStorage
     */
    public function __construct(TokenStorageInterface $tokenStorage)
    {
        $this->tokenStorage = $tokenStorage;
    }

    /**
     * {@inheritdoc}
     */
    public function handle(&$data)
    {
        $token = $this->tokenStorage->getToken();

        if ($token->getUser() && $token->getUser() instanceof UserInterface) {
            $data[] = ['user_id' => md5($token->getUser()->getUsername())];
        }
    }
}
 php
public function registerBundles()
{
    return array(
        // ...
        new Lexik\Bundle\DataLayerBundle\LexikDataLayerBundle(),
    );
}