問題描述

在 wordpress 中插入 Google Analytics(分析) 代碼的最佳位置在哪裏?頁眉或頁腳?我更喜歡頁腳,因為我希望我的網站通過減少頭文件中的腳本數來加載更快,但即使腳本在頁腳中也可以工作?

最佳解決方案

Google 曾經建議將它放在</body> 標籤之前,因為它們為 ga.js 提供的原始方法是阻止的。然而,較新的異步語法可以安全地放在頭部,同時最小的阻塞,因此當前的建議就在</head> 標籤之前。

<head> 會增加一點延遲; 在頁腳中會減少以一定的小幅度記錄的網頁瀏覽量。這是一個權衡。 ga.js 被大量緩存並存在於整個網絡上的大量網站上,因此它通常從緩存中提供,從而將延遲降至幾乎為零。

作為個人喜好,我喜歡將其包含在<head> 中,但它真的是一個偏好的問題。

次佳解決方案

As google says

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

參考文獻

注:本文內容整合自 Google/Baidu/Bing 輔助翻譯的英文資料結果。如果您對結果不滿意,可以加入我們改善翻譯效果:薇曉朵技術論壇。