javascript Google Analytics 应该放在 HTML 页面的头部还是底部?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/5370490/
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
Should Google Analytics go in the head or bottom of an HTML page?
提问by Sam
Google advises putting the google analytics script right before closing the </head>
.
Google 建议在关闭</head>
.
However, I would prefer to combine it with the rest my javascript that are now all together in a cached, external file, which is loaded at the bottom of my HTML file. Can I do it my way? If so, what am I risking then/what's the cost of putting the below code not in the head but at the bottom of the HTML?
但是,我更愿意将它与我的 javascript 的其余部分结合起来,这些 javascript 现在都在一个缓存的外部文件中,该文件加载在我的 HTML 文件的底部。我可以按我的方式做吗?如果是这样,那么我有什么风险/将下面的代码放在 HTML 的底部而不是头部的成本是多少?
<script type="text/javascript">
var _gaq = _gaq || [];
_gaq.push(['_setAccount', 'UA-22180365-1']);
_gaq.push(['_setDomainName', 'none']);
_gaq.push(['_setAllowLinker', true]);
_gaq.push(['_trackPageview']);
(function() {
var ga = document.createElement('script'); ga.type = 'text/javascript'; ga.async = true;
ga.src = ('https:' == document.location.protocol ? 'https://ssl' : 'http://www') + '.google-analytics.com/ga.js';
var s = document.getElementsByTagName('script')[0]; s.parentNode.insertBefore(ga, s);
})();
</script>
回答by jAndy
You're risking nothing really. Putting <script>
tags at the end of the <body>
is recommendable because of the blockingnature while executing the Javascript it encloses. Since the Javascript for GA is dynamically inserted and therefore non-blocking, you can put it anywhere.
你真的什么都没有冒险。由于在执行它所包含的 Javascript 时具有阻塞性质,因此建议将<script>
标签放在 的末尾。由于 GA 的 Javascript 是动态插入的,因此是非阻塞的,你可以把它放在任何地方。<body>
I guess the only "problem" Google sees is, when putting that code at the bottom of your site, you might not catch visitors / pageloads cancel the request before completed. If someone calls your HTML document and cancels the request, the GA code might not get encountered (and therefore, not executed).
我猜谷歌看到的唯一“问题”是,当将该代码放在您网站的底部时,您可能没有发现访问者/页面加载在完成之前取消请求。如果有人调用您的 HTML 文档并取消请求,则可能不会遇到 GA 代码(因此不会执行)。
回答by MMMdata
You're risking nothing really. Putting
<script>
tags at the end of the is recommendable because of the blocking nature while executing the Javascript it encloses...
你真的什么都没有冒险。将
<script>
标签放在 .
That is simply not true, jAndy.
这根本不是真的,jAndy。
You won't be able to get some features, such as real-time reporting, or Google Webmaster Tools verification, unless the code sits within the <head>
. The whole point of the asynchronous code is that it does it can load while the rest of the page is loading. There is no UX risk from placing it within the <head>
, and it is best-practice to do so. source: Google Analytics Support
您将无法获得某些功能,例如实时报告或 Google 网站管理员工具验证,除非代码位于<head>
. 异步代码的全部意义在于它可以在页面的其余部分加载时加载。将其放置在<head>
. 来源:谷歌分析支持
@Stratboy I doubt it's the GA code that's breaking the layout. A properly tagged script - especially one that does not display any content, like GA's - would not affect the layout. I'd look elsewhere for your layout issues. Perhaps try validating it with W3C for some clues?
@Stratboy 我怀疑是 GA 代码破坏了布局。正确标记的脚本 - 特别是不显示任何内容的脚本,如 GA 的 - 不会影响布局。我会在别处寻找您的布局问题。也许尝试用 W3C 验证它以获得一些线索?
回答by Undistraction
From Google Analytics setup instructions:
从 Google Analytics 设置说明:
Paste your snippet (unaltered, in it's entirety) into every web page that you want to track. Paste it immediately 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
<head>
section.
将您的片段(未更改,完整)粘贴到您要跟踪的每个网页中。将其粘贴在结束
</head>
标记之前 。如果您的网站使用模板生成页面,请在包含该
<head>
部分的文件中的结束标记之前输入它。
回答by Bart
I agree with the accepted answer. I would like to add that I noticed Google Webmaster Tools (GWT) requires the tracking code (asynchroneous version) to be in the head to be able to register as admin for your site via an existing Google Analytics registration for the same site. So if the JS tracking code is not in the <head>
, this registration option is not available.
我同意接受的答案。我想补充一点,我注意到 Google 网站管理员工具 (GWT) 需要跟踪代码(异步版本)才能通过同一站点的现有 Google Analytics 注册为您的站点注册为管理员。因此,如果 JS 跟踪代码不在 中<head>
,则此注册选项不可用。
This might be Google's way of coercing more websites to do this. Perhaps Google can keep better track of precisely those visitors who cancel during page load. These might make interesting Analytics for them to optimize their search results (e.g. maintain 'early-bounce' events for their statistics).
这可能是谷歌强迫更多网站这样做的方式。也许谷歌可以更好地跟踪那些在页面加载期间取消的访问者。这些可能会为他们提供有趣的分析,以优化他们的搜索结果(例如,为他们的统计数据维护“早期反弹”事件)。
回答by Headshota
you can put it before </body>
I don't think there would be any problems with that.
你可以把它放在</body>
我认为不会有任何问题之前。
回答by Faraona
Implementing the code
Once you find the code snippet, copy and paste it into the bottom of your content, immediately before the </body>
tag of each page you are planning to track. If you use a common include or template, you can enter it there. To implement tracking code for secure pages (e.g. https://), please read How do I obtain tracking code for secure pages?
实施代码 找到代码片段后,将其复制并粘贴到内容底部,紧接在</body>
您计划跟踪的每个页面的标记之前。如果您使用通用的包含或模板,您可以在那里输入。要实现安全页面的跟踪代码(例如 https://),请阅读如何获取安全页面的跟踪代码?
回答by Hitesh
I had the script in body section just before ending body tag and it was always showing that my site is missing tracking code analytics admin panel so I moved to head section and it works fine.
我在结束 body 标签之前在 body 部分有脚本,它总是显示我的网站缺少跟踪代码分析管理面板,所以我移到 head 部分并且它工作正常。