Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Position of Google Analytics code

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?

like image 779
Soldarnal Avatar asked Jul 01 '09 20:07

Soldarnal


2 Answers

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.

like image 56
Marquis Wang Avatar answered Sep 28 '22 07:09

Marquis Wang


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.

like image 21
viam0Zah Avatar answered Sep 28 '22 07:09

viam0Zah