Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How does the embedded Google Analytics Javascript work?

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>
like image 625
Mark K Avatar asked Aug 20 '10 20:08

Mark K


People also ask

How does Google Analytics work with JavaScript?

How does Google Analytics work? Google Analytics acquires user data from each website visitor through the use of page tags. A JavaScript page tag is inserted into the code of each page. This tag runs in the web browser of each visitor, collecting data and sending it to one of Google's data collection servers.

How do I integrate Google Analytics into JavaScript?

Install the Google tag Replace GA_TRACKING_ID with the ID of the Google Analytics property to which you want to send data. You need only one snippet per page. This snippet loads the gtag. js library, establishes GA_TRACKING_ID as the default Google Analytics property ID, and sends a pageview hit to Google Analytics.

Can you embed Google Analytics?

The Google Analytics Embed API is a JavaScript library that allows you to easily create and embed a dashboard on a 3rd party website in a matter of minutes. It gives you a set of pluggable components that can work together to build complex tools, making it both simple and powerful at the same time.


1 Answers

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.

like image 158
Yahel Avatar answered Oct 04 '22 13:10

Yahel