xref: /plugin/googletagmanager/action.php (revision bcb1a129a382c5dce447a723df54f0f9557e49f1)
1bfa47fb6SAlex<?php
2bfa47fb6SAlex
3737086b3SAlexander Lehmannclass action_plugin_googletagmanager extends DokuWiki_Action_Plugin
4737086b3SAlexander Lehmann{
5737086b3SAlexander Lehmann    public const GTMID = 'GTMID';
631d01fc3SNickeau
7bfa47fb6SAlex    /**
8bfa47fb6SAlex         * return some info
9bfa47fb6SAlex         */
10737086b3SAlexander Lehmann    public function getInfo()
11737086b3SAlexander Lehmann    {
12bfa47fb6SAlex            return array(
13bfa47fb6SAlex                    'author' => 'Alexander Lehmann',
14bfa47fb6SAlex                    'email'  => 'alexlehm@gmail.com',
150fc69df8SAlexander Lehmann                    'date'   => '2022-12-29',
16bfa47fb6SAlex                    'name'   => 'Google Tag Manager Plugin',
17bfa47fb6SAlex                    'desc'   => 'Plugin to embed Google Tag Manager in your wiki.',
180fc69df8SAlexander Lehmann                    'url'    => 'https://www.lehmann.cx/wiki/projects:dokuwiki_gtm',
19bfa47fb6SAlex            );
20bfa47fb6SAlex    }
21bfa47fb6SAlex
22bfa47fb6SAlex        /**
23bfa47fb6SAlex         * Register its handlers with the DokuWiki's event controller
24bfa47fb6SAlex         */
25737086b3SAlexander Lehmann    public function register(Doku_Event_Handler $controller)
26737086b3SAlexander Lehmann    {
278b69576fSAlexander Lehmann        $controller->register_hook('TPL_METAHEADER_OUTPUT', 'BEFORE', $this, 'addHeaders');
28bfa47fb6SAlex    }
29bfa47fb6SAlex
30737086b3SAlexander Lehmann    public function addHeaders(&$event, $param)
31737086b3SAlexander Lehmann    {
328b69576fSAlexander Lehmann            $GTMID = $this->getConf(self::GTMID);
338b69576fSAlexander Lehmann            if (!$GTMID) return;
3431d01fc3SNickeau
358b69576fSAlexander Lehmann            $is_AW_tag = substr($GTMID, 0, 3) == 'AW-';
360fc69df8SAlexander Lehmann
370fc69df8SAlexander Lehmann        if ($is_AW_tag) {
380fc69df8SAlexander Lehmann            $event->data['script'][] = array (
398b69576fSAlexander Lehmann                'src' => "https://www.googletagmanager.com/gtag/js?id=" . $GTMID,
400fc69df8SAlexander Lehmann            );
410fc69df8SAlexander Lehmann            $event->data['script'][] = array (
420fc69df8SAlexander Lehmann                'type' => 'text/javascript',
43*bcb1a129SAlexander Lehmann                '_data' => "window.dataLayer = window.dataLayer || []; function gtag(){dataLayer.push(arguments);}" .
44*bcb1a129SAlexander Lehmann                    " gtag('js', new Date()); gtag('config', '" .
45737086b3SAlexander Lehmann                    $GTMID .
46737086b3SAlexander Lehmann                    "');",
470fc69df8SAlexander Lehmann            );
480fc69df8SAlexander Lehmann        } else {
49bfa47fb6SAlex            $event->data['noscript'][] = array (
50*bcb1a129SAlexander Lehmann                '_data' => '<iframe src="https://www.googletagmanager.com/ns.html?id=' .
51*bcb1a129SAlexander Lehmann                    $GTMID .
52*bcb1a129SAlexander Lehmann                    '" height="0" width="0" style="display:none;visibility:hidden"></iframe>',
53bfa47fb6SAlex            );
54bfa47fb6SAlex            $event->data['script'][] = array (
55bfa47fb6SAlex                'type' => 'text/javascript',
56bfa47fb6SAlex                '_data' => "(function(w,d,s,l,i){w[l]=w[l]||[];w[l].push({'gtm.start':
57bfa47fb6SAlexnew Date().getTime(),event:'gtm.js'});var f=d.getElementsByTagName(s)[0],
58bfa47fb6SAlexj=d.createElement(s),dl=l!='dataLayer'?'&l='+l:'';j.async=true;j.src=
598b69576fSAlexander Lehmann'https://www.googletagmanager.com/gtm.js?id='+i+dl;f.parentNode.insertBefore(j,f);
60737086b3SAlexander Lehmann})(window,document,'script','dataLayer','" .
61737086b3SAlexander Lehmann                    $GTMID .
62737086b3SAlexander Lehmann                    "');",
63bfa47fb6SAlex            );
64bfa47fb6SAlex        }
65bfa47fb6SAlex    }
660fc69df8SAlexander Lehmann}
67