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