Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

JAVASCRIPT TRACKER CSP / outsource matomo tracking code to external javascript file #14470

Closed
dantracking opened this issue May 22, 2019 · 5 comments
Labels
answered For when a question was asked and we referred to forum or answered it.

Comments

@dantracking
Copy link

when putting the tracking code [1] into files and placing it at the bottom or at the top
(within head part bevor tag [2]) of the website the FireFox browser is placing
a message/warning in the console (F12 inspector view).

Message:
"_paq.push() was used but Matomo tracker was not initialized before the matomo.js file was loaded.
Make sure to configure the tracker via _paq.push before loading matomo.js. Alternatively, you can
create a tracker..."

The message is not displayed all the time!
Its needed to reload the website in the browser (F5) a few times 5-6 to force this message.
When switching the position of 'tracking.js' and 'piwik.js' so that 'tracking.js' is loaded first,
it looks fine, so that there's no message appearing anymore.

Assumption:
Fail: If the 'piwik.js' is loaded fully first and then the 'tracking.js' the message appears.
OK : Otherwise if 'tracking.js' is finished loaded to be first and the 'piwik.js' no message appears.

Using Matomo 3.9.1
[1] https://matomo.org/faq/general/faq_20904/
[2] https://matomo.org/docs/installation/#install-the-javascript-tracking-tag

@fdellwing
Copy link
Contributor

I was just asking myself why anyone would load the piwik.js by himself, instead of doing this at the end of the tracking code. But than I saw that this is exactly how it is done in the FAQ.

I'm no CSP guru, but it is possible to load the piwik.js dynamically at the end of the tracking.js?

var d = document, g = d.createElement('script'), s = d.getElementsByTagName('script')[0];
g.type = 'text/javascript';
g.async = true;
g.defer = true;
g.src = u + 'piwik.js';
s.parentNode.insertBefore(g, s);

@tsteur
Copy link
Member

tsteur commented May 22, 2019

Not sure what the problem is here? If you load it differently, you just need to make sure to load it in the right order. That's what the message is telling you. Make sure the tracker is initialized before tracker is loaded. If there's any bug, I'm happy to reopen.

@tsteur tsteur closed this as completed May 22, 2019
@tsteur tsteur added the answered For when a question was asked and we referred to forum or answered it. label May 22, 2019
@dantracking
Copy link
Author

When I am using the official method proposed in [1] the message occurs. So it seems to me that the published way [1] is not correct, isn't it ?

Of course I can change the order loading 'tracking.js' first but it would against your documentation [1].
Why you do not change the documentation on you website :o ?

@tsteur
Copy link
Member

tsteur commented May 23, 2019

I've inverted the order of the scripts so it should work?

@dantracking
Copy link
Author

OK.
I quess this is the right way.
With loading 'tracking.js' first it works now in my case without message/warning.
Thanks so far.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
answered For when a question was asked and we referred to forum or answered it.
Projects
None yet
Development

No branches or pull requests

3 participants