How to minify JavaScript like Google Analytics? : <p>Most of you are probably familiar with this little tracking code offered by Google Analytics.</p>

<pre><code>&lt;script&gt;
(
    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','https://www.google-analytics.com/analytics.js','ga');

ga('create', 'UA-00000000-0', 'auto');
ga('send', 'pageview');
&lt;/script&gt;
</code></pre>

<p>The interesting part is that this little excerpt contains arguments that form the word isogram. This script also uses arguments to declare variables to shave off some bits from the final file size. Arguably, you would not use this pattern when writing the code (?), so my question is, how does Google minify its code and are these techniques available to mere mortals, too?</p>

<p>I found online this <a href="http://code.stephenmorley.org/javascript/understanding-the-google-analytics-tracking-code/" rel="noreferrer">example</a> by Stephen Morley that includes a code that looks like something you'd write before minifying it. I took that code and run it through Google's very own <a href="https://closure-compiler.appspot.com/home" rel="noreferrer">Closure Compiler</a> on advanced optimisation. As expected, the resulting code is nothing like the actual script used by Google Analytics.</p>

<pre><code>(function(){window.b="ga";"ga"in window||(window.a=function(){window.a.q.push(arguments)},window.a.q=[]);window.a.c=(new Date).getTime();var c=document.createElement("script");c.src="//www.google-analytics.com/analytics.js";c.async=!0;var d=document.getElementsByTagName("script")[0];d.parentNode.insertBefore(c,d)})();
</code></pre>

<p>This time, the code is less DRY and bigger, even without the two extra commands.</p>

<p>So to clarify, I am curious how the Google engineers arrived to the above result (I do not think their code actually looks like the one in Stephen's example), and could this process be replicated even if you are not a part of Google? Thank you in advance!</p>
