1

我使用分析在我的瀏覽器擴展程序是這樣開始啓用Chrome擴展分析時,互聯網是不可用的擴展

background.js

// Standard Google Universal Analytics code 
(function(i,s,o,g,r,a,m)  
{i['GoogleAnalyticsObject']=r;i[r]=i[r]||function(){ 
(i[r].q=i[r].q||[]).push(arguments)},i[r].l=1*new  
Date();a=s.createElement(o), 
m=s.getElementsByTagName(o) 
[0];a.async=1;a.src=g;m.parentNode.insertBefore(a,m) 
})(window,document,'script','https://www.google- 
analytics.com/analytics.js','ga'); 

ga('create', 'UA-XXXXX-YY', 'auto'); 
ga('set', 'checkProtocolTask', function(){}); // Removes failing protocol 
ga('require', 'displayfeatures'); 

了所有必要的權限manifest.json中

除了在擴展開始時沒有互聯網的情況外,一切都按預期工作。在這些情況下,事件不會作爲analytics.js發送到服務器。這會導致事件丟失,直到下一次成功連接Internet的Chrome重啓。

是否有解決這個問題的方法。

回答

0

找到這樣

function loadAnalyticsScript() { 
    // The standard Google Universal Analytics code 
    (function(i,s,o,g,r,a,m){i['GoogleAnalyticsObject']=r;i[r]=i[r]||function(){ 
    (i[r].q=i[r].q||[]).push(arguments)},i[r].l=1*new Date();a=s.createElement(o), 
    m=s.getElementsByTagName(o)[0];a.async=1;a.src=g;m.parentNode.insertBefore(a,m) 
    })(window,document,'script','https://www.google-analytics.com/analytics.js','ga'); // Note: https protocol here 
} 

var analyticsLoaded = false; // initialise 

loadAnalyticsScript(); 


/************Detect if analytics script has loaded **********/ 

// Method 1 src:https://davidsimpson.me/2014/05/27/add-googles-universal-analytics-tracking-chrome-extension/#comment-190946 

ga(function(){ 
    // This function will be triggered when GA is successfully loaded for the first time. 
    console.log(' +++ Google Analytics has loaded'); 
    analyticsLoaded = true; 
}); 

// Alternative Method 2 src:https://www.domsammut.com/code/workaround-for-when-the-hitcallback-function-does-not-receive-a-response-analytics-js 
if (ga.hasOwnProperty('loaded') && ga.loaded === true) { 

    console.log(' +++ Google Analytics has loaded'); 
    analyticsLoaded = true; 

} 

/*************************************************/ 

// All the events tracking goes here 
ga('create', 'UA-XXXXX-YY', 'auto'); 
ga('set', 'checkProtocolTask', function(){}); // Removes failing protocol check. @see: http://stackoverflow.com/a/22152353/1958200 
ga('require', 'displayfeatures'); 
ga('send', 'pageview', '/options.html'); 

// Test to see if it's loaded yet. 
var gaLoadTimer = setInterval(function(){ 

    if (analyticsLoaded === false) { 
     console.log("Retrying for GA script"); 
     loadAnalyticsScript(); //try again 
    } 
    else{ 
     clearInterval(gaLoadTimer); 
    } 
}, 20000); // every 20s 
+1

注意使用20s'setInterval',如果你使用Event頁面('「persistent」:false'),它會失敗。 – Xan

2

使用XMLHttpRequest來測試https://www.google-analytics.com/analytics.js是否可到達。如果不是,請使用chrome.alarms API(最好)或setInterval創建一個計時器,它將重複此測試。一旦網站可以訪問,初始化GA。

+0

致謝優雅的方式爲answer.Also發現的替代,以檢查的analytics.js是否加載https://www.domsammut.com/code/workaround-for-當hitcallback-function-does-not-receive-a-response-analytics-js – mallik1055

+0

@ mallik1055時,其他方法值得作爲與源文章鏈接的答案呈現。您是否願意添加它,甚至可以選擇作爲解決方案? – wOxxOm