Can we reduce the size of Matomo's javascript file?

Most pages on my site are static so I need to use Matomo’s Javascript tracker. The thing is, piwik.js is a 21.50kb download (after compression).

I’ve disabled most of piwik’s features, so I feel like this file could be much smaller. Even if it doesn’t block page load, I’d like to respect my users’ bandwidth (and my own!).

Does anyone know if there’s a built-in way to set what features should be included in piwik.js? Any plugins? Has anyone had success in modifying the file manually?

I have found the unminified javascript file is in the “js” folder, so I could try to tweak it myself, but I wanted to make sure I wasn’t reinventing the wheel first. :slight_smile:

I’ve noticed that this user also asked something similar back in 2015 but never got any responses: Recompile piwik.js to reduce it’s size

1 Like

I have modified the unminified file that you mention, then packed/minified my version, and it worked fine.
I have just went over all the code and deleted the stuff that seemed irrelevant to me (link tracking, commerce, etc), it was easy to understand.

1 Like

Hi @Obscerno,

Reducing the size is definitly a good idea.
The reason for most of it’s “bloat” is, that the tracker needs to support ancient browser versions that e.g. don’t support JSON.
So if your user basis doesn’t consist of a lot of people who use IE7 and below (I hope so), you can simply remove ~8kb by removing the JSON fallback.

If you go through the rest of the file (it is quite well structured and commented) you’ll be able to find more code you can remove.

For minifiing the modified piwik.js look here: (even though every other minifier should work in theory)
https://github.com/matomo-org/matomo/tree/3.x-dev/js#deployment

More information:

1 Like

Thank you both so much; I guess I’ll get tinkering!

Small followup from here:

@Lukas:

Would it be possible to drop the support for IE<7 in Matomo 4 and remove the JSON fallback from the piwik.js?

Now that plugins can modify the piwik.js it may be possible to write a plugin that adds JSON3 for those who still need it (and reducing the load time for everyone else)

Inspired by a forum post

@thomas_matomo:

:+1: that is doable I’d say. A plugin that adds support for it may be doable as well.

Just a side note: JSON3 is currently also used for compatibility with some JS frameworks like older versions of prototype and others see eg https://github.com/matomo-org/matomo/issues/11023 .

1 Like

Why not have a js file specific to IE / Old served to those users, while having a Non-IE / Edge / Modern version for the rest. Wouldn’t that be better? For certain demographics we are still a few years away from calling IE completely dead.

Hi @ksec,

Do you mean that there is a significant amount of people using IE<7 (not IE in general)?

And it would still be a breaking change as people would need to change their tracking code.
So we have to wait for Matomo 4 and then it is probably the easiest way to just drop support for IE<7 (and all other browsers that don’t support JSON)

Or just offer 2 versions. The modern version without fallbacks.
And a matomo-legacy.js (or whatever) for those sites that still have grandpa visiting.

Then in the tracking code that gets put o nthe site they can specify which file to load/use.

Hi @arnandegans,

In Matomo 4 the huge part of matomo.js that is JSON support for IE7 and older has been removed, so the file is significantly smaller now.

If you have more ideas about how to make this smaller, just report them: