xref: /plugin/googletagmanager/action.php (revision 0fc69df8483e8f621d0a6e0698bde92fcaa37a03)
1bfa47fb6SAlex<?php
2bfa47fb6SAlexif(!defined('DOKU_INC')) die();
3bfa47fb6SAlexif(!defined('DOKU_PLUGIN')) define('DOKU_PLUGIN',DOKU_INC.'lib/plugins/');
4bfa47fb6SAlexrequire_once(DOKU_PLUGIN.'action.php');
5bfa47fb6SAlex
6bfa47fb6SAlexclass action_plugin_googletagmanager extends DokuWiki_Action_Plugin {
7bfa47fb6SAlex
831d01fc3SNickeau    const GTMID = 'GTMID';
931d01fc3SNickeau
10bfa47fb6SAlex    /**
11bfa47fb6SAlex         * return some info
12bfa47fb6SAlex         */
13bfa47fb6SAlex        function getInfo(){
14bfa47fb6SAlex                return array(
15bfa47fb6SAlex                        'author' => 'Alexander Lehmann',
16bfa47fb6SAlex                        'email'  => 'alexlehm@gmail.com',
17*0fc69df8SAlexander Lehmann                        'date'   => '2022-12-29',
18bfa47fb6SAlex                        'name'   => 'Google Tag Manager Plugin',
19bfa47fb6SAlex                        'desc'   => 'Plugin to embed Google Tag Manager in your wiki.',
20*0fc69df8SAlexander Lehmann                        'url'    => 'https://www.lehmann.cx/wiki/projects:dokuwiki_gtm',
21bfa47fb6SAlex                );
22bfa47fb6SAlex        }
23bfa47fb6SAlex
24bfa47fb6SAlex        /**
25bfa47fb6SAlex         * Register its handlers with the DokuWiki's event controller
26bfa47fb6SAlex         */
274747d7b2SAndreas Gohr        function register(Doku_Event_Handler $controller) {
28bfa47fb6SAlex            $controller->register_hook('TPL_METAHEADER_OUTPUT', 'BEFORE',  $this, '_addHeaders');
29bfa47fb6SAlex        }
30bfa47fb6SAlex
31bfa47fb6SAlex        function _addHeaders (&$event, $param) {
3231d01fc3SNickeau
3331d01fc3SNickeau                if(!$this->getConf(self::GTMID)) return;
34*0fc69df8SAlexander Lehmann
35*0fc69df8SAlexander Lehmann                $is_AW_tag = substr($this->getConf(self::GTMID),0,3)=='AW-';
36*0fc69df8SAlexander Lehmann
37*0fc69df8SAlexander Lehmann                if($is_AW_tag) {
38*0fc69df8SAlexander Lehmann                  $event->data['script'][] = array (
39*0fc69df8SAlexander Lehmann                    'src' => "https://www.googletagmanager.com/gtag/js?id=".$this->getConf(self::GTMID),
40*0fc69df8SAlexander Lehmann                  );
41*0fc69df8SAlexander Lehmann                  $event->data['script'][] = array (
42*0fc69df8SAlexander Lehmann                    'type' => 'text/javascript',
43*0fc69df8SAlexander Lehmann                    '_data' => "window.dataLayer = window.dataLayer || []; function gtag(){dataLayer.push(arguments);} gtag('js', new Date()); gtag('config', '".$this->getConf(self::GTMID)."');",
44*0fc69df8SAlexander Lehmann                  );
45*0fc69df8SAlexander Lehmann                } else {
46bfa47fb6SAlex                  $event->data['noscript'][] = array (
4731d01fc3SNickeau                    '_data' => '<iframe src="//www.googletagmanager.com/ns.html?id='.$this->getConf(self::GTMID).'" height="0" width="0" style="display:none;visibility:hidden"></iframe>',
48bfa47fb6SAlex                  );
49bfa47fb6SAlex                  $event->data['script'][] = array (
50bfa47fb6SAlex                    'type' => 'text/javascript',
51bfa47fb6SAlex                    '_data' => "(function(w,d,s,l,i){w[l]=w[l]||[];w[l].push({'gtm.start':
52bfa47fb6SAlexnew Date().getTime(),event:'gtm.js'});var f=d.getElementsByTagName(s)[0],
53bfa47fb6SAlexj=d.createElement(s),dl=l!='dataLayer'?'&l='+l:'';j.async=true;j.src=
54bfa47fb6SAlex'//www.googletagmanager.com/gtm.js?id='+i+dl;f.parentNode.insertBefore(j,f);
5531d01fc3SNickeau})(window,document,'script','dataLayer','".$this->getConf(self::GTMID)."');",
56bfa47fb6SAlex                    );
57bfa47fb6SAlex                }
58bfa47fb6SAlex        }
59*0fc69df8SAlexander Lehmann}
60bfa47fb6SAlex?>
61