custom/plugins/BilobaAdFacebookPixel/src/Subscriber/PageLoadedSubscriber.php line 161

Open in your IDE?
  1. <?php declare(strict_types=1);
  2. namespace Biloba\AdFacebookPixel\Subscriber;
  3. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  4. use Shopware\Storefront\Page\Product\ProductPageLoadedEvent;
  5. use Shopware\Core\Content\Category\Event\NavigationLoadedEvent;
  6. use Shopware\Storefront\Page\Checkout\Confirm\CheckoutConfirmPageLoadedEvent;
  7. use Shopware\Storefront\Page\Checkout\Register\CheckoutRegisterPageLoadedEvent;
  8. use Shopware\Storefront\Page\Checkout\Finish\CheckoutFinishPageLoadedEvent;
  9. use Shopware\Core\Checkout\Cart\Event\AfterLineItemAddedEvent;
  10. use Shopware\Storefront\Pagelet\Footer\FooterPageletLoadedEvent;
  11. use Biloba\AdFacebookPixel\Service\ConversionApiService;
  12. use Biloba\AdFacebookPixel\Structs\StoreFrontPageCollection;
  13. use Exception;
  14. class PageLoadedSubscriber implements EventSubscriberInterface 
  15. {
  16.     private $conversionApiService;
  17.     private $eventIds = [];
  18.     private $extensionData;
  19.     public function __construct(ConversionApiService $conversionApiService
  20.     {
  21.         $this->conversionApiService $conversionApiService;
  22.     }
  23.     public static function getSubscribedEvents(): array
  24.     {   
  25.         return [
  26.             FooterPageletLoadedEvent::class => 'onPageLoaded',
  27.             ProductPageLoadedEvent::class => 'onProductPageLoaded',
  28.             NavigationLoadedEvent::class => 'onNavigationLoaded',
  29.             CheckoutConfirmPageLoadedEvent::class => 'onCheckoutConfirmPageLoaded',
  30.             CheckoutRegisterPageLoadedEvent::class => 'onInitiateCheckoutPageLoaded',
  31.             CheckoutFinishPageLoadedEvent::class => 'onCheckoutFinishPageLoaded',
  32.             AfterLineItemAddedEvent::class => 'onAfterLineItemAddedEvent'
  33.         ];
  34.     }
  35.     
  36.     public function onAfterLineItemAddedEvent(AfterLineItemAddedEvent $event) :void
  37.     {
  38.         $contextToken $event->getSalesChannelContext()->getToken();
  39.         
  40.         if(array_key_exists(0$event->getLineItems())) {
  41.             $lineItemArrayId $event->getLineItems()[0]->getId();
  42.             if($event->getCart()->getLineItems()->getPayload() && 
  43.                     array_key_exists($lineItemArrayId$event->getCart()->getLineItems()->getPayload()) &&
  44.                     array_key_exists('productNumber'$event->getCart()->getLineItems()->getPayload()[$lineItemArrayId]))  {
  45.                 $productNumber $event->getCart()->getLineItems()->getPayload()[$lineItemArrayId]['productNumber'];
  46.                 $this->extensionData = new StoreFrontPageCollection();
  47.                 $this->conversionApiService->setData($event);
  48.                 // use order nr and token as event_id for server and client AddToCart event
  49.                 $this->eventIds["AddToCart"] = $productNumber $contextToken;
  50.                 // adding eventIds array to context
  51.                 $this->extensionData->setValue('BilobaAdFacebookPixelEventIds'$this->eventIds);
  52.                 $event->getContext()->addExtension('BilobaAdFacebookPixel'$this->extensionData);
  53.                 
  54.                 try {
  55.                     $this->conversionApiService->send("AddToCart"$productNumber $contextToken);
  56.                 } catch(Exception $e) {
  57.                     // ignoring exception
  58.                 }
  59.             }
  60.         }
  61.     }
  62.     public function onCheckoutFinishPageLoaded(CheckoutFinishPageLoadedEvent $event) :void
  63.     {
  64.         $this->extensionData = new StoreFrontPageCollection();
  65.         $this->conversionApiService->setData($event);
  66.         $this->eventIds["Purchase"] = $this->conversionApiService->getDataForConversion()['fbEventId'];
  67.         // adding eventIds array to context
  68.         $this->extensionData->setValue('BilobaAdFacebookPixelEventIds'$this->eventIds);
  69.         $event->getContext()->addExtension('BilobaAdFacebookPixel'$this->extensionData);
  70.         
  71.         try {
  72.             $this->conversionApiService->send("Purchase");
  73.         } catch(Exception $e) {
  74.             // ignoring exception
  75.         }
  76.     }
  77.     public function onInitiateCheckoutPageLoaded(CheckoutRegisterPageLoadedEvent $event) :void
  78.     {
  79.         $this->extensionData = new StoreFrontPageCollection();
  80.         $this->conversionApiService->setData($event);
  81.         $this->eventIds["InitiateCheckout"] = $this->conversionApiService->getDataForConversion()['fbEventId'];
  82.         // adding eventIds array to context
  83.         $this->extensionData->setValue('BilobaAdFacebookPixelEventIds'$this->eventIds);
  84.         $event->getContext()->addExtension('BilobaAdFacebookPixel'$this->extensionData);
  85.         
  86.         try {
  87.             $this->conversionApiService->send("InitiateCheckout");
  88.         } catch(Exception $e) {
  89.             // ignoring exception
  90.         }
  91.     }
  92.     public function onCheckoutConfirmPageLoaded(CheckoutConfirmPageLoadedEvent $event) :void
  93.     {
  94.         // $this->extensionData = new StoreFrontPageCollection(); Customer Fix
  95.         // setData sets neccesaryDataForConversion, userData and customData
  96.         $this->conversionApiService->setData($event);
  97.         $this->eventIds["CompleteRegistration"] = $this->conversionApiService->getDataForConversion()['fbEventId'];
  98.         // adding eventIds array to context
  99.         $this->extensionData->setValue('BilobaAdFacebookPixelEventIds'$this->eventIds);
  100.         $event->getContext()->addExtension('BilobaAdFacebookPixel'$this->extensionData);
  101.         
  102.         try {
  103.             $this->conversionApiService->send("CompleteRegistration");
  104.         } catch(Exception $e) {
  105.             // ignoring exception
  106.         }
  107.     }
  108.     public function onNavigationLoaded(NavigationLoadedEvent $event) :void
  109.     {
  110.         $this->extensionData = new StoreFrontPageCollection();
  111.         $name $event->getNavigation()->getActive()->getName();
  112.         $categoryId $event->getNavigation()->getActive()->getId();
  113.         
  114.         $this->conversionApiService->setData($event);
  115.         $this->conversionApiService->setCustomData(["name" => $name"id" => $categoryId]);
  116.         $this->eventIds["ViewCategory"] = $this->conversionApiService->getDataForConversion()['fbEventId'];
  117.         // adding eventIds array to context
  118.         $this->extensionData->setValue('BilobaAdFacebookPixelEventIds'$this->eventIds);
  119.         $event->getContext()->addExtension('BilobaAdFacebookPixel'$this->extensionData);
  120.         try {
  121.             $this->conversionApiService->send("ViewCategory");
  122.         } catch(Exception $e) {
  123.             // ignoring exception
  124.         }
  125.     }
  126.     public function onProductPageLoaded(ProductPageLoadedEvent $event) :void
  127.     {
  128.         $this->extensionData = new StoreFrontPageCollection();
  129.         $currencyShortName $event->getSalesChannelContext()->getCurrency()->getShortName();
  130.         $name $event->getPage()->getProduct()->get('translated')['name'];
  131.         $productId $event->getPage()->getProduct()->get('id');
  132.         $unitPrice $event->getPage()->getProduct()->getCalculatedPrice()->getUnitPrice();
  133.         $this->conversionApiService->setData($event);
  134.         // setting custom data this way because ViewContent needs just these specific parameters 
  135.         $this->conversionApiService->setCustomData(["name" => $name"currency" => $currencyShortName"conten_id" => $productId"value" => $unitPrice]);
  136.         $this->eventIds["ViewContent"] = $this->conversionApiService->getDataForConversion()['fbEventId'];
  137.         // adding eventIds array to context
  138.         $this->extensionData->setValue('BilobaAdFacebookPixelEventIds'$this->eventIds);
  139.         $event->getContext()->addExtension('BilobaAdFacebookPixel'$this->extensionData);
  140.         
  141.         try {
  142.             $this->conversionApiService->send("ViewContent");
  143.         } catch(Exception $e) {
  144.             // ignoring exception
  145.         }
  146.     }
  147.     
  148.     public function onPageLoaded(FooterPageletLoadedEvent $event) :void
  149.     {   
  150.         $this->extensionData = new StoreFrontPageCollection();
  151.         $this->conversionApiService->setData($event);
  152.         // setting custom data empty because FooterPageletLoadedEvent gives no payload
  153.         $this->conversionApiService->setCustomData([]);
  154.         $this->eventIds["PageView"] = $this->conversionApiService->getDataForConversion()['fbEventId'];
  155.         // adding eventIds array to context
  156.         $this->extensionData->setValue('BilobaAdFacebookPixelEventIds'$this->eventIds);
  157.         $event->getContext()->addExtension('BilobaAdFacebookPixel'$this->extensionData);
  158.         
  159.         try {
  160.             $this->conversionApiService->send("PageView");
  161.         } catch(Exception $e) {
  162.             // ignoring exception
  163.         }
  164.     }
  165. }