Track offline events with Google Analytics

I tried to track user activity on my site, for example, a click or mouse, as well as various events. Is there a solution to monitor events, even when users are working offline ... Can I store them as cookies and send them to the server when searching for an active Internet connection?

Is it possible?

thanks

+8
javascript google-analytics google-analytics-api
source share
5 answers

Depending on what types of browsers you configure. Are they for standalone HTML5 web applications?

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

Some potentially workable code using standalone events, local JSON storage, and HTML5:

 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 just use _ogaq as a wrapper for _gaq .

t

 _ogaq.push(["_trackEvent","Category", "Action"]); 
+14
source share

Another thing to keep in mind is that the time that will be recorded will be the time the data was sent, and not collected locally on the device. Fortunately, however, there is a way to avoid this 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 tried this in real time, and it appears at a time that was recorded as expected.

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

Understand this old question, but this weekend it turns me on.

+3
source share

You may find this helpful. ononline / offline is not as reliable as you might think.

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

+2
source share

according to this article , offline behavior is supported offline. you just need to add this library

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

Not working for me, so I found out that pressing the GA button does not work correctly. So I looped through the array, clicked each entry in 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 = ""; }); 
0
source share

All Articles