Minfied Static site - using matomo paste-in JS - breaks other scripts

Matomo On-Premise … latest version - 4.4.2 ?

Manually MINIFY the index.html on my test static site, breaks the matomo add in code. And also breaks other scripts on that page. They do not run. I replace with the un-minify version of index.html that includes the matomo script, and everything runs again… I have tried 2 different minify service and same happens on both. My static test page has one simple javascript on it works fine with minify. But if I add in the matomo script and minify my script does not work… If I add in the matomo script and NOT minify my script does work… I believe that no scripts are working on the page (including the matomo paste in ) after the matomo script is added if the page is then minified.

<!-- Matomo -->
<script>
var _paq = window._paq = window._paq || [];
/* tracker methods like "setCustomDimension" should be called before "trackPageView" */
_paq.push(['trackPageView']);
_paq.push(['enableLinkTracking']);
(function() {
var u="//xxxxxxx.xxxx/";
_paq.push(['setTrackerUrl', u+'matomo.php']);
_paq.push(['setSiteId', '1']);
var d=document, g=d.createElement('script'), s=d.getElementsByTagName('script')[0];
g.async=true; g.src=u+'matomo.js'; s.parentNode.insertBefore(g,s);
})();
</script>

What is the error (in the JavaScript console for example)?
Do you have a very simple version of the code of your minified page?

1 Like

You can view the test page (my daughter’s page actually) . As minify (of index.html) stops my script working the page is online now NOT minifiied, so it is working now… There are 9 photos on the page - click a photo ( to enlarge it ) - that is my simple script… test page is : https://ira.emupay.me

Hi,

Could you maybe share your minified HTML file (e.g. by naming it index.minified.html in the web root)?

1 Like

ok - I will do that :slight_smile: DONE
here’s that link namely - index.minified.html

To see my script running - just CLICK on one of the 9 photos ( not the profile photo). My script just zoom/enlarge it in a modal… When minified without the matomo paste-in , my script runs… When minified WITH the matomo paste-in the script is broken…

Hi,

In the minified version the tracking code contains var u="//matomo.mab/"; which is not the valid URL to your Matomo instance.

1 Like

That is an old cache copy of the file … try refresh cache… I will TRY ( but I am no expert) to purge that file from the CDN… I apologize, that was an earlier version/control that got in there (because I am tired and made a mistake before I corrected it) , but the true one with : emupay.me:8887 is in place… I have tried just now to purge the old version from the CDN so you get the right version of that file.

Okay, I now have the right version and found the issue:

You have JS code similar to this:

// Get the modal
var modal = document.getElementById("myModal");
var modalImg = document.getElementById("img01");
var captionText = document.getElementById("caption");

now your “minification-tool” simply removes newlines, but there are a few cases in JS where this changes the meaning:

// Get the modalvar modal = document.getElementById("myModal");var modalImg = document.getElementById("img01");

As the syntax highlighting already shows, // makes everything until the end of the line a comment, including the code.

So either use a minification tool that also removes the comment.

Or don’t minify your HTML at all, as it isn’t as useful as you might think, it is. It seems to bring down the size from 5.68KB to 4.76KB. But the things it removes are very repetitive and therefore compress well. If you look at the compressed file sizes (as you have enabled gzip compression on your webserver) the difference is only 2.44KB vs. 2.59KB and therefore neglectable.

1 Like

thank you so much … and you seem to be genius :wink: to find the problem so quickly…

So in summary: Going forward I will not minify that index.html AND I will change my comments to same style used in matomo scripts, i.e.: /* comment */

But one question is still confusing me… If I minify my index.html (always using same tool) without the matomo plugin then my script works… Only when I minify it with the matomo paste-in does my script break by the minify.

With your help, I now know that the problem is on my side caused by the comments in my script… but it did run - after minify - until I also add in the matomo code.

Thank you so much for finding the problem ! :pray: