Analytics: Add GA so we can get a view of what's popular on the site

This commit is contained in:
Andy Williams 2017-12-07 14:45:11 +00:00
parent 941594a1e4
commit 1a04218393
11 changed files with 226 additions and 0 deletions

View File

@ -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 <tjgrant@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

View File

@ -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;
}
}

View File

@ -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'] = '';

View File

@ -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');

View File

@ -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';

View File

@ -0,0 +1,4 @@
<?php
$lang['GAID'] = 'Google Analitycs ID';
$lang['dont_count_admin'] = '管理者を解析対象外にする';
$lang['dont_count_users'] = 'ログインユーザーを解析対象外にする';

View File

@ -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';

View File

@ -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';

View File

@ -0,0 +1,4 @@
<?php
$lang['GAID'] = 'Google Analitycs 跟踪 ID';
$lang['dont_count_admin'] = '不统计管理员和超级管理员';
$lang['dont_count_users'] = '不统计已登录用户';

View File

@ -0,0 +1,7 @@
base googleanalytics
author Terence J. Grant
email tjgrant@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

View File

@ -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;
}
});
});
});
}
}