Skip to content
This repository has been archived by the owner on Jul 20, 2022. It is now read-only.

Commit

Permalink
Update the javascript tracker on several events:
Browse files Browse the repository at this point in the history
- when Piwik updates
- when a plugin is enabled or disabled
- when the plugin settings are updated
  • Loading branch information
mnapoli committed Oct 14, 2014
1 parent df7d41f commit 4cd7b4d
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 1 deletion.
18 changes: 17 additions & 1 deletion CustomTrackerJs.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,20 @@

namespace Piwik\Plugins\CustomTrackerJs;

use Piwik\Log;
use Piwik\Plugin;

class CustomTrackerJs extends Plugin
{
public function getListHooksRegistered()
{
return array(
'CustomTrackerJs.getTrackerJsAdditions' => 'getTrackerJsAdditions',
'CustomTrackerJs.getTrackerJsAdditions' => 'getTrackerJsAdditions',
// Update the tracker when one of these events is raised
'Settings.CustomTrackerJs.settingsUpdated' => 'updateTracker',
'CoreUpdater.update.end' => 'updateTracker',
'PluginManager.pluginDeactivated' => 'updateTracker',
'PluginManager.pluginActivated' => 'updateTracker',
);
}

Expand All @@ -34,4 +40,14 @@ public function getTrackerJsAdditions(&$code)
$code .= PHP_EOL . $addition;
}
}

public function updateTracker()
{
try {
$trackerUpdater = new TrackerUpdater();
$trackerUpdater();
} catch (\Exception $e) {
Log::error('There was an error while updating the javascript tracker: ' . $e->getMessage());
}
}
}
4 changes: 4 additions & 0 deletions TrackerUpdater.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,10 @@ public function __construct($file = null)

public function __invoke()
{
if (! (file_exists($this->file) && is_readable($this->file) && is_writable($this->file))) {
throw new \InvalidArgumentException("The file '$this->file' doesn't exist or is not writable");
}

$originalJs = file_get_contents($this->file);
$addition = $this->getCustomJsAdditions();

Expand Down
10 changes: 10 additions & 0 deletions tests/Integration/TrackerUpdaterTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -97,4 +97,14 @@ public function testMultipleEventListeners()

$this->assertEquals($expected, file_get_contents($this->file));
}

/**
* @expectedException \InvalidArgumentException
* @expectedExceptionMessage The file 'foobar' doesn't exist or is not writable
*/
public function testUnknownFile()
{
$updater = new TrackerUpdater('foobar');
$updater();
}
}

0 comments on commit 4cd7b4d

Please sign in to comment.