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