Check if analytics.js is loaded

后端 未结 3 354
梦如初夏
梦如初夏 2020-12-07 02:08

I have to use a local analytics.js that I serve from my server. I just want to use the local version if necessary - so is there a solution for checking if the call for analy

3条回答
  •  慢半拍i
    慢半拍i (楼主)
    2020-12-07 03:00

    There's a function to track if the library has loaded. From the docs:

    ga(function(tracker) {
       var defaultPage = tracker.get('page');
    });
    

    The passed in function is executed when the library is loaded, so you could set a variable to keep track of whether or not it has loaded. You'd have to put it on some sort of timer to decide when you want to consider it failed:

    var loaded = false;
    ga(function() {
       loaded = true;
    });
    
    // after one second do something if the library hasn't loaded
    setTimeout(function(){
        if (!loaded){
            //do something
        }
    },1000);
    

提交回复
热议问题