Google analytics in header or footer?

JavascriptHtmlGoogle AnalyticsWebpage

Javascript Problem Overview


My questions is, do you put Google Analytics Javascript code in the header or at the very bottom of the page (before </body>). I've heard people say it's best to put Google Analytics Javascript code at the very end of your html. Others say its best to put it in the header. Is there a best practice?

Javascript Solutions


Solution 1 - Javascript

Put it in your header. It's asynchronous so it won't block your page from loading and by placing it in your header you'll be more likely to get a accurate stats.

Solution 2 - Javascript

This what google says about it:

> Once you find the code snippet, copy and paste it into your web page, > just 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 section. For the best > performance across all browsers we suggest you position other scripts > in your site in one of these ways:

> 1. before the tracking code snippet in > the <head> section of your HTML > 2. after both the tracking code snippet > and all page content (e.g. at the bottom of the HTML body)

and here comes the *:

> *One of the main advantages of the asynchronous snippet is that you can position it at the top of the HTML document. This increases the > likelihood that the tracking beacon will be sent before the user > leaves the page. It is customary to place JavaScript code in the > section, and we recommend placing the snippet at the bottom of > the section for best performance.

Solution 3 - Javascript

The difference is simple, and the answer depends of your needs Suppose someone clicked your site by mistake and close it really quick.

If the script is in the top, you have a visitor, If the script is in the bottom, you don't have a visitor.

I use it in the bottom, cause to me if someone don't look to my site, is not a visitor.

Solution 4 - Javascript

According to google Analytic

> Where to Place the Tracking Code

The tracking code is designed to read data from your page after the content for the page has finished loading. For this reason, the snippet should be located just before the closing tag for your web page. Once the content for your page has loaded, the tracking code, when executed, reads the content for your page following the Document Object Model (DOM). All information relevant to tracking is then used to establish page information, set/update cookies, and to send the GIF request to the Google Analytics servers.

By placing the script at the end of the page body, you ensure that the tracking code is executed as the last element of the DOM. If a given page load is interrupted for some reason, it is possible that the GIF request for that page view will not be executed. However, should you place the tracking code at the top of the page, any load interruptions might result in incomplete or inaccurate reporting anyhow, since the tracking code relies on page data for its reports.

Additionally, the physical placement of the tracking code call at the bottom of the page is more effective than using an onLoad() function to call the tracking code. If you use onLoad() to execute the tracking code, execution relies on the event model for the browser instead of the DOM. In such a situation, should a remote image fail to load on a page, onLoad() will not be called, whereas the DOM for the page could still load completely.

Solution 5 - Javascript

For best practices you must understand the new asynchronous code which clear most of the earlier page load and performance issues.
Although Google official recommendation says that it should be placed before the close of the <head> tag but there are few exceptions which are noteworthy.

  1. For using the code for general purposes code placement before the head tag is justified and works perfectly.
  2. But if there are events tracking or e-commerce conversion tracking and there are custom codes involved, then it is recommended to put it before the </body> tag to get the better results.

Note: There will still be discrepancies in the Search Console clicks and reals users with 10-20% but regular integration of Google Adwords it has shown 100% accuracy with precision so if you are using Google Adwords for your site try to integrate them all.
Also read Google Analytics Header or Footer

Solution 6 - Javascript

Things changed over time as they evolved. On my GA account, as 2016, when I get the code they said:

> This is the Universal Analytics tracking code for this property. To > get all the benefits of Universal Analytics for this property, copy > and paste this code into every webpage you want to track. > > GA code > > Use the code above to create a file named "analyticstracking.php", and > include the file on each PHP template page. Add the following line to > each template page immediately after the opening <body> tag:

<?php include_once("analyticstracking.php") ?>

Solution 7 - Javascript

I would say it is best to put it into the footer - as it is best that the punter gets the whole page and has a chance to see everything. removes some of the possibility of getting false positives where the person has selected your site on error.

Solution 8 - Javascript

I would put it in the footer, above the </body> tag, so that once the tracking code loads, the whole page has loaded for the analytics tracking code to track.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestiontylerView Question on Stackoverflow
Solution 1 - JavascriptJohn CondeView Answer on Stackoverflow
Solution 2 - JavascriptguidoView Answer on Stackoverflow
Solution 3 - JavascriptEdu RuizView Answer on Stackoverflow
Solution 4 - JavascriptJosh15View Answer on Stackoverflow
Solution 5 - Javascriptsachin aroraView Answer on Stackoverflow
Solution 6 - JavascriptAdrian P.View Answer on Stackoverflow
Solution 7 - JavascriptEd HealView Answer on Stackoverflow
Solution 8 - JavascriptdesbestView Answer on Stackoverflow