Matomo (Piwik) and SharePoint – Tracking on Sites with Minimal Download Strategy or Search

Using Matomo ‏(Piwik) with SharePoint is easy. Tracking the search can be challenging. In this post I describe a way on how to track your SharePoint search properly.

Using Matomo (Piwik) with SharePoint can be very straight forward. There is no need to install any extension for SharePoint or any .wsp solution. All you need is Piwik installation and some lines of JavaScript in a file and a bit of PnP PowerShell.

Piwik offers you a default JavaScript snippet that allows you to track a user on your web site. The default code will somehow look like this:


var _paq = _paq || [];
/* tracker methods like "setCustomDimension" should be called before "trackPageView" */
_paq.push(['enableLinkTracking']);
_paq.push(['trackPageView']);
(function () {
	var u = "//piwikserver.domain.de/";
	_paq.push(['setTrackerUrl', u + 'piwik.php']);
	_paq.push(['setSiteId', '1']);
	var d = document, g = d.createElement('script'), s = d.getElementsByTagName('script')[0];
	g.type = 'text/javascript';
	g.async = true;
	g.defer = true;
	g.src = u + 'piwik.js';
	s.parentNode.insertBefore(g, s);
})();<span id="mce_SELREST_start" style="overflow:hidden;line-height:0;">&#65279;</span>

As soon as you save this JavaScript code in file, you can get started with SharePoint tracking with the following steps:

  1. Upload this file to your SharePoint Root Site Collection and place it in the Style Library (Why? Regularly I use this lib as a on-premises CDN)
  2. Make sure the file is accessible
  3. Create a Script Link in the Site Collection you want to track.

For step 3 You can use the following PowerShell code to add a script link to your site collection:


Connect-PnPOnline -CurrentCredentials -Url https://mywebapp.domain.de
Add-PnPJavaScriptLink -Name SpPiwik -Url "/Style Library/SpPiwik.js" -Sequence 900 -Scope Site

With this setting you can track a user and gain basic insights about his flow through your website. The data you are gaining by now is very basic and there are some SharePoint only specialties:

Minimal Download Strategy (MDS)

In cases that you want to track a SharePoint Site with Minimal Download Strategy enabled, you will need to register your script with MDS. This means you will have change the anonymous code to something with a defined entry point.

 

Enterprise Search Center

Sometimes it is fun and very interesting to gain deeper insights about the users search behavior.

What’s the problem with Piwik and the SharePoint Search: In case you update your SharePoint Search keyword, Piwik yon’t be able to log information about your new search Keyword.

The default URL for a SharePoint result site looks like that: …/results.apsx?k=YourKeyWord

When you change YourKeyWord to YourNewKeyWord, your URL will look like this: ../results.aspx?k=YourKeyWord#k=YourNewKeyWord

Piwik will not notice this event and even if you press refresh, it will get the wrong key word (YourKeyWord and not YourNewKeyWord). To overcome this issue you will need to extend the prior script to:

  1. Start tracking when the hash changes
  2. Call a other Piwik tracking method to provide additional information about the search key word, and maybe the search result source used.

This script could look like this:

This script makes usage of the trackSiteSearch function, that allows you to provide additional insights about the search key word, the category and the number of results. From my experience there is an issue with the timing, when the count of results will be refreshed. So in this case false looks like the best option.

One thought on “Matomo (Piwik) and SharePoint – Tracking on Sites with Minimal Download Strategy or Search”

  1. Hi Andi
    Great article and look so simple!
    I have a question though- Will Matomo work with SPT 2019 Modern experience sites as well?
    Thanks in advance for your reply!
    Kind Regards
    Luis

    Like

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.