Skip to content
This repository has been archived by the owner on Jul 27, 2022. It is now read-only.

Commit

Permalink
5342: Matomo - Track WebAnalytics Site Search results (#1910)
Browse files Browse the repository at this point in the history
5342: Matomo - Track WebAnalytics Site Search results
  • Loading branch information
claudiu-cristea authored Dec 4, 2019
2 parents 9c6cfce + 691ea48 commit ed725d4
Show file tree
Hide file tree
Showing 4 changed files with 53 additions and 3 deletions.
2 changes: 1 addition & 1 deletion config/sync/matomo.settings.yml
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ track:
colorbox: true
userid: false
messages: { }
site_search: false
site_search: true
privacy:
donottrack: true
custom:
Expand Down
10 changes: 10 additions & 0 deletions tests/features/isa2_analytics/site_search.feature
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
@api
Feature: Site search
As an analytics engineer
I want search statistics grouped by the keyword
So that I can better understand the needs of our users

Scenario: Search keywords are tracked in analytics.
When I am on the homepage
And I enter "sample text 1" in the header search bar and hit enter
Then the response should contain "\"search\":{\"keyword\":\"sample text 1\"}"
Original file line number Diff line number Diff line change
@@ -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 }
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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.
*
Expand All @@ -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;
}

Expand Down Expand Up @@ -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;
}
Expand Down

0 comments on commit ed725d4

Please sign in to comment.