如何用 Javascript 确定网站停留时间?

发布于 2024-07-14 18:38:35 字数 91 浏览 7 评论 0原文

Google Analytics 跟踪“网站停留时间”。 你会如何有效地做到这一点? 它真的会跟踪用户何时关闭浏览器或离开浏览器吗?

想法?

Google Analytics tracks 'Time On Site'. How would you do that effectively? Does it actually track when the user closes the browser or navigates away?

Thoughts?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

弱骨蛰伏 2024-07-21 18:38:35

当然,我不能 100% 确定,但我猜他们使用 javascript 以及 onload 和 onunload 事件和/或 setTimer 通过 AJAX 与 Web 服务进行通信。 这样他们就可以知道用户何时访问或离开您网站上的页面。 一旦浏览器停止“ping”网络服务,就假定他们离开了您的网站。

我确信无论你如何做,都会存在一定的误差范围,但这样你就可以得到一个相当不错的估计。

I can't be 100% certain of course, but I would guess they use javascript and the onload and onunload events and/or setTimer to communicate with a web service via AJAX. This way they could figure out when users go to or leave a page on your site. Once the browser stops "pinging" the web service, it's assumed that they left your site.

I'm sure there's some margin of error involved no matter how you do it, but you could get a pretty decent estimate that way.

三寸金莲 2024-07-21 18:38:35

有一个名为 onBeforeUnload 的 JavaScript 事件,当用户离开页面时执行。 这可能包括关闭窗口/选项卡或导航到不同的页面(即使它位于同一站点上)。 通过在页面加载后立即获取时间,并使用 JavaScript 发送同步请求 onBeforeUnload 以及加载和离开之间的时间差,您可以有效地跟踪页面上花费的时间。 我认为这就是谷歌所做的。

如果您想要工作代码示例和更多信息,请检查 出来。

There is a javascript event called onBeforeUnload which executes when a user leaves a page. This could include closing the windows/tab or navigating to a different page(even if it's on the same site). By getting the time immediately after the page loads and using JavaScript to send a synchronous request onBeforeUnload with the difference in time between loading and leaving, you can effectively track the time spent on a page. I would assume this is what Google does.

If you want working code examples and a little more info, check this out.

一梦浮鱼 2024-07-21 18:38:35

只需设置一个像这样的函数:

function track()
{
   setTimeout(track ,1000);
   now = new Date;
   now_string = now.getHours() + ":" + now.getMinutes() + ":" + now.getSeconds();
   (... whatever you want to do with this data)
}

在 javascript 中,如果小时/分钟/秒数字少于 2 个字符,您会得到一个字符串,表示 HH:MM:SS 或 H:M:S

所以只需做一些数学运算只需

将其运行在窗口中的某个位置,并将其与会话相关联(保证它在所有页面中),您就可以获得某个用户在您的页面中的确切时间。

编辑:必须删除我拥有的一个函数,它不是来自 javascript sry :p

Just set a function like this:

function track()
{
   setTimeout(track ,1000);
   now = new Date;
   now_string = now.getHours() + ":" + now.getMinutes() + ":" + now.getSeconds();
   (... whatever you want to do with this data)
}

In javascript you get like that a string saying HH:MM:SS, or H:M:S in case the hour/minute/second number as less than 2 characters

So just make a bit of math arround it

Just put this running somewhere in your window, and associating it with the session (guaranteing this is in all pages) you get exactly how much time some user was in your page.

Edit: had to remove a function i had there that was mine and it isn't from javascript sry :p

旧故 2024-07-21 18:38:35

我不认为它会跟踪您何时关闭浏览器或导航离开,即使它跟踪,也不能在 100% 的情况下起作用。

我的猜测是,他们估计在退出页面的停留时间,可能基于之前的页面或所有访问者该页面的平均值。 如果您在几分钟内获得三个页面点击,则您可以准确了解前两页的浏览时间,但您永远不知道第三页的浏览时间有多长。 也许您会立即关闭该页面,或者将其保持打开状态并稍后再阅读。

这是统计数据,所以从长远来看它通常相当准确,但它从来都不是事实。 ;)

I don't think that it tracks when you close the browser or navigates away, and even if it does, that doesn't work in 100% of the cases.

My guess is that they estimate the stay at the exit page, perhaps based on the previous pages or an average for that page for all visitors. If you get three page hits in a few minutes, you have an exact viewing time for the first two pages, but you never know how long the third page is viewed. Perhaps you close the page right away, or leave it open and read it later.

It's statistics, so in the long run it's usually rather accurate, but it's never really the truth. ;)

近箐 2024-07-21 18:38:35

我怀疑他们可能运行一个计时器,在每个循环上重复并执行某种回调,使用 cookie 来识别用户和会话

I suspect they probably run a timer that repeats and executes some kind of callback on each loop using a cookie to identify the user and session

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文