Can someone explain this code 'step by step','line by line'? I would like to learn more about Asynch code and how Google loads their script, how to 'hide' javascrippt from users (I know that I can't hide it but at least make it something like Google does, not to show all code in one file)
<script>
(function(i,s,o,g,r,a,m){i['GoogleAnalyticsObject']=r;i[r]=i[r]||function(){
(i[r].q=i[r].q||[]).push(arguments)},i[r].l=1*new Date();a=s.createElement(o),
m=s.getElementsByTagName(o)[0];a.async=1;a.src=g;m.parentNode.insertBefore(a,m)
})(window,document,'script','//www.google-analytics.com/analytics.js','ga');
ga('create', 'UA-xxxxxxxx-x', 'xxxxxx.com');
ga('send', 'pageview');
</script>
i['GoogleAnalyticsObject']=r;
This is assigning 'ga' to the 'GoogleAnalyticsObject' property of 'window'This part is assigning the 'ga' property of window as a function (or itself if it already exists). The function then assigns the q property as an empty array and adds all of the functions arguments to it. It then assigns the l property the current timestamp (it's multiplied by 1 to force it as an integer).
The next lines just make a script tag and assign it some attributes such as source and async = true and then it adds this script tag to the document.
The code has been run through a minifier, and looks like this when pretty printed:
To know what it exactly does, you'd probably have to take a look at their analytics.js file, but as that's very likely to be minified as well, you're not going to get much out of it.
If you want to do the same thing, you could use a code minifier like JSMin. It replaces any unnessecary whitespace and newline characters, among other things, to help reduce bandwidth.
First of all, I would pass this through a beautifier, e.g. http://jsbeautifier.org/
After that lets evaluate the closure
by replacing each of the named parameters:
i, s, o, g, r
with their corresponding valueswindow, document, 'script', '//www.google-analytics.com/analytics.js', 'ga'
Note that
a
andm
parameters do not have input values and are more like result variables.This would be roughly (not bothering about variable scope, etc.) equivalent to
In short what this code does in essence, is that it creates a new script tag with the line:
Then finds the first script tag
Then it sets the newly created script tag to asynchronous execution (More insight on async execution could be obtained at Understanding Asynchronous Code in Layman's terms should you need it)
1 in the line above is equivalent to
true
, it is used 1 just because it is shorter.After that the src of this script tag is set
Note that above no protocol (http or https) is specified in the URL. This would allow for the script to be loaded in the current browser protocol.
And finally it is inserted before the first script tag, so the browser could start loading it.
So to summarize:
async=true
Specifics related to google analytics.
defines global function named
ga
that pushes its arguments in a queue Array (namedq
)Then with the lines
it pushes these "events" in the queue Array.
When the script is loaded, it checks the value of
GoogleAnalyticsObject
, which earlier was set to point to the name ofga
with the lineHope this helps
Google has published the un-minified version of this code:
https://developers.google.com/analytics/devguides/collection/analyticsjs/tracking-snippet-reference
Zlatin's line by line explanation is still valid.
The code is minified. Using http://jsbeautifier.org/ you can revert that (sort off) and make it a bit more readable. Basically It's a small function that adds another javascript (www.google-analytics.com/analytics.js) to the dom using the same protocol, http or https.