xref: /plugin/piwiktagmanager/action.php (revision 65120627bd3358816196df1a27d110f14338d9de)
1d10bec37SAlexander Lehmann<?php
2d10bec37SAlexander Lehmann
3*65120627SAlexander Lehmannclass action_plugin_piwiktagmanager extends DokuWiki_Action_Plugin
4*65120627SAlexander Lehmann{
5*65120627SAlexander Lehmann    public const PWTMID = 'PWTMID';
6*65120627SAlexander Lehmann    public const PWTMHOST = 'PWTMHOST';
7d10bec37SAlexander Lehmann
8d10bec37SAlexander Lehmann    /**
9d10bec37SAlexander Lehmann         * return some info
10d10bec37SAlexander Lehmann         */
11*65120627SAlexander Lehmann    public function getInfo()
12*65120627SAlexander Lehmann    {
13d10bec37SAlexander Lehmann            return array(
14d10bec37SAlexander Lehmann                    'author' => 'Alexander Lehmann',
15d10bec37SAlexander Lehmann                    'email'  => 'alexlehm@gmail.com',
16ea5fd02aSAlexander Lehmann                    'date'   => '2025-12-06',
17ea5fd02aSAlexander Lehmann                    'name'   => 'Piwik (Matomo) Tag Manager',
18ea5fd02aSAlexander Lehmann                    'desc'   => 'Plugin to embed Piwik/Matomo Tag Manager in your wiki.',
19ea5fd02aSAlexander Lehmann                    'url'    => 'https://wiki.lehmann.cx/projects:dokuwiki_piwik',
20d10bec37SAlexander Lehmann            );
21d10bec37SAlexander Lehmann    }
22d10bec37SAlexander Lehmann
23d10bec37SAlexander Lehmann        /**
24d10bec37SAlexander Lehmann         * Register its handlers with the DokuWiki's event controller
25d10bec37SAlexander Lehmann         */
26*65120627SAlexander Lehmann    public function register(Doku_Event_Handler $controller)
27*65120627SAlexander Lehmann    {
28*65120627SAlexander Lehmann        $controller->register_hook('TPL_METAHEADER_OUTPUT', 'BEFORE', $this, 'addHeaders');
29d10bec37SAlexander Lehmann    }
30d10bec37SAlexander Lehmann
31*65120627SAlexander Lehmann    private function addHeaders(&$event, $param)
32*65120627SAlexander Lehmann    {
33d10bec37SAlexander Lehmann
34d10bec37SAlexander Lehmann            if (!$this->getConf(self::PWTMID)) return;
35d10bec37SAlexander Lehmann
36d10bec37SAlexander Lehmann            $event->data['script'][] = array (
37d10bec37SAlexander Lehmann                'type' => 'text/javascript',
38d10bec37SAlexander Lehmann                '_data' => "
39d10bec37SAlexander Lehmann  var _mtm = window._mtm = window._mtm || [];
40d10bec37SAlexander Lehmann  _mtm.push({'mtm.startTime': (new Date().getTime()), 'event': 'mtm.Start'});
41d10bec37SAlexander Lehmann  (function() {
42d10bec37SAlexander Lehmann    var d=document, g=d.createElement('script'), s=d.getElementsByTagName('script')[0];
43*65120627SAlexander Lehmann    g.async=true; g.src='https://" .
44*65120627SAlexander Lehmann        $this->getConf(self::PWTMHOST) .
45*65120627SAlexander Lehmann        "/piwik/js/container_" .
46*65120627SAlexander Lehmann        $this->getConf(self::PWTMID) .
47*65120627SAlexander Lehmann        ".js'; s.parentNode.insertBefore(g,s);
48d10bec37SAlexander Lehmann  })();",
49d10bec37SAlexander Lehmann            );
50d10bec37SAlexander Lehmann    }
51d10bec37SAlexander Lehmann}
52