matomo-org / tag-manager

Free Open Source Matomo Tag Manager - A simple way to manage and maintain all of your (third-party) tags on your website.
https://matomo.org
GNU General Public License v3.0
170 stars 58 forks source link

Tag-Manager script declares variables in global context #654

Closed Mercutio42 closed 1 year ago

Mercutio42 commented 1 year ago

Tag-Manager script declares variables in global context and might collide with other scripts

error: identifier 'd' ('g','s') has already been declared

might apply IIFE (function(){ var d=document, g=d.createElement('script'), s=d.getElementsByTagName('script')[0]; g.async=true; g.src=''; s.parentNode.insertBefore(g,s); })();

snake14 commented 1 year ago

Hi @Mercutio42 . Thank you for taking the time to create this issue. It looks like using an IIFE to insert the MTM script would be a good way to avoid polluting the global scope. Thank you for the excellent suggestion. I will mark this issue to be prioritised by our Product team.

heurteph-ei commented 1 year ago

Then the same for standard JavaScript tracking bootstrap code ;-)

snake14 commented 1 year ago

Thank you for mentioning the regular JavaScript tracking code @heurteph-ei . I'm not sure when, but it appears to have already been removed from the global scope: image

snake14 commented 1 year ago

@Mercutio42 I went ahead and updated the Matomo Tag Manager code snippet while I was working on some other improvements in MTM. The changes were included in the Matomo 4.15.0 release.