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

7

22

Google advises putting the google analytics script right before closing the </head>.

However, I would prefer to combine it with the rest my javascript that are now all together in a cached, external file, which is loaded at the bottom of my HTML file. Can I do it my way? If so, what am I risking then/what's the cost of putting the below code not in the head but at the bottom of the HTML?

<script type="text/javascript">
 var _gaq = _gaq || [];
  _gaq.push(['_setAccount', 'UA-22180365-1']);
  _gaq.push(['_setDomainName', 'none']);
  _gaq.push(['_setAllowLinker', true]);
  _gaq.push(['_trackPageview']);

  (function() {
    var ga = document.createElement('script'); ga.type = 'text/javascript'; ga.async = true;
    ga.src = ('https:' == document.location.protocol ? 'https://ssl' : 'http://www') + '.google-analytics.com/ga.js';
    var s = document.getElementsByTagName('script')[0]; s.parentNode.insertBefore(ga, s);
  })();
</script>
Kus answered 20/3, 2011 at 18:27 Comment(1)
missed somethingNobile
A
29

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).

Aquiculture answered 20/3, 2011 at 18:32 Comment(3)
It would appear Google's tracking snippet is non-blocking, even when used in the <head> section.Cowen
This is no longer correct. Google recommend that the code is put before the closing head tagKaronkaross
From Google: "Paste your snippet (unaltered, in it’s entirety) into every web page you want to track. Paste it immediately before the closing </head> tag." - support.google.com/analytics/answer/1008080?hl=enCyd
S
22

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?

Stepfather answered 30/10, 2012 at 14:26 Comment(3)
jAndy's comment is true for "traditional" script includes, but you're correct, Google Analytics has worked to make their script non-blocking, so that advice is wrong in this case.Cowen
@Analyticus What's the benefit of "Google Webmaster Tools verification" or how do I find more about "real-time reporting". The reason Google cites for placing in the <head> (found in your link) seems weak to me "This increases the likelihood that the tracking beacon will be sent before the user leaves the page." If the leave the page in 1-2 seconds do I even want to count them anyway?Eichelberger
That depends a lot on your site. If your pages take, on average, 20 seconds to load (god forbid!), then this can definitely affect the way you are collecting data. Real-time reporting - for most people - is not a great use of time. If there are other real-time events affecting your decision making, then yeah, definitely take a look. However, one of my biggest tales of caution for my clients would be not to enter Analytics-land without a clear goal in mind. It's kind-of like going grocery shopping without a list: you'll always spend more (time) than you had anticipated.Stepfather
T
4

From Google Analytics setup instructions:

Paste your snippet (unaltered, in it’s entirety) into every web page that you want to track. Paste it immediately before the closing </head> tag.

If your website uses templates to generate pages, enter it just before the closing tag in the file that contains the <head> section.

Toshikotoss answered 22/2, 2014 at 15:48 Comment(0)
S
1

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).

Sollows answered 20/12, 2012 at 9:52 Comment(0)
S
0

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

Sheepshank answered 20/3, 2011 at 18:31 Comment(0)
F
0

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?

Flute answered 20/3, 2011 at 18:32 Comment(0)
C
-1

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.

Capers answered 28/6, 2016 at 15:30 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.