vendor/elements/tracking-bundle/src/Service/Ga4Service.php line 65

Open in your IDE?
  1. <?php
  2. namespace Elements\Bundle\TrackingBundle\Service;
  3. use Pimcore\Bundle\EcommerceFrameworkBundle\Tracking\AbstractProductData;
  4. use Symfony\Component\HttpFoundation\Session\Attribute\NamespacedAttributeBag;
  5. use Symfony\Component\HttpFoundation\RequestStack;
  6. use Pimcore\Bundle\EcommerceFrameworkBundle\Tracking\ProductAction;
  7. class Ga4Service
  8. {
  9.     protected $requestStack;
  10.     /**
  11.      * Ga4Service constructor
  12.      *
  13.      * @param RequestStack $requestStack
  14.      */
  15.     public function __construct(RequestStack $requestStack)
  16.     {
  17.         $this->requestStack $requestStack;
  18.     }
  19.     public function addProductToList(AbstractProductData $productstring $list): void
  20.     {
  21.         $request $this->requestStack->getCurrentRequest();
  22.         if ($request->hasSession()) {
  23.             $productId $product->getId();
  24.             $session $request->getSession();
  25.             /** @var NamespacedAttributeBag $bag */
  26.             $bag $session->getBag('session_ga4');
  27.             if (!$productList $bag->get('product_list')) {
  28.                 $productList = [];
  29.             }
  30.             if (!$fixedList $bag->get('product_fixed_list')) {
  31.                 $fixedList = [];
  32.             }
  33.             if (!array_key_exists($productId$fixedList) || !$fixedList[$productId]) {
  34.                 $productList[$productId] = $list;
  35.                 $fixedList[$productId] = false;
  36.                 // Add other related product ids to session
  37.                 $attributes $product->getAdditionalAttributes();
  38.                 if (array_key_exists('product_ids'$attributes)) {
  39.                     $additionalProductIds $attributes['product_ids'];
  40.                     foreach($additionalProductIds as $id) {
  41.                         $productList[$id] = $list;
  42.                         $fixedList[$id] = false;
  43.                     }
  44.                 }
  45.             }
  46.             $bag->set('product_fixed_list'$fixedList);
  47.             $bag->set('product_list'$productList);
  48.         }
  49.     }
  50.     public function getListOfProduct(AbstractProductData $product): string
  51.     {
  52.         $request $this->requestStack->getCurrentRequest();
  53.         if ($request->hasSession()) {
  54.             $session $request->getSession();
  55.             $productList $session->getBag('session_ga4')->get('product_list');
  56.             if (!$productList) {
  57.                 #this can only be, if the produkt-link is opened directly -> list "default"
  58.                 return 'default';
  59.             }
  60.             if (array_key_exists($product->getId(), $productList)) {
  61.                 return $productList[$product->getId()];
  62.             }
  63.         }
  64.         #this can only be, if the produkt-link is opened directly -> list "default"
  65.         return 'default';
  66.     }
  67.     public function fixateListName(ProductAction $product): void
  68.     {
  69.         $request $this->requestStack->getCurrentRequest();
  70.         if ($request->hasSession()) {
  71.             $session $request->getSession();
  72.             /** @var NamespacedAttributeBag $bag */
  73.             $bag $session->getBag('session_ga4');
  74.             if (!$fixedList $bag->get('product_fixed_list')) {
  75.                 $fixedList = [];
  76.             }
  77.             $fixedList[$product->getId()] = true;
  78.             $bag->set('product_fixed_list'$fixedList);
  79.         }
  80.     }
  81. }