Skip to content

Commit

Permalink
D8CORE-7446 Scope google analytics cookie to the individual site (#339)
Browse files Browse the repository at this point in the history
  • Loading branch information
pookmish authored Oct 31, 2024
1 parent 09d9d91 commit 99be1d4
Show file tree
Hide file tree
Showing 6 changed files with 92 additions and 13 deletions.
1 change: 1 addition & 0 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@
"drupal/field_formatter_class": ">=1.6",
"drupal/field_group": ">=3.4",
"drupal/flat_taxonomy": ">=2.0",
"drupal/google_analytics": "^4.0",
"drupal/hook_event_dispatcher": ">=4.0",
"drupal/jsonapi_extras": ">=3.24",
"drupal/layout_builder_restrictions": ">=2.19",
Expand Down
49 changes: 49 additions & 0 deletions src/EventSubscriber/GoogleAnalyticsSubscriber.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
<?php

declare(strict_types=1);

namespace Drupal\stanford_profile_helper\EventSubscriber;

use Drupal\config_pages\ConfigPagesLoaderServiceInterface;
use Drupal\Core\State\StateInterface;
use Drupal\google_analytics\Constants\GoogleAnalyticsEvents;
use Drupal\google_analytics\Event\GoogleAnalyticsConfigEvent;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Symfony\Component\HttpFoundation\RequestStack;

/**
* Event subscriber for Google Analytics module.
*/
final class GoogleAnalyticsSubscriber implements EventSubscriberInterface {

/**
* {@inheritdoc}
*/
public static function getSubscribedEvents(): array {
return [
GoogleAnalyticsEvents::ADD_CONFIG => 'addGoogleAnalyticsConfig',
];
}

/**
* Constructs a GoogleAnalyticsSubscriber object.
*/
public function __construct(private readonly ConfigPagesLoaderServiceInterface $configPagesLoader, private readonly RequestStack $requestStack, private readonly StateInterface $state) {}

/**
* Adjust Google Analytics configuration.
*
* @param \Drupal\google_analytics\Event\GoogleAnalyticsConfigEvent $event
* GA module event.
*/
public function addGoogleAnalyticsConfig(GoogleAnalyticsConfigEvent $event) {
$canonical_url = $this->configPagesLoader->getValue('stanford_basic_site_settings', 'su_site_url', 0, 'uri');
$current_host = $this->requestStack->getCurrentRequest()->getHttpHost();
$domain = $canonical_url ? parse_url($canonical_url, PHP_URL_HOST) : $current_host;

$event->addConfig('cookie_domain', $this->state->get('ga-domain', $domain));
$event->addConfig('cookie_prefix', $this->state->get('ga-prefix', 'su'));
$event->addConfig('cookie_expires', $this->state->get('ga-expire', 60 * 60 * 24 * 180));
}

}
5 changes: 5 additions & 0 deletions stanford_profile_helper.services.yml
Original file line number Diff line number Diff line change
Expand Up @@ -42,3 +42,8 @@ services:
decorates: cache_context.route.menu_active_trails
pubic: false
arguments: ['@stanford_profile_helper.cache_context.route.menu_active_trails.inner']
stanford_profile_helper.event_subscriber.google:
class: Drupal\stanford_profile_helper\EventSubscriber\GoogleAnalyticsSubscriber
arguments: ['@config_pages.loader', '@request_stack', '@state']
tags:
- { name: event_subscriber }
36 changes: 36 additions & 0 deletions tests/src/Kernel/EventSubscriber/GoogleAnalyticsSubscriberTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
<?php

namespace Drupal\Tests\stanford_profile_helper\Kernel\EventSubscriber;

use Drupal\google_analytics\Constants\GoogleAnalyticsEvents;
use Drupal\google_analytics\Event\GoogleAnalyticsConfigEvent;
use Drupal\google_analytics\GaAccount;
use Drupal\google_analytics\GaJavascriptObject;
use Drupal\Tests\stanford_profile_helper\Kernel\SuProfileHelperKernelTestBase;

/**
* Test the event subscriber.
*
* @coversDefaultClass \Drupal\stanford_profile_helper\EventSubscriber\GoogleAnalyticsSubscriber
*/
class GoogleAnalyticsSubscriberTest extends SuProfileHelperKernelTestBase {

public function testConfigChanges() {
$config = $this->container->get('config.factory')
->getEditable('google_analytics.settings');
$config->set('account', '');
$config->save();

$account = new GaAccount('');
$javascript = new GaJavascriptObject('');
$ga_config = new GoogleAnalyticsConfigEvent($javascript, $account);
$event_dispatcher = \Drupal::service('event_dispatcher');
$event_dispatcher->dispatch($ga_config, GoogleAnalyticsEvents::ADD_CONFIG);

$config = $ga_config->getConfig();
$this->assertNotEmpty($config['cookie_domain']);
$this->assertEquals('su', $config['cookie_prefix']);
$this->assertEquals(15552000, $config['cookie_expires']);
}

}
13 changes: 0 additions & 13 deletions tests/src/Kernel/EventSubscriber/test.html

This file was deleted.

1 change: 1 addition & 0 deletions tests/src/Kernel/SuProfileHelperKernelTestBase.php
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ abstract class SuProfileHelperKernelTestBase extends KernelTestBase {
'file',
'next',
'menu_link',
'google_analytics',
];

/**
Expand Down

0 comments on commit 99be1d4

Please sign in to comment.