]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - plugins/PiwikAnalytics/PiwikAnalyticsPlugin.php
924e36c264c1f370cd9b0b53743b0a9ee424a7ee
[quix0rs-gnu-social.git] / plugins / PiwikAnalytics / PiwikAnalyticsPlugin.php
1 <?php
2 /**
3  * StatusNet, the distributed open-source microblogging tool
4  *
5  * Plugin to use Piwik Analytics
6  *
7  * PHP version 5
8  *
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.
13  *
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.
18  *
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/>.
21  *
22  * @category  Plugin
23  * @package   StatusNet
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/
29  */
30
31 if (!defined('STATUSNET')) {
32     exit(1);
33 }
34
35 /**
36  * Plugin to use Piwik Analytics (based on the Analytics plugin by Evan)
37  *
38  * This plugin will spoot out the correct JavaScript spell to invoke
39  * Piwik Analytics on a page.
40  *
41  * To use this plugin add the following to your config.php
42  *
43  *  addPlugin('PiwikAnalytics', array('piwikroot' => 'example.com/piwik/',
44  *                                    'piwikId' => 'id'));
45  *
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'.
50  *
51  */
52 class PiwikAnalyticsPlugin extends Plugin
53 {
54     const PLUGIN_VERSION = '2.0.0';
55
56     /** the base of your Piwik installation */
57     public $piwikroot = null;
58     /** the Piwik Id of your statusnet installation */
59     public $piwikId   = null;
60
61     /**
62      * constructor
63      *
64      * @param string $root Piwik root URL
65      * @param string $id   Piwik ID of this app
66      */
67     function __construct($root=null, $id=null)
68     {
69         $this->piwikroot = $root;
70         $this->piwikId   = $id;
71         parent::__construct();
72     }
73
74     /**
75      * Called when all scripts have been shown
76      *
77      * @param Action $action Current action
78      *
79      * @return boolean ignored
80      */
81     function onEndShowScripts($action)
82     {
83         // Slight modification to the default code.
84         // Loading the piwik.js file from a <script> created in a document.write
85         // meant that the browser had no way to preload it, ensuring that its
86         // loading will be synchronous, blocking further page rendering.
87         //
88         // User-agents understand protocol-relative links, so instead of the
89         // URL produced in JS we can just give a universal one. Since it's
90         // sitting there in the DOM ready to go, the browser can preload the
91         // file for us and we're less likely to have to wait for it.
92         $piwikUrl = '//' . $this->piwikroot . 'piwik.js';
93         $piwikCode = <<<ENDOFPIWIK
94 try {
95 var pkBaseURL = (("https:" == document.location.protocol) ? "https://{$this->piwikroot}" : "http://{$this->piwikroot}");
96     var piwikTracker = Piwik.getTracker(pkBaseURL + "piwik.php", {$this->piwikId});
97     piwikTracker.trackPageView();
98     piwikTracker.enableLinkTracking();
99 } catch( err ) {}
100 ENDOFPIWIK;
101
102         // Don't use $action->script() here; it'll try to preface the URL.
103         $action->element('script', array('type' => 'text/javascript', 'src' => $piwikUrl), ' ');
104         $action->inlineScript($piwikCode);
105         return true;
106     }
107
108     function onPluginVersion(array &$versions)
109     {
110         $versions[] = array('name' => 'PiwikAnalytics',
111                             'version' => self::PLUGIN_VERSION,
112                             'author' => 'Tobias Diekershoff, Evan Prodromou',
113                             'homepage' => 'https://git.gnu.io/gnu/gnu-social/tree/master/plugins/Piwik',
114                             'rawdescription' =>
115                             // TRANS: Plugin description.
116                             _m('Use <a href="http://piwik.org/">Piwik</a> Open Source web analytics software.'));
117         return true;
118     }
119 }