When we started with Moodle at City back in 2009 we wanted to use Google Analytics (GA) to augment the stats that Moodle collects itself. Partly this is because user stats are a bit hard to get out of Moodle in a flexible and visually attractive way. True, you could deploy custom queries to the back-end database but we wanted to leverage the power of GA. Trouble is, GA uses as its base unit the URL, and Moodle’s URLs are not very informative.

We want to be able to look at stats for different type of activities and user role (e.g. student vs teacher), and to be able to differentiate between schools (and even departments). This just isn’t possible with the standard moodle/mod/{mod-name}/{action}.php?id={id} format of Moodle URLs – all this gives you is the ability to breakdown stats by activity type alone.

What we’re aiming for then is to send to GA some kind of pseudo-URL that contains the information we need to be able to analyse. A model for this pseudo-URL might look something like this:


However an example URL from Moodle might look like this:


this tells GA nothing about the page other than it is a Turnitin assignment.

Using custom php in the Moodle theme header.html we can construct our pseudo-URL and send this to Google instead of the actual URL.

The URL we generate will look something like this:


so now we can interrogate our analytics for school*, role, module, activity and action, down to an individual activity if we want but usually aggregated to some degree. There is also scope to extend this, for example, the Turnitin tool uses another request variable &do (values such as “intro” & “submissions”) which could tell GA a bit more about what is actually happening if it was included in the pseudo-URL. Other plugins and activities have other similarly useful request variables in their URLs.

[*Where do we get school from? Our user table grabs school and department codes from our user database on login via LDAP so it is available in the global $USER]

I won’t show the code we used to create the pseudo-URL (if anyone wants that see this post) but the adapted GA script from the header.html <head> section  is here **:

  $pseudoURL = "blah"; // insert code here to build pseudo-URL
  $gaAccount = "UA-xxxxxxxx-1"; // enter your GA account code here
// Google Analytics code as a function instead of inline
  function gaSSDSLoad (acct,pseudo_url) {
    var gaJsHost = (("https:" == document.location.protocol) ? "https://ssl." :
    var s,pageTracker;
    s = document.createElement('script');
    s.src = gaJsHost + 'google-analytics.com/ga.js';
    s.type = 'text/javascript';
    s.onloadDone = false;
    function init () {
      try { pageTracker = _gat._getTracker(acct); } catch (err) {}
  s.onload = function () {
    s.onloadDone = true;
  s.onreadystatechange = function() {
    if (('loaded' === s.readyState || 'complete' === s.readyState) &&
!s.onloadDone) {
      s.onloadDone = true;

// now run script using GA account # and pseudo_url
// fix for IE8
if (window.addEventListener) {
  /* W3C method. */
  window.addEventListener('load', function(){gaSSDSLoad ("<? echo $gaAccount; ?>","<? echo $pseudoUrl; ?>") }, false);
} else if (window.attachEvent) {
  /* IE method. */
  window.attachEvent('onload', function(){gaSSDSLoad ("<? echo $gaAccount; ?>","<? echo $pseudoUrl; ?>") });
} else {
  /* Old school method. */
  window.onload = function() {gaSSDSLoad ("<? echo $gaAccount; ?>","<? echo $pseudoUrl; ?>"); };

[**Not sure who to credit this to, it wasn’t me, someone in the webteam came up with this before I got here.]

In a future post I’ll talk about how we use Google Analytics and Moodle’s own stats here at City.