Unfortunately, this may not be a valid Code-Golf question as it is likely Javascript only; however, since this is likely to be the only useful-in-the-real-world code-golf contest I'm going to go ahead and post it.
The Google Analytics Asyncronous Tracking snippet is used by many websites.
The script goes a little something like this:
<script type="text/javascript">
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);
})();
</script>
Winner will be determined by the shortest RAW DEFLATE (there is 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.
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.
Some Rules:
- The
gaq || []
check is not required and should be removed. - must be protocol "aware" (http vs https).
- must not pollute the global namespace (except for the
_gaq
var). - must be copy-pastable to any (X)HTML document, i.e., not dependent on the page's markup.
- must work in all A-Grade browsers.
- This does NOT have to pass JSLINT or any HTML validators.
- must set the
async
flag. - must use this deflator for the byte count of the deflate-compressed output.
Tip:
- Understand the basics of the DEFLATE algorithm. And more importantly, LZ77 compression.
UDPATE 216/275
Since my original version has been beaten I'll go ahead and post it here:
Note: this has a bug where async gets set to false for all "http" requests
(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")
First try, credit for ideas to everybody here.
200/253 (I don't know how to optimize for deflate... the only thing I did was change the variable names)
195/258 Would not using (document,"script") help?
For the smallest possible code, you don't even need the "ssl." and "www." prefixes. This still fits within the competition regulations...
175/216
174/224 And the second and third optimizations combined...
Edit: using the regex /s:/ now, and more...
194/266 Apparently reusing _gaq helps...
166/229 and without "ssl" and "www"...
183/223 (17.94%) -- async, protocol aware, no namespace pollution
185/228 (18.86%) -- async, protocol aware,
nonamespace pollution (with thed
ande
variables)186/233 (20.17%) -- async, protocol aware, no namespace pollution
Works on Chrome, Firefox, IE 7+, Opera and Safari.
Updated in accordance with updated rules:
Including the
<script>
tags: Deflated Length is: 226 bytes. Original length is: 298 bytes.Without the
<script>
tags: Deflated Length is: 216 bytes. Original length is: 281 bytes.Throwing in @some's ideas (only checking
location[4]
and using1
fortrue
) saves 4 more bytes (212), but I can't take credit for that.(1) first attempt
(2) (1) + some's 4th
Updated with versions tested in FF3.6, Opera10, Chrome6, MSIE8:
194/270: with async, with getElementsByTagName cached
192/297: with async, no cache
189/259: no async, with getElementsByTagName cached
188/286: no async, no cache
184/242, no async, appendChild (no cache needed), unknown if it's supported everywhere
Credits:
type="text/javascript"
Also, changing
'
to"
may improve compression in your HTML source if you use"
" to quote tag attributes.See comments on this post for more information
Since this post now is community wiki and the accepted answer, I removed my first attempts (you can find them in the revision history if you are interested) and only have the latest revisions visible. See the comments on this post for more information. /some
This is probably my best attempt, considering my inexperience with Javascript. Not much going on here:
EDIT
Fixed the bugs @some talked about.
Deflated length: 219 bytes
Original length: 285 bytes