Skip to content

Commit e3e639d

Browse files
Add disable_cookies config settings to disable use of cookies (#30)
The new `disable_cookies` setting allows to disable cookies to better comply with the GDPR. For now, the default value will be `false` to stay BC. Make sure to configure this value explicitly if it really is what you want – the default will be changed in the next major version. Thanks @FabianSchmick.
1 parent c64eb5d commit e3e639d

File tree

6 files changed

+41
-5
lines changed

6 files changed

+41
-5
lines changed

DependencyInjection/Configuration.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ public function getConfigTreeBuilder()
2323
->scalarNode('piwik_host')->isRequired()->end()
2424
->scalarNode('tracker_path')->defaultValue('/js/')->end()
2525
->scalarNode('site_id')->isRequired()->end()
26+
->scalarNode('disable_cookies')->defaultValue(null)->end()
2627
->end();
2728

2829
return $treeBuilder;

DependencyInjection/WebfactoryPiwikExtension.php

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,16 @@ public function load(array $configs, ContainerBuilder $container)
1818
$configuration = new Configuration();
1919
$config = $this->processConfiguration($configuration, $configs);
2020

21-
foreach (['disabled', 'piwik_host', 'tracker_path', 'site_id'] as $configParameterKey) {
21+
if (null === $config['disable_cookies']) {
22+
$config['disable_cookies'] = false;
23+
@trigger_error(
24+
'The "disableCookies" configuration key is missing. In the next major version, it will default to "true".
25+
Please configure the "disableCookies" key explicitly if this is not what you want.',
26+
E_USER_DEPRECATED
27+
);
28+
}
29+
30+
foreach (['disabled', 'piwik_host', 'tracker_path', 'site_id', 'disable_cookies'] as $configParameterKey) {
2231
$container->setParameter("webfactory_piwik.$configParameterKey", $config[$configParameterKey]);
2332
}
2433
}

README.md

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -46,12 +46,17 @@ You can configure the bundle in your `config.yml`. Full Example:
4646
webfactory_piwik:
4747
# Required, no default. Must be set to the site id found in the Matomo control panel
4848
site_id: 1
49-
# Required, has default. Usually, you only want to include the tracking code in a production environment
50-
disabled: '%kernel.debug%'
5149
# Required. no default. Hostname and path to the Matomo host.
5250
piwik_host: my.piwik.hostname
53-
# Required, has default. Path to the tracking script on the host.
51+
# Optional, has default. Usually, you only want to include the tracking code in a production environment
52+
disabled: '%kernel.debug%'
53+
# Optional, has default. Path to the tracking script on the host.
5454
tracker_path: "/js/"
55+
# Optional, has default. Disable cookies in favor of GDPR
56+
# https://matomo.org/faq/new-to-piwik/how-do-i-use-matomo-analytics-without-consent-or-cookie-banner/ & https://matomo.org/faq/general/faq_157/
57+
#
58+
# In the next major release the default will be true!
59+
disable_cookies: false
5560
```
5661
5762
Add calls to the JavaScript tracker API

Resources/config/services.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
<argument>%webfactory_piwik.site_id%</argument>
1010
<argument>%webfactory_piwik.piwik_host%</argument>
1111
<argument>%webfactory_piwik.tracker_path%</argument>
12+
<argument>%webfactory_piwik.disable_cookies%</argument>
1213
<tag name="twig.extension" />
1314
</service>
1415

Tests/Twig/ExtensionTest.php

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,12 @@ public function testTrackPageViewEnabledByDefault()
6161
$this->assertContains('"trackPageView"', $extension->piwikCode());
6262
}
6363

64+
public function testCookiesCanBeDisabled()
65+
{
66+
$extension = new Extension(false, 1, 'my.host', false, true);
67+
$this->assertContains('"disableCookies"', $extension->piwikCode());
68+
}
69+
6470
public function testTrackSiteSearchDisablesPageTracking()
6571
{
6672
$extension = new Extension(false, 1, 'my.host', false);

Twig/Extension.php

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,17 +27,23 @@ class Extension extends AbstractExtension
2727
*/
2828
private $trackerPath;
2929

30+
/**
31+
* @var bool
32+
*/
33+
private $disableCookies;
34+
3035
/**
3136
* @var array
3237
*/
3338
private $paqs = [];
3439

35-
public function __construct(bool $disabled, string $siteId, string $piwikHost, string $trackerPath)
40+
public function __construct(bool $disabled, string $siteId, string $piwikHost, string $trackerPath, bool $disableCookies = false)
3641
{
3742
$this->disabled = $disabled;
3843
$this->siteId = $siteId;
3944
$this->piwikHost = rtrim($piwikHost, '/');
4045
$this->trackerPath = ltrim($trackerPath, '/');
46+
$this->disableCookies = $disableCookies;
4147
}
4248

4349
public function getFunctions()
@@ -59,6 +65,14 @@ public function piwikCode()
5965
return '<!-- Piwik is disabled due to webfactory_piwik.disabled=true in your configuration -->';
6066
}
6167

68+
/*
69+
* https://matomo.org/faq/general/faq_157/
70+
* Call disableCookies before calling trackPageView
71+
*/
72+
if (true === $this->disableCookies) {
73+
$this->paqs[] = ['disableCookies'];
74+
}
75+
6276
$this->addDefaultApiCalls();
6377

6478
$paq = json_encode($this->paqs);

0 commit comments

Comments
 (0)