-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
D8CORE-7446 Scope google analytics cookie to the individual site (#339)
- Loading branch information
Showing
6 changed files
with
92 additions
and
13 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)); | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
36 changes: 36 additions & 0 deletions
36
tests/src/Kernel/EventSubscriber/GoogleAnalyticsSubscriberTest.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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']); | ||
} | ||
|
||
} |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters