异步谷歌分析 [Javascript Golf] [英] Async Google Analytics [Javascript Golf]
问题描述
不幸的是,这可能不是一个有效的 Code-Golf 问题,因为它可能只是 Javascript;然而,由于 这可能是唯一在现实世界中有用的代码高尔夫比赛 我将继续发布它.em>
Google Analytics 异步跟踪 片段被许多人使用网站.
The Google Analytics Asyncronous Tracking snippet is used by many websites.
脚本有点像这样:
<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 之间存在差异)按字节计数确定,这些代码将加载和初始化 Async Google Analytics在一个页面上.
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.
一些规则:
gaq ||[]
检查不是必需的,应该被删除.- 必须感知"协议(http 与 https).
- 不得污染全局命名空间(
_gaq
变量除外). - 必须可复制粘贴到任何 (X)HTML 文档,即不依赖于页面的标记.
- 必须适用于所有 A 级浏览器.
- 这不需要必须通过 JSLINT 或任何 HTML 验证器.
- 必须设置
async
标志. - 必须使用 这个 deflator 来计算 deflate 的字节数-压缩输出.
- 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.
提示:
- 了解DEFLATE 算法的基础知识.更重要的是,LZ77 压缩.
- Understand the basics of the DEFLATE algorithm. And more importantly, LZ77 compression.
由于我的原始版本已被击败,我将继续在此处发布:
注意:这有一个错误,所有http"请求的 async 都设置为 false
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")
推荐答案
更新了在 FF3.6、Opera10、Chrome6、MSIE8 中测试的版本:
Updated with versions tested in FF3.6, Opera10, Chrome6, MSIE8:
194/270:使用异步,使用缓存的 getElementsByTagName
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:使用异步,无缓存
(_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
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:无异步,无缓存
(_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,无异步,appendChild(无需缓存),不知道是否到处都支持
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']]
信用:
- casablanca:/^https/.test(location)
- matyr:相对路径、语句之间的逗号、异步赋值
- 一些:没有匿名函数和_gaq的使用,getElementsByTagName的非缓存,异步的移动分配,/^....s/
- 大卫·默多克 drop
type="text/javascript"
- 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"
此外,如果您使用 "
" 引用标签属性,将 '
更改为 "
可能会提高 HTML 源代码的压缩率.
Also, changing '
to "
may improve compression in your HTML source if you use "
" to quote tag attributes.
查看此帖子的评论以了解更多信息
由于这篇文章现在是社区维基和公认的答案,我删除了我的第一次尝试(如果您感兴趣,可以在修订历史中找到它们)并且只显示最新修订.有关更多信息,请参阅此帖子的评论./一些
这篇关于异步谷歌分析 [Javascript Golf]的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!