1bfa47fb6SAlex<?php 2bfa47fb6SAlex 3*737086b3SAlexander Lehmannclass action_plugin_googletagmanager extends DokuWiki_Action_Plugin 4*737086b3SAlexander Lehmann{ 5*737086b3SAlexander Lehmann public const GTMID = 'GTMID'; 631d01fc3SNickeau 7bfa47fb6SAlex /** 8bfa47fb6SAlex * return some info 9bfa47fb6SAlex */ 10*737086b3SAlexander Lehmann public function getInfo() 11*737086b3SAlexander 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 */ 25*737086b3SAlexander Lehmann public function register(Doku_Event_Handler $controller) 26*737086b3SAlexander Lehmann { 278b69576fSAlexander Lehmann $controller->register_hook('TPL_METAHEADER_OUTPUT', 'BEFORE', $this, 'addHeaders'); 28bfa47fb6SAlex } 29bfa47fb6SAlex 30*737086b3SAlexander Lehmann public function addHeaders(&$event, $param) 31*737086b3SAlexander 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*737086b3SAlexander Lehmann '_data' => "window.dataLayer = window.dataLayer || []; function gtag(){dataLayer.push(arguments);} gtag('js', new Date()); gtag('config', '" . 44*737086b3SAlexander Lehmann $GTMID . 45*737086b3SAlexander Lehmann "');", 460fc69df8SAlexander Lehmann ); 470fc69df8SAlexander Lehmann } else { 48bfa47fb6SAlex $event->data['noscript'][] = array ( 498b69576fSAlexander Lehmann '_data' => '<iframe src="https://www.googletagmanager.com/ns.html?id=' . $GTMID . '" height="0" width="0" style="display:none;visibility:hidden"></iframe>', 50bfa47fb6SAlex ); 51bfa47fb6SAlex $event->data['script'][] = array ( 52bfa47fb6SAlex 'type' => 'text/javascript', 53bfa47fb6SAlex '_data' => "(function(w,d,s,l,i){w[l]=w[l]||[];w[l].push({'gtm.start': 54bfa47fb6SAlexnew Date().getTime(),event:'gtm.js'});var f=d.getElementsByTagName(s)[0], 55bfa47fb6SAlexj=d.createElement(s),dl=l!='dataLayer'?'&l='+l:'';j.async=true;j.src= 568b69576fSAlexander Lehmann'https://www.googletagmanager.com/gtm.js?id='+i+dl;f.parentNode.insertBefore(j,f); 57*737086b3SAlexander Lehmann})(window,document,'script','dataLayer','" . 58*737086b3SAlexander Lehmann $GTMID . 59*737086b3SAlexander Lehmann "');", 60bfa47fb6SAlex ); 61bfa47fb6SAlex } 62bfa47fb6SAlex } 630fc69df8SAlexander Lehmann} 64