├── .gitignore ├── logo.png ├── index.php ├── version.php ├── lib.php ├── README.txt ├── lang └── en │ └── local_analytics.php ├── ganalytics.php ├── guniversal.php ├── piwik.php └── settings.php /.gitignore: -------------------------------------------------------------------------------- 1 | .idea/ 2 | -------------------------------------------------------------------------------- /logo.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/DBezemer/moodle-local_analytics/master/logo.png -------------------------------------------------------------------------------- /index.php: -------------------------------------------------------------------------------- 1 | . 16 | 17 | /** 18 | * Analytics 19 | * 20 | * This module provides extensive analytics on a platform of choice 21 | * Currently support Google Analytics and Piwik 22 | * 23 | * @package local_analytics 24 | * @copyright David Bezemer , www.davidbezemer.nl 25 | * @author David Bezemer 26 | * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later 27 | */ 28 | 29 | require_once(dirname(__FILE__).'/lib.php'); -------------------------------------------------------------------------------- /version.php: -------------------------------------------------------------------------------- 1 | . 16 | 17 | /** 18 | * Analytics 19 | * 20 | * This module provides extensive analytics on a platform of choice 21 | * Currently support Google Analytics and Piwik 22 | * 23 | * @package local_analytics 24 | * @copyright David Bezemer , www.davidbezemer.nl 25 | * @author David Bezemer 26 | * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later 27 | */ 28 | 29 | $plugin->version = 2014100801; 30 | $plugin->requires = 2011033010; 31 | $plugin->release = '1.5 (Build: 2014100801)'; 32 | $plugin->maturity = MATURITY_STABLE; 33 | $plugin->component = 'local_analytics'; 34 | -------------------------------------------------------------------------------- /lib.php: -------------------------------------------------------------------------------- 1 | . 16 | 17 | /** 18 | * Analytics 19 | * 20 | * This module provides extensive analytics on a platform of choice 21 | * Currently support Google Analytics and Piwik 22 | * 23 | * @package local_analytics 24 | * @copyright David Bezemer , www.davidbezemer.nl 25 | * @author David Bezemer 26 | * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later 27 | */ 28 | 29 | require_once(dirname(dirname(dirname(__FILE__))).'/config.php'); 30 | 31 | $enabled = get_config('local_analytics', 'enabled'); 32 | $analytics = get_config('local_analytics', 'analytics'); 33 | 34 | if ($enabled) { 35 | if ($analytics === "piwik") { 36 | require_once(dirname(__FILE__).'/piwik.php'); 37 | } elseif ($analytics === "ganalytics") { 38 | require_once(dirname(__FILE__).'/ganalytics.php'); 39 | } elseif ($analytics === "guniversal") { 40 | require_once(dirname(__FILE__).'/guniversal.php'); 41 | } 42 | } -------------------------------------------------------------------------------- /README.txt: -------------------------------------------------------------------------------- 1 | moodle-local_analytics 2 | ================== 3 | 4 | A local Moodle Module adding Site Analytics 5 | 6 | The plugin features the following options: 7 | - exclude tracking of admin users 8 | - build full navigation tree for Piwik course category and activity tracking 9 | - image based tracking in case javascript is disabled (for Piwik) 10 | - advanced analytics for Google analytics (based on Bas Brands and Gavin Henricks work in 2013 http://www.somerandomthoughts.com/blog/2012/04/18/ireland-uk-moodlemoot-analytics-to-the-front/) 11 | 12 | Install instructions: 13 | 1. Copy the analytics directory to the local directory of your Moodle instance 14 | 2. Visit the notifications page 15 | 3. Configure the plugin 16 | 17 | Configuration: 18 | The plugin currently supports 3 Analytics modes, Piwik, Google Universal Analytics and Google Legacy Analytics. 19 | 20 | Piwik 21 | - Set the Site ID 22 | - Choose whether you want image fallback tracking 23 | - Enter the URL to your Piwik install excluding http/https and trailing slashes 24 | - Choose whether you want to track admins (not recommended) 25 | - Choose whether you want to send Clean URLs (recommended): 26 | Piwik will aggregrate Page Titles and show a nice waterfall cascade of all sites, including categories and action types 27 | 28 | Google Universal Analytics 29 | - Plugin modifies the page speed sample to have 50% of your visitors samples for page speed instead of 1% making it much more useful 30 | - Set your Google tracking ID 31 | - Choose whether you want to track admins (not recommended) 32 | - Choose whether you want to send Clean URLs (not recommended): 33 | Google analytics will no longer be able to use overlays and linking back to your Moodle site 34 | 35 | Google Legacy Analytics (soon deprecated by Google) 36 | - Plugin modifies the page speed sample to have 50% of your visitors samples for page speed instead of 1% making it much more useful 37 | - Set your Google tracking ID 38 | - Choose whether you want to track admins (not recommended) 39 | - Choose whether you want to send Clean URLs (not recommended): 40 | Google analytics will no longer be able to use overlays and linking back to your Moodle site 41 | 42 | Workings: 43 | The plugin will inject tracking code for Analytics purposes into every page. 44 | If debugging is enabled the URL that is tracked will be displayed on the bottom of the page. 45 | -------------------------------------------------------------------------------- /lang/en/local_analytics.php: -------------------------------------------------------------------------------- 1 | . 16 | 17 | /** 18 | * Piwik Analytics 19 | * 20 | * This module provides extensive analytics, without the privacy concerns 21 | * of using Google Analytics, see install_piwik.txt for installing Piwik 22 | * 23 | * @package local_analytics 24 | * @copyright 2013 David Bezemer, www.davidbezemer.nl 25 | * @author David Bezemer 26 | * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later 27 | */ 28 | 29 | $string['pluginname'] = 'Analytics'; 30 | $string['analytics'] = 'Analytics'; 31 | $string['analyticsdesc'] = 'Choose the type of Analytics you want to insert'; 32 | $string['siteid'] = 'Site ID'; 33 | $string['siteid_desc'] = 'Enter your Site ID or Google Analytics'; 34 | $string['siteurl'] = 'Analytics URL'; 35 | $string['siteurl_desc'] = 'Enter your Piwik Analytics URL without http(s) or a trailing slash (for both Google Analytics types leave empty)'; 36 | $string['enabled'] = 'Enabled'; 37 | $string['enabled_desc'] = 'Enable Analytics for Moodle'; 38 | $string['imagetrack'] = 'Image Tracking'; 39 | $string['cleanurl'] = 'Clean URLs'; 40 | $string['cleanurl_desc'] = 'Generate clean URL for in advanced tracking'; 41 | $string['imagetrack_desc'] = 'Enable Image Tracking for Moodle for browsers with JavaScript disabled (only for Piwik)'; 42 | $string['trackadmin'] = 'Tracking Admins'; 43 | $string['trackadmin_desc'] = 'Enable tracking of Admin users (not recommended)'; 44 | $string['view'] = 'View'; 45 | $string['edit'] = 'Edit'; 46 | $string['piwik'] = 'Piwik'; 47 | $string['ganalytics'] = 'Google Analytics (deprecated)'; 48 | $string['guniversal'] = 'Google Universal Analytics'; 49 | $string['location'] = 'Tracking code location'; 50 | $string['locationdesc'] = 'The place on the page where you want to place the code, header will yield the most reliable results, but footer gives the best performance. If you do not get correct results in Google/Piwik set this to "Header"'; 51 | $string['head'] = 'Header'; 52 | $string['topofbody'] = 'Top of body'; 53 | $string['footer'] = 'Footer'; 54 | -------------------------------------------------------------------------------- /ganalytics.php: -------------------------------------------------------------------------------- 1 | . 16 | 17 | /** 18 | * Analytics 19 | * 20 | * This module provides extensive analytics on a platform of choice 21 | * Currently support Google Analytics and Piwik 22 | * 23 | * @package local_analytics 24 | * @copyright David Bezemer , www.davidbezemer.nl 25 | * @author David Bezemer , Bas Brands , Gavin Henrick 26 | * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later 27 | */ 28 | 29 | function analytics_trackurl() { 30 | global $DB, $PAGE, $COURSE; 31 | $pageinfo = get_context_info_array($PAGE->context->id); 32 | $trackurl = "'/"; 33 | 34 | // Adds course category name. 35 | if (isset($pageinfo[1]->category)) { 36 | if ($category = $DB->get_record('course_categories', array('id'=>$pageinfo[1]->category))) { 37 | $cats=explode("/",$category->path); 38 | foreach (array_filter($cats) as $cat) { 39 | if ($categorydepth = $DB->get_record("course_categories", array("id" => $cat))) {; 40 | $trackurl .= urlencode($categorydepth->name).'/'; 41 | } 42 | } 43 | } 44 | } 45 | 46 | // Adds course full name. 47 | if (isset($pageinfo[1]->fullname)) { 48 | if (isset($pageinfo[2]->name)) { 49 | $trackurl .= urlencode($pageinfo[1]->fullname).'/'; 50 | } else if ($PAGE->user_is_editing()) { 51 | $trackurl .= urlencode($pageinfo[1]->fullname).'/'.get_string('edit', 'local_analytics'); 52 | } else { 53 | $trackurl .= urlencode($pageinfo[1]->fullname).'/'.get_string('view', 'local_analytics'); 54 | } 55 | } 56 | 57 | // Adds activity name. 58 | if (isset($pageinfo[2]->name)) { 59 | $trackurl .= urlencode($pageinfo[2]->modname).'/'.urlencode($pageinfo[2]->name); 60 | } 61 | 62 | $trackurl .= "'"; 63 | return $trackurl; 64 | } 65 | 66 | function insert_analytics_tracking() { 67 | global $CFG; 68 | $enabled = get_config('local_analytics', 'enabled'); 69 | $siteid = get_config('local_analytics', 'siteid'); 70 | $trackadmin = get_config('local_analytics', 'trackadmin'); 71 | $cleanurl = get_config('local_analytics', 'cleanurl'); 72 | $location = "additionalhtml".get_config('local_analytics', 'location'); 73 | 74 | if ($enabled && (!is_siteadmin() || $trackadmin)) { 75 | $CFG->$location .= " 76 | 89 | "; 90 | } 91 | 92 | } 93 | 94 | insert_analytics_tracking(); 95 | 96 | if (debugging() && ($CFG->debugdisplay)) { 97 | $CFG->additionalhtmlfooter .= "Tracking: ".analytics_trackurl().""; 98 | } -------------------------------------------------------------------------------- /guniversal.php: -------------------------------------------------------------------------------- 1 | . 16 | 17 | /** 18 | * Analytics 19 | * 20 | * This module provides extensive analytics on a platform of choice 21 | * Currently support Google Analytics and Piwik 22 | * 23 | * @package local_analytics 24 | * @copyright David Bezemer , www.davidbezemer.nl 25 | * @author David Bezemer , Bas Brands , Gavin Henrick 26 | * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later 27 | */ 28 | 29 | function analytics_trackurl() { 30 | global $DB, $PAGE, $COURSE; 31 | $pageinfo = get_context_info_array($PAGE->context->id); 32 | $trackurl = "'/"; 33 | 34 | // Adds course category name. 35 | if (isset($pageinfo[1]->category)) { 36 | if ($category = $DB->get_record('course_categories', array('id'=>$pageinfo[1]->category))) { 37 | $cats=explode("/",$category->path); 38 | foreach(array_filter($cats) as $cat) { 39 | if ($categorydepth = $DB->get_record("course_categories", array("id" => $cat))) {; 40 | $trackurl .= urlencode($categorydepth->name).'/'; 41 | } 42 | } 43 | } 44 | } 45 | 46 | // Adds course full name. 47 | if (isset($pageinfo[1]->fullname)) { 48 | if (isset($pageinfo[2]->name)) { 49 | $trackurl .= urlencode($pageinfo[1]->fullname).'/'; 50 | } else if ($PAGE->user_is_editing()) { 51 | $trackurl .= urlencode($pageinfo[1]->fullname).'/'.get_string('edit', 'local_analytics'); 52 | } else { 53 | $trackurl .= urlencode($pageinfo[1]->fullname).'/'.get_string('view', 'local_analytics'); 54 | } 55 | } 56 | 57 | // Adds activity name. 58 | if (isset($pageinfo[2]->name)) { 59 | $trackurl .= urlencode($pageinfo[2]->modname).'/'.urlencode($pageinfo[2]->name); 60 | } 61 | 62 | $trackurl .= "'"; 63 | return $trackurl; 64 | } 65 | 66 | function insert_analytics_tracking() { 67 | global $CFG,$PAGE; 68 | $enabled = get_config('local_analytics', 'enabled'); 69 | $siteid = get_config('local_analytics', 'siteid'); 70 | $trackadmin = get_config('local_analytics', 'trackadmin'); 71 | $cleanurl = get_config('local_analytics', 'cleanurl'); 72 | $location = "additionalhtml".get_config('local_analytics', 'location'); 73 | 74 | if ($cleanurl) { 75 | $addition = 76 | "{'hitType' : 'pageview', 77 | 'page' : ".analytics_trackurl().", 78 | 'title' : '".addslashes($PAGE->heading)."' 79 | }"; 80 | } else { 81 | $addition = "'pageview'"; 82 | } 83 | 84 | 85 | if ($enabled && (!is_siteadmin() || $trackadmin)) { 86 | $CFG->$location .= " 87 | 96 | "; 97 | } 98 | } 99 | 100 | insert_analytics_tracking(); 101 | 102 | if (debugging() && ($CFG->debugdisplay)) { 103 | $CFG->additionalhtmlfooter .= "Tracking: ".analytics_trackurl().""; 104 | } -------------------------------------------------------------------------------- /piwik.php: -------------------------------------------------------------------------------- 1 | . 16 | 17 | /** 18 | * Analytics 19 | * 20 | * This module provides extensive analytics on a platform of choice 21 | * Currently support Google Analytics and Piwik 22 | * 23 | * @package local_analytics 24 | * @copyright David Bezemer , www.davidbezemer.nl 25 | * @author David Bezemer , Bas Brands , Gavin Henrick 26 | * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later 27 | */ 28 | 29 | function analytics_trackurl() { 30 | global $DB, $PAGE, $COURSE, $SITE; 31 | $pageinfo = get_context_info_array($PAGE->context->id); 32 | $trackurl = "'"; 33 | 34 | // Adds course category name. 35 | if (isset($pageinfo[1]->category)) { 36 | if ($category = $DB->get_record('course_categories', array('id'=>$pageinfo[1]->category))) { 37 | $cats=explode("/",$category->path); 38 | foreach(array_filter($cats) as $cat) { 39 | if ($categorydepth = $DB->get_record("course_categories", array("id" => $cat))) {; 40 | $trackurl .= $categorydepth->name.'/'; 41 | } 42 | } 43 | } 44 | } 45 | 46 | // Adds course full name. 47 | if (isset($pageinfo[1]->fullname)) { 48 | if (isset($pageinfo[2]->name)) { 49 | $trackurl .= $pageinfo[1]->fullname.'/'; 50 | } else if ($PAGE->user_is_editing()) { 51 | $trackurl .= $pageinfo[1]->fullname.'/'.get_string('edit', 'local_analytics'); 52 | } else { 53 | $trackurl .= $pageinfo[1]->fullname.'/'.get_string('view', 'local_analytics'); 54 | } 55 | } 56 | 57 | // Adds activity name. 58 | if (isset($pageinfo[2]->name)) { 59 | $trackurl .= $pageinfo[2]->modname.'/'.$pageinfo[2]->name; 60 | } 61 | 62 | $trackurl .= "'"; 63 | return $trackurl; 64 | } 65 | 66 | function insert_analytics_tracking() { 67 | global $CFG; 68 | $enabled = get_config('local_analytics', 'enabled'); 69 | $imagetrack = get_config('local_analytics', 'imagetrack'); 70 | $siteurl = get_config('local_analytics', 'siteurl'); 71 | $siteid = get_config('local_analytics', 'siteid'); 72 | $trackadmin = get_config('local_analytics', 'trackadmin'); 73 | $cleanurl = get_config('local_analytics', 'cleanurl'); 74 | $location = "additionalhtml".get_config('local_analytics', 'location'); 75 | 76 | if (!empty($siteurl)) { 77 | if ($imagetrack) { 78 | $addition = ''; 79 | } else { 80 | $addition = ''; 81 | } 82 | 83 | if ($cleanurl) { 84 | $doctitle = "_paq.push(['setDocumentTitle', ".analytics_trackurl()."]);"; 85 | } else { 86 | $doctitle = ""; 87 | } 88 | 89 | if ($enabled && (!is_siteadmin() || $trackadmin)) { 90 | $CFG->$location .= " 91 | 92 | ".$addition. 105 | ""; 106 | } 107 | } 108 | } 109 | 110 | insert_analytics_tracking(); 111 | 112 | if (debugging() && ($CFG->debugdisplay)) { 113 | $CFG->additionalhtmlfooter .= "Tracking: ".analytics_trackurl().""; 114 | } -------------------------------------------------------------------------------- /settings.php: -------------------------------------------------------------------------------- 1 | . 16 | 17 | /** 18 | * Analytics 19 | * 20 | * This module provides extensive analytics on a platform of choice 21 | * Currently support Google Analytics and Piwik 22 | * 23 | * @package local_analytics 24 | * @copyright David Bezemer , www.davidbezemer.nl 25 | * @author David Bezemer 26 | * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later 27 | */ 28 | 29 | defined('MOODLE_INTERNAL') || die; 30 | 31 | if (is_siteadmin()) { 32 | $settings = new admin_settingpage('local_analytics', get_string('pluginname', 'local_analytics')); 33 | $ADMIN->add('localplugins', $settings); 34 | 35 | $name = 'local_analytics/enabled'; 36 | $title = get_string('enabled', 'local_analytics'); 37 | $description = get_string('enabled_desc', 'local_analytics'); 38 | $default = true; 39 | $setting = new admin_setting_configcheckbox($name, $title, $description, $default, true, false); 40 | $settings->add($setting); 41 | 42 | $name = 'local_analytics/analytics'; 43 | $title = get_string('analytics' , 'local_analytics'); 44 | $description = get_string('analyticsdesc', 'local_analytics'); 45 | $ganalytics = get_string('ganalytics', 'local_analytics'); 46 | $guniversal = get_string('guniversal', 'local_analytics'); 47 | $piwik = get_string('piwik', 'local_analytics'); 48 | $default = 'piwik'; 49 | $choices = array( 50 | 'piwik' => $piwik, 51 | 'ganalytics' => $ganalytics, 52 | 'guniversal' => $guniversal, 53 | ); 54 | $setting = new admin_setting_configselect($name, $title, $description, $default, $choices); 55 | $settings->add($setting); 56 | 57 | $name = 'local_analytics/siteid'; 58 | $title = get_string('siteid', 'local_analytics'); 59 | $description = get_string('siteid_desc', 'local_analytics'); 60 | $default = '1'; 61 | $setting = new admin_setting_configtext($name, $title, $description, $default); 62 | $settings->add($setting); 63 | 64 | $name = 'local_analytics/imagetrack'; 65 | $title = get_string('imagetrack', 'local_analytics'); 66 | $description = get_string('imagetrack_desc', 'local_analytics'); 67 | $default = true; 68 | $setting = new admin_setting_configcheckbox($name, $title, $description, $default, true, false); 69 | $settings->add($setting); 70 | 71 | $name = 'local_analytics/siteurl'; 72 | $title = get_string('siteurl', 'local_analytics'); 73 | $description = get_string('siteurl_desc', 'local_analytics'); 74 | $default = ''; 75 | $setting = new admin_setting_configtext($name, $title, $description, $default); 76 | $settings->add($setting); 77 | 78 | $name = 'local_analytics/trackadmin'; 79 | $title = get_string('trackadmin', 'local_analytics'); 80 | $description = get_string('trackadmin_desc', 'local_analytics'); 81 | $default = false; 82 | $setting = new admin_setting_configcheckbox($name, $title, $description, $default, true, false); 83 | $settings->add($setting); 84 | 85 | $name = 'local_analytics/cleanurl'; 86 | $title = get_string('cleanurl', 'local_analytics'); 87 | $description = get_string('cleanurl_desc', 'local_analytics'); 88 | $default = true; 89 | $setting = new admin_setting_configcheckbox($name, $title, $description, $default, true, false); 90 | $settings->add($setting); 91 | 92 | $name = 'local_analytics/location'; 93 | $title = get_string('location' , 'local_analytics'); 94 | $description = get_string('locationdesc', 'local_analytics'); 95 | $head = get_string('head', 'local_analytics'); 96 | $topofbody = get_string('topofbody', 'local_analytics'); 97 | $footer = get_string('footer', 'local_analytics'); 98 | $default = 'head'; 99 | $choices = array( 100 | 'head' => $head, 101 | 'topofbody' => $topofbody, 102 | 'footer' => $footer, 103 | ); 104 | $setting = new admin_setting_configselect($name, $title, $description, $default, $choices); 105 | $settings->add($setting); 106 | } --------------------------------------------------------------------------------