PHP code example of tourze / user-track-bundle

1. Go to this page and download the library: Download tourze/user-track-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/ */

    

tourze / user-track-bundle example snippets


// config/bundles.php
return [
    // ...
    Tourze\UserTrackBundle\UserTrackBundle::class => ['all' => true],
];

use Tourze\UserEventBundle\Event\UserInteractionEvent;

// This event will be automatically recorded
$event = new UserInteractionEvent($sender, $systemUser, 'user.login');
$eventDispatcher->dispatch($event);

// Frontend JavaScript
const response = await jsonRpcClient.call('SubmitCrmTrackLog', {
    event: 'page.view',
    params: {
        page: '/products/123',
        referrer: 'google.com',
        duration: 30
    }
});
// Response: { time: 1234567890 }

// Backend PHP
$result = $jsonRpcClient->call('SubmitCrmTrackLog', [
    'event' => 'order.completed',
    'params' => [
        'order_id' => '12345',
        'amount' => 99.99
    ]
]);

use Tourze\UserEventBundle\Event\UserInteractionEvent;
use Tourze\UserTrackBundle\Event\TrackContextInterface;

class ProductViewEvent extends UserInteractionEvent implements TrackContextInterface
{
    /** @var array<string, mixed> */
    private array $trackParams = [];

    public function __construct($user, $product)
    {
        parent::__construct($user, $systemUser, 'product.view');

        $this->setTrackingParams([
            'product_id' => $product->getId(),
            'category' => $product->getCategory(),
            'price' => $product->getPrice()
        ]);
    }

    /**
     * @return array<string, mixed>
     */
    public function getTrackingParams(): array
    {
        return $this->trackParams;
    }

    /**
     * @param array<string, mixed> $params
     */
    public function setTrackingParams(array $params): void
    {
        $this->trackParams = $params;
    }
}

use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Tourze\UserTrackBundle\Event\TrackLogReportEvent;

class TrackLogResponseEnricher implements EventSubscriberInterface
{
    public static function getSubscribedEvents()
    {
        return [
            TrackLogReportEvent::class => 'onTrackLogReport',
        ];
    }
    
    public function onTrackLogReport(TrackLogReportEvent $event)
    {
        $result = $event->getResult();
        $result['tracking_id'] = $event->getTrackLog()->getId();
        $event->setResult($result);
    }
}