xref: /plugin/googletagmanager/action.php (revision 8b69576f2c23587b30bc03355c10aeff2f67dc55)
1bfa47fb6SAlex<?php
2bfa47fb6SAlex
3bfa47fb6SAlexclass action_plugin_googletagmanager extends DokuWiki_Action_Plugin {
4bfa47fb6SAlex
531d01fc3SNickeau    const GTMID = 'GTMID';
631d01fc3SNickeau
7bfa47fb6SAlex    /**
8bfa47fb6SAlex         * return some info
9bfa47fb6SAlex         */
10bfa47fb6SAlex        function getInfo(){
11bfa47fb6SAlex                return array(
12bfa47fb6SAlex                        'author' => 'Alexander Lehmann',
13bfa47fb6SAlex                        'email'  => 'alexlehm@gmail.com',
140fc69df8SAlexander Lehmann                        'date'   => '2022-12-29',
15bfa47fb6SAlex                        'name'   => 'Google Tag Manager Plugin',
16bfa47fb6SAlex                        'desc'   => 'Plugin to embed Google Tag Manager in your wiki.',
170fc69df8SAlexander Lehmann                        'url'    => 'https://www.lehmann.cx/wiki/projects:dokuwiki_gtm',
18bfa47fb6SAlex                );
19bfa47fb6SAlex        }
20bfa47fb6SAlex
21bfa47fb6SAlex        /**
22bfa47fb6SAlex         * Register its handlers with the DokuWiki's event controller
23bfa47fb6SAlex         */
244747d7b2SAndreas Gohr        function register(Doku_Event_Handler $controller) {
25*8b69576fSAlexander Lehmann            $controller->register_hook('TPL_METAHEADER_OUTPUT', 'BEFORE',  $this, 'addHeaders');
26bfa47fb6SAlex        }
27bfa47fb6SAlex
28*8b69576fSAlexander Lehmann        function addHeaders (&$event, $param) {
29*8b69576fSAlexander Lehmann                $GTMID=$this->getConf(self::GTMID);
30*8b69576fSAlexander Lehmann                if(!$GTMID) return;
3131d01fc3SNickeau
32*8b69576fSAlexander Lehmann                $is_AW_tag = substr($GTMID,0,3)=='AW-';
330fc69df8SAlexander Lehmann
340fc69df8SAlexander Lehmann                if($is_AW_tag) {
350fc69df8SAlexander Lehmann                  $event->data['script'][] = array (
36*8b69576fSAlexander Lehmann                    'src' => "https://www.googletagmanager.com/gtag/js?id=".$GTMID,
370fc69df8SAlexander Lehmann                  );
380fc69df8SAlexander Lehmann                  $event->data['script'][] = array (
390fc69df8SAlexander Lehmann                    'type' => 'text/javascript',
40*8b69576fSAlexander Lehmann                    '_data' => "window.dataLayer = window.dataLayer || []; function gtag(){dataLayer.push(arguments);} gtag('js', new Date()); gtag('config', '".$GTMID."');",
410fc69df8SAlexander Lehmann                  );
420fc69df8SAlexander Lehmann                } else {
43bfa47fb6SAlex                  $event->data['noscript'][] = array (
44*8b69576fSAlexander Lehmann                    '_data' => '<iframe src="https://www.googletagmanager.com/ns.html?id='.$GTMID.'" height="0" width="0" style="display:none;visibility:hidden"></iframe>',
45bfa47fb6SAlex                  );
46bfa47fb6SAlex                  $event->data['script'][] = array (
47bfa47fb6SAlex                    'type' => 'text/javascript',
48bfa47fb6SAlex                    '_data' => "(function(w,d,s,l,i){w[l]=w[l]||[];w[l].push({'gtm.start':
49bfa47fb6SAlexnew Date().getTime(),event:'gtm.js'});var f=d.getElementsByTagName(s)[0],
50bfa47fb6SAlexj=d.createElement(s),dl=l!='dataLayer'?'&l='+l:'';j.async=true;j.src=
51*8b69576fSAlexander Lehmann'https://www.googletagmanager.com/gtm.js?id='+i+dl;f.parentNode.insertBefore(j,f);
52*8b69576fSAlexander Lehmann})(window,document,'script','dataLayer','".$GTMID."');",
53bfa47fb6SAlex                    );
54bfa47fb6SAlex                }
55bfa47fb6SAlex        }
560fc69df8SAlexander Lehmann}
57bfa47fb6SAlex?>
58