跟踪用户活动,同时考虑用户在新标签页中打开的情况。

huangapple go评论68阅读模式
英文:

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(); 
}

huangapple
  • 本文由 发表于 2023年6月27日 20:01:06
  • 转载请务必保留本文链接:https://go.coder-hub.com/76564652.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定