3 * StatusNet, the distributed open-source microblogging tool
5 * Plugin to use Piwik Analytics
9 * LICENCE: This program is free software: you can redistribute it and/or modify
10 * it under the terms of the GNU Affero General Public License as published by
11 * the Free Software Foundation, either version 3 of the License, or
12 * (at your option) any later version.
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU Affero General Public License for more details.
19 * You should have received a copy of the GNU Affero General Public License
20 * along with this program. If not, see <http://www.gnu.org/licenses/>.
24 * @author Evan Prodromou <evan@status.net>
25 * @author Tobias Diekershoff <tobias.diekershoff@gmx.net>
26 * @copyright 2008 StatusNet, Inc.
27 * @license http://www.fsf.org/licensing/licenses/agpl-3.0.html GNU Affero General Public License version 3.0
28 * @link http://status.net/
31 if (!defined('STATUSNET')) {
36 * Plugin to use Piwik Analytics (based on the Analytics plugin by Evan)
38 * This plugin will spoot out the correct JavaScript spell to invoke
39 * Piwik Analytics on a page.
41 * To use this plugin add the following to your config.php
43 * addPlugin('PiwikAnalytics', array('piwikroot' => 'example.com/piwik/',
44 * 'piwikId' => 'id'));
46 * Replace 'example.com/piwik/' with the URL to your Piwik installation and
47 * make sure you don't forget the final /.
48 * Replace 'id' with the ID your statusnet installation has in your Piwik
49 * analytics setup - for example '8'.
52 class PiwikAnalyticsPlugin extends Plugin
54 /** the base of your Piwik installation */
55 public $piwikroot = null;
56 /** the Piwik Id of your statusnet installation */
57 public $piwikId = null;
62 * @param string $root Piwik root URL
63 * @param string $id Piwik ID of this app
65 function __construct($root=null, $id=null)
67 $this->piwikroot = $root;
69 parent::__construct();
73 * Called when all scripts have been shown
75 * @param Action $action Current action
77 * @return boolean ignored
79 function onEndShowScripts($action)
81 // Slight modification to the default code.
82 // Loading the piwik.js file from a <script> created in a document.write
83 // meant that the browser had no way to preload it, ensuring that its
84 // loading will be synchronous, blocking further page rendering.
86 // User-agents understand protocol-relative links, so instead of the
87 // URL produced in JS we can just give a universal one. Since it's
88 // sitting there in the DOM ready to go, the browser can preload the
89 // file for us and we're less likely to have to wait for it.
90 $piwikUrl = '//' . $this->piwikroot . 'piwik.js';
91 $piwikCode = <<<ENDOFPIWIK
93 var pkBaseURL = (("https:" == document.location.protocol) ? "https://{$this->piwikroot}" : "http://{$this->piwikroot}");
94 var piwikTracker = Piwik.getTracker(pkBaseURL + "piwik.php", {$this->piwikId});
95 piwikTracker.trackPageView();
96 piwikTracker.enableLinkTracking();
100 // Don't use $action->script() here; it'll try to preface the URL.
101 $action->element('script', array('type' => 'text/javascript', 'src' => $piwikUrl), ' ');
102 $action->inlineScript($piwikCode);
106 function onPluginVersion(array &$versions)
108 $versions[] = array('name' => 'PiwikAnalytics',
109 'version' => GNUSOCIAL_VERSION,
110 'author' => 'Tobias Diekershoff, Evan Prodromou',
111 'homepage' => 'http://status.net/wiki/Plugin:Piwik',
113 // TRANS: Plugin description.
114 _m('Use <a href="http://piwik.org/">Piwik</a> Open Source web analytics software.'));