开发者

Loading and executing the traditional (synchronous) Google Analytics tracking code snippet as the first DOM element

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._track开发者_StackOverflowPageview();
  } 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."


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜