views:

50

answers:

1

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>
+2  A: 

The first script block adds ga.js into a script tag. That script gathers data about you and your browser, collecting information from where you came from (referrer information), where you're located, etc. All of this is collected on every hit.

The script creates a global _gat object and calls methods to that object. Some methods (like trackPageview) make a _utm.gif request every time they're loaded. (A _utm.gif request means that the script requests a 1x1 invisible gif file from Google's servers. Each request has all of the information appended at the end of it. Google's servers use the information about that request to process your data

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

http://www.google-analytics.com/__utm.gif?utmwv=4.7.2&amp;utmn=909339250&amp;utmhn=stackoverflow.com&amp;utmcs=UTF-8&amp;utmsr=1920x1080&amp;utmsc=24-bit&amp;utmul=en-us&amp;utmje=1&amp;utmfl=10.1%20r82&amp;utmdt=Newest%20'google-analytics'%20Questions%20-%20Stack%20Overflow&amp;utmhid=456625342&amp;utmr=0&amp;utmp=%2Fquestions%2Ftagged%2Fgoogle-analytics&amp;utmac=UA-5620270-1&amp;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&amp;gaq=1

So, if you installed this script at the bottom of every page, every time someone loads a page, the script will embed, read the previous 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.

yc
Thanks, that was informative. One question about deducing time, what if you go to other site it doesn't have Google Analytics instead of about.html?
Mark K
This blog post from the Google Analytics Evangelist Avinash Kaushik goes into that: http://www.kaushik.net/avinash/2008/01/standard-metrics-revisited-time-on-page-and-time-on-site.html (The short answer is that the last page you visit on the site is counted as a 0.)
yc