Files
2025-08-02 16:30:27 +02:00

41 lines
998 B
PHP

<?php
declare(strict_types=1);
namespace KupShop\ComponentsBundle\GTM\PostMount;
use KupShop\ComponentsBundle\Twig\Components\Utils\GTM\Script;
use Symfony\UX\TwigComponent\Attribute\PostMount;
trait GTMClickData
{
#[PostMount]
public function addGTMDataAttribute(array $componentsData): array
{
if (!Script::getGTMId()) {
return $componentsData;
}
if (empty($this->gtmClass)) {
throw new \Exception('GTM on component enabled, but GTM class is not defined!');
}
if ($gtmData = $this->fetchGTMData()) {
$componentsData['data-tracking-click'] = htmlentities(json_encode($gtmData, JSON_NUMERIC_CHECK | JSON_HEX_APOS));
return $componentsData;
}
return $componentsData;
}
protected function fetchGTMData()
{
if (method_exists($this->gtmClass, 'getPushData')) {
return $this->gtmClass->getPushData($this);
}
return null;
}
}