Open froger-me opened 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.
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 :).
Settings:
Tag Manager
Footer
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 thebody
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 firstscript
element in theheader
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 changes=d.getElementsByTagName('script')[0];
of the generated code intos=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.