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

Change default recommended JavaScript Tracking Code #10797

Closed
tsteur opened this issue Oct 27, 2016 · 10 comments · Fixed by #10900
Closed

Change default recommended JavaScript Tracking Code #10797

tsteur opened this issue Oct 27, 2016 · 10 comments · Fixed by #10900
Assignees
Labels
c: Platform For Matomo platform changes that aren't impacting any of our APIs but improve the core itself. c: Usability For issues that let users achieve a defined goal more effectively or efficiently. Enhancement For new feature suggestions that enhance Matomo's capabilities or add a new report, new API etc.
Milestone

Comments

@tsteur
Copy link
Member

tsteur commented Oct 27, 2016

Our default tracking code that we generate in Piwik and show on websites looks like this:

<!-- Piwik -->
<script type="text/javascript">
  var _paq = _paq || [];
  _paq.push(['trackPageView']);
  _paq.push(['enableLinkTracking']);
  (function() {
    var u="//demo.piwik.org/";
    _paq.push(['setTrackerUrl', u+'piwik.php']);
    _paq.push(['setSiteId', '1']);
    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);
  })();
</script>
<noscript><p><img src="//demo.piwik.org/piwik.php?idsite=1" style="border:0;" alt="" /></p></noscript>
<!-- End Piwik Code -->

There are a couple of problems with this:

  1. Noscript does not work and it is not obvious to users that rec=1 would need to be added to make it actually work
  2. Adding custom dimensions and custom variables is hard for users because they need to place it before trackPageView see https://github.com/piwik/plugin-CustomDimensions/issues/23 . I ran into this issue myself recently and took me a while to figure out why set custom variables were not tracked.
  3. I'm sure there are more problems with it, please comment or create an issue

As it is not good to discuss about different topics / problems in one issue I want to focus on point "2) Positioning of trackPageView" .

To solve this there are many options:
a) We could just add a comment to the tracking code showing them where to add calls like setCustomVariable.
b) Move trackPageView further down
c) We could automatically execute an initially set custom variable and custom dimension before trackPageView but this may not be wanted under circumstances and doesn't fix the problem in general for other tracking methods etc.
d) I'm sure there are some more ideas

We should ideally not change anything here in Piwik 2. I know some users perform replacements on a fetched / generated tracking code such as the WordPress Piwik Plugin (str_replace('...trackPageview...', '...', $trackingCode)). To make sure to not break anything there it would be better to only change it in Piwik 3.

I also wanted to embed the JS file via <script async defer src="piwik.js"></script> as it is better readable etc but then users need to specify the domain twice which is no good.

@tsteur tsteur added the RFC Indicates the issue is a request for comments where the author is looking for feedback. label Oct 27, 2016
@tsteur tsteur added this to the 3.0.0-b3 milestone Oct 27, 2016
@mattab
Copy link
Member

mattab commented Nov 12, 2016

  • Noscript: we could by default remove the noscript tag from the JS code output. And maybe introduce a checkbox in "Advanced", for "Track users with JavaScript disabled" which adds the noscript call with rec=1
  • trackPageView: adding a comment may work! (maybe let's not mention setCustomVariable but only setCustomDimension)

@mattab mattab modified the milestones: 3.0.0-b4, 3.0.0-b3 Nov 14, 2016
tsteur added a commit that referenced this issue Nov 23, 2016
@tsteur tsteur self-assigned this Nov 23, 2016
@tsteur tsteur added c: Platform For Matomo platform changes that aren't impacting any of our APIs but improve the core itself. c: Usability For issues that let users achieve a defined goal more effectively or efficiently. Enhancement For new feature suggestions that enhance Matomo's capabilities or add a new report, new API etc. and removed RFC Indicates the issue is a request for comments where the author is looking for feedback. labels Nov 23, 2016
@tsteur
Copy link
Member Author

tsteur commented Nov 27, 2016

FYI: In #10878 (comment) we also discuss about how to be using Piwik JavaScript Tracker when piwik.js is loaded first and only initialized later. Eg we could initialize Piwik like this:

_paq.push(['addTracker', [
  ['enableLinkTracking'],
  ['setSiteId', '5'], 
  ['setTrackerUrl', u+'piwik.php'],
  ['trackPageView']
]]);

Ideally, it would have always looked like this as this is clear. We actually define to add a tracker and configure it. If no addTracker is called, no tracker gets added and no methods executed. This would have so many advantages and I cannot think of any downside. If idSite and trackerUrl is not configured, we would pretty much ignore these requests because we know there is no tracker configured instead of current behaviour where we would send the requests to the current website URL which doesn't work.

When receiving an array of configurations we would be able to sort the methods before executing them and it wouldn't matter whether Piwik is already loaded or not. I think this version is even more readable etc and best: I think we could implement it in a way to keep BC although not 100% sure as it is a bit tricky to not ending up with 2 trackers in this case.

Any thoughts on this?

@mattab
Copy link
Member

mattab commented Dec 2, 2016

Any thoughts on this?

@tsteur the idea is good I think, but not needed for now so maybe you could move the comment to a new issue if you think it's worth it as this issue is already closed

@tsteur
Copy link
Member Author

tsteur commented Dec 3, 2016

OK will create new issue. This might be pretty much the only way to make tracker work for people that initialize tracker with paq after piwik was loaded and for people that use multiple trackers and not paq

@waa8288
Copy link

waa8288 commented May 3, 2017

I am unable to edit the tracking code generated by Piwik 3.0.3. I need to add 2nd Piwik server configuration
on the tracking code. How can I edit the tracking? Help!

@sgiehl
Copy link
Member

sgiehl commented May 3, 2017

@waa8288 what exactly are you trying to achieve? You can modify the tracking code however you want before inserting it into your website.

@waa8288
Copy link

waa8288 commented May 3, 2017 via email

@Findus23
Copy link
Member

Findus23 commented May 3, 2017

@waa8288 You don't need to edit the code in the box piwik shows. That's just an example of the code you need to add into the header of your website.

So if you want to modify the tracking code just change it on your website.

@waa8288
Copy link

waa8288 commented May 3, 2017 via email

@tsteur
Copy link
Member Author

tsteur commented May 3, 2017

The tracking code shown in Piwik should be copied into your website. For more information about tracking multiple Piwiks check out https://piwik.org/blog/2017/02/complete-guide-tracking-websites-web-apps-multiple-piwiks-easily-efficiently/

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
c: Platform For Matomo platform changes that aren't impacting any of our APIs but improve the core itself. c: Usability For issues that let users achieve a defined goal more effectively or efficiently. Enhancement For new feature suggestions that enhance Matomo's capabilities or add a new report, new API etc.
Projects
None yet
5 participants