matomo-org / matomo-for-wordpress

Get a fully functioning Matomo Analytics for your WordPress. Star us on Github? +1. Matomo is the leading open alternative to Google Analytics that gives you full control over your data. Privacy is built-in. 100% data ownership, no one else can see your data. We love Pull Requests!
https://matomo.org
GNU General Public License v3.0
119 stars 25 forks source link

Clarify that dynamically created scripts are not affected by the JS code position setting #556

Open froger-me opened 2 years ago

froger-me commented 2 years ago

Settings:

Expected result:
<script type="text/javascript" async="" src="https://domain.tld/wp-content/uploads/matomo/container_[container_ID].js"></script> somewhere at the end of the body element (relative position to other scripts TBD).

Actual result:
<script type="text/javascript" async="" src="https://domain.tld/wp-content/uploads/matomo/container_[container_ID].js"></script> inserted before the first script element in the header element.

Reason:
The generated tracking code forces element position no matters the chosen option - equivalent to document.getElementsByTagName('script')[0].parentNode.insertBefore(scriptToInsert, firstScriptElement)

Solution (?) :
For now, one can use the Add tracking code: Enter manually option and change s=d.getElementsByTagName('script')[0]; of the generated code into s=d.body.getElementsByTagName('script')[0];.
For an actual fix, changing WpMatomo\TrackingCode\TrackingCodeGenerator::prepare_tagmanger_code() to take the chosen option into account seems the way to go.

tsteur commented 2 years ago

@froger-me the tracking code that places the container file in the header should be placed in the footer though, is that right? Would there be any difference whether this script from the footer afterwards loads the file into the header or footer? I can see that it might be confusing and looks like it's not working correctly. Maybe an alternative could be to rename it into something like "Load scripts as early as possible (more data accuracy)" vs " load scripts as late as possible (slightly better for performance)".

Generally, we could also adjust the script though to append it to body when "footer" is selected. Be great to know if there's a difference as the script itself that loads the container is already in the footer.

froger-me commented 2 years ago

the tracking code that places the container file in the header should be placed in the footer though, is that right?

Indeed, that's right!

In that case, it seems to be a matter of wording rather than functionality - I'm not sure about the performance difference, I originally just thought script order was what prevented me from accessing window._paq (later I learnt of Tag Manager #300 and now I know better), and noticed the behavior described in the OP.

Something like you suggested, or changing the help text ("Choose whether the JavaScript code is added to the footer or the header." into "Choose whether the Tracking code above is added to the footer or the header - does not affect dynamically added script elements.") could help to clarify what this setting precisely does, but that's definitely not critical :).