How does the embedded Google Analytics Javascript work?

大城市里の小女人 提交于 2019-12-18 11:12:03

问题


I was just wondering the general idea how the embedded Google Analytics Javascript works? Example, how do they calculate how long you been visiting the site? Does the embedded Javascript calls home every time someone visit a site?... I just need to know the big picture

Thanks.

Edit: how does the following work?

<script type="text/javascript">
var gaJsHost = (("https:" == document.location.protocol) ? "https://ssl." : "http://www.");
document.write(unescape("%3Cscript src='" + gaJsHost + "google-analytics.com/ga.js' type='text/javascript'%3E%3C/script%3E"));
</script>
<script type="text/javascript">
var pageTracker = _gat._getTracker("UA-xxxxxx-x");
pageTracker._trackPageview();
</script>

回答1:


The first script block creates a script tag, which downloads ga.js from Google's servers. That script gathers data about the webpage, you, and your browser, collecting information from where you came from (referrer information), etc. All of this is collected on every hit.

The script creates a global _gat object. Some methods on _gat (like _trackPageview) make a _utm.gif request to Google's server every time they're loaded.

The request itself is how Google gets your estimated location (via the requesting IP address) and your browser (via your user agent string, sent in the request headers). A _utm.gif request means that the script requests a 1x1 transparent gif file from Google's servers. Each request has all of the information appended within the query string. Google's servers process their request logs and use the information about that request to process the data and reconstruct the session based on the hits.

That request, for example on stackoverflow.com, looks like this:

http://www.google-analytics.com/__utm.gif?utmwv=4.7.2&utmn=909339250&utmhn=stackoverflow.com&utmcs=UTF-8&utmsr=1920x1080&utmsc=24-bit&utmul=en-us&utmje=1&utmfl=10.1%20r82&utmdt=Newest%20'google-analytics'%20Questions%20-%20Stack%20Overflow&utmhid=456625342&utmr=0&utmp=%2Fquestions%2Ftagged%2Fgoogle-analytics&utmac=UA-5620270-1&utmcc=__utma%3D140029553.1672509655.1273785261.1282328140.1282335818.167%3B%2B__utmz%3D140029553.1282158995.159.95.utmcsr%3Dgoogle%7Cutmccn%3D(organic)%7Cutmcmd%3Dorganic%7Cutmctr%3Dforce%2520download%2520image%2520in%2520php%2520stackoverflow%3B&gaq=1

If you installed this script at the bottom of every page, every time someone loads a page the script will embed, download ga.js (which is likely to be cached), read the previous cookies (utm prefixed cookies), and send the updated information to Google's servers (via the _utm.gif request).

The calculation of time on site is pretty piecemeal; it deduces time on page based on your browsing.

So, if you load index.html at 12:00:00 and send a _utm.gif hit to Google's server, and then at 12:01:30, you load about.html, it deduces that you've spent 1:30 on index.html. This also means that most sessions will have a 0 second time on page for the final pageview of the session.



来源:https://stackoverflow.com/questions/3534524/how-does-the-embedded-google-analytics-javascript-work

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!