]> git.mxchange.org Git - friendica-addons.git/blob - piwik/piwik.php
Merge remote-tracking branch 'old/master' into mergeto
[friendica-addons.git] / piwik / piwik.php
1 <?php
2 /**
3  * Name: Piwik Analytics
4  * Description: Piwik Analytics Plugin for Friendica
5  * Version: 1.0
6  * Author: Tobias Diekershoff <https://diekershoff.homeunix.net/friendika/profile/tobias>
7  */
8  
9
10 /*   Piwik Analytics Plugin for Friendica
11  *
12  *   Author: Tobias Diekershoff
13  *           tobias.diekershoff@gmx.net
14  *
15 *   License: 3-clause BSD license
16  *
17  *   Configuration:
18  *     Add the following two lines to your .htconfig.php file:
19  *
20  *     $a->config['piwik']['baseurl'] = 'www.example.com/piwik/';
21  *     $a->config['piwik']['siteid'] = '1';
22  *     $a->config['piwik']['optout'] = true;  // set to false to disable
23  *
24  *     Change the siteid to the ID that the Piwik tracker for your Friendica
25  *     installation has. Alter the baseurl to fit your needs, don't care
26  *     about http/https but beware to put the trailing / at the end of your
27  *     setting.
28  *
29  *     Documentation see http://diekershoff.homeunix.net/redmine/wiki/friendikaplugin/Piwik_Plugin
30  */
31
32 function piwik_install() {
33         register_hook('page_end', 'addon/piwik/piwik.php', 'piwik_analytics');
34
35         logger("installed piwik plugin");
36 }
37
38 function piwik_uninstall() {
39         unregister_hook('page_end', 'addon/piwik/piwik.php', 'piwik_analytics');
40
41         logger("uninstalled piwik plugin");
42 }
43
44 function piwik_analytics($a,&$b) {
45
46         /*
47          *   styling of every HTML block added by this plugin is done in the
48          *   associated CSS file. We just have to tell Friendica to get it
49          *   into the page header.
50          */
51         $a->page['htmlhead'] .= '<link rel="stylesheet"  type="text/css" href="' . $a->get_baseurl() . '/addon/piwik/piwik.css' . '" media="all" />' . "\r\n";
52
53         /*
54          *   Get the configuration variables from the .htconfig file.
55          */
56         $baseurl = get_config('piwik','baseurl');
57         $siteid  = get_config('piwik','siteid');
58         $optout  = get_config('piwik','optout');
59
60         /*
61          *   Add the Piwik code for the site.
62          */
63         $b .= "<div id='piwik-code-block'> <!-- Piwik -->\r\n <script type=\"text/javascript\">\r\n var pkBaseURL = ((\"https:\" == document.location.protocol) ? \"https://".$baseurl."\" : \"http://".$baseurl."\");\r\n document.write(unescape(\"%3Cscript src='\" + pkBaseURL + \"piwik.js' type='text/javascript'%3E%3C/script%3E\"));\r\n </script>\r\n<script type=\"text/javascript\">\r\n try {\r\n var piwikTracker = Piwik.getTracker(pkBaseURL + \"piwik.php\", ".$siteid.");\r\n piwikTracker.trackPageView();\r\n piwikTracker.enableLinkTracking();\r\n }\r\n catch( err ) {}\r\n </script>\r\n<noscript><p><img src=\"http://".$baseurl."/piwik.php?idsite=".$siteid."\" style=\"border:0\" alt=\"\" /></p></noscript>\r\n <!-- End Piwik Tracking Tag --> </div>";
64         /*
65          *   If the optout variable is set to true then display the notice
66          *   otherwise just include the above code into the page.
67          */
68         if ($optout) {
69             $b .= "<div id='piwik-optout-link'>";
70             $b .= t("This website is tracked using the <a href='http://www.piwik.org'>Piwik</a> analytics tool.");
71             $b .= " ";
72             $the_url =  "http://".$baseurl ."index.php?module=CoreAdminHome&action=optOut";
73             $b .= sprintf(t("If you do not want that your visits are logged this way you <a href='%s'>can set a cookie to prevent Piwik from tracking further visits of the site</a> (opt-out)."), $the_url);
74             $b .= "</div>";
75         }
76
77 }
78 function piwik_plugin_admin (&$a, &$o) {
79     $t = file_get_contents( dirname(__file__)."/admin.tpl");
80     $o = replace_macros( $t, array(
81             '$submit' => t('Submit'),
82             '$baseurl' => array('baseurl', t('Piwik Base URL'), get_config('piwik','baseurl' ), ''),
83             '$siteid' => array('siteid', t('Site ID'), get_config('piwik','siteid' ), ''),
84             '$optout' => array('optout', t('Show opt-out cookie link?'), get_config('piwik','optout' ), ''),
85         ));
86 }
87 function piwik_plugin_admin_post (&$a) {
88     $url = ((x($_POST, 'baseurl')) ? notags(trim($_POST['baseurl'])) : '');
89     $id = ((x($_POST, 'siteid')) ? trim($_POST['siteid']) : '');
90     $optout = ((x($_POST, 'optout')) ? trim($_POST['optout']) : '');
91     set_config('piwik', 'baseurl', $url);
92     set_config('piwik', 'siteid', $id);
93     set_config('piwik', 'optout', $optout);
94     info( t('Settings updated.'). EOL);
95 }