Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there a reason to put google analytics in head and not in body?

Does exist reason to put google analytics in head and not in the end of body? (I working on big website that its works in this way)

Option 1:

<head>
<script src="http://www.google-analytics.com/ga.js"></script>
</head>

Option 2 - in bottom of body :

<body>
//html code

<script src="http://www.google-analytics.com/ga.js"></script>
</body>

Edit1: Also the same question with jquery ui

Edit2: add ga.js in the end of script (fix)

Thanks

like image 678
Ben Avatar asked Sep 12 '10 09:09

Ben


People also ask

Can I put Google Analytics code the body?

You can put it anywhere you want on page, and ll run anywhere on the page whether it's in the head or the body. But according to Google support (Add the tracking code directly to your site) it is recommended to it in the head tag, paste it immediately before the closing </head> tag.

Where Google Analytics code should be placed?

You can add the Analytics tag directly to the HTML of each page on your site, or indirectly using a tag management system such as Google Tag Manager.

What is the advantage of putting tracking code in the header PHP file?

The Benefits of Adding Analytics Code into Header This is why Google recommends you add your tracking code in the header of your site. It helps you to accurately record traffic stats because your header scripts are loaded earlier than any other scripts on your page.

Do I need to put Google Analytics code on every page?

To measure a website, you first have to create a Google Analytics account. Then you need to add a small piece of JavaScript measurement code to each page on your site. Every time a user visits a webpage, the tracking code will collect anonymous information about how that user interacted with the page.


1 Answers

Embedding the ga.js code the way you describe (with a hardcoded <script> tag) is indeed blocking, and if you load the script like that, the best practice is considered to be loading it at the just before the </body> tag. But this is not the recommended practice if you're using the new asynchronous code. Google explicitly recommends placing the new asynchronous code in the <head>.

The new asynchoronous code is non-blocking in two ways. First, it queues up the variables for the page in a global _gaq variable. That way, the data is prepared either way.

Then, as described in this SO answer, using javascript directly to write out the script as in the new async code is non-blocking (this direct inject method is the way to achieve asynchronous-ness, even in browsers that don't directly observe the async attribute). The rest of the site can continue to load if for some reason Google's servers are down or slow to respond. And that's only if the user doesn't have ga.js cached already, as many do, since ga.js is used on many, many popular websites.

The benefit of all this is that the earlier the ga.js loads and is able to transmit the _gaq object to Google, the more likely you'll be to capture ALL of your potential data, like the data of the users who click very quickly on your page. This is particularly important for 'big' websites that tend to have lots of regular users who follow quick-clicking habits.

If you're skeptical, test it out using a page load inspector like the webkit developer tools. I've tested it extensively and found no evidence of significant blocking when using the async code in the </head> as described.

like image 171
Yahel Avatar answered Sep 18 '22 12:09

Yahel