加载并执行传统(同步)Google Analytics 跟踪代码片段作为第一个 DOM 元素

发布于 2024-11-27 03:21:19 字数 1059 浏览 5 评论 0原文

Google 表示,当使用传统(同步)ga.js 跟踪代码片段时,您应该确保它作为 DOM 的最后一个元素执行。

通过将脚本放置在页面主体的末尾,可以确保跟踪代码作为 DOM 的最后一个元素执行。如果给定页面加载由于某种原因被中断,则该页面视图的 GIF 请求可能不会被执行。但是,如果您将跟踪代码放置在页面顶部,则任何加载中断都可能导致报告不完整或不准确,因为跟踪代码依赖于其报告的页面数据。

http://code.google.com/apis/analytics /docs/tracking/gaTrackingOverview.html

这是真的吗?跟踪代码如何依赖其报告的页面数据?在初始 DOM 加载(大量)之后我们添加到 DOM 的所有内容都会被跟踪。那么 Google 到底指的是哪种类型的页面数据呢?

是的,我知道有一个异步片段。但我正在尝试一种将代码片段(如下)作为 DOM 中的第一个元素运行的方法。

<script src="http://www.google-analytics.com" >
<script>
  try{
    var pageTracker = _gat._getTracker("UA-xxxxxx-x");
    pageTracker._trackPageview();
  } catch(err) {}
</script>

这会以何种方式扰乱我的页面和事件跟踪?此外,异步跟踪代码在执行之前不会等待 DOM 加载。那么,跟踪代码真的依赖于页面数据吗?

更新:感谢您的回答,马蒂亚斯!因此,如果我错了,请纠正我,但如果我将跟踪代码放在头部,唯一可能发生的事情是,当页面加载中途中止时,我可能会得到不准确的报告。

否则,如果页面完全加载,跟踪就能完美工作吗?

因此,这就是他们所说的“因为跟踪代码依赖于其报告的页面数据”。

Google says that when using the traditional (synchronous) ga.js tracking code snippet you should make sure it's executed as the last element of the DOM. 

By placing the script at the end of the page body, you ensure that the tracking code is executed as the last element of the DOM. If a given page load is interrupted for some reason, it is possible that the GIF request for that page view will not be executed. However, should you place the tracking code at the top of the page, any load interruptions might result in incomplete or inaccurate reporting anyhow, since the tracking code relies on page data for its reports.

http://code.google.com/apis/analytics/docs/tracking/gaTrackingOverview.html

Is this really true? How does the tracking code rely on page data for it's report? Everything we add to the DOM after the initial DOM load (alot) gets tracked. So exactly what type of page data are Google referring to?

Yes, I know there's an asynchronous snippet. But I'm experimenting with a thing that would benefit from running the snippet (below) as the first element in the DOM. 

<script src="http://www.google-analytics.com" >
<script>
  try{
    var pageTracker = _gat._getTracker("UA-xxxxxx-x");
    pageTracker._trackPageview();
  } catch(err) {}
</script>

In what way could this mess up my page and event tracking? Also the asynchronous tracking code doesn't wait for the DOM to load before it's executed. So, again, does the tracking code really rely on page data?

UPDATE: Thanks for the answer, Mathias! So correct me if I'm wrong, but the only thing that can happend if I put the tracking code in the head is that I might get inaccurate reports when a page load is aborted halfway through.

Otherwise, if the page is loaded completely, the tracking will work perfectly?

So, that's all they mean with "since the tracking code relies on page data for its reports."

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

∝单色的世界 2024-12-04 03:21:19

通过将脚本放置在页面主体的末尾,可以确保跟踪代码作为 DOM 的最后一个元素执行。如果给定页面加载由于某种原因被中断,则该页面视图的 GIF 请求可能不会被执行。

他们的意思是说,如果页面加载中途中止(网络/服务器问题,用户在页面完全加载之前导航离开页面,或者用户单击“停止”按钮等)并且 GA 片段接近结束 < code> 标签,很可能它还没有被加载,所以它永远不会被执行。 GA 不会注册页面视图。

但是,如果您将跟踪代码放置在页面顶部,则任何加载中断都可能导致报告不完整或不准确,因为跟踪代码依赖于其报告的页面数据。

如果将 GA 代码段放在 中,它将更快加载并执行。但是,这也意味着即使页面未完全加载,它也会注册“访问”(因此“不准确”)。

这是我更喜欢将脚本放在 附近的部分原因。 /body>。这也是我们在 HTML5 Boilerplate 中这样做的原因。

PS 请使用(优化的)异步 Google Analytics 代码段,而不是旧的/阻塞的跟踪代码。它总是更好的选择。

By placing the script at the end of the page body, you ensure that the tracking code is executed as the last element of the DOM. If a given page load is interrupted for some reason, it is possible that the GIF request for that page view will not be executed.

They mean to say that if page loading is aborted halfway through (network/server issue, the user navigates away from the pages before it’s fully loaded, or the user clicks the Stop button, etc.) and the GA snippet is near the closing </body> tag, chances are that it won’t be loaded yet so it will never be executed. GA won’t register the page view.

However, should you place the tracking code at the top of the page, any load interruptions might result in incomplete or inaccurate reporting anyhow, since the tracking code relies on page data for its reports.

If you place the GA snippet in the <head>, it will be loaded and executed sooner. However, this also means that it will register a “visit” even if the page isn’t completely loaded (hence, “inaccurate”).

This is part of the reason why I prefer to keep the script near </body>. It’s also why we do it this way in the HTML5 Boilerplate.

P.S. Please use the (optimized) asynchronous Google Analytics snippet instead of the old/blocking tracking code. It’s always the better option.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文