If Google Analytics goes down, how do I keep my site working?

前端 未结 4 519
傲寒
傲寒 2020-12-25 08:43

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

相关标签:
4条回答
  • 2020-12-25 09:25

    Trying to fix something that isn't broken

    If you want to use Google analytics you are going to need to communicate with there server some how, period. There service, as pointed out by many of the other answers, run asynchronously meaning it doesn't prevent your site from running what so ever. The 'loading visuals' are there to show something is loading... there is very little you can do about this.

    You have two options; put up with Google analytics or use some server side script to run analytics.

    If the website is hanging it is because of something else.

    Edit

    Also not including GA immediately also reduces the accuracy of the stats; bounce rates in particular will not be correct.

    0 讨论(0)
  • 2020-12-25 09:27

    You could host a copy of www.google-analytics.com/ga.js on your own server and load it from there. If you do go that route, I'd set up a cron job to periodically fetch ga.js to make sure you've got the most recent version.

    Regarding tracking code placement: one of the main reasons for placing the tracking code earlier in the page (before </head>) has to do with visitors who leave the page quickly (possibly before the entire page has rendered). In that case, you're more likely to collect some data the sooner your tracking code is executed.

    0 讨论(0)
  • 2020-12-25 09:42

    Latest Approach :

    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>
      

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

    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

    0 讨论(0)
  • 2020-12-25 09:44

    Given where it's loaded, the rest of your site would load before the request to Google Analytics. Plus GA is loaded asynchronously, not halting the execution of any of your code, so I really don't see there being an issue.

    0 讨论(0)
提交回复
热议问题