custom/plugins/TanmarNgGoogleAdsTracking/src/Storefront/Page/Search/SearchPageSubscriber.php line 30

Open in your IDE?
  1. <?php
  2. declare(strict_types=1);
  3. namespace Tanmar\GoogleAdsTracking\Storefront\Page\Search;
  4. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  5. use Shopware\Storefront\Page\Search\SearchPageLoadedEvent;
  6. use Tanmar\GoogleAdsTracking\Storefront\BaseSubscriber;
  7. use Tanmar\GoogleAdsTracking\Components\Helper\LoggerHelper;
  8. use Tanmar\GoogleAdsTracking\Service\ConfigService;
  9. use Tanmar\GoogleAdsTracking\Service\Article\ArticleIdentifierService;
  10. use Tanmar\GoogleAdsTracking\Service\RemarketingEvent\RemarketingEventService;
  11. class SearchPageSubscriber extends BaseSubscriber implements EventSubscriberInterface {
  12.     protected $remarketingEventService;
  13.     public function __construct(ConfigService $configServiceLoggerHelper $loggerHelperArticleIdentifierService $articleIdentifierServiceRemarketingEventService $remarketingEventService) {
  14.         parent::__construct($configService$loggerHelper$articleIdentifierService);
  15.         $this->remarketingEventService $remarketingEventService;
  16.     }
  17.     public static function getSubscribedEvents(): array {
  18.         return [
  19.             SearchPageLoadedEvent::class => 'onSearchPageLoaded'
  20.         ];
  21.     }
  22.     public function onSearchPageLoaded(SearchPageLoadedEvent $event): void {
  23.         try {
  24.             $googleAdsData $this->getExtension($event);
  25.             if (!is_null($googleAdsData) && $this->getConfig()->isRetargetingActive()) {
  26.                 $eventData $this->remarketingEventService->getEventData($event->getPage()->getListing()->getElements(), 0);
  27.                 $googleAdsData->assign([
  28.                     'eventName' => RemarketingEventService::GOOGLE_ADS_EVENT_NAME_SEARCH,
  29.                     'eventData' => (count($eventData) > 0) ? $eventData false
  30.                 ]);
  31.             }
  32.             $this->addExtension($event$googleAdsData);
  33.         } catch (\Throwable $e) {
  34.             $this->loggerHelper->logThrowable($e);
  35.         }
  36.     }
  37. }