异步Google Analytics [Javascript高尔夫] [英] Async Google Analytics [Javascript Golf]
问题描述
不幸的是,这可能不是一个有效的Code-Golf问题,因为它可能只是Javascript;然而,因为 ,这很可能是唯一有用的现实世界代码高尔夫比赛 我将继续并发布它。 em>
Google Analytics(分析)异步跟踪片段已被许多网站使用。
脚本有点像这样:
< 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>
获胜者将由最短的RAW DEFLATE决定(这里是的差异之间的HTTP 1.1 DEFLATE(又名zlib)和RAW DEFLATE)压缩的代码字节数,将加载和初始化页面上的异步谷歌分析。
在一个领带,赢家将由原始字符数决定。如果我们仍然有领带,我们会根据上次编辑/提交时间来决定。
一些规则: 提示: 由于我的原始版本已被殴打,我会继续并在此处发布: 已更新在FF3.6,Opera10,Chrome6,MSIE8中测试版本: 194/270 :with async,with getElementsByTagName cached 192/297 :带异步,无缓存 188/286 :无异步,无缓存
strong>,no async,appendChild(不需要缓存),如果在任何地方支持都是未知的
gaq ||
_gaq
var)外。
async
标志。
184/242
< h2> UDPATE 216/275
注意:这有一个错误,所有http请求的异步设置为false
$ b
(function(d,t ,克){_ 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)
(_ gaq = document.createElement(script))。src =(/ ^ .... s / .test(location)? //ssl\":\"//www\")+\".google-analytics.com/ga.js\",(_gaq.a=_gaq.async=document.getEleme ntsByTagName(script)[0])。parentNode.insertBefore(_gaq,_gaq.a),_ gaq = [[_ setAccount,UA-XXXXX-X],[_trackPageview]]
(_ gaq = document.createElement('script'))。src =(/ ^ .... s / .test(location)?'// ssl': '//www')+'.google-analytics.com/ga.js',_gaq.async=document.getElementsByTagName('script')[0].parentNode.insertBefore(_gaq,document.getElementsByTagName('script') [0]),_ gaq = [['_ setAccount','UA-XXXXX-X'],['_trackPageview']]
189/259 :无异步,getElementsByTagName缓存
(_ gaq =使用document.createElement( '脚本'))SRC =(/ ^ ... S /。测试(位置) '// SSL':? '// WWW')+ google-analytics.com/ga的.js',(_ gaq.a = document.getElementsByTagName( '脚本')[0])。parentNode.insertBefore(_gaq,_gaq.a),_ GAQ = [[ '_ setAccount', 'UA-XXXXX-X'], ['_trackPageview']]
(_ GAQ =使用document.createElement( '脚本'))SRC =(/ ^ ... S /。测试(位置) '// SSL': '// WWW')。+ google- analytics.com/ga.js',document.getElementsByTagName('script')[0].parentNode.insertBefore(_gaq,document.getElementsByTagName('script')[0]),_gaq=[['_setAccount','UA -XXXXX-X'],['_ trackPageview']]
(_ gaq = document.createElement('脚本 '))SRC =(/ ^ ... S /。测试(位置)?' // SSL ':' // WWW ')+' google-analytics.com/ga.js',document。 getElementsByTagName('script')[0] .parentNode.appendChild(_gaq),_ gaq = [['_ setAccount','UA-XXXXX-X'],['_trackPageview']]
casablanca :/^https/.test(location)
matyr :相对路径,语句之间的逗号,赋予异步
某些:无_gaq的匿名函数和用法,getElementsByTagName的非缓存,异步移动赋值,/ ^ .... s /
David Murdoch drop type =text / javascript
>
此外,将
'
更改为可能会改善HTML中的压缩如果您使用
引用标记属性,则可以使用源代码。
有关详细信息,请参阅有关此帖子的评论由于这篇文章现在是社区wiki和被接受的答案,所以我删除了我的第一次尝试(如果你是在修订历史中,你可以找到它们)有兴趣),只有最新版本可见。请参阅此帖子的评论以获取更多信息。 / 一些
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")
Updated with versions tested in FF3.6, Opera10, Chrome6, MSIE8:
194/270: with async, with getElementsByTagName cached
(_gaq=document.createElement("script")).src=(/^....s/.test(location)?"//ssl":"//www")+".google-analytics.com/ga.js",(_gaq.a=_gaq.async=document.getElementsByTagName("script")[0]).parentNode.insertBefore(_gaq,_gaq.a),_gaq=[["_setAccount","UA-XXXXX-X"],["_trackPageview"]]
192/297: with async, no cache
(_gaq=document.createElement('script')).src=(/^....s/.test(location)?'//ssl':'//www')+'.google-analytics.com/ga.js',_gaq.async=document.getElementsByTagName('script')[0].parentNode.insertBefore(_gaq,document.getElementsByTagName('script')[0]),_gaq=[['_setAccount','UA-XXXXX-X'],['_trackPageview']]
189/259: no async, with getElementsByTagName cached
(_gaq=document.createElement('script')).src=(/^....s/.test(location)?'//ssl':'//www')+'.google-analytics.com/ga.js',(_gaq.a=document.getElementsByTagName('script')[0]).parentNode.insertBefore(_gaq,_gaq.a),_gaq=[['_setAccount','UA-XXXXX-X'],['_trackPageview']]
188/286: no async, no cache
(_gaq=document.createElement('script')).src=(/^....s/.test(location)?'//ssl':'//www')+'.google-analytics.com/ga.js',document.getElementsByTagName('script')[0].parentNode.insertBefore(_gaq,document.getElementsByTagName('script')[0]),_gaq=[['_setAccount','UA-XXXXX-X'],['_trackPageview']]
184/242, no async, appendChild (no cache needed), unknown if it's supported everywhere
(_gaq=document.createElement('script')).src=(/^....s/.test(location)?'//ssl':'//www')+'.google-analytics.com/ga.js',document.getElementsByTagName('script')[0].parentNode.appendChild(_gaq),_gaq=[['_setAccount','UA-XXXXX-X'],['_trackPageview']]
Credits:
- casablanca: /^https/.test(location)
- matyr: relative path, commas between statements, assignment to async
- some: no anonymous function and usage of _gaq, non-cacheing of getElementsByTagName, move assignment of async, /^....s/
- David Murdoch drop
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
这篇关于异步Google Analytics [Javascript高尔夫]的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!