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