简体   繁体   中英

Google analytics in header or footer?

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?

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.

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 (eg 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.

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.

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.

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") ?>

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.

我会将它放在</body>标记上方的页脚中,这样一旦加载跟踪代码,整个页面就会加载,以便跟踪分析跟踪代码。

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM