gpt4 book ai didi

javascript - 为什么 piwik 跟踪脚本通常不直接包含在页面中?

转载 作者:塔克拉玛干 更新时间:2023-11-02 21:00:20 32 4
gpt4 key购买 nike

Piwik(分析软件),通过在 </body> 之前包含一个小脚本来工作:

<script type="text/javascript">
var _paq = _paq || [];
_paq.push(['trackPageView']);
_paq.push(['enableLinkTracking']);
(function() {
var u="//piwik.example.com";
_paq.push(['setTrackerUrl', u+'piwik.php']);
_paq.push(['setSiteId', 1]);
var d=document, g=d.createElement('script'), s=d.getElementsByTagName('script')[0];
g.type='text/javascript'; g.async=true; g.defer=true; g.src=u+'piwik.js'; s.parentNode.insertBefore(g,s);
})();
</script>

此脚本将包含 piwik.js (基本上是 this script )来自页面上的 piwik 安装。 piwik.js记录一些事情(屏幕尺寸、IP 等)并在 GET 请求中将其发送到您的 piwik 安装,以注册页面浏览量。

现在我不明白为什么你不直接在你的页面上包含 piwik.js。当您可以将它与其余脚本连接并缩小时,为什么还要从单独的位置获取它呢?

可以托管 piwik.js在 cdn 上(请参阅 herehere ),所以我想知道您为什么不跳过该步骤并将其与其余脚本连接起来并从那里进行优化?


我相信 Google 分析会做同样的事情,所以答案不需要特定于 Piwik,只要它适用于两者即可。

最佳答案

这是为了防止 piwik.js 文件阻塞页面的呈现。正如您所说,Google Analytics 提供的代码段的工作方式大致相同。史蒂夫·苏德斯 (Steve Souders) 在 http://www.stevesouders.com/blog/2009/12/01/google-analytics-goes-async/ 上写了很多好处.

另见: http://googlecode.blogspot.nl/2009/12/google-analytics-launches-asynchronous.html

The second half of the snippet provides the logic that loads the tracking code in parallel with other scripts on the page. It executes an anonymous function that dynamically creates a element and sets the source with the proper protocol. As a result, most browsers will load the tracking code in parallel with other scripts on the page, thus reducing the web page load time.

关于javascript - 为什么 piwik 跟踪脚本通常不直接包含在页面中?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27566431/

32 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com