11

The Google Analytics setup instructions state:

This tracking code snippet should be included in your site's pages so that it appears at the bottom of the page's HTML (or generated-HTML) structure, before the closing <body> tag.

Does their code snippet require this placement to function fully, or does Google suggest this solely because it improves page load performance to have scripts at the bottom?

Soldarnal
  • 7,558
  • 9
  • 47
  • 65
  • 4
    Note that you're referring to outdated documentation. In 2009, Google put out their asynchronous tracking snippet, which loads in the background. They recommend placing it at the bottom of the HEAD section now: http://code.google.com/apis/analytics/docs/tracking/asyncTracking.html – Brian Mar 26 '11 at 20:29
  • None of the answers provided answered your question : Can position of the script reflect whether it is working or not. Could the placement in the bottom be a reason why it is not working ? Did you figure this out ? – bogatyrjov Oct 01 '13 at 13:29

7 Answers7

4

It's just to improve page load performance. If this code were at the beginning, then if for some reason the analytics code ran slowly, the rest of the page would wait for it to finish or timeout before loading.

The analytics code likely makes queries to Google's servers, so they have to wait for the servers to respond before finishing. If (god forbid) Google's webservers were to be backed up or lagging, this would seriously impact the load time of your website.

Marquis Wang
  • 10,878
  • 5
  • 30
  • 25
  • I looked through their code quickly, and didn't see any document.write() calls. Seems to support your claim that is just to improve performance. – Soldarnal Jul 02 '09 at 21:54
3

There is an article on Better Google Analytics JavaScript that doesn’t block page downloading. See related How do I dynamically load Google Analytics JavaScript? question on SO, too.

Community
  • 1
  • 1
viam0Zah
  • 25,949
  • 8
  • 77
  • 100
  • The question is why Google itself doesn't use it. – TFM Jul 02 '09 at 11:09
  • 1
    @TFM because this method requires solid knowledge of JavaScript, while the page tag Google distributes can be easily utilized by anyone. – viam0Zah Jul 02 '09 at 15:50
2

I find that at times Google's supplied code can cause a delay in page load due to clientside latency anyway. by having it at the bottom of the source code, it won't cause the browser to halt and wait on the javascript to finish before continuing the page rendition.

Move the Analytics code into your domready / onload function for the best performance results.

Jon Adams
  • 24,464
  • 18
  • 82
  • 120
Dimitar Christoff
  • 26,147
  • 8
  • 50
  • 69
2

From YSlow guide

The problem caused by scripts is that they block parallel downloads. The HTTP/1.1 specification suggests that browsers download no more than two components in parallel per hostname. If you serve your images from multiple hostnames, you can get more than two downloads to occur in parallel. While a script is downloading, however, the browser won't start any other downloads, even on different hostnames.

In some situations it's not easy to move scripts to the bottom. If, for example, the script uses document.write to insert part of the page's content, it can't be moved lower in the page. There might also be scoping issues. In many cases, there are ways to workaround these situations.

An alternative suggestion that often comes up is to use deferred scripts. The DEFER attribute indicates that the script does not contain document.write, and is a clue to browsers that they can continue rendering. Unfortunately, Firefox doesn't support the DEFER attribute. In Internet Explorer, the script may be deferred, but not as much as desired. If a script can be deferred, it can also be moved to the bottom of the page. That will make your web pages load faster.

skaffman
  • 398,947
  • 96
  • 818
  • 769
0

I was asked this yesterday by a co-worker. I would imagine it would have something to do with the entire document being loaded before the script is executed.

If more of the document is loaded, more of the documented can be processed by the script.

Sampson
  • 265,109
  • 74
  • 539
  • 565
0

I'm fairly certain it is just for page performance. This can, of course, be done with any javascript library/snippet that isn't needed right when the page loads everything initially.

Peter
  • 421
  • 2
  • 8
0

Tag at the bottom used to be solely to minimize the possible impact of the tracking scripts on the web site behavior: the priority is to have a functional web site, once it works, track it. You may not track all page views, but that's less important for the end user.

The asynchronous syntax solves both the reliability and performance issue, while remaining simple to deploy.

It goes at the end of the HEAD section.

Open SEO
  • 1,682
  • 14
  • 16