<?php
declare(strict_types=1);
namespace Tanmar\GoogleAdsTracking\Storefront\Page\Search;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Shopware\Storefront\Page\Search\SearchPageLoadedEvent;
use Tanmar\GoogleAdsTracking\Storefront\BaseSubscriber;
use Tanmar\GoogleAdsTracking\Components\Helper\LoggerHelper;
use Tanmar\GoogleAdsTracking\Service\ConfigService;
use Tanmar\GoogleAdsTracking\Service\Article\ArticleIdentifierService;
use Tanmar\GoogleAdsTracking\Service\RemarketingEvent\RemarketingEventService;
class SearchPageSubscriber extends BaseSubscriber implements EventSubscriberInterface {
protected $remarketingEventService;
public function __construct(ConfigService $configService, LoggerHelper $loggerHelper, ArticleIdentifierService $articleIdentifierService, RemarketingEventService $remarketingEventService) {
parent::__construct($configService, $loggerHelper, $articleIdentifierService);
$this->remarketingEventService = $remarketingEventService;
}
public static function getSubscribedEvents(): array {
return [
SearchPageLoadedEvent::class => 'onSearchPageLoaded'
];
}
public function onSearchPageLoaded(SearchPageLoadedEvent $event): void {
try {
$googleAdsData = $this->getExtension($event);
if (!is_null($googleAdsData) && $this->getConfig()->isRetargetingActive()) {
$eventData = $this->remarketingEventService->getEventData($event->getPage()->getListing()->getElements(), 0);
$googleAdsData->assign([
'eventName' => RemarketingEventService::GOOGLE_ADS_EVENT_NAME_SEARCH,
'eventData' => (count($eventData) > 0) ? $eventData : false
]);
}
$this->addExtension($event, $googleAdsData);
} catch (\Throwable $e) {
$this->loggerHelper->logThrowable($e);
}
}
}