Skip to content

Commit 3344413

Browse files
authored
Add a function to generate the opt-out javascript code (#52)
This adds a new method to the Twig extension to insert the JavaScript-based tracking opt-out code as described at https://developer.matomo.org/guides/in-depth-opt-out#opt-out-using-the-matomo-javascript-tracker).
1 parent 8aaf1e7 commit 3344413

File tree

3 files changed

+20
-0
lines changed

3 files changed

+20
-0
lines changed

README.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,10 @@ Somewhere in your views, right before the closing `</body>` tag, insert
3737

3838
This will add the appropriate Matomo tracking code as [described in the API reference](https://developer.matomo.org/api-reference/tracking-javascript#where-can-i-find-the-piwik-tracking-code).
3939

40+
When you want to display the opt-out dialog as [described in the documentation](https://developer.matomo.org/guides/in-depth-opt-out#opt-out-using-the-matomo-javascript-tracker), use the following Twig code:
41+
42+
{{ piwik_opt_out_code() }}
43+
4044
Configuration
4145
-------------
4246
You can configure the bundle in your `config.yml`. Full Example:

Tests/Twig/ExtensionTest.php

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,13 @@ public function testPiwikCodeContainsHostName()
4848
self::assertStringContainsString($hostname, $extension->piwikCode());
4949
}
5050

51+
public function testPiwikOptOutCodeContainsHostName()
52+
{
53+
$hostname = 'myHost.de';
54+
$extension = new Extension(false, 1, $hostname, false);
55+
self::assertStringContainsString($hostname, $extension->piwikOptOutCode());
56+
}
57+
5158
public function testAdditionalApiCallsCanBeAdded()
5259
{
5360
$extension = new Extension(false, 1, 'my.host', false);

Twig/Extension.php

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,7 @@ public function getFunctions(): array
5555
return [
5656
new TwigFunction('piwik_code', [$this, 'piwikCode'], ['is_safe' => ['html']]),
5757
new TwigFunction('piwik', [$this, 'piwikPush']),
58+
new TwigFunction('piwik_opt_out_code', [$this, 'piwikOptOutCode'], ['is_safe' => ['html']]),
5859
];
5960
}
6061

@@ -107,6 +108,14 @@ public function piwikCode(): string
107108
return $piwikCode;
108109
}
109110

111+
public function piwikOptOutCode(): string
112+
{
113+
return <<<EOT
114+
<div id="matomo-opt-out"></div>
115+
<script src="https://{$this->piwikHost}/index.php?module=CoreAdminHome&action=optOutJS&divId=matomo-opt-out&language=auto&showIntro=1"></script>
116+
EOT;
117+
}
118+
110119
private function addDefaultApiCalls()
111120
{
112121
$this->paqs[] = ['enableLinkTracking'];

0 commit comments

Comments
 (0)