Using Piwik with Moodle

Using Piwik with Moodle

by Tomasz Woźniczka -
Number of replies: 9

I 've arledy installed Piwik with my Moodle. Standard analytics tools works fine but I have to make stats for custom variables, eg. users institutions, domain names, cities.

I think I need to pirnt this variables in javascript  and later read them with piwik but I have no idea how mixed

Can you help me?

Average of ratings: -
In reply to Tomasz Woźniczka

Re: Using Piwik with Moodle

by Danny Wahl -

you could use the "extra footer html" admin setting to throw in a javascript tag and define your variables - that's how we call/embed piwik.  I don't see why you couldn't throw your vars in there too.

In reply to Tomasz Woźniczka

Re: Using Piwik with Moodle

by Alex Walker -

I already use Piwik's custom variables in Moodle, to track the most popular courses and the most active usernames.

I did this by editing my theme's files, rather than by using the 'extra html' interface. This works for us, because we force our users to use one theme (plus a few accessible variations).

Here's an example. We're storing the username in custom variable 1, or 'Not logged in' if they're not logged in. We're storing the course the user is currently looking at in custom variable 2.

<script type="text/javascript">
Your script that actually includes Piwik will be here.
</script><script type="text/javascript">
try {
var piwikTracker = Piwik.getTracker(pkBaseURL + "piwik.php", 1);
piwikTracker.disableCookies();
<?php if(isset($USER->username)) { ?>
piwikTracker.setCustomVariable(1,"Username","<?php echo $USER->username; ?>", "visit");
<?php } else { ?>
piwikTracker.setCustomVariable(1,"Username","Not logged in", "visit");
<?php } ?>
<?php if(isset($COURSE->fullname)) { ?>
piwikTracker.setCustomVariable(2,"Course","<?php echo $COURSE->fullname . ' (' . $COURSE->shortname . ')'; ?>", "page");
<?php } ?>
piwikTracker.trackPageView();
piwikTracker.enableLinkTracking();
} catch( err ) {}
</script>

You'll probably want to put this just before the </body> in your theme layout files, in /theme/theme_name/layout. Pro tip: we have the same footer on all our pages. We have a file called footer-content.php in that folder, then just do include("footer-content.php"); in the right place in each of our /layout files.

The currently logged-in user's settings are stored in a PHP object called $USER. If you want to see what you can grab from that object, you could run var_dump($USER); somewhere subtle.

Average of ratings: Useful (1)
In reply to Alex Walker

Odp: Re: Using Piwik with Moodle

by Tomasz Woźniczka -

Thanks for your reply Alex. I've added few more variables and it works great smile 

In reply to Alex Walker

Re: Using Piwik with Moodle

by Björn Fisseler -

Current versions of Piwik use asynchronous JavaScript, so that the above code is not working anymore. The current solution looks like this:

<script type="text/javascript">
<?php if(isset($USER->username)) { ?>
    _paq.push(['setCustomVariable','1','Username','<?php echo $USER->username; ?>','visit']);
<?php } else { ?>
    _paq.push(['setCustomVariable','1','Username','Not logged in','visit']);
<?php } ?>

<?php if(isset($COURSE->id)) { ?>
    _paq.push(['setCustomVariable','2','Course-ID','<?php echo $COURSE->id; ?>','page']);
<?php } ?>
    _paq.push(['trackPageView']);
    _paq.push(['enableLinkTracking']);
</script>
Average of ratings: Useful (1)
In reply to Björn Fisseler

Re: Using Piwik with Moodle

by Soizic POILVE -

Hello,

i hope this old conversation can be re-activated with my query smile

Could you please indicate exactly in which file I can test this script? I'm new to Moodle and would appreciate to build PIwik report (already installed by my predecessor) to set nice customized features to help monitoring and improve our learning activities.

Thanks in advance

Soizic

In reply to Soizic POILVE

Re: Using Piwik with Moodle

by Marcus Green -
Picture of Core developers Picture of Particularly helpful Moodlers Picture of Plugin developers Picture of Testers

There is a plugin that may help format your Piwik information

https://moodle.org/plugins/pluginversions.php?plugin=local_analytics

In reply to Marcus Green

Re: Using Piwik with Moodle

by Soizic POILVE -

Dear Marcus,


thank you for your feedback. The thing is I already have Piwik installed, so could you please confirm this is a plugin that format data extracted from Moodle by Piwik?


Sorry I'm a bit new to this so I would like to start properly smile

Thanks for your comprehension

Have a nice day

Soizic

In reply to Soizic POILVE

Re: Using Piwik with Moodle

by Marcus Green -
Picture of Core developers Picture of Particularly helpful Moodlers Picture of Plugin developers Picture of Testers

I too have Piwik installed and it is giving me data about my visitors.  When I say I have Piwik installed I mean the software that allows tracking of any web activity provided it has the correct javascript code installed.

I don't have the Moodle Piwik plugin installed, but I understand that it allows better formatting of the data that goes into Piwik.  Lewis Carr has written some stuff about some benefits of using Piwik with Moodle.

http://lewiscarr.co.uk/2013/08/10-reasons-piwik-moodle/


In reply to Marcus Green

Re: Using Piwik with Moodle

by Soizic POILVE -

Thank you for your reply.

I'll see if I can try this plugin but I would have liked also to know how to modify code as indicated ealier in this post...