Javascript 页眉或页脚中的 Google 分析?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/10712908/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Google analytics in header or footer?
提问by styler1972
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?
我的问题是,您是将 Google Analytics Javascript 代码放在页眉中还是放在页面的最底部(之前</body>
)。我听说有人说最好将 Google Analytics Javascript 代码放在 html 的最后。其他人说最好把它放在标题中。有最佳实践吗?
回答by John Conde
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.
把它放在你的标题中。它是异步的,因此它不会阻止您的页面加载,并且通过将其放置在您的标题中,您将更有可能获得准确的统计信息。
回答by guido
This what google saysabout 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:
- before the tracking code snippet in the
<head>
section of your HTML- after both the tracking code snippet and all page content (e.g. at the bottom of the HTML body)
找到代码片段后,将其复制并粘贴到网页中,就在结束
</head>
标记之前*
。如果您的网站使用模板生成页面,请在包含该部分的文件中的结束标记之前输入它。为了在所有浏览器中获得最佳性能,我们建议您通过以下方式之一在您的站点中放置其他脚本:
- 在
<head>
HTML 部分中的跟踪代码段之前- 在跟踪代码段和所有页面内容之后(例如在 HTML 正文底部)
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.
*异步代码段的主要优点之一是您可以将其放置在 HTML 文档的顶部。这增加了在用户离开页面之前发送跟踪信标的可能性。习惯上将 JavaScript 代码放在该部分,我们建议将代码片段放在该部分的底部以获得最佳性能。
回答by Edu Ruiz
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.
我在底部使用它,因为对我来说,如果有人不看我的网站,就不是访问者。
回答by Josh15
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.
跟踪代码旨在在页面内容加载完毕后从您的页面读取数据。因此,该代码段应位于网页的结束标记之前。加载页面内容后,跟踪代码在执行时会按照文档对象模型 (DOM) 读取页面内容。然后使用与跟踪相关的所有信息来建立页面信息、设置/更新 cookie,并将 GIF 请求发送到 Google Analytics 服务器。
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.
通过将脚本放在页面正文的末尾,您可以确保跟踪代码作为 DOM 的最后一个元素执行。如果给定的页面加载由于某种原因中断,则可能不会执行该页面视图的 GIF 请求。但是,如果您将跟踪代码放在页面顶部,任何加载中断都可能导致报告不完整或不准确,因为跟踪代码依赖于其报告的页面数据。
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.
此外,在页面底部放置跟踪代码调用的物理位置比使用 onLoad() 函数调用跟踪代码更有效。如果您使用 onLoad() 执行跟踪代码,则执行依赖于浏览器的事件模型而不是 DOM。在这种情况下,如果远程图像无法在页面上加载,则不会调用 onLoad(),而页面的 DOM 仍然可以完全加载。
回答by Adrian P.
Things changed over time as they evolved. On my GA account, as 2016, when I get the code they said:
事情随着时间的推移而变化。在我的 GA 帐户上,如 2016 年,当我收到代码时,他们说:
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:
这是该媒体资源的 Universal Analytics 跟踪代码。要为此媒体资源获得 Universal Analytics 的所有优势,请将此代码复制并粘贴到您要跟踪的每个网页中。
通用代码
使用上面的代码创建一个名为“analyticstracking.php”的文件,并将该文件包含在每个 PHP 模板页面上。在每个模板页面的 <body> 开头标记之后立即添加以下行:
<?php include_once("analyticstracking.php") ?>
回答by sachin arora
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.
对于最佳实践,您必须了解新的异步代码,这些代码清除了大部分早期的页面加载和性能问题。
虽然谷歌官方建议它应该放在<head>
标签的关闭之前,但很少有值得注意的例外。
- For using the code for general purposes code placement before the head tag is justified and works perfectly.
- 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.
- 用于将代码用于通用代码放置在 head 标记之前是合理的并且可以完美运行。
- 但是如果有事件跟踪或者电商转化跟踪,并且涉及到自定义代码,那么建议放在
</body>
标签之前,以获得更好的效果。
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
注意:Search Console 中的点击次数和真实用户仍有 10-20% 的差异,但定期集成 Google Adwords 已显示 100% 准确度和精确度,因此如果您在网站上使用 Google Adwords,请尝试将它们全部集成。
另请阅读Google Analytics 页眉或页脚
回答by Ed Heal
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.
我会说最好把它放在页脚中 - 因为最好让下注者获得整个页面并有机会看到所有内容。在此人错误地选择了您的网站的情况下,消除了一些误报的可能性。
回答by desbest
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.
我会将它放在</body>
标签上方的页脚中,这样一旦加载了跟踪代码,整个页面就会加载,以便分析跟踪代码进行跟踪。