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

61 lines
2.0 KiB
PHP

<?php
declare(strict_types=1);
namespace KupShop\ComponentsBundle\GTM\PostMount;
use KupShop\ComponentsBundle\GTM\PageViews\CommonPageData;
use KupShop\ComponentsBundle\Twig\Components\Utils\GTM\Script;
use Symfony\Contracts\Service\Attribute\Required;
use Symfony\UX\TwigComponent\Attribute\PostMount;
trait GTMViewData
{
use GTMDefaultData;
#[Required]
public CommonPageData $commonPage;
protected function fetchGTMData(): object
{
$data = (object) [];
/* Pokud mame nejake push data (muze byt kterakoliv classa, tak je vytahnu */
if (method_exists($this->gtmClass, 'getPushData') && $pushData = $this->gtmClass->getPushData($this)) {
$data = $pushData;
}
/* Vytahnu obecny data o strance (uzivatel, stranka, detaily o webu, ...)
A prilepim k tomu o jakou stranku se jedna (to je definovany na kazdym view pres gtmClass)
*/
$this->commonPage->getPageData(dataContainer: $data, component: $this);
if (method_exists($this->gtmClass, 'getPageTypeString')) {
$data->page->type = $this->gtmClass->getPageTypeString($this);
}
if (method_exists($this->gtmClass, 'getPageType')) {
// TODO: tohohle se do budoucna zbavit, až se nebude používat gtm v2
$this->gtmClass->getPageType(dataContainer: $data);
}
return $data;
}
#[PostMount]
public function addGTMPlaceholdersData(array $componentsData): array
{
if (!Script::getGTMId()) {
return $componentsData;
}
$placeholders = ['breadcrumbs' => $this->commonPage->getBreadcrumbs()];
if (method_exists($this, 'getGTMPlaceholders') && $data = $this->getGTMPlaceholders()) {
$placeholders = array_merge($placeholders, $data);
}
$componentsData['data-gtm-placeholders'] = htmlentities(json_encode($placeholders, JSON_NUMERIC_CHECK | JSON_HEX_APOS));
return $componentsData;
}
}