谷歌异步加载执行js_异步Google Analytics

谷歌异步加载执行js

It seems like every website these days uses Google Analytics. And why not? It's an outstanding tool that gives you any and every statistic you could ever want. The problem with Google Analytics is that it's been, well, slow loading at times. That's why I was so pumped when I saw Google was releasing an async version of their script.

如今,似乎每个网站都使用Google Analytics(分析)。 那么为何不? 这是一款出色的工具,可为您提供任何想要的统计信息。 Google Analytics(分析)的问题在于,有时加载速度一直很慢。 这就是为什么当我看到Google发布其脚本的异步版本时,我非常激动的原因。

Google Analytics JavaScript (Google Analytics JavaScript)


<script type="text/javascript">
	var _gaq = _gaq || [];
	_gaq.push(['_setAccount', 'UA-#######-#']); //your code here
	_gaq.push(['_trackPageview']);
</script>
<script type="text/javascript">
(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';
	(document.getElementsByTagName('head')[0] || document.getElementsByTagName('body')[0]).appendChild(ga);
})();
</script>


The one thing you'll notice is the async attribute being set to true. Obviously you'll want to use your own Analytics code within the top portion of the JavaScript. You'll also want to put this at the top of the BODY. What a difference the async version can make -- my website seems to load faster due to this upgrade!

您会注意到的一件事是async属性设置为true 。 显然,您希望在JavaScript的顶部使用自己的Google Analytics(分析)代码。 您还希望将其放在BODY的顶部。 异步版本可以带来什么不同-由于此升级,我的网站加载速度似乎更快!

翻译自: https://davidwalsh.name/async-google-analytics

谷歌异步加载执行js

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值