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