views:

552

answers:

6

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?

A: 

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.

Jonathan Sampson
A: 

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
+1  A: 

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
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
+1  A: 

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 javscript to finish before continuing the page rendition.

move the analytics code into your domready / onload function for the best performance results.

edit: heh, 3 ppl relied at same time.

Dimitar Christoff
+1  A: 

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
+2  A: 

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.

Török Gábor
The question is why Google itself doesn't use it.
TFM
@TFM because this method requires solid knowledge of JavaScript, while the page tag Google distributes can be easily utilized by anyone.
Török Gábor