xref: /plugin/numatomo/action.php (revision fce6bfe4ece0cdc69dbfc99fb7f7834aa00a7db5)
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		$loggedin = isset($INFO['userinfo']);
36		$exgrp = $this->getConf('exclude');
37		$exclUser = false;
38		if ($exgrp === 'admins' && ($loggedin && ($INFO['isadmin'] === 1))) {
39			$exclUser = true;
40		} elseif ($exgrp === 'users' && $loggedin) {
41			$exclUser = true;
42		}
43
44		$code = NL . DOKU_TAB . DOKU_TAB . "var _paq = window._paq = window._paq || [];" . NL;
45
46		foreach (explode(',', $this->getConf('feature-flags')) as $flag) {
47			$code .= DOKU_TAB . DOKU_TAB . "_paq.push(['$flag']);" . NL;
48		}
49		if ($exclUser) {
50			$code .= DOKU_TAB . DOKU_TAB . "_paq.push(['optUserOut']);" . NL;
51		}
52		$code .= DOKU_TAB . DOKU_TAB . "_paq.push(['setLinkClasses', ['interwiki','urlextern']]);" . NL;
53		$code .= DOKU_TAB . DOKU_TAB . "_paq.push(['setExcludedQueryParams', ['do']]);" . NL;
54		$code .= DOKU_TAB . DOKU_TAB . "_paq.push(['setDoNotTrack', 'true']);" . NL;
55		$code .= DOKU_TAB . DOKU_TAB . "_paq.push(['trackPageView']);" . NL;
56		$code .= DOKU_TAB . DOKU_TAB . "(function() {" . NL;
57		$code .= DOKU_TAB . DOKU_TAB . DOKU_TAB . "var u='{$this->getConf('server')}';" . NL;
58		$code .= DOKU_TAB . DOKU_TAB . DOKU_TAB . "_paq.push(['setTrackerUrl', u+'{$this->getConf('instance')}.php']);" . NL;
59		$code .= DOKU_TAB . DOKU_TAB . DOKU_TAB . "_paq.push(['setSiteId', '{$this->getConf('siteid')}}']);" . NL;
60		$code .= DOKU_TAB . DOKU_TAB . DOKU_TAB . "var d=document, g=d.createElement('script'), s=d.getElementsByTagName('script')[0];" . NL;
61		$code .= DOKU_TAB . DOKU_TAB . DOKU_TAB . "g.async=true; g.src=u+'{$this->getConf('instance')}.js'; s.parentNode.insertBefore(g,s);" . NL;
62		$code .= DOKU_TAB . DOKU_TAB . "})();" . NL. DOKU_TAB;
63
64		// $code .= '<!-- ' . print_r($INFO, true) . ' -->' . NL;
65
66        $event->data['script'][] = [
67			'_data'   => $code
68        ];
69    }
70}