Javascript 如果Google Analytics宕机,我如何保持我的网站正常运行?
好的,现在是2013年1月19日NZST 22:30,由于谷歌分析运行速度非常慢,互联网的大部分内容似乎都在爬行。Stackoverflow、Firefox.com、reddit和google搜索速度较慢。对我来说最重要的是,我的生产业务网站运行缓慢或根本没有加载。不,这不仅仅是我的连接,我还用3G手机测试了它。没有谷歌分析的网站似乎运行良好 下面是一些发生的情况的屏幕截图 这是在Firefox窗口的左下角。它将在那里像那样放置20多秒。如果无法连接,我希望它在3秒钟后消失 这张旋转的绿色图片位于Firefox选项卡中,它看起来就像页面仍在加载20多秒。如果无法连接,我希望它在3秒钟后消失 现在可能不是谷歌分析,我国的国际门户可能运行缓慢或其他什么。但有证据强烈表明,这可能是谷歌分析。现在,即使不是谷歌分析,如果服务完全关闭,我仍然会对一些方法感兴趣。假设Google Analytics的数据中心发生大火,灭火系统失灵。现在Google Analytics已经完全离线好几天了。没有备份服务器。没有备用数据中心。假设情景ok。现在,我的网站仍然需要运行,因为我负担不起我的网站依赖谷歌分析服务的费用。但是,如果该服务能够及时运行,那么分析功能将是一个很好的补充 好的,我在这里提出一些想法:Javascript 如果Google Analytics宕机,我如何保持我的网站正常运行?,javascript,jquery,ajax,asynchronous,google-analytics,Javascript,Jquery,Ajax,Asynchronous,Google Analytics,好的,现在是2013年1月19日NZST 22:30,由于谷歌分析运行速度非常慢,互联网的大部分内容似乎都在爬行。Stackoverflow、Firefox.com、reddit和google搜索速度较慢。对我来说最重要的是,我的生产业务网站运行缓慢或根本没有加载。不,这不仅仅是我的连接,我还用3G手机测试了它。没有谷歌分析的网站似乎运行良好 下面是一些发生的情况的屏幕截图 这是在Firefox窗口的左下角。它将在那里像那样放置20多秒。如果无法连接,我希望它在3秒钟后消失 这张旋转的绿色图片
<script type="text/javascript">
var _gaq = _gaq || [];
_gaq.push(['_setAccount', 'UA-123456789-1']);
_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>
var _gaq=_gaq | |[];
_gaq.push([''设置帐户','UA-123456789-1']);
_gaq.push([''u trackPageview']);
(功能(){
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);
})();
现在,我可以采用哪些方法来修复这个假设的灾难场景,并在Google Analytics关闭时保持我的网站的连续性?我对潜在的软件或硬件解决方案感兴趣。假设我可以完全访问运行PHP/MySQL/HTML5网站的Linux VPS
还有,权威的答案是什么:有些人说把代码放在结束标记之前。其他人则说将其放在结束标记之前。哪种方式最好
非常感谢
解决方案更新
好的,我已经找到了在Jaspal的帮助下工作的方法。解决方案如下
<script type="text/javascript">
// Load Google Analytics after my site has completely loaded
// Then when Google Analytics request is made it won't show any visuals in the browser
setTimeout(loadGoogleAnalytics, 5000);
// Setup _gaq array as global so that the code in the ga.js file can access it
var _gaq = _gaq || [];
_gaq.push(['_setAccount', 'UA-123456789-1']);
_gaq.push(['_trackPageview']);
/**
* Loads Google Analytics
*/
function loadGoogleAnalytics()
{
var srcUrl = ('https:' == document.location.protocol ? 'https://ssl' : 'http://www') + '.google-analytics.com/ga.js';
$.ajax(
{
url: srcUrl,
cache: true,
dataType: "script",
success: function(data)
{
console.log('got the script');
},
error: function()
{
console.log('failed to get the script');
},
timeout: 5000
});
};
</script>
//在我的网站完全加载后加载Google分析
//然后,当谷歌分析请求发出时,它将不会在浏览器中显示任何视觉效果
setTimeout(loadGoogleAnalytics,5000);
//将gaq数组设置为全局数组,以便ga.js文件中的代码可以访问它
var _gaq=_gaq | |[];
_gaq.push([''设置帐户','UA-123456789-1']);
_gaq.push([''u trackPageview']);
/**
*加载谷歌分析
*/
函数loadGoogleAnalytics()
{
var srcUrl=('https:'==document.location.protocol?'https://ssl' : 'http://www“)+”.google analytics.com/ga.js';
$.ajax(
{
url:srcUrl,
是的,
数据类型:“脚本”,
成功:功能(数据)
{
log('gotthescript');
},
错误:函数()
{
log('获取脚本失败');
},
超时:5000
});
};
基本上,它之所以有效是因为有一个5秒的设置超时。这使我的页面有足够的时间加载所有内容、JS、CSS、图像等。然后,它启动$.ajax请求并下载ga.JS和u_utm.gif,这实际上是将数据发送到Google Analytics。在最初的5秒钟之后,基本上我前面提到的所有浏览器视觉效果都消失了,Google Analytics请求在后台悄无声息地进行,没有为浏览器用户加载视觉效果。然后我尝试用主机文件阻止谷歌分析,但我仍然没有得到任何浏览器的视觉效果-完美
应该注意,请求中的timeout:5000
属性似乎没有任何作用。最初我希望它在5秒钟内无法获取数据时会中止请求,但API文档中有一条注释说
仅在Firefox 3.0+中,无法取消脚本和JSONP请求
超时;脚本将运行,即使它在超时后到达
句号
反正我会把它留在那里以防万一。根据我的测试,如果无法访问Google Analytics(通过观察Firebug/Chrome中的网络/网络面板),那么它将在Firefox中21-23秒和Chrome中16秒后中止请求。这可能是TCP超时。我并不担心这一点,因为它会悄悄超时,用户不会注意到,因为浏览器中没有加载视觉效果
我已经接受了贾斯帕尔下面的回答,并授予他奖金,因为他的解决方案是cr
<script type="text/javascript">
var loadedGoogleAnalytics = 0;
var gaClone;
var _gaq = _gaq || [];
_gaq.push(['_setAccount', 'UA-123456789-1']);
_gaq.push(['_trackPageview']);
_gaq.push(function() {
loadedGoogleAnalytics = 1;
//console.log('GA Actualy executed!');
});
(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);
gaClone = ga;
})();
setTimeout(function() {
//console.log('timeout fired');
if (loadedGoogleAnalytics != 1) {
gaClone.parentNode.removeChild(gaClone);
}
}, 30000);
</script>
<script type="text/javascript">
$(document).ready(function() {
var _gaq = _gaq || [];
_gaq.push(['_setAccount', 'UA-123456789-1']);
_gaq.push(['_trackPageview']);
srcUrl = ('https:' == document.location.protocol ? 'https://ssl' : 'http://www') + '.google-analytics.com/ga.js';
$.ajax({
url: srcUrl,
cache: true,
dataType: "script",
success: function(data) {
//console.log('got the script');
},
error: function() {
//console.log('failed to get the script');
},
timeout: 30000
});
});
</script>