vendor/pimcore/pimcore/bundles/EcommerceFrameworkBundle/Tracking/TrackingManager.php line 155

Open in your IDE?
  1. <?php
  2. /**
  3.  * Pimcore
  4.  *
  5.  * This source file is available under two different licenses:
  6.  * - GNU General Public License version 3 (GPLv3)
  7.  * - Pimcore Commercial License (PCL)
  8.  * Full copyright and license information is available in
  9.  * LICENSE.md which is distributed with this source code.
  10.  *
  11.  *  @copyright  Copyright (c) Pimcore GmbH (http://www.pimcore.org)
  12.  *  @license    http://www.pimcore.org/license     GPLv3 and PCL
  13.  */
  14. namespace Pimcore\Bundle\EcommerceFrameworkBundle\Tracking;
  15. use Pimcore\Bundle\EcommerceFrameworkBundle\CartManager\CartInterface;
  16. use Pimcore\Bundle\EcommerceFrameworkBundle\CheckoutManager\CheckoutStepInterface as CheckoutManagerCheckoutStepInterface;
  17. use Pimcore\Bundle\EcommerceFrameworkBundle\EnvironmentInterface;
  18. use Pimcore\Bundle\EcommerceFrameworkBundle\EventListener\Frontend\TrackingCodeFlashMessageListener;
  19. use Pimcore\Bundle\EcommerceFrameworkBundle\Model\AbstractOrder;
  20. use Pimcore\Bundle\EcommerceFrameworkBundle\Model\ProductInterface;
  21. use Symfony\Component\HttpFoundation\Session\Session;
  22. use Symfony\Component\HttpFoundation\Session\SessionInterface;
  23. class TrackingManager implements TrackingManagerInterface
  24. {
  25.     /**
  26.      * @var TrackerInterface[]
  27.      */
  28.     protected $trackers = [];
  29.     /**
  30.      * @var TrackerInterface[]
  31.      */
  32.     protected $activeTrackerCache = [];
  33.     /**
  34.      * @var string
  35.      */
  36.     protected $cachedAssortmentTenant null;
  37.     /**
  38.      * @var string
  39.      */
  40.     protected $cachedCheckoutTenant null;
  41.     /**
  42.      * @var null|EnvironmentInterface
  43.      */
  44.     protected $enviroment null;
  45.     /**
  46.      * @var Session
  47.      */
  48.     protected $session;
  49.     /**
  50.      * @param TrackerInterface[] $trackers
  51.      * @param EnvironmentInterface $environment
  52.      */
  53.     public function __construct(EnvironmentInterface $environment, array $trackers = [])
  54.     {
  55.         foreach ($trackers as $tracker) {
  56.             $this->registerTracker($tracker);
  57.         }
  58.         $this->enviroment $environment;
  59.     }
  60.     /**
  61.      * @param Session $session
  62.      * @required
  63.      */
  64.     public function setSession(SessionInterface $session)
  65.     {
  66.         $this->session $session;
  67.     }
  68.     /**
  69.      * Register a tracker
  70.      *
  71.      * @param TrackerInterface $tracker
  72.      */
  73.     public function registerTracker(TrackerInterface $tracker)
  74.     {
  75.         $this->trackers[] = $tracker;
  76.     }
  77.     /**
  78.      * Get all registered trackers
  79.      *
  80.      * @return TrackerInterface[]
  81.      */
  82.     public function getTrackers(): array
  83.     {
  84.         return $this->trackers;
  85.     }
  86.     /**
  87.      * Get all for current tenants active trackers
  88.      *
  89.      * @return TrackerInterface[]
  90.      */
  91.     public function getActiveTrackers(): array
  92.     {
  93.         $currentAssortmentTenant $this->enviroment->getCurrentAssortmentTenant() ?: 'default';
  94.         $currentCheckoutTenant $this->enviroment->getCurrentCheckoutTenant() ?: 'default';
  95.         if ($currentAssortmentTenant !== $this->cachedAssortmentTenant || $currentCheckoutTenant !== $this->cachedCheckoutTenant) {
  96.             $this->cachedCheckoutTenant $currentCheckoutTenant;
  97.             $this->cachedAssortmentTenant $currentAssortmentTenant;
  98.             $this->activeTrackerCache = [];
  99.             foreach ($this->trackers as $tracker) {
  100.                 $active false;
  101.                 if (empty($tracker->getAssortmentTenants()) || in_array($currentAssortmentTenant$tracker->getAssortmentTenants())) {
  102.                     $active true;
  103.                 }
  104.                 if (empty($tracker->getCheckoutTenants()) || in_array($currentCheckoutTenant$tracker->getCheckoutTenants())) {
  105.                     $active true;
  106.                 }
  107.                 if ($active) {
  108.                     $this->activeTrackerCache[] = $tracker;
  109.                 }
  110.             }
  111.         }
  112.         return $this->activeTrackerCache;
  113.     }
  114.     /**
  115.      * Tracks a category page view
  116.      *
  117.      * @param array|string $category One or more categories matching the page
  118.      * @param mixed $page            Any kind of page information you can use to track your page
  119.      */
  120.     public function trackCategoryPageView($category$page null)
  121.     {
  122.         foreach ($this->getActiveTrackers() as $tracker) {
  123.             if ($tracker instanceof CategoryPageViewInterface) {
  124.                 $tracker->trackCategoryPageView($category$page);
  125.             }
  126.         }
  127.     }
  128.     /**
  129.      * Track product impression
  130.      *
  131.      * @param ProductInterface $product
  132.      * @param string $list
  133.      */
  134.     public function trackProductImpression(ProductInterface $productstring $list 'default')
  135.     {
  136.         foreach ($this->getActiveTrackers() as $tracker) {
  137.             if ($tracker instanceof ProductImpressionInterface) {
  138.                 $tracker->trackProductImpression($product$list);
  139.             }
  140.         }
  141.     }
  142.     /**
  143.      * Track product view
  144.      *
  145.      * @param ProductInterface $product
  146.      */
  147.     public function trackProductView(ProductInterface $product)
  148.     {
  149.         foreach ($this->getActiveTrackers() as $tracker) {
  150.             if ($tracker instanceof ProductViewInterface) {
  151.                 $tracker->trackProductView($product);
  152.             }
  153.         }
  154.     }
  155.     /**
  156.      * Track a cart update
  157.      *
  158.      * @param CartInterface $cart
  159.      */
  160.     public function trackCartUpdate(CartInterface $cart)
  161.     {
  162.         foreach ($this->getActiveTrackers() as $tracker) {
  163.             if ($tracker instanceof CartUpdateInterface) {
  164.                 $tracker->trackCartUpdate($cart);
  165.             }
  166.         }
  167.     }
  168.     /**
  169.      * Track product add to cart
  170.      *
  171.      * @param CartInterface $cart
  172.      * @param ProductInterface $product
  173.      * @param int|float $quantity
  174.      */
  175.     public function trackCartProductActionAdd(CartInterface $cartProductInterface $product$quantity 1)
  176.     {
  177.         foreach ($this->getActiveTrackers() as $tracker) {
  178.             if ($tracker instanceof CartProductActionAddInterface) {
  179.                 $tracker->trackCartProductActionAdd($cart$product$quantity);
  180.             }
  181.         }
  182.     }
  183.     /**
  184.      * Track product remove from cart
  185.      *
  186.      * @param CartInterface $cart
  187.      * @param ProductInterface $product
  188.      * @param int|float $quantity
  189.      */
  190.     public function trackCartProductActionRemove(CartInterface $cartProductInterface $product$quantity 1)
  191.     {
  192.         foreach ($this->getActiveTrackers() as $tracker) {
  193.             if ($tracker instanceof CartProductActionRemoveInterface) {
  194.                 $tracker->trackCartProductActionRemove($cart$product$quantity);
  195.             }
  196.         }
  197.     }
  198.     /**
  199.      * Track start checkout with first step
  200.      *
  201.      * @param CartInterface $cart
  202.      */
  203.     public function trackCheckout(CartInterface $cart)
  204.     {
  205.         foreach ($this->getActiveTrackers() as $tracker) {
  206.             if ($tracker instanceof CheckoutInterface) {
  207.                 $tracker->trackCheckout($cart);
  208.             }
  209.         }
  210.     }
  211.     /**
  212.      * Track checkout complete
  213.      *
  214.      * @param AbstractOrder $order
  215.      */
  216.     public function trackCheckoutComplete(AbstractOrder $order)
  217.     {
  218.         if ($order->getProperty('os_tracked')) {
  219.             return;
  220.         }
  221.         // add property to order object in order to prevent multiple checkout complete tracking
  222.         $order->setProperty('os_tracked''bool'true);
  223.         $order->save();
  224.         foreach ($this->getActiveTrackers() as $tracker) {
  225.             if ($tracker instanceof CheckoutCompleteInterface) {
  226.                 $tracker->trackCheckoutComplete($order);
  227.             }
  228.         }
  229.     }
  230.     /**
  231.      * Track checkout step
  232.      *
  233.      * @param CheckoutManagerCheckoutStepInterface $step
  234.      * @param CartInterface $cart
  235.      * @param string|null $stepNumber
  236.      * @param string|null $checkoutOption
  237.      */
  238.     public function trackCheckoutStep(CheckoutManagerCheckoutStepInterface $stepCartInterface $cart$stepNumber null$checkoutOption null)
  239.     {
  240.         foreach ($this->getActiveTrackers() as $tracker) {
  241.             if ($tracker instanceof CheckoutStepInterface) {
  242.                 $tracker->trackCheckoutStep($step$cart$stepNumber$checkoutOption);
  243.             }
  244.         }
  245.     }
  246.     public function getTrackedCodes(): string
  247.     {
  248.         $result '';
  249.         foreach ($this->getTrackers() as $tracker) {
  250.             if ($tracker instanceof TrackingCodeAwareInterface) {
  251.                 if (count($tracker->getTrackedCodes())) {
  252.                     $result .= implode(PHP_EOL$tracker->getTrackedCodes()).PHP_EOL.PHP_EOL;
  253.                 }
  254.             }
  255.         }
  256.         return $result;
  257.     }
  258.     public function forwardTrackedCodesAsFlashMessage(): TrackingManagerInterface
  259.     {
  260.         $trackedCodes = [];
  261.         foreach ($this->getTrackers() as $tracker) {
  262.             if ($tracker instanceof TrackingCodeAwareInterface) {
  263.                 if (count($tracker->getTrackedCodes())) {
  264.                     $trackedCodes[get_class($tracker)] = $tracker->getTrackedCodes();
  265.                 }
  266.             }
  267.         }
  268.         $this->session->getFlashBag()->set(TrackingCodeFlashMessageListener::FLASH_MESSAGE_BAG_KEY$trackedCodes);
  269.         return $this;
  270.     }
  271.     public function trackEvent(
  272.         string $eventCategory,
  273.         string $eventAction,
  274.         string $eventLabel null,
  275.         int $eventValue null
  276.     ) {
  277.         foreach ($this->getTrackers() as $tracker) {
  278.             if ($tracker instanceof TrackEventInterface) {
  279.                 $tracker->trackEvent($eventCategory$eventAction$eventLabel$eventValue);
  280.             }
  281.         }
  282.     }
  283. }