Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

like image 758
pooya Avatar asked Nov 13 '11 21:11

pooya


People also ask

Can Google Analytics track offline?

When you create the data source, select Offline event data. For offline events, your imported fields get mapped to Analytics fields automatically. After you upload your data, it can take up to 24 hours for Analytics to make that data available in reports, audiences, and explorations.

How do I track offline conversions?

Google Ads provides you with a unique ID, called Google Click ID (GCLID), for every click that comes to your website from an ad. To track offline conversions from clicks, you'll save that ID along with whatever lead information you collect from the person who clicked your ad.

What is offline event tracking?

Offline Events (or offline conversions) allow you to track when transactions occur at your store and other offline channels after people see or engage with your Facebook ads.

What Cannot be tracked in Google Analytics?

Google Analytics strictly prohibits sending Personally Identifiable Information(PII) instead it allows sending a unique user id. You are not permitted to store any personal information of any individual user like username or IP address in any custom variable but you can store a unique id for that individual user.


2 Answers

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"]);
like image 50
Yahel Avatar answered Oct 20 '22 01:10

Yahel


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.

like image 31
Tim Knight Avatar answered Oct 19 '22 23:10

Tim Knight