summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAndy Williams <andy@andywilliams.me>2017-12-07 14:45:11 +0000
committerAndy Williams <andy@andywilliams.me>2017-12-07 14:45:11 +0000
commit1a0421839374f00ded5fe2e5604f7e9065a164b7 (patch)
tree986c64837066fedca81769fbda56cfd27b3cdfeb
parent941594a1e4815367a88ea29bcbfb229f0022a5bc (diff)
Analytics: Add GA so we can get a view of what's popular on the site
-rw-r--r--public_html/lib/plugins/googleanalytics/README28
-rw-r--r--public_html/lib/plugins/googleanalytics/action.php96
-rw-r--r--public_html/lib/plugins/googleanalytics/conf/default.php9
-rw-r--r--public_html/lib/plugins/googleanalytics/conf/metadata.php9
-rw-r--r--public_html/lib/plugins/googleanalytics/lang/en/settings.php9
-rw-r--r--public_html/lib/plugins/googleanalytics/lang/ja/settings.php4
-rw-r--r--public_html/lib/plugins/googleanalytics/lang/pl/settings.php4
-rw-r--r--public_html/lib/plugins/googleanalytics/lang/se/settings.php4
-rw-r--r--public_html/lib/plugins/googleanalytics/lang/zh/settings.php4
-rw-r--r--public_html/lib/plugins/googleanalytics/plugin.info.txt7
-rw-r--r--public_html/lib/plugins/googleanalytics/script.js52
11 files changed, 226 insertions, 0 deletions
diff --git a/public_html/lib/plugins/googleanalytics/README b/public_html/lib/plugins/googleanalytics/README
new file mode 100644
index 00000000..03bbc47f
--- /dev/null
+++ b/public_html/lib/plugins/googleanalytics/README
@@ -0,0 +1,28 @@
1googleanalytics Plugin for DokuWiki
2
3Plugin to embed your Google Analytics code for your site, which
4allows you to track your visitors.
5
6All documentation for this plugin can be found at
7https://www.dokuwiki.org/plugin:googleanalytics
8
9If you install this plugin manually, make sure it is installed in
10lib/plugins/googleanalytics/ - if the folder is called different it
11will not work!
12
13Please refer to http://www.dokuwiki.org/plugins for additional info
14on how to install plugins in DokuWiki.
15
16----
17Copyright (C) Terence J. Grant <tjgrant@tatewake.com> et al.
18
19This program is free software; you can redistribute it and/or modify
20it under the terms of the GNU General Public License as published by
21the Free Software Foundation; version 2 of the License
22
23This program is distributed in the hope that it will be useful,
24but WITHOUT ANY WARRANTY; without even the implied warranty of
25MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
26GNU General Public License for more details.
27
28See the COPYING file in your DokuWiki folder for details
diff --git a/public_html/lib/plugins/googleanalytics/action.php b/public_html/lib/plugins/googleanalytics/action.php
new file mode 100644
index 00000000..f255dc59
--- /dev/null
+++ b/public_html/lib/plugins/googleanalytics/action.php
@@ -0,0 +1,96 @@
1<?php
2if(!defined('DOKU_INC')) die();
3if(!defined('DOKU_PLUGIN')) define('DOKU_PLUGIN', DOKU_INC . 'lib/plugins/');
4
5/**
6 * Class action_plugin_googleanalytics
7 */
8class action_plugin_googleanalytics extends DokuWiki_Action_Plugin {
9
10 private $gaEnabled = true;
11
12 /**
13 * Register its handlers with the DokuWiki's event controller
14 *
15 * @param Doku_Event_Handler $controller
16 */
17 function register(Doku_Event_Handler $controller) {
18 $controller->register_hook('DOKUWIKI_STARTED', 'AFTER', $this, 'gaConfig');
19 }
20
21 /**
22 * Initialize the Google Analytics config
23 *
24 * @param Doku_Event $event
25 * @param array $param
26 */
27 public function gaConfig(Doku_Event $event, $param) {
28 global $JSINFO;
29 global $INFO;
30 global $ACT;
31
32 if(!$this->gaEnabled) return;
33 $trackingId = $this->getConf('GAID');
34 if(!$trackingId) return;
35 if($this->getConf('dont_count_admin') && $INFO['isadmin']) return;
36 if($this->getConf('dont_count_users') && $_SERVER['REMOTE_USER']) return;
37 act_clean($ACT);
38
39 $options = array();
40 if($this->getConf('track_users') && $_SERVER['REMOTE_USER']) {
41 $options['userId'] = md5(auth_cookiesalt() . 'googleanalytics' . $_SERVER['REMOTE_USER']);
42 }
43 if($this->getConf('domainName')) {
44 $options['cookieDomain'] = $this->getConf('domainName');
45 $options['legacyCookieDomain'] = $this->getConf('domainName');
46 }
47
48 $JSINFO['ga'] = array(
49 'trackingId' => $trackingId,
50 'anonymizeIp' => (bool) $this->getConf('anonymize'),
51 'action' => $ACT,
52 'trackOutboundLinks' => (bool) $this->getConf('track_links'),
53 'options' => $options,
54 'pageview' => $this->getPageView(),
55 );
56 }
57
58 /**
59 * normalize the pageview
60 *
61 * @return string
62 */
63 protected function getPageView() {
64 global $QUERY;
65 global $ID;
66 global $INPUT;
67 global $ACT;
68
69 // clean up parameters to log
70 $params = $_GET;
71 if(isset($params['do'])) unset($params['do']);
72 if(isset($params['id'])) unset($params['id']);
73
74 // decide on virtual views
75 if($ACT == 'search') {
76 $view = '~search/';
77 $params['q'] = $QUERY;
78 } elseif($ACT == 'admin') {
79 $page = $INPUT->str('page');
80 $view = '~admin';
81 if($page) $view .= '/' . $page;
82 if(isset($params['page'])) unset($params['page']);
83 } else {
84 $view = str_replace(':', '/', $ID); // slashes needed for Content Drilldown
85 }
86
87 // prepend basedir, allows logging multiple dir based animals in one tracker
88 $view = DOKU_REL . $view;
89
90 // append query parameters
91 $query = http_build_query($params, '', '&');
92 if($query) $view .= '?' . $query;
93
94 return $view;
95 }
96}
diff --git a/public_html/lib/plugins/googleanalytics/conf/default.php b/public_html/lib/plugins/googleanalytics/conf/default.php
new file mode 100644
index 00000000..32682a9f
--- /dev/null
+++ b/public_html/lib/plugins/googleanalytics/conf/default.php
@@ -0,0 +1,9 @@
1<?php
2
3$conf['GAID'] = '';
4$conf['dont_count_admin'] = 0;
5$conf['dont_count_users'] = 0;
6$conf['anonymize'] = 1;
7$conf['track_users'] = 0;
8$conf['track_links'] = 0;
9$conf['domainName'] = '';
diff --git a/public_html/lib/plugins/googleanalytics/conf/metadata.php b/public_html/lib/plugins/googleanalytics/conf/metadata.php
new file mode 100644
index 00000000..1a70c430
--- /dev/null
+++ b/public_html/lib/plugins/googleanalytics/conf/metadata.php
@@ -0,0 +1,9 @@
1<?php
2
3$meta['GAID'] = array('string');
4$meta['dont_count_admin'] = array('onoff');
5$meta['dont_count_users'] = array('onoff');
6$meta['anonymize'] = array('onoff');
7$meta['track_users'] = array('onoff');
8$meta['track_links'] = array('onoff');
9$meta['domainName'] = array('string');
diff --git a/public_html/lib/plugins/googleanalytics/lang/en/settings.php b/public_html/lib/plugins/googleanalytics/lang/en/settings.php
new file mode 100644
index 00000000..198ab304
--- /dev/null
+++ b/public_html/lib/plugins/googleanalytics/lang/en/settings.php
@@ -0,0 +1,9 @@
1<?php
2$lang['GAID'] = 'Your Google Analytics ID (UA-XXXXXX-XX)';
3$lang['dont_count_admin'] = 'Don\'t count admin/superuser';
4$lang['dont_count_users'] = 'Don\'t count logged in users';
5$lang['anonymize'] = 'Send anonymized IP addresses to Google.';
6$lang['track_users'] = 'Track logged in users across different devices. Needs <a href="https://support.google.com/analytics/answer/3123662?hl=en">user tracking</a> enabled. Users will not be identifable in Google Analytics!';
7$lang['track_links'] = 'Track outgoing links.';
8$lang['domainName'] = 'Configure the cookie domain. Should usually be left empty';
9
diff --git a/public_html/lib/plugins/googleanalytics/lang/ja/settings.php b/public_html/lib/plugins/googleanalytics/lang/ja/settings.php
new file mode 100644
index 00000000..e9e29fd1
--- /dev/null
+++ b/public_html/lib/plugins/googleanalytics/lang/ja/settings.php
@@ -0,0 +1,4 @@
1<?php
2$lang['GAID'] = 'Google Analitycs ID';
3$lang['dont_count_admin'] = '管理者を解析対象外にする';
4$lang['dont_count_users'] = 'ログインユーザーを解析対象外にする';
diff --git a/public_html/lib/plugins/googleanalytics/lang/pl/settings.php b/public_html/lib/plugins/googleanalytics/lang/pl/settings.php
new file mode 100644
index 00000000..d1211bd2
--- /dev/null
+++ b/public_html/lib/plugins/googleanalytics/lang/pl/settings.php
@@ -0,0 +1,4 @@
1<?php
2$lang['GAID'] = 'Google Analitycs ID';
3$lang['dont_count_admin'] = 'Nie zliczaj: admin/superuser';
4$lang['dont_count_users'] = 'Nie zliczaj zalogownych użytkowników'; \ No newline at end of file
diff --git a/public_html/lib/plugins/googleanalytics/lang/se/settings.php b/public_html/lib/plugins/googleanalytics/lang/se/settings.php
new file mode 100644
index 00000000..5a2b9a1e
--- /dev/null
+++ b/public_html/lib/plugins/googleanalytics/lang/se/settings.php
@@ -0,0 +1,4 @@
1<?php
2$lang['GAID'] = 'Google Analitycs ID';
3$lang['dont_count_admin'] = 'Räkna inte admin/superuser';
4$lang['dont_count_users'] = 'Räkna inte inloggade användare';
diff --git a/public_html/lib/plugins/googleanalytics/lang/zh/settings.php b/public_html/lib/plugins/googleanalytics/lang/zh/settings.php
new file mode 100644
index 00000000..24586bc6
--- /dev/null
+++ b/public_html/lib/plugins/googleanalytics/lang/zh/settings.php
@@ -0,0 +1,4 @@
1<?php
2$lang['GAID'] = 'Google Analitycs 跟踪 ID';
3$lang['dont_count_admin'] = '不统计管理员和超级管理员';
4$lang['dont_count_users'] = '不统计已登录用户';
diff --git a/public_html/lib/plugins/googleanalytics/plugin.info.txt b/public_html/lib/plugins/googleanalytics/plugin.info.txt
new file mode 100644
index 00000000..ab22a9dc
--- /dev/null
+++ b/public_html/lib/plugins/googleanalytics/plugin.info.txt
@@ -0,0 +1,7 @@
1base googleanalytics
2author Terence J. Grant
3email tjgrant@tatewake.com
4date 2017-02-07
5name Google Analytics Plugin
6desc Plugin to embed your Google Analytics code for your site, which allows you to track your visitors.
7url https://www.dokuwiki.org/plugin:googleanalytics
diff --git a/public_html/lib/plugins/googleanalytics/script.js b/public_html/lib/plugins/googleanalytics/script.js
new file mode 100644
index 00000000..0c82406e
--- /dev/null
+++ b/public_html/lib/plugins/googleanalytics/script.js
@@ -0,0 +1,52 @@
1/**
2 * Set up Google analytics
3 *
4 * All configuration is done in the JSINFO.ga object initialized in
5 * action.php
6 */
7if (JSINFO.ga) {
8 /* default google tracking initialization */
9 (function (i, s, o, g, r, a, m) {
10 i['GoogleAnalyticsObject'] = r;
11 //noinspection CommaExpressionJS
12 i[r] = i[r] || function () {
13 (i[r].q = i[r].q || []).push(arguments)
14 }, i[r].l = 1 * new Date();
15 //noinspection CommaExpressionJS
16 a = s.createElement(o),
17 m = s.getElementsByTagName(o)[0];
18 a.async = 1;
19 a.src = g;
20 m.parentNode.insertBefore(a, m)
21 })(window, document, 'script', 'https://www.google-analytics.com/analytics.js', 'ga');
22
23 // initalize and set options
24 ga('create', JSINFO.ga.trackingId, 'auto', JSINFO.ga.options);
25 ga('set', 'forceSSL', true);
26 ga('set', 'anonymizeIp', JSINFO.ga.anonymizeIp);
27
28 // track pageview and action
29 ga('set', 'dimension1', JSINFO.ga.action);
30 ga('set', 'dimension2', JSINFO.ga.id);
31 ga('send', 'pageview', JSINFO.ga.pageview);
32 ga('send', 'event', 'wiki-action', JSINFO.ga.action, JSINFO.id, {
33 nonInteraction: true // this is an automatic event with the page load
34 });
35
36 // track outgoing links, once the document was loaded
37 if (JSINFO.ga.trackOutboundLinks) {
38 jQuery(function () {
39 // https://support.google.com/analytics/answer/1136920?hl=en
40 jQuery('a.urlextern, a.interwiki').click(function (e) {
41 e.preventDefault();
42 var url = this.href;
43 ga('send', 'event', 'outbound', 'click', url, {
44 'transport': 'beacon',
45 'hitCallback': function () {
46 document.location = url;
47 }
48 });
49 });
50 });
51 }
52}