插入谷歌分析代码的最佳位置[duplicate]

时间:2021-08-27 15:49:46

This question already has an answer here:

这个问题已经有了答案:

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、header或footer中插入谷歌分析代码的最佳位置是哪里?我更喜欢页脚,因为我希望我的站点通过减少页眉中的脚本数量来更快地加载,但是即使脚本在页脚中,它也能工作吗?

4 个解决方案

#1


219  

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.

谷歌曾建议将其放在标签之前,因为它们提供了用于加载ga的原始方法。js是阻塞。但是,新的异步语法可以安全地在最小阻塞的情况下放在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.

会增加一些延迟;在页脚中,将减少在一些小范围内记录的页面浏览量。这是一个权衡。ga。js被大量缓存,并且在web上的大部分站点上都有,因此它通常是由缓存提供的,从而将延迟降低到几乎为零。

As a matter of personal preference, I like to include it in the <head>, but its really a matter of preference.

作为个人偏好的问题,我喜欢把它包含在中,但它实际上是一个偏好的问题。

#2


29  

As google says:

谷歌说:

Paste it into your web page, just before the closing </head> tag.

粘贴到您的web页面,就在关闭标记之前。

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

异步代码片段的一个主要优点是可以将其放置在HTML文档的顶部。这增加了跟踪信标在用户离开页面之前发送的可能性。通常在小节中放置JavaScript代码,我们建议将代码片段放在小节的底部以获得最佳性能

#3


2  

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代码放在页脚中,因为在读取所有标记之前,页面不应该算作页面访问。

#4


2  

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
});

#1


219  

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.

谷歌曾建议将其放在标签之前,因为它们提供了用于加载ga的原始方法。js是阻塞。但是,新的异步语法可以安全地在最小阻塞的情况下放在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.

会增加一些延迟;在页脚中,将减少在一些小范围内记录的页面浏览量。这是一个权衡。ga。js被大量缓存,并且在web上的大部分站点上都有,因此它通常是由缓存提供的,从而将延迟降低到几乎为零。

As a matter of personal preference, I like to include it in the <head>, but its really a matter of preference.

作为个人偏好的问题,我喜欢把它包含在中,但它实际上是一个偏好的问题。

#2


29  

As google says:

谷歌说:

Paste it into your web page, just before the closing </head> tag.

粘贴到您的web页面,就在关闭标记之前。

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

异步代码片段的一个主要优点是可以将其放置在HTML文档的顶部。这增加了跟踪信标在用户离开页面之前发送的可能性。通常在小节中放置JavaScript代码,我们建议将代码片段放在小节的底部以获得最佳性能

#3


2  

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代码放在页脚中,因为在读取所有标记之前,页面不应该算作页面访问。

#4


2  

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
});