Google Analytics 应该放在 HTML 页面的头部还是底部? [英] Should Google Analytics go in the head or bottom of an HTML page?
问题描述
Google 建议在关闭 </head>
之前放置 google 分析脚本.
Google advises putting the google analytics script right before closing the </head>
.
但是,我更愿意将它与我的 javascript 的其余部分结合在一起,这些 javascript 现在都在一个缓存的外部文件中,该文件加载在我的 HTML 文件的底部.我可以按我的方式做吗?如果是这样,那么我要冒什么风险/将下面的代码放在 HTML 的底部而不是头部的成本是多少?
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?
<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>
推荐答案
你真的没有冒任何风险.建议将 标签放在
的末尾,因为在执行它包含的 Javascript 时具有阻塞性质.由于 GA 的 Javascript 是动态插入的,因此是非阻塞的,你可以把它放在任何地方.
You're risking nothing really. Putting <script>
tags at the end of the <body>
is recommendable because of the blocking nature while executing the Javascript it encloses. Since the Javascript for GA is dynamically inserted and therefore non-blocking, you can put it anywhere.
我猜谷歌看到的唯一问题"是,当将该代码放在您网站的底部时,您可能没有发现访问者/页面加载在完成之前取消请求.如果有人调用您的 HTML 文档并取消请求,则可能不会遇到 GA 代码(因此不会执行).
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).
这篇关于Google Analytics 应该放在 HTML 页面的头部还是底部?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!