This issue is about improving our documentation: sometimes we recommend to add the tracking code before </body> and sometimes after <body>.
Actually it is recommended to include the tracking code in the <head> section or after the opening <body> tag.
Guides to fix:
The logic for suggesting to add code after the </body> was that incomplete transmitted pages wouldn't be in the stats. What changed since the original recommendation ?
I think this topic is really important!
From setting up several websites using piwik, we learned the lessons to load piwik as late as possible.
The reason is, when people uses browsers, browser plugins or browser settings which blocks tracking tools, there is a good chance that not only the tracker script is blocked but also everything after the blocked content (piwik) gets damaged too.
This leads to a complete broken website.
This is the worst experience on can give a visitor.
If piwik is loaded late -after most of the visible content-and it is blocked in a bad way, the website still looks mostly fine.
just a feedback:
As mentioned putting piwik as late as possible was the lessons learned from having problems with some tracking blockers...
For us this does not mean at end of head but at end of body.
This is how to looks:
<!-- START PIWIK TRACKING CODE -->
<script type="text/javascript">
//<![CDATA[
...
<!-- END PIWIK TRACKING CODE -->
</div>
</div>
</body>
</html>
@OP: This suggestion leads to having the fallback code (<noscript><p><img>...</p></noscript>) inside the <head>. It should mention that the fallback code is to be placed inside the <body>.
@novahyth good point, I just realized this as well while having a look at https://github.com/piwik/piwik/issues/9770 . It's not valid HTML otherwise and might not even be executed. I'll create a new issue for this.
So, why was this done? I thought that webperf dictated to include all scripts as late as possible in the page, and especially the non-critical ones. Analytics is clearly the least to me. Why should I block rendering by adding Piwik in <head> rather than before the closing </body>?
Most helpful comment
So, why was this done? I thought that webperf dictated to include all scripts as late as possible in the page, and especially the non-critical ones. Analytics is clearly the least to me. Why should I block rendering by adding Piwik in
<head>rather than before the closing</body>?