英文:
Track user activity while taking into account when the user opens in a new tab
问题
JavaScript
我想要追踪用户在我的网站上的活动情况(活跃或非活跃),并希望将此信息存储在我的数据库
中。我决定使用activity-detector库来确实追踪用户活动,然而如果用户在两个标签页中打开了相同的网页,那么我将得到不准确的信息,那么我应该如何考虑到用户可以在两个标签页中打开相同的网页这一事实呢?
英文:
Using JavaScript
I want to track the user activity (active or inactive) on my website and I'd like to store this info in my database
. I decided to use the activity-detector library which does indeed track the user activity, however if the user has the same webpage opened in 2 tabs then I'm going to end up with inaccurate information so how would I take into account the fact that the user can open the same webpage in 2 tabs?
答案1
得分: 4
使用`localStorage`和`self.window.name`的组合可以确定是否有其他窗口/选项卡由同一用户使用。
if (!localStorage.getItem('windowName')) {
// 第一次访问
self.window.name = (new Date()).getTime();
localStorage.setItem('windowName', self.window.name);
}
if (localStorage.getItem('windowName') && localStorage.getItem('windowName') !== self.window.name) {
// 我认识你,但这是一个不同的窗口
console.log('新窗口');
// 做一些事情,解绑,删除,... 你需要做的任何事情
self.window.name = (new Date()).getTime();
}
英文:
With the combination of localStorage
and self.window.name
you can determine whether other windows/tabs are being used by the same user.
if (!localStorage.getItem('windowName')) {
// first visit
self.window.name = (new Date()).getTime();
localStorage.setItem('windowName', self.window.name);
}
if (localStorage.getItem('windowName') && localStorage.getItem('windowName') !== self.window.name) {
// i know you, but it's a different window
console.log('new window'):
// do stuff, unbind, delete, ... whatever you need to do
self.window.name = (new Date()).getTime();
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论