forked from DBezemer/moodle-local_analytics
-
Notifications
You must be signed in to change notification settings - Fork 0
/
guniversal.php
103 lines (90 loc) · 3.65 KB
/
guniversal.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
<?php
// This file is part of Moodle - http://moodle.org/
//
// Moodle 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, either version 3 of the License, or
// (at your option) any later version.
//
// Moodle 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.
//
// You should have received a copy of the GNU General Public License
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
/**
* Piwik Analytics
*
* This module provides extensive analytics, without the privacy concerns
* of using Google Analytics, see install_piwik.txt for installing Piwik
*
* @package local_analytics
* @copyright 2013 David Bezemer, www.davidbezemer.nl
* @author David Bezemer
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
function analytics_trackurl() {
global $DB, $PAGE, $COURSE;
$pageinfo = get_context_info_array($PAGE->context->id);
$trackurl = "'/";
// Adds course category name.
if (isset($pageinfo[1]->category)) {
if ($category = $DB->get_record('course_categories', array('id'=>$pageinfo[1]->category))) {
$cats=explode("/",$category->path);
foreach ($cats as $cat) {
if ($categorydepth = $DB->get_record("course_categories", array("id" => $cat))) {;
$trackurl .= urlencode($categorydepth->name).'/';
}
}
}
}
// Adds course full name.
if (isset($pageinfo[1]->fullname)) {
if (isset($pageinfo[2]->name)) {
$trackurl .= urlencode($pageinfo[1]->fullname).'/';
} else if ($PAGE->user_is_editing()) {
$trackurl .= urlencode($pageinfo[1]->fullname).'/'.get_string('edit', 'local_analytics');
} else {
$trackurl .= urlencode($pageinfo[1]->fullname).'/'.get_string('view', 'local_analytics');
}
}
// Adds activity name.
if (isset($pageinfo[2]->name)) {
$trackurl .= urlencode($pageinfo[2]->modname).'/'.urlencode($pageinfo[2]->name);
}
$trackurl .= "'";
return $trackurl;
}
function insert_analytics_tracking() {
global $CFG,$PAGE;
$enabled = get_config('local_analytics', 'enabled');
$siteid = get_config('local_analytics', 'siteid');
$trackadmin = get_config('local_analytics', 'trackadmin');
$cleanurl = get_config('local_analytics', 'cleanurl');
if ($cleanurl) {
$addition =
"{'hitType' : 'pageview',
'page' : ".analytics_trackurl().",
'title' : '".addslashes($PAGE->heading)."'
}";
} else {
$addition = "'pageview'";
}
if ($enabled && (!is_siteadmin() || $trackadmin)) {
$CFG->additionalhtmlhead .= "
<script>
(function(i,s,o,g,r,a,m){i['GoogleAnalyticsObject']=r;i[r]=i[r]||function(){
(i[r].q=i[r].q||[]).push(arguments)},i[r].l=1*new Date();a=s.createElement(o),
m=s.getElementsByTagName(o)[0];a.async=1;a.src=g;m.parentNode.insertBefore(a,m)
})(window,document,'script','//www.google-analytics.com/analytics.js','ga');
ga('create', '".$siteid."', {'siteSpeedSampleRate': 50});
ga('send', ".$addition.");
</script>
";
}
}
insert_analytics_tracking();
if (debugging() && ($CFG->debugdisplay)) {
$CFG->additionalhtmlfooter .= "<span class='badge badge-success'>Tracking: ".analytics_trackurl()."</span>";
}