Skip to content

Commit 37967f0

Browse files
authored
Fix "setDoNotTrack" handling (#46)
The `setDoNotTrack` configuration value has to be pushed to the `_paq`s _before_ the `trackPageView` setting. `trackPageView` seems to trigger the actual logging request when it is being processed. With the old state of the code, `setDoNotTrack` was set too late and never became effective. A request to the Matomo server would be sent in any case, relying on the server-side "do not track" privacy feature to be enabled. This change adds a new `enable_do_not_track` configuration setting which defaults to `true`. When you set it to `false`, the `setDoNotTrack` command will not be used. You can push it to the `_paq` array yourself, possibly depending on client-side logic. But remember: It has to be in the `_paq` array _before_ the `trackPageView` entry.
1 parent 64218d9 commit 37967f0

File tree

4 files changed

+8
-3
lines changed

4 files changed

+8
-3
lines changed

DependencyInjection/Configuration.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ public function getConfigTreeBuilder()
2424
->scalarNode('tracker_path')->defaultValue('/js/')->end()
2525
->scalarNode('site_id')->isRequired()->end()
2626
->scalarNode('disable_cookies')->defaultValue(true)->end()
27+
->scalarNode('enable_do_not_track')->defaultValue(true)->info('Include ["setDoNotTrack", true] in default _paqs')->end()
2728
->end();
2829

2930
return $treeBuilder;

DependencyInjection/WebfactoryPiwikExtension.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ 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', 'disable_cookies'] as $configParameterKey) {
21+
foreach (['disabled', 'piwik_host', 'tracker_path', 'site_id', 'disable_cookies', 'enable_do_not_track'] as $configParameterKey) {
2222
$container->setParameter("webfactory_piwik.$configParameterKey", $config[$configParameterKey]);
2323
}
2424
}

Resources/config/services.xml

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

Twig/Extension.php

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,13 +37,17 @@ class Extension extends AbstractExtension
3737
*/
3838
private $paqs = [];
3939

40-
public function __construct(bool $disabled, string $siteId, string $piwikHost, string $trackerPath, bool $disableCookies = true)
40+
public function __construct(bool $disabled, string $siteId, string $piwikHost, string $trackerPath, bool $disableCookies = true, bool $enableDoNotTrack = true)
4141
{
4242
$this->disabled = $disabled;
4343
$this->siteId = $siteId;
4444
$this->piwikHost = rtrim($piwikHost, '/');
4545
$this->trackerPath = ltrim($trackerPath, '/');
4646
$this->disableCookies = $disableCookies;
47+
48+
if ($enableDoNotTrack) {
49+
$this->paqs[] = ['setDoNotTrack', true];
50+
}
4751
}
4852

4953
public function getFunctions(): array
@@ -81,7 +85,6 @@ public function piwikCode(): string
8185
<!-- Piwik -->
8286
<script type="text/javascript">//<![CDATA[
8387
var _paq = (_paq||[]).concat({$paq});
84-
_paq.push(["setDoNotTrack", true]);
8588
(function() {
8689
var u=(("https:" == document.location.protocol) ? "https" : "http") + "://{$this->piwikHost}/";
8790
_paq.push(["setTrackerUrl", u+"{$this->trackerPath}"]);

0 commit comments

Comments
 (0)