To avoid redundancy to modify the embedded code, I would like to set the tracking code in an external JS file, and include it into all the sites I manage.
Bellow is how I set the external tracking code, but it is not working.
It just doesn’t measure anything and it seems there seems a problem with tracking.
root/
├ TrackingCode.js (tracking code embedded here)
├ siteA/index.html (TrackingCode.js is included here)
├ siteB/index.html (TrackingCode.js is included here)
├ siteC/index.html (TrackingCode.js is included here)
Is it possible to set matomo tracking code like above?
I appreciate, if anyone can help me with this issue.
What you are doing should be working. So you’ll need to check your browsers developer tools to find out why it isn’t (error in the console and piwik.{js|php} in the network tab)
But you have to keep in mind, that the tracking code needs to be loaded before you can access any _paq() functions (e.g. to track events). And therefore the extra request will probably slow down the page load as the execution halts until the file has finished loading (this should be less of an issue with HTTP 2.0).