1<?php 2 3use dokuwiki\Extension\EventHandler; 4use dokuwiki\Extension\Event; 5 6/** 7 * Action Component for the Nu Matomo Plugin 8 * 9 * @license GPL 2 (http://www.gnu.org/licenses/gpl.html) 10 * @author Sascha Leib <sascha.leib(at)kolmio.com> 11 */ 12 13class action_plugin_numatomo extends DokuWiki_Action_Plugin { 14 15 /** 16 * Registers a callback functions 17 * 18 * @param EventHandler $controller DokuWiki's event controller object 19 * @return void 20 */ 21 public function register(EventHandler $controller) 22 { 23 $controller->register_hook('TPL_METAHEADER_OUTPUT', 'BEFORE', $this, 'handleHeader'); 24 } 25 26 /** 27 * Adds the preview parameter to the stylesheet loading in non-js mode 28 * 29 * @param Event $event event object by reference 30 * @return void 31 */ 32 public function handleHeader(Event $event, $param) { 33 34 global $INFO; 35 36 // is the user logged in? 37 $loggedin = isset($INFO['userinfo']); 38 39 // build the tracker code: 40 $code = NL . DOKU_TAB . DOKU_TAB . "var _paq = window._paq = window._paq || [];" . NL; 41 42 // add the feature flags: 43 foreach (explode(',', $this->getConf('feature-flags')) as $flag) { 44 $code .= DOKU_TAB . DOKU_TAB . "_paq.push(['$flag']);" . NL; 45 } 46 47 // get setting about excluded user groups: 48 $exgrp = $this->getConf('exclude'); 49 $exclUser = false; 50 if ($exgrp === 'admins' && ($loggedin && ($INFO['isadmin'] === 1))) { 51 $exclUser = true; 52 } elseif ($exgrp === 'users' && $loggedin) { 53 $exclUser = true; 54 } 55 if ($exclUser) { 56 $code .= DOKU_TAB . DOKU_TAB . "_paq.push(['optUserOut']);" . NL; 57 } 58 59 // honour the DoNotTrack header? 60 if ($this->getConf('donottrack') !== 0) { 61 $code .= DOKU_TAB . DOKU_TAB . "_paq.push(['setDoNotTrack', 'true']);" . NL; 62 } 63 64 // useful settings for DokuWiki sites 65 $code .= DOKU_TAB . DOKU_TAB . "_paq.push(['setLinkClasses', ['interwiki','urlextern']]);" . NL; 66 $code .= DOKU_TAB . DOKU_TAB . "_paq.push(['setExcludedQueryParams', ['do']]);" . NL; 67 68 // continue default code: 69 $code .= DOKU_TAB . DOKU_TAB . "_paq.push(['trackPageView']);" . NL; 70 $code .= DOKU_TAB . DOKU_TAB . "(function() {" . NL; 71 $code .= DOKU_TAB . DOKU_TAB . DOKU_TAB . "var u='{$this->getConf('server')}';" . NL; 72 $code .= DOKU_TAB . DOKU_TAB . DOKU_TAB . "_paq.push(['setTrackerUrl', u+'{$this->getConf('instance')}.php']);" . NL; 73 $code .= DOKU_TAB . DOKU_TAB . DOKU_TAB . "_paq.push(['setSiteId', '{$this->getConf('siteid')}']);" . NL; 74 $code .= DOKU_TAB . DOKU_TAB . DOKU_TAB . "var d=document, g=d.createElement('script'), s=d.getElementsByTagName('script')[0];" . NL; 75 $code .= DOKU_TAB . DOKU_TAB . DOKU_TAB . "g.async=true; g.src=u+'{$this->getConf('instance')}.js'; s.parentNode.insertBefore(g,s);" . NL; 76 $code .= DOKU_TAB . DOKU_TAB . "})();" . NL. DOKU_TAB; 77 78 $event->data['script'][] = [ 79 '_data' => $code 80 ]; 81 } 82}