如果谷歌Analytics(分析)出现故障,我该如何让我的网站的工作? [英] If Google Analytics goes down, how do I keep my site working?

查看:127
本文介绍了如果谷歌Analytics(分析)出现故障,我该如何让我的网站的工作?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

确定这是2013年1月19日22:30 NZST和大多数互联网似乎已经走到了一个抓取的谷歌Analytics(分析)似乎运行很慢。计算器,Firefox.com,Reddit和谷歌搜索是缓慢的。最重要的是我,我的生产经营性网站运行缓慢或没有加载在所有。不,这不只是我的连接我测试了我的手机,3G也是如此。网站没有谷歌Analytics(分析)似乎运行正常。

Ok it's 19 Jan 2013 22:30 NZST and most of the internet seems to have come to a crawl as Google Analytics seems to be running really slow. Stackoverflow, Firefox.com, reddit and google searches are slow. Most importantly for me, my production business website is running slow or not loading at all. No it's not just my connection I tested it on my phone with 3G as well. Sites without Google Analytics seemed to be running fine.

下面会发生什么

这是在Firefox窗口的左下角。它会坐在那里20多秒之类的。我想它消失后3秒,如果它不能连接。

This is in the bottom left corner of the Firefox window. It will sit there for 20+ seconds like that. I'd like it to go away after 3 seconds if it can't connect.

这纺绿色形象是在Firefox的标签,只是坐在那里使它看起来像的页面仍在加载了20多秒。我想它消失后3秒,如果它不能连接。

This spinning green image is in the Firefox tab and just sits there making it look like the page is still loading for 20+ seconds. I'd like it to go away after 3 seconds if it can't connect.

现在它可能不是谷歌分析,我国的国际化门户可能运行缓慢或什么的。但有证据强烈显示这可能是谷歌Analytics(分析)。现在,即使它不是谷歌Analytics(分析),然后我还是很想在一些方法来对付它,如果服务是完全下来。因此,假设让我们假设有一个巨大的火在谷歌Analytics(分析)的数据中心和不合格消防燮pression系统。现在,谷歌Analytics(分析)去完全脱机了好几天。没有备份服务器。无备用数据中心。假设的情况确定。现在,我的网站还需要跑,我不能有我的网站依靠谷歌分析服务,往上冲。但是,分析功能将是件好事,作为一个额外的奖金,如果该服务被及时实际运行。

Now it may not be Google Analytics, my country' international gateway may be running slow or something. But evidence strongly suggests it might be Google Analytics. Now even if it isn't Google Analytics then I'd still be interested in some methods to counter it if the service is completely down. So hypothetically lets assume there's a massive fire at the datacenter of Google Analytics and the fire suppression systems failed. Now Google Analytics goes completely offline for several days. No backup servers. No standby datacenters. Hypothetical scenario ok. Now my site still needs to run as I can't afford to have my site reliant on Google Analytics service being up. But the analytics functionality would be good to have as an added bonus if the service is actually running in a timely manner.

好了,所以我抛出一些想法在这里:

Ok so I'm throwing out some ideas here:

  1. 有一个超时,我可以添加到我的脚本,将取消连接到谷歌Analytics(分析)和暂停请求/下载,如果它的时间太长?然后,它会继续在2秒钟后装入自己的网站。
  2. 或者更好的,也许它可以只是完全载入我的网站,然后使用Ajax之后,我的网站是完全完成加载发送一个请求关闭谷歌分析?默认情况下,为什么没有这样做呢?

这里的code,我们必须努力与当前插入只是之前结束< / BODY> 标记

Here's the code we have to work with which is currently inserted just before the closing </body> tag.

<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>

现在,什么是一些方法,我可以使用,将修正这个假设的灾难场景,给我的网站连续性,同时谷歌Analytics(分析)是跌?我感兴趣的任何潜在的软件或硬件解决方案。假设我有充分的机会到我的PHP / MySQL / HTML 5网页运行在Linux VPS。

Now, what are some methods I could employ that would fix this hypothetical disaster scenario and give my website continuity while Google Analytics is down? I'm interested in either potential software or hardware solutions. Assume I have full access to a Linux VPS which my PHP/MySQL/HTML5 website is running on.

另外,你对这个权威的答案:有的人说的地方结束&LT前的code; /头&GT; 标记。还有人说,把它放在结束&LT之前; /身体GT; 标记。这是最好的方法是什么?

Also, what's the authoritative answer on this: some people say place the code before the closing </head> tag. Others say place it before the closing </body> tag. Which is the best way?

感谢

解决方案更新

Solution Update

好,我找到了什么工作与Jaspal帮助。该解决方案是以下

Ok I've found out what works with help from Jaspal. The solution is below.

<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>

基本上,为什么它的工作原理是因为有一个setTimeout的5秒。这使我的网页足够的时间来加载所有内容,JS,CSS,图片等,然后之后,它揭开序幕$就请求和下载的ga.js和__utm.gif这实际上是传送数据到谷歌Analytics(分析)。这最初的5秒钟后,基本上所有的浏览器的视觉效果我前面提到的消失,谷歌Analytics(分析)请求发生在后台默默无负载的视觉效果为浏览器用户。然后我试图阻止谷歌Analytics(分析)与主机文件,我仍然没有得到任何浏览器的视觉效果 - 完美

Basically why it works is because there is a setTimeout for 5 seconds. This gives my page enough time to load all the content, JS, CSS, images etc. Then after that it kicks off the $.ajax request and downloads ga.js and __utm.gif which is actually sending the data to Google Analytics. After that initial 5 seconds, basically all the browser visuals I mentioned earlier disappear and the Google Analytics request happens silently in the background with no loading visuals for the browser user. Then I tried blocking Google Analytics with the host file and I still don't get any browser visuals - perfect.

应当指出的是,在超时:5000 $阿贾克斯的要求似乎并不做任何事情。本来我希望它会中止请求,如果它不能获取数据,持续5秒,但有一记中的API文档说:

It should be noted that the timeout: 5000 property in the $.ajax request doesn't appear to do anything. Originally I was hoping it would abort the request if it couldn't get data for 5 seconds but there's a note in the API docs saying

在Firefox 3.0+只,脚本和JSONP请求不能被取消   通过超时;如果它在超时后到达该脚本将仍然运行   期。

In Firefox 3.0+ only, script and JSONP requests cannot be cancelled by a timeout; the script will run even if it arrives after the timeout period.

我会离开它在那里反正以防万一。从我的测试,如果谷歌Analytics(分析)不能达成(从观察网络/网络小组在Firebug /浏览器),那么它将中止21-23秒,在Firefox和16秒后,Chrome浏览器的要求。这可能是一些TCP超时。我并不担心无论如何,因为它选择的时机没有任何提示,用户不会注意到,因为没有加载的视觉效果在浏览器中。

I'll leave it in there anyway just in case. From my testing if Google Analytics can't be reached (from observing the Net/Network panel in Firebug/Chrome) then it will abort the request after 21-23 seconds in Firefox and 16 seconds in Chrome. This may be some TCP timeout. I'm not worried about that anyway as it's timing out silently and the user will not notice as there's no loading visuals in the browser.

我已经接受了低于Jaspal的答案,并为他颁发了奖金作为他的解决方案是解决这个关键。然而,他的解决方案仍然出现加载浏览器中的视觉效果。所以我相信张贴在这里使用的setTimeout(他原来的略有修改)的解决方案是要走的路,并经检验工作100%,对我来说。

I have accepted Jaspal's answer below and awarded him the bounty as his solution was critical to solving this. However his solution still has loading visuals appearing in the browser. So I believe the solution posted here using the setTimeout (a slight modification on his original one) is the way to go and is tested to be working 100% for me.

推荐答案

最新方法:

  1. 我们让GA正常加载。
  2. 我们存储的引用,GA DOM元素在一个全局变量(gaClone)。
  3. 我们设置30秒的超时。我们还设置另一个全局变量(loadedGoogleAnalytics),并将其设置为0开始。当GA负荷,我们将此变量设置为1。在超时到期后,我们检查GA是否加载与否。如果不是这样,我们删除的DOM元素GA。

  1. We allow ga to load normally.
  2. We store a reference to ga dom element in a global variable (gaClone).
  3. We set a timeout of 30seconds. We also set another global variable (loadedGoogleAnalytics) and set it to 0 initially. When ga loads, we set this variable to 1. On the timeout expiry, we check whether ga was loaded or not. If not, we delete the dom element ga.

<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>

[我已经验证了这种方法与我的实际GA的一个帐户,我能够看到所有的实时跟踪]

[I have verified this approach with one of my actual GA accounts and i am able to see all the realtime tracking]

previous方法 [注:嘎不执行,如果我们试图将它放在(文件)。就绪();] [此解决方案不起作用]

Previous Approach [Note: Ga does not execute if we try to place it in (document).ready();] [This solution does not work]

<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>

希望这有助于

Hope this helps

这篇关于如果谷歌Analytics(分析)出现故障,我该如何让我的网站的工作?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆