Deconstructing the Google Analytics tracking script
billfranklin.svbtle.com
billfranklin.svbtle.com
* @param {Window} i The global context object.
* @param {HTMLDocument} s The DOM document object.
* @param {string} o Must be 'script'.
* @param {string} g Protocol relative URL of the analytics.js script.
* @param {string} r Global name of analytics object. Defaults to 'ga'.
* @param {HTMLElement} a Async script tag.
* @param {HTMLElement} m First script tag in document.
It might be easier to read the code if they used different names, but any good refactoring tool will replace them with your own names without any problems. !function(k,a,n,y,e){k.GoogleAnalyticsObject=n;k[n]||(k[n]=function(){
(k[n].q=k[n].q||[]).push(arguments)});k[n].l=+new Date;y=a.createElement('script');
e=a.scripts[0];y.src='//www.google-analytics.com/analytics.js';
e.parentNode.insertBefore(y,e)}(window,document,'ga');
ga('create', 'UA-34495711-11', 'auto');
ga('send', 'pageview'); (function(h,o,t,j,a,r){
h.hj=h.hj||function(){(h.hj.q=h.hj.q||[]).push(arguments)};
h._hjSettings={hjid:1,hjsv:5};
a=o.getElementsByTagName('head')[0];
r=o.createElement('script');r.async=1;
r.src=t+h._hjSettings.hjid+j+h._hjSettings.hjsv;
a.appendChild(r);
})(window,document,'//static.hotjar.com/c/hotjar-','.js?sv=');
https://docs.hotjar.com/v1.0/docs/understanding-the-tracking...I believe that Google set a trend, 'HotJar' is a newbie on the block. If anyone can find an example that predates 'urchin.js' (or wherever this started) please share!
In the 80s and early 90s my mother was an interior designer at a high end furniture store. There were no prices on the floor models because most things could be customized (fabric, arm shape, wood, etc) before delivery. However there was a base price that the designer could use to (I guess) keep people within their budget.
They used a ten-letter isogram to print the base price covertly on the furniture labels so that associates could figure it out without going to the back office and looking it up in the computer.
The last word I remember them using was Cumberland. C=0, d=9. So if the base price for a dining room table was $15,840 the tag would have urnec written on it somewhere.
Web version: https://isogrammer.com/
Lots of questions, I would love to understand how GA are capturing events that are sometimes trapped and not allowed to bubble, etc. Are they capturing events at document.body level, etc? What type of IDs do they use to bring sessions together, do the ID events similarly? Do they queue up events to batch them or do they trickle them in. How do they deliver events when unloading a page, etc.
Nowadays, connecting Sessions and Users are handled by storing an ID in a first-party cookie named _ga. There is no such thing as an Event ID in Google Analytics, unless you implement one manually in a Custom Dimension. The Event report has Total Event and Unique Event metrics, and it's up to the user to choose whether they want to pay attention to deduplicated Events or not.
On the web, hits are not batched, they are handled as they come in (except for a throttling limit). The Android and iOS SDK batch hits by default.
On pageload, queued hits can be sent with the "beacon" protocol, which is a HTML standard that exists specifically to solve this exact problem. In browsers that do not use the beacon protocol, the hit is simply dropped.
The "protocol" is just a deferred HTTP POST request that does not block page unload. Please use this as it is widely supported and provides better UX (page is not blocked by sync XHR).
GA does not store any data that persists from page to page other than the client ID cookie.
Unless I'm misremembering, something that I found interesting was that the cookie wasn't randomized, it was a fingerprint based on a large number of params.
https://deedpolloffice.com/blog/articles/decoding-gclid-para...
The others (segment, heap) use explicit functions for different actions. Ex: heap.init()
The main benefit is you can detect API violations without loading the async script. If you call an invalid function with the former you have to wait for it to execute at some point in the future.
This allows you to surface errors immediately - breaking current exection - even if it's additional libraries havent loaded.
https://developers.google.com/analytics/devguides/collection...
"Used to collect offline / latent hits. The value represents the time delta (in milliseconds) between when the hit being reported occurred and the time the hit was sent. The value must be greater than or equal to 0. Values greater than four hours may lead to hits not being processed."
Would be nice if that delta time could be greater than 24 hours, eg. a human approves that a low-volume contact form submission isn't spam and that hit is tied to a GA goal.
I like mine better :)
i opened the article and "oh, really?"
For all those here mentioning that he's only "deconstructed" the snippet, and not the actual main linked analytics script, you're the first to observe that he's deconstructed the snippet incorrectly.
Don't get me wrong, it's really great and heartening to see someone new to JS getting back to basics and trying to understand things in proper minute detail (instead of doing another tutorial about a boilerplate React app), but this is hardly HN front-page material? It's a very simple JS snippet.
Edit: found it. Gets the timestamp[1]
1. https://stackoverflow.com/questions/24182317/multiplication-...
https://github.com/vesparny/fair-analytics
Hope it comes along; there is surprisingly not much else in the way of Node/JS based open source web stats/analytics solutions.
I actually much prefer the Piwik interface, I don't need to dig through masses of menu options to find the data I actually care about. Not helping the Google panopticon is a bonus.
1. Has a large footprint (won't run on tiny instances) 2. Relies on blockable user side JS.
Is there something like that that just analyzes server logs?
But Piwik can use methods other than JavaScript in the browser, including importing server-side logs [1].
For me, Google Analytics privacy issue is that it's used everywhere - Google can track users across a fair chunk of the web. Your site running its own private Piwik install? Fill yer boots, not a problem.
<script>_gaq=[['_setAccount','UA-????????-?'],['_trackPageview']]</script><script async src=//ssl.google-analytics.com/ga.js></script>
With analytics.js: <script>ga={q:[['create','UA-????????-?','auto'],['send','pageview']],l:+new Date}</script><script async src=//www.google-analytics.com/analytics.js></script>Examples:
· Hackers Make $5M a Day by Faking 300M Video Views | https://news.ycombinator.com/item?id=13219871 (6 months ago)
· Uncovering an advertising fraud scheme | https://news.ycombinator.com/item?id=2333824 (6 years ago)
Discussions:
· Alleged $7.5B fraud in online advertising | https://news.ycombinator.com/item?id=9796102 (2 years ago)
· Inside Google's Secret War Against Ad Fraud | https://news.ycombinator.com/item?id=9628967 (2 years ago)
I thought that was a kind of recursive joke (not necessarily a very witty one) : if they want to make the parameters spell out a word, the word has to be an isogram, otherwise you'd define a parameter twice. And since "isogram" is an isogram, maybe the temptation was too great..
Precisely. A trick to reduce the character count, nothing more. Love doing this kind of thing (http://gabrielgambetta.com/tiny_raytracer.html).
> Making a.async truthy ensures
"Truthy"???
Many bugs have been caused by incorrect uses of truthy and falsy.
Example: (function(i, s, o, g, r, a, m) {...}) -> (function(y, c, o, m, b, i) {...})
(Originally noted in some old article that I can't find right now.)
(references to undefined variables `a` and `m` in some sort of IIIFE)
It's not a pejorative, it's sarcasm. You could replace "Sherlock" with "This genius" in that sentence for essentially same effect.
I'm not really sure why, but for some reason "Sherlock" has become very common to use in a sarcastic context like that. Kinda like how "fat chance" and "slim chance" now mean basically the same thing due to "fat chance" almost always being used sarcastically.
that's a nice line and describes some very high karma HN denizens.
for more on async loading techniques, this is a great blog post.
http://blog.errorception.com/2011/08/reliable-high-performan...