ajwillia-ms pushed a commit to branch master. http://git.enlightenment.org/website/www.git/commit/?id=1a0421839374f00ded5fe2e5604f7e9065a164b7
commit 1a0421839374f00ded5fe2e5604f7e9065a164b7 Author: Andy Williams <a...@andywilliams.me> Date: Thu Dec 7 14:45:11 2017 +0000 Analytics: Add GA so we can get a view of what's popular on the site --- public_html/lib/plugins/googleanalytics/README | 28 +++++++ public_html/lib/plugins/googleanalytics/action.php | 96 ++++++++++++++++++++++ .../lib/plugins/googleanalytics/conf/default.php | 9 ++ .../lib/plugins/googleanalytics/conf/metadata.php | 9 ++ .../plugins/googleanalytics/lang/en/settings.php | 9 ++ .../plugins/googleanalytics/lang/ja/settings.php | 4 + .../plugins/googleanalytics/lang/pl/settings.php | 4 + .../plugins/googleanalytics/lang/se/settings.php | 4 + .../plugins/googleanalytics/lang/zh/settings.php | 4 + .../lib/plugins/googleanalytics/plugin.info.txt | 7 ++ public_html/lib/plugins/googleanalytics/script.js | 52 ++++++++++++ 11 files changed, 226 insertions(+) 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 @@ +googleanalytics Plugin for DokuWiki + +Plugin to embed your Google Analytics code for your site, which +allows you to track your visitors. + +All documentation for this plugin can be found at +https://www.dokuwiki.org/plugin:googleanalytics + +If you install this plugin manually, make sure it is installed in +lib/plugins/googleanalytics/ - if the folder is called different it +will not work! + +Please refer to http://www.dokuwiki.org/plugins for additional info +on how to install plugins in DokuWiki. + +---- +Copyright (C) Terence J. Grant <tjgr...@tatewake.com> et al. + +This program is free software; you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation; version 2 of the License + +This program is distributed in the hope that it will be useful, +but WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +GNU General Public License for more details. + +See 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 @@ +<?php +if(!defined('DOKU_INC')) die(); +if(!defined('DOKU_PLUGIN')) define('DOKU_PLUGIN', DOKU_INC . 'lib/plugins/'); + +/** + * Class action_plugin_googleanalytics + */ +class action_plugin_googleanalytics extends DokuWiki_Action_Plugin { + + private $gaEnabled = true; + + /** + * Register its handlers with the DokuWiki's event controller + * + * @param Doku_Event_Handler $controller + */ + function register(Doku_Event_Handler $controller) { + $controller->register_hook('DOKUWIKI_STARTED', 'AFTER', $this, 'gaConfig'); + } + + /** + * Initialize the Google Analytics config + * + * @param Doku_Event $event + * @param array $param + */ + public function gaConfig(Doku_Event $event, $param) { + global $JSINFO; + global $INFO; + global $ACT; + + if(!$this->gaEnabled) return; + $trackingId = $this->getConf('GAID'); + if(!$trackingId) return; + if($this->getConf('dont_count_admin') && $INFO['isadmin']) return; + if($this->getConf('dont_count_users') && $_SERVER['REMOTE_USER']) return; + act_clean($ACT); + + $options = array(); + if($this->getConf('track_users') && $_SERVER['REMOTE_USER']) { + $options['userId'] = md5(auth_cookiesalt() . 'googleanalytics' . $_SERVER['REMOTE_USER']); + } + if($this->getConf('domainName')) { + $options['cookieDomain'] = $this->getConf('domainName'); + $options['legacyCookieDomain'] = $this->getConf('domainName'); + } + + $JSINFO['ga'] = array( + 'trackingId' => $trackingId, + 'anonymizeIp' => (bool) $this->getConf('anonymize'), + 'action' => $ACT, + 'trackOutboundLinks' => (bool) $this->getConf('track_links'), + 'options' => $options, + 'pageview' => $this->getPageView(), + ); + } + + /** + * normalize the pageview + * + * @return string + */ + protected function getPageView() { + global $QUERY; + global $ID; + global $INPUT; + global $ACT; + + // clean up parameters to log + $params = $_GET; + if(isset($params['do'])) unset($params['do']); + if(isset($params['id'])) unset($params['id']); + + // decide on virtual views + if($ACT == 'search') { + $view = '~search/'; + $params['q'] = $QUERY; + } elseif($ACT == 'admin') { + $page = $INPUT->str('page'); + $view = '~admin'; + if($page) $view .= '/' . $page; + if(isset($params['page'])) unset($params['page']); + } else { + $view = str_replace(':', '/', $ID); // slashes needed for Content Drilldown + } + + // prepend basedir, allows logging multiple dir based animals in one tracker + $view = DOKU_REL . $view; + + // append query parameters + $query = http_build_query($params, '', '&'); + if($query) $view .= '?' . $query; + + return $view; + } +} 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 @@ +<?php + +$conf['GAID'] = ''; +$conf['dont_count_admin'] = 0; +$conf['dont_count_users'] = 0; +$conf['anonymize'] = 1; +$conf['track_users'] = 0; +$conf['track_links'] = 0; +$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 @@ +<?php + +$meta['GAID'] = array('string'); +$meta['dont_count_admin'] = array('onoff'); +$meta['dont_count_users'] = array('onoff'); +$meta['anonymize'] = array('onoff'); +$meta['track_users'] = array('onoff'); +$meta['track_links'] = array('onoff'); +$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 @@ +<?php +$lang['GAID'] = 'Your Google Analytics ID (UA-XXXXXX-XX)'; +$lang['dont_count_admin'] = 'Don\'t count admin/superuser'; +$lang['dont_count_users'] = 'Don\'t count logged in users'; +$lang['anonymize'] = 'Send anonymized IP addresses to Google.'; +$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!'; +$lang['track_links'] = 'Track outgoing links.'; +$lang['domainName'] = 'Configure the cookie domain. Should usually be left empty'; + 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 @@ +<?php +$lang['GAID'] = 'Google Analitycs ID'; +$lang['dont_count_admin'] = '管理者を解析対象外にする'; +$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 @@ +<?php +$lang['GAID'] = 'Google Analitycs ID'; +$lang['dont_count_admin'] = 'Nie zliczaj: admin/superuser'; +$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 @@ +<?php +$lang['GAID'] = 'Google Analitycs ID'; +$lang['dont_count_admin'] = 'Räkna inte admin/superuser'; +$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 @@ +<?php +$lang['GAID'] = 'Google Analitycs 跟踪 ID'; +$lang['dont_count_admin'] = '不统计管理员和超级管理员'; +$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 @@ +base googleanalytics +author Terence J. Grant +email tjgr...@tatewake.com +date 2017-02-07 +name Google Analytics Plugin +desc Plugin to embed your Google Analytics code for your site, which allows you to track your visitors. +url 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 @@ +/** + * Set up Google analytics + * + * All configuration is done in the JSINFO.ga object initialized in + * action.php + */ +if (JSINFO.ga) { + /* default google tracking initialization */ + (function (i, s, o, g, r, a, m) { + i['GoogleAnalyticsObject'] = r; + //noinspection CommaExpressionJS + i[r] = i[r] || function () { + (i[r].q = i[r].q || []).push(arguments) + }, i[r].l = 1 * new Date(); + //noinspection CommaExpressionJS + a = s.createElement(o), + m = s.getElementsByTagName(o)[0]; + a.async = 1; + a.src = g; + m.parentNode.insertBefore(a, m) + })(window, document, 'script', 'https://www.google-analytics.com/analytics.js', 'ga'); + + // initalize and set options + ga('create', JSINFO.ga.trackingId, 'auto', JSINFO.ga.options); + ga('set', 'forceSSL', true); + ga('set', 'anonymizeIp', JSINFO.ga.anonymizeIp); + + // track pageview and action + ga('set', 'dimension1', JSINFO.ga.action); + ga('set', 'dimension2', JSINFO.ga.id); + ga('send', 'pageview', JSINFO.ga.pageview); + ga('send', 'event', 'wiki-action', JSINFO.ga.action, JSINFO.id, { + nonInteraction: true // this is an automatic event with the page load + }); + + // track outgoing links, once the document was loaded + if (JSINFO.ga.trackOutboundLinks) { + jQuery(function () { + // https://support.google.com/analytics/answer/1136920?hl=en + jQuery('a.urlextern, a.interwiki').click(function (e) { + e.preventDefault(); + var url = this.href; + ga('send', 'event', 'outbound', 'click', url, { + 'transport': 'beacon', + 'hitCallback': function () { + document.location = url; + } + }); + }); + }); + } +} --