matomo-org / matomo

Empowering People Ethically with the leading open source alternative to Google Analytics that gives you full control over your data. Matomo lets you easily collect data from websites & apps and visualise this data and extract insights. Privacy is built-in. Liberating Web Analytics. Star us on Github? +1. And we love Pull Requests!
https://matomo.org/
GNU General Public License v3.0
19.71k stars 2.62k forks source link

Multiple inclusions of Matomo across different versions breaks functionality #13007

Open OscarEriksen opened 6 years ago

OscarEriksen commented 6 years ago

We have seen issues where a client already is including Matomo (or older Piwik-versions) from a third party provider, and then they include our instance of Piwik/Matomo. Regular script-tag inclusion with your tags.

This non-encapsulation creates issues where they (kind of) override each other and results in crashes, especially if several different versions of Piwik/Matomo from different providers, is included where versions may differ.

How would we go about solving this issue; and encapsulating (namespacing?) one instance to not be able to get modified by other inclusions of the same software?

sgiehl commented 6 years ago

You want to track the same site into multiple matomo instances? Guess this post should help: https://matomo.org/blog/2017/02/complete-guide-tracking-websites-web-apps-multiple-piwiks-easily-efficiently/

Findus23 commented 6 years ago

I agree, you can find more about this idea here: https://forum.matomo.org/t/rename-paq-possible/27460

OscarEriksen commented 6 years ago

Thanks for sending those URLs, we've checked them earlier but together they may solve our requirements. We have checked it out previously but the issue in its core is that we have clients who from one agency include Piwik, and then they include our (other version) of Piwik; and then bad things happen. Especially if the previous included Piwik major version differs.

tsteur commented 6 years ago

What exactly isn't working? I don't recall too many changes in the tracker or tracker API since the last major version.

The only problem I can see is that the piwikAsyncInit and piwikPluginAsyncInit will be executed twice in this case which it actually very likely shouldn't. This is something that could be likely easily fixed but needs to be verified.

Otherwise depends how you embed the various trackers and how everything is defined. Likely the problem is really due to multiple _paq overriding each other or so. In this case features like addTracker etc would need to be used I suppose (if possible).

OscarEriksen commented 6 years ago

Really appreciate how helpful you are with this issue. And any further pointers in the right direction would be great. But we thought initially that it would be possible to just rename _paq-variable, or in any other way not reveal "our" version of the script to the outside world by variable scoping.

If we could easily rename piwik.js, and window.piwik (or the equivalent) and the _paq-variable it would be a godsend, but wouldn't really fix the issue for real.

The actual error is this: TypeError: The method 'setAccountId' was not found in "_paq" variable. Please have a look at the Piwik tracker documentation: http://developer.piwik.org/api-reference/tracking-javascript ourTag.js:12:8933

And is caused when a client (such as a company) includes piwik/matomo (often different versions, maybe version 2 or version 3, etctera) from different agencies that help them with tracking.

tsteur commented 6 years ago

setAccountId is not a method we have in our tracking code so that can't really work. Seems like they customize the tracking code which is not quite supported.

feyssericAzalead commented 6 years ago

Hi, We faced a similar problem. We have a tag tracker used by our clients which is using Piwik 2, and we use the Piwik.addTracker method. The site is using another tag based on Piwik 1 I think, not sure about the version but at least it is not providing Piwik.addTracker interface. Their tag is loaded first so we are using their implementation of the global variable Piwik (because Piwik client JS is checking variable existence before loading it) and it fails with window.Piwik.addTracker is not a function error.

It would be great if we could easily rename, or even better, use a local variable scoped inside our tag, not shared with other third party tags.

tsteur commented 6 years ago

I don't think renaming _paq would help much when using two different Piwik/Matomo versions when some methods aren't available. That's because once window.Piwik is loaded, the 2nd file won't assign window.Piwik again. A solution may be probably to limit the scope of window.Piwik using modules or so.

I doubt we will be working on this here soon. I would highly! recommend to get the people to update their Piwik 1 and Piwik 2 to the latest Matomo for security purposes etc which fixes the problem as well but I understand you can't get other companies to do that...