diff --git a/web/modules/custom/isa2_analytics/isa2_analytics.services.yml b/web/modules/custom/isa2_analytics/isa2_analytics.services.yml index 13a51b941d..6d73c5ce41 100644 --- a/web/modules/custom/isa2_analytics/isa2_analytics.services.yml +++ b/web/modules/custom/isa2_analytics/isa2_analytics.services.yml @@ -1,6 +1,6 @@ services: isa2_analytics.oe_webtools_analytics_subscriber: class: Drupal\isa2_analytics\EventSubscriber\WebtoolsAnalyticsSubscriber - arguments: ['@entity_type.manager', '@og.context'] + arguments: ['@entity_type.manager', '@request_stack', '@current_route_match', '@og.context'] tags: - { name: event_subscriber } diff --git a/web/modules/custom/isa2_analytics/src/EventSubscriber/WebtoolsAnalyticsSubscriber.php b/web/modules/custom/isa2_analytics/src/EventSubscriber/WebtoolsAnalyticsSubscriber.php index afef670978..51f887b323 100644 --- a/web/modules/custom/isa2_analytics/src/EventSubscriber/WebtoolsAnalyticsSubscriber.php +++ b/web/modules/custom/isa2_analytics/src/EventSubscriber/WebtoolsAnalyticsSubscriber.php @@ -5,10 +5,12 @@ namespace Drupal\isa2_analytics\EventSubscriber; use Drupal\Core\Entity\EntityTypeManagerInterface; +use Drupal\Core\Routing\CurrentRouteMatch; use Drupal\oe_webtools_analytics\AnalyticsEventInterface; use Drupal\og\OgContextInterface; use Symfony\Component\EventDispatcher\EventSubscriberInterface; use Drupal\oe_webtools_analytics\Event\AnalyticsEvent; +use Symfony\Component\HttpFoundation\RequestStack; /** * Subscriber that acts on visitor analytics being collected for reporting. @@ -22,6 +24,20 @@ class WebtoolsAnalyticsSubscriber implements EventSubscriberInterface { */ protected $entityTypeManager; + /** + * The current request. + * + * @var \Symfony\Component\HttpFoundation\Request + */ + protected $currentRequest; + + /** + * The current route match. + * + * @var \Drupal\Core\Routing\CurrentRouteMatch + */ + protected $routeMatch; + /** * The OG context service. * @@ -34,11 +50,17 @@ class WebtoolsAnalyticsSubscriber implements EventSubscriberInterface { * * @param \Drupal\Core\Entity\EntityTypeManagerInterface $entityTypeManager * The entity type manager service. + * @param \Symfony\Component\HttpFoundation\RequestStack $requestStack + * The http request stack. + * @param \Drupal\Core\Routing\CurrentRouteMatch $routeMatch + * The current route match. * @param \Drupal\og\OgContextInterface $ogContext * The OG context service. */ - public function __construct(EntityTypeManagerInterface $entityTypeManager, OgContextInterface $ogContext) { + public function __construct(EntityTypeManagerInterface $entityTypeManager, RequestStack $requestStack, CurrentRouteMatch $routeMatch, OgContextInterface $ogContext) { $this->entityTypeManager = $entityTypeManager; + $this->currentRequest = $requestStack->getCurrentRequest(); + $this->routeMatch = $routeMatch; $this->ogContext = $ogContext; } @@ -86,11 +108,29 @@ public function setSiteSection(AnalyticsEventInterface $event) { } } + /** + * Sets the webtools search data in the MATOMO json. + * + * @param \Drupal\oe_webtools_analytics\AnalyticsEventInterface $event + * Response event. + */ + public function setSearchData(AnalyticsEventInterface $event) { + if ($this->routeMatch->getRouteName() !== 'view.search.page_1') { + return; + } + + if ($keys = $this->currentRequest->get('keys')) { + $search_data = $event->getSearch(); + $search_data->setKeyword($keys); + } + } + /** * {@inheritdoc} */ public static function getSubscribedEvents() { $events[AnalyticsEvent::NAME][] = ['setSiteSection']; + $events[AnalyticsEvent::NAME][] = ['setSearchData']; return $events; }