简体   繁体   English

使用Google Analytics跟踪离线活动

[英]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? 我试图跟踪我的网站中的用户活动,例如点击或鼠标悬停以及不同类型的事件....是否有任何解决方案来跟踪事件,即使用户正在脱机工作......我可以将它们存储在像cookie这样的东西中并发送找到活动的互联网连接时,他们到服务器?

Is that possible? 那可能吗?

Thank you 谢谢

Depends on what browser types you're targeting. 取决于您要定位的浏览器类型。 Are these for HTML5 offline webapps? 这些是HTML5离线webapps吗?

If they support ononline and onoffline events , you can implement this yourself fairly trivially. 如果他们支持ononlineonoffline事件 ,你可以相当平凡地自己实现。

Some potentially workable code, using offline events, native JSON and HTML5 Local Storage: 一些可能可行的代码,使用脱机事件,本机JSON和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 would just use _ogaq as a wrapper for _gaq . 然后你只需使用_ogaq作为_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. 幸运的是,现在有一种方法可以避免使用测量协议和qt参数(队列时间)这一问题,它允许您以毫秒为单位发送事件/视图等的年龄。 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 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. ononline / offline并不像人们想象的那么可靠。

http://www.google.com/support/forum/p/Google%20Analytics/thread?tid=67e8cf894a003c64&hl=en 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. 没有为我工作,所以我发现,阵列的GA推送无法正常工作。 So i looped the array, pushed each entry to Google Analytics. 所以我循环播放了数组,将每个条目推送到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 = "";
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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