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