Should Google Analytics go in the head or bottom of an HTML page?

前端 未结 7 1186
渐次进展
渐次进展 2021-02-02 12:14

Google advises putting the google analytics script right before closing the .

However, I would prefer to combine it with the rest my javascript

相关标签:
7条回答
  • 2021-02-02 12:29

    I had the script in body section just before ending body tag and it was always showing that my site is missing tracking code analytics admin panel so I moved to head section and it works fine.

    0 讨论(0)
  • 2021-02-02 12:32

    I agree with the accepted answer. I would like to add that I noticed Google Webmaster Tools (GWT) requires the tracking code (asynchroneous version) to be in the head to be able to register as admin for your site via an existing Google Analytics registration for the same site. So if the JS tracking code is not in the <head>, this registration option is not available.

    This might be Google's way of coercing more websites to do this. Perhaps Google can keep better track of precisely those visitors who cancel during page load. These might make interesting Analytics for them to optimize their search results (e.g. maintain 'early-bounce' events for their statistics).

    0 讨论(0)
  • 2021-02-02 12:36

    You're risking nothing really. Putting <script> tags at the end of the is recommendable because of the blocking nature while executing the Javascript it encloses...

    That is simply not true, jAndy.

    You won't be able to get some features, such as real-time reporting, or Google Webmaster Tools verification, unless the code sits within the <head>. The whole point of the asynchronous code is that it does it can load while the rest of the page is loading. There is no UX risk from placing it within the <head>, and it is best-practice to do so. source: Google Analytics Support

    @Stratboy I doubt it's the GA code that's breaking the layout. A properly tagged script - especially one that does not display any content, like GA's - would not affect the layout. I'd look elsewhere for your layout issues. Perhaps try validating it with W3C for some clues?

    0 讨论(0)
  • 2021-02-02 12:36

    Implementing the code Once you find the code snippet, copy and paste it into the bottom of your content, immediately before the </body> tag of each page you are planning to track. If you use a common include or template, you can enter it there. To implement tracking code for secure pages (e.g. https://), please read How do I obtain tracking code for secure pages?

    0 讨论(0)
  • 2021-02-02 12:42

    You're risking nothing really. Putting <script> tags at the end of the <body> is recommendable because of the blocking nature while executing the Javascript it encloses. Since the Javascript for GA is dynamically inserted and therefore non-blocking, you can put it anywhere.

    I guess the only "problem" Google sees is, when putting that code at the bottom of your site, you might not catch visitors / pageloads cancel the request before completed. If someone calls your HTML document and cancels the request, the GA code might not get encountered (and therefore, not executed).

    0 讨论(0)
  • 2021-02-02 12:45

    you can put it before </body> I don't think there would be any problems with that.

    0 讨论(0)
提交回复
热议问题