Note that there are some explanatory texts on larger screens.

plurals
  1. POAsync Google Analytics [Javascript Golf]
    text
    copied!<p><em>Unfortunately, this may not be a valid Code-Golf question as it is likely Javascript only; however, since</em> <strong>this is likely to be the only useful-in-the-real-world code-golf contest</strong> <em>I'm going to go ahead and post it.</em></p> <hr> <p>The <a href="http://code.google.com/apis/analytics/docs/tracking/asyncTracking.html" rel="noreferrer">Google Analytics Asyncronous Tracking</a> snippet is used by many websites.</p> <p>The script goes a little something like this:</p> <pre><code>&lt;script type="text/javascript"&gt; var _gaq = _gaq || []; _gaq.push(['_setAccount', 'UA-XXXXX-X']); _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); })(); &lt;/script&gt; </code></pre> <p>Winner will be determined by the shortest RAW DEFLATE (there <em>is</em> a difference between HTTP 1.1 DEFLATE (aka zlib) and RAW DEFLATE) compressed code by byte-count that will load and initialize Async Google Analytics on a page.</p> <p>In the case of a tie, winner will be determined by raw character count. If it we still have a tie we'll decide by last edit/time submitted.</p> <p>Some Rules:</p> <ul> <li><strong>The <code>gaq || []</code> check is not required and <em>should</em> be removed.</strong></li> <li>must be protocol "aware" (http vs https).</li> <li>must not pollute the global namespace (except for the <code>_gaq</code> var).</li> <li>must be copy-pastable to any (X)HTML document, i.e., not dependent on the page's markup.</li> <li>must work in all <a href="http://developer.yahoo.com/yui/articles/gbs/" rel="noreferrer">A-Grade browsers</a>.</li> <li>This does <strong>NOT</strong> have to pass JSLINT or any HTML validators.</li> <li>must set the <code>async</code> flag.</li> <li>must use <a href="http://www.vervestudios.co/projects/compression-tests/snippet-deflator" rel="noreferrer">this deflator</a> for the byte count of the deflate-compressed output.</li> </ul> <p>Tip:</p> <ul> <li>Understand the basics of the <a href="http://zlib.net/feldspar.html" rel="noreferrer">DEFLATE algorithm</a>. And more importantly, LZ77 compression.</li> </ul> <hr> <hr> <h2>UDPATE 216/275</h2> <p>Since my original version has been beaten I'll go ahead and post it here:<br> <strong>Note: this has a bug where async gets set to false for all "http" requests</strong></p> <pre><code>(function(d,t,g){_gaq=[["_setAccount","UA-XXXXX-X"],["_trackPageview"]];g=d.createElement(t);g.src=(g.async=location.protocol[5]?"//ssl":"//www")+".google-analytics.com/ga.js";(t=d.getElementsByTagName(t)[0]).parentNode.insertBefore(g,t)})(document,"script") </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload