简体   繁体   中英

Tracking offline event with Google Analytics

I tried to track user activity in my site such as click or mouse over and different kind of events.... Is there any solution to track events even when users are working offline... Can I store them in something like cookie and send them to the server when find active internet connection?

Is that possible?

Thank you

Depends on what browser types you're targeting. Are these for HTML5 offline webapps?

If they support ononline and onoffline events , you can implement this yourself fairly trivially.

Some potentially workable code, using offline events, native JSON and HTML5 Local Storage:

if(!localStorage.getItem("offlineGA")){
  localStorage.setItem("offlineGA","[]");
}
var _ogaq = {
push : function(arr){
    if(navigator.onLine || !("onLine" in navigator)){ // if online or if browser doesn't support onLine/offLine detection.
        _gaq.push(arr);
    }
    else{
     var stored = JSON.parse(localStorage.getItem("offlineGA"));
     stored.push(arr);
     localStorage.setItem("offlineGA", JSON.stringify(stored));
    }
}
};

$(window).bind("online", function(){ // if you don't have jQuery, you can do window.ononline instead
   _gaq.push( JSON.parse(localStorage.getItem("offlineGA")) );
   localStorage.setItem("offlineGA","[]"); //empty it
});

Then you would just use _ogaq as a wrapper for _gaq .

ie:

_ogaq.push(["_trackEvent","Category", "Action"]);

Another thing to consider is that the time that would get recorded would be the time the data was sent as opposed to collected locally on the device. Fortunately however there is now a way to avoid this being an issue using the measurement protocol and the qt parameter (queue time) it allows you to send the age of the event/view etc.. in milliseconds. I have tried this in the realtime viewer and it does show up at the time recorded as expected.

https://developers.google.com/analytics/devguides/collection/protocol/v1/parameters#qt

Realise it's an old question but it has been driving me bonkers this weekend.

You might find this helpful as well. ononline/offline is not as reliable as one might think.

http://www.google.com/support/forum/p/Google%20Analytics/thread?tid=67e8cf894a003c64&hl=en

according to this article , offline behaviour is supported by the google offline. you have just to add these library

    npm install --save-dev sw-offline-google-analytics
    ...
    importScripts('path/to/offline-google-analytics-import.js');
    goog.offlineGoogleAnalytics.initialize();

Did not work for me, so i found out, that the GA push of the array did not work correctly. So i looped the array, pushed each entry to Google Analytics. Now it works like a charm ...

$(window).bind("online", function(){ // if you don't have jQuery, you can do window.ononline instead

   var json = JSON.parse(localStorage.getItem("offlineGA"));

   for(var i = 0; i < json.length; i++) {    
        _ogaq.push([json[i][0],json[i][1], json[i][2]]);
    }

   localStorage.setItem("offlineGA","[]"); //empty it
   var json = "";
});

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM