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'   => '2014-02-01',
18			'name'   => 'Google Tag Manager Plugin',
19			'desc'   => 'Plugin to embed Google Tag Manager in your wiki.',
20			'url'    => 'http://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		$event->data['noscript'][] = array (
35		  '_data' => '<iframe src="//www.googletagmanager.com/ns.html?id='.$this->getConf(self::GTMID).'" height="0" width="0" style="display:none;visibility:hidden"></iframe>',
36		);
37		$event->data['script'][] = array (
38		  'type' => 'text/javascript',
39		  '_data' => "(function(w,d,s,l,i){w[l]=w[l]||[];w[l].push({'gtm.start':
40new Date().getTime(),event:'gtm.js'});var f=d.getElementsByTagName(s)[0],
41j=d.createElement(s),dl=l!='dataLayer'?'&l='+l:'';j.async=true;j.src=
42'//www.googletagmanager.com/gtm.js?id='+i+dl;f.parentNode.insertBefore(j,f);
43})(window,document,'script','dataLayer','".$this->getConf(self::GTMID)."');",
44		);
45	}
46}
47?>
48