1<?php
2if (!defined('DOKU_INC')) {
3    die();
4}
5if (!defined('DOKU_PLUGIN')) {
6    define('DOKU_PLUGIN', DOKU_INC . 'lib/plugins/');
7}
8
9/**
10 * Google Analytics for DokuWiki
11 *
12 * @license    GPL 2 (http://www.gnu.org/licenses/gpl.html)
13 * @author     Terence J. Grant<tjgrant@tatewake.com>
14 */
15
16class action_plugin_googleanalytics extends DokuWiki_Action_Plugin
17{
18    private $gaEnabled = true;
19
20    /**
21     * Register its handlers with the DokuWiki's event controller
22     *
23     * @param Doku_Event_Handler $controller
24     */
25    public function register(Doku_Event_Handler $controller)
26    {
27        $controller->register_hook('DOKUWIKI_STARTED', 'AFTER', $this, 'gaConfig');
28    }
29
30    /**
31     * Initialize the Google Analytics config
32     *
33     * @param Doku_Event $event
34     * @param array $param
35     */
36    public function gaConfig(Doku_Event $event, $param)
37    {
38        global $JSINFO;
39        global $INFO;
40        global $ACT;
41
42        if (!$this->gaEnabled) {
43            return;
44        }
45        $trackingId = $this->getConf('GAID');
46        $gtagId = $this->getConf('GTAGID');
47
48        if (!$trackingId && !$gtagId) {
49            return;
50        }
51
52        if ($this->getConf('dont_count_admin') && $INFO['isadmin']) {
53            return;
54        }
55        if ($this->getConf('dont_count_users') && $_SERVER['REMOTE_USER']) {
56            return;
57        }
58        act_clean($ACT);
59
60        $options = array();
61        if ($this->getConf('track_users') && $_SERVER['REMOTE_USER']) {
62            $options['userId'] = md5(auth_cookiesalt() . 'googleanalytics' . $_SERVER['REMOTE_USER']);
63        }
64        if ($this->getConf('domainName')) {
65            $options['cookieDomain'] = $this->getConf('domainName');
66            $options['legacyCookieDomain'] = $this->getConf('domainName');
67        }
68
69        global $conf;
70        $JSINFO['ga'] = array(
71            'trackingId' => $trackingId,
72            'gtagId' => $gtagId,
73            'anonymizeIp' => (bool) $this->getConf('anonymize'),
74            'action' => $ACT,
75            'trackOutboundLinks' => (bool) $this->getConf('track_links'),
76            'options' => $options,
77            'pageview' => $this->getPageView(),
78            'debug' => (bool) $conf['allowdebug']
79        );
80    }
81
82    /**
83     * normalize the pageview
84     *
85     * @return string
86     */
87    protected function getPageView()
88    {
89        global $QUERY;
90        global $ID;
91        global $INPUT;
92        global $ACT;
93
94        // clean up parameters to log
95        $params = $_GET;
96        if (isset($params['do'])) {
97            unset($params['do']);
98        }
99        if (isset($params['id'])) {
100            unset($params['id']);
101        }
102
103        // decide on virtual views
104        if ($ACT == 'search') {
105            $view = '~search/';
106            $params['q'] = $QUERY;
107        } elseif ($ACT == 'admin') {
108            $page = $INPUT->str('page');
109            $view = '~admin';
110            if ($page) {
111                $view .= '/' . $page;
112            }
113            if (isset($params['page'])) {
114                unset($params['page']);
115            }
116        } else {
117            $view = str_replace(':', '/', $ID); // slashes needed for Content Drilldown
118        }
119
120        // prepend basedir, allows logging multiple dir based animals in one tracker
121        $view = DOKU_REL . $view;
122
123        // append query parameters
124        $query = http_build_query($params, '', '&');
125        if ($query) {
126            $view .= '?' . $query;
127        }
128
129        return $view;
130    }
131}
132