Skip to content

Commit

Permalink
Fix "setDoNotTrack" handling (#46)
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
mpdude authored Nov 14, 2023
1 parent 64218d9 commit 37967f0
Show file tree
Hide file tree
Showing 4 changed files with 8 additions and 3 deletions.
1 change: 1 addition & 0 deletions DependencyInjection/Configuration.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ public function getConfigTreeBuilder()
->scalarNode('tracker_path')->defaultValue('/js/')->end()
->scalarNode('site_id')->isRequired()->end()
->scalarNode('disable_cookies')->defaultValue(true)->end()
->scalarNode('enable_do_not_track')->defaultValue(true)->info('Include ["setDoNotTrack", true] in default _paqs')->end()
->end();

return $treeBuilder;
Expand Down
2 changes: 1 addition & 1 deletion DependencyInjection/WebfactoryPiwikExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ public function load(array $configs, ContainerBuilder $container)
$configuration = new Configuration();
$config = $this->processConfiguration($configuration, $configs);

foreach (['disabled', 'piwik_host', 'tracker_path', 'site_id', 'disable_cookies'] as $configParameterKey) {
foreach (['disabled', 'piwik_host', 'tracker_path', 'site_id', 'disable_cookies', 'enable_do_not_track'] as $configParameterKey) {
$container->setParameter("webfactory_piwik.$configParameterKey", $config[$configParameterKey]);
}
}
Expand Down
1 change: 1 addition & 0 deletions Resources/config/services.xml
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
<argument>%webfactory_piwik.piwik_host%</argument>
<argument>%webfactory_piwik.tracker_path%</argument>
<argument>%webfactory_piwik.disable_cookies%</argument>
<argument>%webfactory_piwik.enable_do_not_track%</argument>
<tag name="twig.extension" />
</service>

Expand Down
7 changes: 5 additions & 2 deletions Twig/Extension.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,13 +37,17 @@ class Extension extends AbstractExtension
*/
private $paqs = [];

public function __construct(bool $disabled, string $siteId, string $piwikHost, string $trackerPath, bool $disableCookies = true)
public function __construct(bool $disabled, string $siteId, string $piwikHost, string $trackerPath, bool $disableCookies = true, bool $enableDoNotTrack = true)
{
$this->disabled = $disabled;
$this->siteId = $siteId;
$this->piwikHost = rtrim($piwikHost, '/');
$this->trackerPath = ltrim($trackerPath, '/');
$this->disableCookies = $disableCookies;

if ($enableDoNotTrack) {
$this->paqs[] = ['setDoNotTrack', true];
}
}

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

0 comments on commit 37967f0

Please sign in to comment.