如何跟踪网站上的用户使用情况? [英] How to track user usage on site?

查看:171
本文介绍了如何跟踪网站上的用户使用情况?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用PHP/MYSQL.每次用户登录时,我们都会输入他们的登录时间和登录日期.虽然很容易告诉我们在特定的一天中有多少用户登录,但是我不确定如何计算他们在该网站上花费了多少时间.

例如,就像用户登录并将记录插入到login_tracking表中一样,当用户单击登出"并注销时,我们也会更新该记录.这样一来,我们减去登录和注销的时间,就可以得到每个人所花费的时间,但是如果大多数人没有点击注销",而是清除cookie/缓存,或者只是关闭网站处于打开状态的窗口/选项卡,会发生什么情况在?

是否有更好的方法可以做到这一点?我正在使用Google Analytics(分析),该工具可以为我提供每天出现的用户和访问次数,但我需要专有的功能来跟踪使用情况?

有什么想法我能做什么?

更新:@Paul问我是否正在使用Sessions.我就是,这就是我的使用方式:

$ username = $ _SESSION ["username"];

解决方案

您可以通过在用户登录时设置日期时间来实现此目的.

因此,当用户登录时,您将更新DATETIME的mysql字段,类似...

 $q = $dbc -> prepare("UPDATE accounts SET loggedOn = NOW() WHERE username = ?");
 $q -> execute(array($_SESSION['username']));

现在您所要做的就是在数据库表中再创建几列,分别称为totalTime和一列称为active或您喜欢的任何内容.

在每个页面上加载更新active,该更新也是DATETIME列,同时包含NOW()

 $q = $dbc -> prepare("UPDATE accounts SET active = NOW() WHERE username = ?");
 $q -> execute(array($_SESSION['username']));

这可确保您知道用户是否仍在线,可以检查他们上次活动的时间.现在说,如果闲置5分钟后,您想查找自己的总时间,

 $q = $dbc -> prepare("UPDATE accounts SET totalTime = ADDDATE(totalTime, DATEDIFF(NOW(), loggedOn)) WHERE active < DATE_SUB(NOW(), INTERVAL 5 MINUTE)");
 $q -> execute();

上面的操作是,如果用户闲置5分钟,它将用loggedOn时间减去当前时间,剩下的总时间将留在网站上,并将其添加到DATETIME.

只需每5分钟将最后一个查询设置为cron即可,您就可以非常准确地衡量网站上的访问者时间.

I am using PHP/MYSQL. Each time a user logs in we insert the time they logged in and the date they logged in. While its easy for us to tell how many users logged in a particular day i am not sure how to calculate how much time they spent on the site.

For instance, just like a user signs in and we insert record into login_tracking table we also update that record when the user hits "logout" with the time they logged out. That way we subtract the time logged in and logged out and we get each persons time spent, but what happens if majority of people don't hit "logout" and instead clear cookies/cache or just close the window/tab the site was open in?

Is there a better way to do this? I am using Google Analytics, which gives me users and visits that showed up everyday but i want something proprietary to track usage?

Any ideas what i can do?

Update: @Paul asked if i was using Sessions. I am and this is how i am using it:

$username = $_SESSION["username"];

解决方案

You can do this by setting a date time at the point of when they log on.

So when a user log on you update the mysql field which is DATETIME, something like...

 $q = $dbc -> prepare("UPDATE accounts SET loggedOn = NOW() WHERE username = ?");
 $q -> execute(array($_SESSION['username']));

Now all you would have to do is create a few more columns in your database table one called totalTime, and one called active or whatever you like upto you really.

On every page load the update active which is a DATETIME column also with NOW(),

 $q = $dbc -> prepare("UPDATE accounts SET active = NOW() WHERE username = ?");
 $q -> execute(array($_SESSION['username']));

This ensures that you know if the user is still online or not, you can check when they were last active. Now say if after 5 minutes of inactivity you want to find the total time you simply do,

 $q = $dbc -> prepare("UPDATE accounts SET totalTime = ADDDATE(totalTime, DATEDIFF(NOW(), loggedOn)) WHERE active < DATE_SUB(NOW(), INTERVAL 5 MINUTE)");
 $q -> execute();

What the above does is if users inactive for 5 minutes, it will take the loggedOn time minus it from the current time, leaving you with the total time on the site and add it to the DATETIME column totalTime.

Simply put the last query as a cron set too every 5 minutes and you have a quite accurate measure of peoples time on your site.

这篇关于如何跟踪网站上的用户使用情况?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆