Skip to content

Commit

Permalink
feat: add "requireConsent" option
Browse files Browse the repository at this point in the history
resolves: #23
  • Loading branch information
brotkrueml committed Jul 31, 2023
1 parent 855f31a commit 7e02f7a
Show file tree
Hide file tree
Showing 11 changed files with 101 additions and 12 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

## [Unreleased]

- Option for requiring cookie consent (#23)

## [1.6.0] - 2023-05-21

### Added
Expand Down
4 changes: 4 additions & 0 deletions Classes/Entity/Configuration.php
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,10 @@ final class Configuration
* @readonly
*/
public bool $performanceTracking = false;
/**
* @readonly
*/
public bool $requireConsent = false;
/**
* @readonly
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,17 @@
/**
* @internal
*/
final class RequireCookieConsent
final class RequireConsent
{
public function __invoke(BeforeTrackPageViewEvent $event): void
{
if ($event->getConfiguration()->requireConsent) {
$event->addMatomoMethodCall('requireConsent');
// "requireConsent" is more restrictive, so this one wins
// if both options are set.
return;
}

if ($event->getConfiguration()->requireCookieConsent) {
$event->addMatomoMethodCall('requireCookieConsent');
}
Expand Down
4 changes: 2 additions & 2 deletions Configuration/Services.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -61,10 +61,10 @@ services:
identifier: 'matomo-integration/performance-tracking'
event: Brotkrueml\MatomoIntegration\Event\AfterTrackPageViewEvent

Brotkrueml\MatomoIntegration\EventListener\RequireCookieConsent:
Brotkrueml\MatomoIntegration\EventListener\RequireConsent:
tags:
- name: event.listener
identifier: 'matomo-integration/require-cookie-consent'
identifier: 'matomo-integration/require-consent'
event: Brotkrueml\MatomoIntegration\Event\BeforeTrackPageViewEvent

Brotkrueml\MatomoIntegration\EventListener\TrackJavaScriptErrors:
Expand Down
4 changes: 4 additions & 0 deletions Configuration/SiteConfiguration/Overrides/sites.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,10 @@
'label' => Brotkrueml\MatomoIntegration\Extension::LANGUAGE_PATH_SITECONF . ':noScript',
'value' => 'noScript',
],
[
'label' => Brotkrueml\MatomoIntegration\Extension::LANGUAGE_PATH_SITECONF . ':requireConsent',
'value' => 'requireConsent',
],
[
'label' => Brotkrueml\MatomoIntegration\Extension::LANGUAGE_PATH_SITECONF . ':requireCookieConsent',
'value' => 'requireCookieConsent',
Expand Down
5 changes: 4 additions & 1 deletion Documentation/Changelog/Index.rst
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,9 @@ and this project adheres to `Semantic Versioning <https://semver.org/spec/v2.0.0
`Unreleased <https://github.com/brotkrueml/typo3-matomo-integration/compare/v1.6.0...HEAD>`_
------------------------------------------------------------------------------------------------


* Option for requiring cookie consent (#23)

`1.6.0 <https://github.com/brotkrueml/typo3-matomo-integration/compare/v1.5.1...v1.6.0>`_ - 2023-05-21
----------------------------------------------------------------------------------------------------------

Expand All @@ -20,7 +23,7 @@ Added

* Multiple tag manager container IDs can be used (#14)
* Option for requiring cookie consent (#20)
* Add nonce attribute to script tag in TYPO3 v12+, if available (#7)
* CSP nonce attribute to script tag in TYPO3 v12+, if available (#7)

`1.5.1 <https://github.com/brotkrueml/typo3-matomo-integration/compare/v1.5.0...v1.5.1>`_ - 2023-04-01
----------------------------------------------------------------------------------------------------------
Expand Down
17 changes: 16 additions & 1 deletion Documentation/Configuration/Index.rst
Original file line number Diff line number Diff line change
Expand Up @@ -55,15 +55,30 @@ Track users with JavaScript disabled

Default: *disabled*

Require consent
.. versionadded:: 1.7.0

Enable this option when a consent manager is used which sets up consent
tracking: no tracking request will be sent to Matomo and no cookies will be
set. For more information have a look into
`Implementing tracking or cookie consent with the Matomo JavaScript Tracking Client`_.

Default: *disabled*

Require cookie consent
.. versionadded:: 1.6.0

Enable this option when a consent manager is used which sets up consent
tracking. For more information have a look into
tracking: tracking requests will still be sent but no cookies will be set.
For more information have a look into
`Implementing tracking or cookie consent with the Matomo JavaScript Tracking Client`_.

Default: *disabled*

.. important::
When enabling this option *and* the option "Require consent", "Require
cookie consent" will be ignored in favour of the more restrictive one.

Cookie tracking
Activating this option `enables cookies tracking`_.

Expand Down
Binary file modified Documentation/Images/SiteConfiguration.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
5 changes: 4 additions & 1 deletion Resources/Private/Language/SiteConfiguration.xlf
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?xml version="1.0" encoding="utf-8" ?>
<xliff version="1.2" xmlns="urn:oasis:names:tc:xliff:document:1.2">
<file source-language="en" datatype="plaintext" original="EXT:matomo_integration/Resources/Private/Language/Siteonfiguration.xlf" date="2023-05-15T15:56:48Z" product-name="matomo_integration">
<file source-language="en" datatype="plaintext" original="EXT:matomo_integration/Resources/Private/Language/Siteonfiguration.xlf" date="2023-07-31T16:04:18Z" product-name="matomo_integration">
<body>
<trans-unit id="cookieTracking" resname="cookieTracking">
<source><![CDATA[Use tracking cookies]]></source>
Expand Down Expand Up @@ -32,6 +32,9 @@
<trans-unit id="performanceTracking" resname="performanceTracking">
<source><![CDATA[Performance tracking]]></source>
</trans-unit>
<trans-unit id="requireConsent" resname="requireConsent">
<source><![CDATA[Require consent]]></source>
</trans-unit>
<trans-unit id="requireCookieConsent" resname="requireCookieConsent">
<source><![CDATA[Require cookie consent]]></source>
</trans-unit>
Expand Down
13 changes: 13 additions & 0 deletions Tests/Unit/Entity/ConfigurationTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ public function createFromSiteConfigurationWithEmptyArrayReturnsInstanceWithDefa
self::assertSame('', $subject->url);
self::assertSame(0, $subject->siteId);
self::assertFalse($subject->noScript);
self::assertFalse($subject->requireConsent);
self::assertFalse($subject->requireCookieConsent);
self::assertFalse($subject->cookieTracking);
self::assertFalse($subject->disableBrowserFeatureDetection);
Expand Down Expand Up @@ -102,6 +103,18 @@ public function createFromSiteConfigurationWithRequireCookieConsentEnabledSetsIn
self::assertTrue($subject->requireCookieConsent);
}

/**
* @test
*/
public function createFromSiteConfigurationWithRequireConsentEnabledSetsInstanceValuesCorrectly(): void
{
$subject = Configuration::createFromSiteConfiguration([
'matomoIntegrationOptions' => 'requireConsent',
]);

self::assertTrue($subject->requireConsent);
}

/**
* @test
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,29 +13,29 @@

use Brotkrueml\MatomoIntegration\Entity\Configuration;
use Brotkrueml\MatomoIntegration\Event\BeforeTrackPageViewEvent;
use Brotkrueml\MatomoIntegration\EventListener\RequireCookieConsent;
use Brotkrueml\MatomoIntegration\EventListener\RequireConsent;
use PHPUnit\Framework\MockObject\Stub;
use PHPUnit\Framework\TestCase;
use Psr\Http\Message\ServerRequestInterface;

final class RequireCookieConsentTest extends TestCase
final class RequireConsentTest extends TestCase
{
/**
* @var ServerRequestInterface&Stub
*/
private $requestStub;
private RequireCookieConsent $subject;
private RequireConsent $subject;

protected function setUp(): void
{
$this->requestStub = $this->createStub(ServerRequestInterface::class);
$this->subject = new RequireCookieConsent();
$this->subject = new RequireConsent();
}

/**
* @test
*/
public function disabledOption(): void
public function disabledBothOptions(): void
{
$configuration = Configuration::createFromSiteConfiguration([
'matomoIntegrationUrl' => 'https://www.example.net/',
Expand All @@ -52,7 +52,26 @@ public function disabledOption(): void
/**
* @test
*/
public function enabledOption(): void
public function enabledOptionRequireConsent(): void
{
$configuration = Configuration::createFromSiteConfiguration([
'matomoIntegrationUrl' => 'https://www.example.net/',
'matomoIntegrationSiteId' => 123,
'matomoIntegrationOptions' => 'requireConsent',
]);

$event = new BeforeTrackPageViewEvent($configuration, $this->requestStub);
$this->subject->__invoke($event);

$actual = $event->getMatomoMethodCalls();
self::assertCount(1, $actual);
self::assertSame('_paq.push(["requireConsent"]);', (string)$actual[0]);
}

/**
* @test
*/
public function enabledOptionRequireCookieConsent(): void
{
$configuration = Configuration::createFromSiteConfiguration([
'matomoIntegrationUrl' => 'https://www.example.net/',
Expand All @@ -67,4 +86,23 @@ public function enabledOption(): void
self::assertCount(1, $actual);
self::assertSame('_paq.push(["requireCookieConsent"]);', (string)$actual[0]);
}

/**
* @test
*/
public function enabledBothOptionsRequireConsentAndRequireCookieConsentThenOnlyRequireConsentIsConsidered(): void
{
$configuration = Configuration::createFromSiteConfiguration([
'matomoIntegrationUrl' => 'https://www.example.net/',
'matomoIntegrationSiteId' => 123,
'matomoIntegrationOptions' => 'requireConsent,requireCookieConsent',
]);

$event = new BeforeTrackPageViewEvent($configuration, $this->requestStub);
$this->subject->__invoke($event);

$actual = $event->getMatomoMethodCalls();
self::assertCount(1, $actual);
self::assertSame('_paq.push(["requireConsent"]);', (string)$actual[0]);
}
}

0 comments on commit 7e02f7a

Please sign in to comment.