Skip to content

Commit

Permalink
Added an option to disable Piwik tracking
Browse files Browse the repository at this point in the history
  • Loading branch information
mnapoli committed Sep 16, 2014
1 parent 2aa781e commit 9ff7523
Show file tree
Hide file tree
Showing 5 changed files with 65 additions and 1 deletion.
10 changes: 10 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,16 @@ Don't forget to add the tracker script at the end of the HTML document, for exam
</html>
```

### Development environment

In some environments, you want to disable Piwik's tracking (for example on your local machine).
That is easily possible by passing `false` for the `$enabled` parameter:

```php
$twig->addExtension(new PiwikTwigExtension($host, $siteId, false));
```


## License

This library is released under the [MIT license](http://opensource.org/licenses/MIT).
20 changes: 19 additions & 1 deletion src/PiwikTwigExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,18 @@ class PiwikTwigExtension extends Twig_Extension
*/
private $siteId;

public function __construct($piwikHost = null, $siteId = null)
/**
* Useful for disabling Piwik tracking in dev environment.
*
* @var bool
*/
private $enabled = true;

public function __construct($piwikHost = null, $siteId = null, $enabled = true)
{
$this->piwikHost = $piwikHost;
$this->siteId = $siteId;
$this->enabled = (bool) $enabled;
}

public function getName()
Expand All @@ -45,6 +53,11 @@ public function getFunctions()
];
}

/**
* @param string|null $piwikHost
* @param string|null $siteId
* @return string
*/
public function generatePiwikTrackerCode($piwikHost = null, $siteId = null)
{
$piwikHost = $piwikHost ?: $this->piwikHost;
Expand All @@ -57,6 +70,11 @@ public function generatePiwikTrackerCode($piwikHost = null, $siteId = null)
throw new InvalidArgumentException('No Piwik site ID was configured or given to generate the tracker code');
}

// Check only now so that exceptions are still thrown in dev environment
if (! $this->enabled) {
return '';
}

$piwikHost = rtrim($piwikHost, '/');

$code = <<<HTML
Expand Down
8 changes: 8 additions & 0 deletions tests/DisabledExtensionFixture/EmptyFunctionCall.test
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
--TEST--
Calling "piwik" function without parameters when the extension is disabled
--TEMPLATE--
{{ piwik() }}
--DATA--
return array();
--EXPECT--
Twig_Error: Twig_Error_Runtime: An exception has been thrown during the rendering of a template ("No Piwik host was configured or given to generate the tracker code") in "index.twig" at line 2 in "EmptyFunctionCall.test".
7 changes: 7 additions & 0 deletions tests/DisabledExtensionFixture/FunctionCall.test
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
--TEST--
Calling "piwik" function when the extension is disabled
--TEMPLATE--
{{ piwik('foo', 123) }}
--DATA--
return array();
--EXPECT--
21 changes: 21 additions & 0 deletions tests/DisabledExtensionTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<?php

namespace Tests\PiwikTwigExtension;

use PiwikTwigExtension\PiwikTwigExtension;
use Twig_Test_IntegrationTestCase;

class DisabledExtensionTest extends Twig_Test_IntegrationTestCase
{
protected function getExtensions()
{
return [
new PiwikTwigExtension(null, null, false),
];
}

protected function getFixturesDir()
{
return __DIR__ . '/DisabledExtensionFixture/';
}
}

0 comments on commit 9ff7523

Please sign in to comment.