我已经集成了现场时间库,以便将用户在站点上花费的时间存储在MySQL数据库中。我正在使用下面的代码来实现同样的目标。
然而,,这些数据并不是存储在IOS设备中,比如iPhone或iPad,而是可以在所有其他浏览器中工作,比如Chrome、Edge、Opera、Firefox等等,包括Android和firefox。
var Tos;
(function(d, s, id, file) {
var js, fjs = d.getElementsByTagName(s)[0];
if (d.getElementById(id)) return;
js = d.createElement(s);
js.id = id;
js.onload = function() {
var config = {
trackBy: 'seconds',
callback: function(data) {
console.log(data);
// give your endpoint URL server-side URL
// that is going to handle your TOS data which is of POST method.
// Eg. PHP, nodejs or python URL which saves this data to your DB
// replace with your endpoint URL
var endPointUrl = 'http://localhost:4500/tos';
if (data && data.trackingType) {
if (data.trackingType == 'tos') {
if (Tos.verifyData(data) != 'valid') {
console.log('Data abolished!');
return;
}
}
// make use of sendBeacon if this API is supported by your browser.
// sendBeacon is experimental technology; it may not work well
if (navigator && typeof navigator.sendBeacon === 'function') {
var blob = new Blob([JSON.stringify(data)], {
type: 'application/json'
});
navigator.sendBeacon(endPointUrl, blob);
}
}
}
};
if (TimeOnSiteTracker) {
Tos = new TimeOnSiteTracker(config);
}
};
js.src = file;
fjs.parentNode.insertBefore(js, fjs);
}(document, 'script', 'TimeOnSiteTracker', 'https://cdnjs.cloudflare.com/ajax/libs/timeonsite/1.2.0/timeonsitetracker.js'));这个问题的原因是什么,如何解决?
发布于 2022-06-13 07:39:57
"NO“
这里给出了理由(摘自https://github.com/saleemkce/timeonsite/issues/9)
Iphone、Ipad和移动safari浏览器等IOS设备根本不支持“卸载前”事件。仅对于这些设备,在启动跟踪器时,我们必须检查Navigator.platform,暂时回到"request /localStorage“。这是一种双赢的情况,因为我们可以实时跟踪所有设备,但IOS设备除外,我们可以安全地应用“请求对象选项”,并在稍后获得最后的记录。
由于上述原因,实时API仅在IOS设备中失败,但好的是,您可以使用request选项以延迟方式捕获数据。浏览一次链接,以便更好地理解它。但问题仍然是,如果我在IOS设备上使用request选项,如何为桌面和移动设备中的所有其他浏览器捕获实时信息。调整很简单,请替换
var config用这个对象,
// checking for IOS based browser or not
var isIOS = (/iPad|iPhone|iPod/i.test(navigator.platform)) && !window.MSStream;
if (!isIOS) { /* All browsers desktop and mobile except IOS devices */
var config = {
// track page by seconds. Default tracking is by milliseconds
trackBy: 'seconds',
callback: function(data) { /* callback denotes your data tracking is real-time */
console.log(data);
if (data && data.trackingType) {
if (data.trackingType == 'tos') {
if (Tos.verifyData(data) != 'valid') {
console.log('Data abolished!');
return;
}
}
// make use of sendBeacon if this API is supported by your browser.
if (navigator && typeof navigator.sendBeacon === 'function') {
data.trasferredWith = 'sendBeacon';
var blob = new Blob([JSON.stringify(data)], {type : 'application/json'});
navigator.sendBeacon(endPointUrl, blob);
}
}
}
};
} else { /* For IOS devices only since "beforeunload" event is not supported */
var config = {
// track page by seconds. Default tracking is by milliseconds
trackBy: 'seconds',
request: { /* Presence of request object denotes we're going to use Localstorage */
url: endPointUrl
}
};
}现在,您将能够以两种方式捕获数据。IOS设备的延迟浏览(N1)和移动和桌面中所有其他浏览器的实时(N)页面浏览。
https://stackoverflow.com/questions/70617698
复制相似问题