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

assign _paq to window in tracking code #16189

Closed
Findus23 opened this issue Jul 7, 2020 · 1 comment
Closed

assign _paq to window in tracking code #16189

Findus23 opened this issue Jul 7, 2020 · 1 comment
Assignees
Labels
c: Tracking For issues related to getting tracking data into Matomo. c: Usability For issues that let users achieve a defined goal more effectively or efficiently.
Milestone

Comments

@Findus23
Copy link
Member

Findus23 commented Jul 7, 2020

When putting the default Matomo tracking code into a function, tracking stops working as matomo.js expects window._paq to exist and the variable is just local inside the function.

For comparison the default Google Analytics tracking code:

(function(i,s,o,g,r,a,m){i['GoogleAnalyticsObject']=r;i[r]=i[r]||function(){
(i[r].q=i[r].q||[]).push(arguments)},i[r].l=1*new Date();a=s.createElement(o),
m=s.getElementsByTagName(o)[0];a.async=1;a.src=g;m.parentNode.insertBefore(a,m)
})(window,document,'script','https://www.google-analytics.com/analytics.js','ga');

ga('create', 'UA-XXXXX-Y', 'auto');
ga('send', 'pageview');

slightly more readable form:

window['GoogleAnalyticsObject'] = 'ga';
window['ga'] = window['ga'] || function () {
    (window['ga'].q = window['ga'].q || []).push(arguments)
}, window['ga'].l = 1 * new Date();
a = document.createElement('script'),
    m = document.getElementsByTagName('script')[0];
a.async = 1;
a.src = 'https://www.google-analytics.com/analytics.js';
m.parentNode.insertBefore(a, m)

ga('create', 'UA-XXXXX-Y', 'auto');
ga('send', 'pageview');

completly unminified form:

window.GoogleAnalyticsObject = 'ga';
window.ga = window.ga || function () {
    (window.ga.q = window.ga.q || []).push(arguments)
};
now = new Date()
window.ga.l = now.getTime();
scriptTag = document.createElement('script');
firstScript = document.getElementsByTagName('script')[0];
scriptTag.async = true;
scriptTag.src = 'https://www.google-analytics.com/analytics.js';
firstScript.parentNode.insertBefore(scriptTag, firstScript)

ga('create', 'UA-XXXXX-Y', 'auto');
ga('send', 'pageview');
@Findus23 Findus23 added c: Usability For issues that let users achieve a defined goal more effectively or efficiently. c: Tracking For issues related to getting tracking data into Matomo. labels Jul 7, 2020
@tsteur tsteur added this to the 3.14.0 milestone Jul 7, 2020
@tsteur tsteur self-assigned this Jul 7, 2020
@tsteur
Copy link
Member

tsteur commented Jul 8, 2020

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
c: Tracking For issues related to getting tracking data into Matomo. c: Usability For issues that let users achieve a defined goal more effectively or efficiently.
Projects
None yet
Development

No branches or pull requests

2 participants