Javascript 插入 Google Analytics 代码的最佳位置
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/6824095/
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
Best place to insert the Google Analytics code
提问by Marky34
Where's the best place to insert the Google Analytics code in WordPress, header or footer? I prefer footer, because I wanted my site to load faster by reducing the number of scripts in the header, but can it work even if the script is in the footer?
在 WordPress、页眉或页脚中插入 Google Analytics 代码的最佳位置在哪里?我更喜欢页脚,因为我希望通过减少页眉中的脚本数量来更快地加载我的网站,但是即使脚本在页脚中它也能工作吗?
回答by Yahel
Google used to recommend putting it just before the </body>
tag, because the original method they provided for loading ga.js
was blocking. The newer async syntax, though, can safely be put in the head with minimal blockage, so the current recommendation is just before the </head>
tag.
谷歌过去建议把它放在</body>
标签之前,因为他们提供的原始加载方法ga.js
是阻塞的。不过,较新的 async 语法可以安全地以最少的阻塞放在头部,因此当前的建议就在</head>
标记之前。
<head>
will add a little latency; in the footer will reduce the number of pageviews recorded at some small margin. It's a tradeoff. ga.js
is heavily cached and present on a large percentage of sites across the web, so its often served from the cache, reducing latency to almost nil.
<head>
会增加一点延迟;在页脚中将减少以很小的幅度记录的浏览量。这是一个权衡。ga.js
被大量缓存并存在于整个网络的大部分站点上,因此它通常从缓存中提供服务,将延迟减少到几乎为零。
As a matter of personal preference, I like to include it in the <head>
, but its really a matter of preference.
作为个人偏好的问题,我喜欢将它包含在 中<head>
,但这确实是一个偏好问题。
回答by Amr
Paste it into your web page, just before the closing
</head>
tag.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
<head>
section, and we recommend placing the snippet at the bottom of the<head>
section for best performance
将其粘贴到您的网页中,就在结束
</head>
标记之前。异步代码段的主要优点之一是您可以将其放置在 HTML 文档的顶部。这增加了在用户离开页面之前发送跟踪信标的可能性。习惯上将 JavaScript 代码放在该
<head>
部分,我们建议将代码段放在该<head>
部分的底部以获得最佳性能
回答by Sparkup
If you want your scripts to load after page has been rendered, you can use:
如果您希望在页面呈现后加载脚本,您可以使用:
function getScript(a, b) {
var c = document.createElement("script");
c.src = a;
var d = document.getElementsByTagName("head")[0],
done = false;
c.onload = c.onreadystatechange = function() {
if (!done && (!this.readyState || this.readyState == "loaded" || this.readyState == "complete")) {
done = true;
b();
c.onload = c.onreadystatechange = null;
d.removeChild(c)
}
};
d.appendChild(c)
}
//call the function
getScript("http://www.google-analytics.com/ga.js", function() {
// do stuff after the script has loaded
});
回答by citizen conn
Yes, it is recommended to put the GA code in the footer anyway, as the page shouldnt count as a page visit until its read all the markup.
是的,建议无论如何都将 GA 代码放在页脚中,因为页面在读取所有标记之前不应算作页面访问。