First of all, I will pass this through a decorator, for example. http://jsbeautifier.org/
(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');
After that, let's evaluate the closure
(function (i, s, o, g, r, a, m) { ... })(window, document, 'script', '//www.google-analytics.com/analytics.js', 'ga');
replacing each of the named parameters: i, s, o, g, r with the corresponding values ββof window, document, 'script', '//www.google-analytics.com/analytics.js', 'ga'
Note that the parameters a and m have no input values ββand are more like result variables.
This will be approximately (not to worry about the scope of the variable, etc.) equivalent
(function (i, s, o, g, r, a, m) { window['GoogleAnalyticsObject'] = 'ga'; window['ga'] = window['ga'] || function () { (window['ga'].q = window['ga'].q || []).push(arguments) }, window['ga'].l = 1 * new Date(); a = document.createElement('script'), m = document.getElementsByTagName('script')[0]; a.async = 1; a.src = '//www.google-analytics.com/analytics.js'; 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');
In short, this code essentially consists in creating a new script tag with a line:
a = document.createElement('script'),
Then finds the first script tag
m = document.getElementsByTagName('script')[0];
He then sets up a new script tag for asynchronous execution (more information on async execution can be obtained in Understanding Asynchronous Code in Layman Terms , if You Need It)
a.async = 1;
The 1 in the line above is equivalent to true , it is used by 1 only because it is shorter.
After this src of this script tag is installed
a.src = '//www.google-analytics.com/analytics.js';
Please note that the protocol is not specified above in the protocol (http or https). This will load the script into the current browser protocol.
And finally, it is inserted before the first script tag, so the browser can start downloading it.
m.parentNode.insertBefore(a, m)
So, to summarize :
- We create a script tag
- We set it for asynchronous loading
async=true - We insert this script tag before the first script tag in the document
Features related to google analytics.
window['ga'] = window['ga'] || function () { (window['ga'].q = window['ga'].q || []).push(arguments) }, window['ga'].l = 1 * new Date();
defines a global function called ga that pushes its arguments into an Array queue (named q )
Then with the lines
ga('create', 'UA-xxxxxxxx-x', 'xxxxxx.com'); ga('send', 'pageview');
he pushes these "events" in the queue array.
When the script loads, it checks the GoogleAnalyticsObject value that was previously set to specify the ga name with the line
window['GoogleAnalyticsObject'] = 'ga';
Hope this helps