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


#1

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


#2

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.


(Lukas Winkler) #3

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)
matomo/js at 3.x-dev · matomo-org/matomo · GitHub

More information:



#4

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


(Lukas Winkler) #5

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 Make JSON usage in piwik.js a bit more stable and reduce conflict with other libraries · Issue #11023 · matomo-org/matomo · GitHub .


#6

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.


(Lukas Winkler) #7

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)