Is it just me, or is Google Analytics not that needed ?
How does google analytics collect its data?
ELI5: Google Analytics. How does it work and what is the information used for?
ELI5: Google Analytics
First you set up an account for Google Analytics. You give your name, your domain (your website address), your emails, and whatever else.
Analytics then gives you some code. All you have to do is copy this code (it's pretty short) onto the beginning of your website, in your "header" which is typically loaded at the beginning of every page. If you like, you could load it only on certain pages.
When someone goes to your website, your browser downloads the entire page like usual, including the Analytics code.
What the Analytics code actually does is simply record things that your browser tells the web pages anyway. Things like location (country, city), language, browser (Internet Explorer, Firefox, Chrome, etc), operating system (Windows, Mac OSX, etc), screen resolution, and many other things that browsers always tell every website they go to. This info is required for the web to function correctly. All Analytics does is record it and present it to you with graphs and charts.
Edit: here is what the actual code looks like. It's in a language called JavaScript.
var _gaq = _gaq || [];
_gaq.push(['_setAccount', '**********']);
_gaq.push(['_trackPageview']);
(function() {
var ga = document.createElement('script'); ga.type = 'text/javascript'; ga.async = true;
ga.src = ('https:' == document.location.protocol ? 'https://ssl' : 'http://www') + '.google-analytics.com/ga.js';
var s = document.getElementsByTagName('script')[0]; s.parentNode.insertBefore(ga, s);
})();I censored the account number, though (line 2). This code "includes" code that is hosted (saved on) the Google servers. So, every time you visit the page, the browser comes across this, reads the code on the Google servers (really quickly, of course) where Google records the info, then finishes loading the page.
More on reddit.comWhat does Google Analytics measure?
Is Google Analytics free?
What is Google Analytics in SEO?
Videos
Most Analytics tools (e.g. Google Analytics...) are non-compliant to GDPR, and for others, you would have to show a cookie consent banner that negatively impacts the user experience.
Adding to that the fact that you would depend on a third party which may impact the performance of your web pages.
So, why do we insist on capturing users' data through these analytics third party when we can simply set up a log visualization tool at the server level, and get the important data from log files (number of unique visitors, viewed pages, referrers, user-agent...) ?
Maybe Analytics tools have the benefit of providing detailed metrics, but these would only be useful if your business depends on user behavior tracking, which isn't the case for many websites out there.
Am I missing something here?
When html page makes a request for a ga.js file the http protocol sends big amount of data, about IP, refer, browers, language, system. There is no need to use ajax.
But still some data cant be achieved this way, so GA script puts image into html with additional parameters, take a look at this example:
http://www.google-analytics.com/__utm.gif?utmwv=4.3&utmn=1464271798&utmhn=www.example.com&utmcs=UTF-8&utmsr=1920x1200&utmsc=32-bit&utmul=en-us&utmje=1&utmfl=10.0%20r22&utmdt=Page title&utmhid=1805038256&utmr=0&utmp=/&utmac=cookie value
This is a blank image, sometimes called a tracking pixel, that GA puts into HTML.
Some good answers here which individually tend to hit on one method or another for sending the data. There's a valuable reference which I feel is missing from the above answers, though, and covers all the methods.
Google refers to the different methods of sending data 'transport mechanisms'
From the Analytics.js documentation Google mentions the three main transport mechanisms that it uses to send data.
This specifies the transport mechanism with which hits will be sent. The options are 'beacon', 'xhr', or 'image'. By default, analytics.js will try to figure out the best method based on the hit size and browser capabilities. If you specify 'beacon' and the user's browser does not support the
navigator.sendBeaconmethod, it will fall back to 'image' or 'xhr' depending on hit size.
- One of the common and standard ways to send some of the data to Google (which is shown in Thinker's answer) is by adding the data as GET parameters to a tracking pixel. This would fall under the category which Google calls an 'image' transport.
- Secondly, Google can use the 'beacon' transport method if the client's browser supports it. This is often my preferred method because it will attempt to send the information immediately. Or in Google's words:
This is useful in cases where you wish to track an event just before a user navigates away from your site, without delaying the navigation.
- The 'xhr' transport mechanism is the third way that Google Analytics can send data back home, and the particular transport mechanism that is used can depend on things such as the size of the hit. (I'm not sure what other factors go into GA deciding the optimal transport mechanism to use)
In case you are curious how to force GA into using a specific transport mechanism, here is a sample code snippet which forces this event hit to be sent as a 'beacon':
ga('send', 'event', 'click', 'download-me', {transport: 'beacon'});
Hope this helps.
Also, if you are curious about this topic because you'd like to capture and send this data to your own site too, I recommend creating a binding to Google Analytics' send, which allows you to grab the payload and AJAX it to your own server.
ga(function(tracker) {
// Grab a reference to the default sendHitTask function.
originalSendHitTask = tracker.get('sendHitTask');
// Modifies sendHitTask to send a copy of the request to a local server after
// sending the normal request to www.google-analytics.com/collect.
tracker.set('sendHitTask', function(model) {
var payload = model.get('hitPayload');
originalSendHitTask(model);
var xhr = new XMLHttpRequest();
xhr.open('POST', '/index.php?task=mycollect', true);
xhr.send(payload);
});
});