Welcome, Guest: Register On Nairaland / LOGIN! / Trending / Recent / New
Stats: 3,194,659 members, 7,955,388 topics. Date: Sunday, 22 September 2024 at 02:11 AM

How To Calculate Total Time A User Spent On My Site - Programming - Nairaland

Nairaland Forum / Science/Technology / Programming / How To Calculate Total Time A User Spent On My Site (1594 Views)

I Want To Automate My Site To Send Data.... / How Do I Get A User Serial Number With Jquery Or Javascript?? / How Can I Allow Share Folder On Windows 10 For A User To Be Able To Acess It (2) (3) (4)

(1) (Reply) (Go Down)

How To Calculate Total Time A User Spent On My Site by greatface(m): 11:24pm On Sep 13, 2016
I am building a website with micro framework which doesn't support sessions and am trying to implement one myself.

Am therefore requesting for ideas on how to implement this strategy.

I dont need code samples. All i need is a clue or at most pseudo code.

Many thanks before hand.
Re: How To Calculate Total Time A User Spent On My Site by DonSegmond(m): 5:17am On Sep 14, 2016
It's diffcult if you don't use javascript. In that case, you will have to store everytime a page is fetched, then crunch that in the backend. That is what analytics are used for. You can just install piwik or google analytics and they do those for you. If you really wanted, you could have a javascript that does an ajax call to a counter on the backend say every 10 second. Then tally up in the backend. If you don't wish to use code, javascript, you could just parse your web server logs, see what time they visited and when they last visited. You can put a timespace so say a user doesn't fetch a link for 1 hr then any link visit after that is regarded as a new visit. Best of luck.
Re: How To Calculate Total Time A User Spent On My Site by FrankLampard: 7:02am On Sep 14, 2016
DonSegmond:
It's diffcult if you don't use javascript. In that case, you will have to store everytime a page is fetched, then crunch that in the backend. That is what analytics are used for. You can just install piwik or google analytics and they do those for you. If you really wanted, you could have a javascript that does an ajax call to a counter on the backend say every 10 second. Then tally up in the backend. If you don't wish to use code, javascript, you could just parse your web server logs, see what time they visited and when they last visited. You can put a timespace so say a user doesn't fetch a link for 1 hr then any link visit after that is regarded as a new visit. Best of luck.

The only sensible thing I can read from here is when you mentioned Google Analytics.
Re: How To Calculate Total Time A User Spent On My Site by FrankLampard: 7:12am On Sep 14, 2016
The simple Algorithm for this is thus:

Stored the IP address of the user as the user visits your sites.

Create two column in your db table "created_at" and "updated_at" or anything you choose to name it,

When a user visits, store the IP and the created_at column as well as the updated_at column, as the user is leaving make sure you update the updated_at column.

You can use JS for this or during Logout process.

You can get the difference in another column by using MySQL TRIGGER if you are using MySQL or just calculate the difference yourself.

Thank you.
Re: How To Calculate Total Time A User Spent On My Site by DonSegmond(m): 7:31am On Sep 14, 2016
FrankLampard:


The only sensible thing I can read from here is when you mentioned Google Analytics.

i don't blame your lack of comprehension skill.

level your game up.

perhaps if i double space what i said you would understand.
Re: How To Calculate Total Time A User Spent On My Site by DonSegmond(m): 7:34am On Sep 14, 2016
FrankLampard:
The simple Algorithm for this is thus:

Stored the IP address of the user as the user visits your sites.

Create two column in your db table "created_at" and "updated_at" or anything you choose to name it,

When a user visits, store the IP and the created_at column as well as the updated_at column, as the user is leaving make sure you update the updated_at column.

You can use JS for this or during Logout process.

You can get the difference in another column by using MySQL TRIGGER if you are using MySQL or just calculate the difference yourself.

Thank you.

this is garbage, you can't use the time the user leaves the browser window to gauge how long they have been at the site.

i'll double space this reply so you can follow.

if i go to a site, and then leave my browser open, and come back 5 hours later then leave. i haven't spent 5 hours at the site.

if you are not going to use say your apache logs, then you must use javascript and check there is a user activity such as mouse or keyboard actions.

again, this is if you wish to get accurate metrics. if you don't care about correct data, then why even bother?

to the original poster, if this is just a personal challenge, then start with javascript and slowly work it out. if not, piwik, google analytics or any of the other dozen alternatives out there should solve your problem rather easily without you having to code.

1 Like

Re: How To Calculate Total Time A User Spent On My Site by greatface(m): 11:09am On Sep 14, 2016
DonSegmond:
It's diffcult if you don't use javascript. In that case, you will have to store everytime a page is fetched, then crunch that in the backend. That is what analytics are used for. You can just install piwik or google analytics and they do those for you. If you really wanted, you could have a javascript that does an ajax call to a counter on the backend say every 10 second. Then tally up in the backend. If you don't wish to use code, javascript, you could just parse your web server logs, see what time they visited and when they last visited. You can put a timespace so say a user doesn't fetch a link for 1 hr then any link visit after that is regarded as a new visit. Best of luck.

Thanks for your help. I really picked up a lot from it. But I need more elaboration on the bolded part.
Thanks again.
Re: How To Calculate Total Time A User Spent On My Site by greatface(m): 11:16am On Sep 14, 2016
FrankLampard:
The simple Algorithm for this is thus:

Stored the IP address of the user as the user visits your sites.

Create two column in your db table "created_at" and "updated_at" or anything you choose to name it,

When a user visits, store the IP and the created_at column as well as the updated_at column, as the user is leaving make sure you update the updated_at column.

You can use JS for this or during Logout process.

You can get the difference in another column by using MySQL TRIGGER if you are using MySQL or just calculate the difference yourself.

Thank you.

Big thanks for helping out.

I have thought about the IP method but one problem, I think is that visitors IP do change occasionally. Or am I wrong here?
Re: How To Calculate Total Time A User Spent On My Site by greatface(m): 11:26am On Sep 14, 2016
DonSegmond:


this is garbage, you can't use the time the user leaves the browser window to gauge how long they have been at the site.

i'll double space this reply so you can follow.

if i go to a site, and then leave my browser open, and come back 5 hours later then leave. i haven't spent 5 hours at the site.

if you are not going to use say your apache logs, then you must use javascript and check there is a user activity such as mouse or keyboard actions.

again, this is if you wish to get accurate metrics. if you don't care about correct data, then why even bother?

to the original poster, if this is just a personal challenge, then start with javascript and slowly work it out. if not, piwik, google analytics or any of the other dozen alternatives out there should solve your problem rather easily without you having to code.

I have to implement it myself.

No wars beautiful minds.

Thanks again.
Re: How To Calculate Total Time A User Spent On My Site by FrankLampard: 12:17pm On Sep 14, 2016
greatface:


Big thanks for helping out.

I have thought about the IP method but one problem, I think is that visitors IP do change occasionally. Or am I wrong here?

Yes, but that is the only way you can get the time spent for unregistered users, but for registered user, simply use that user credentials.
Re: How To Calculate Total Time A User Spent On My Site by FrankLampard: 12:19pm On Sep 14, 2016
greatface:


Thanks for your help. I really picked up a lot from it. But I need more elaboration on the bolded part.
Thanks again.

Sincerely, I no understand one thing that guy say up there. cry
Re: How To Calculate Total Time A User Spent On My Site by greatface(m): 5:03pm On Sep 14, 2016
FrankLampard:


Yes, but that is the only way you can get the time spent for unregistered users, but for registered user, simply use that user credentials.

Well, I wont be keeping records of unregistered user. At least not much. More thanks.
Re: How To Calculate Total Time A User Spent On My Site by greatface(m): 5:09pm On Sep 14, 2016
FrankLampard:


Sincerely, I no understand one thing that guy say up there. cry

I believe he is talking about using JavaScript to listen to events and then trigger functions on the backend that do the actual stuff.
Re: How To Calculate Total Time A User Spent On My Site by DonSegmond(m): 3:16am On Sep 15, 2016
FrankLampard:


Sincerely, I no understand one thing that guy say up there. cry

I'm sorry. What I mean is you use javascript to listen to events, such as mouse movement or keyboard. This lets you know that the user is actually using the website. I leave my computer on and go to sleep with many browser windows open. To get a positive result, you would have to monitor the user's actually usage. Whenever an event occurs, you call a backend and log it.

The first time you save a time, that is your begin time. When you have an event, you set the end time. However, if another event happens within your threshold, you update the time. Else you begin a new time. So let's say we put 5 minutes threshold.

12:00, 12:01, 12:04, 12:08, 12:08, 12:09, 1:00, 1:03, 1:05
We can see that time was from 12:00 to 12:09 for 9 minutes and 1:00-1:05 for 5 minutes.

2 Likes

Re: How To Calculate Total Time A User Spent On My Site by Nobody: 10:05am On Sep 27, 2016
DonSegmond:


I'm sorry. What I mean is you use javascript to listen to events, such as mouse movement or keyboard. This lets you know that the user is actually using the website. I leave my computer on and go to sleep with many browser windows open. To get a positive result, you would have to monitor the user's actually usage. Whenever an event occurs, you call a backend and log it.

The first time you save a time, that is your begin time. When you have an event, you set the end time. However, if another event happens within your threshold, you update the time. Else you begin a new time. So let's say we put 5 minutes threshold.

12:00, 12:01, 12:04, 12:08, 12:08, 12:09, 1:00, 1:03, 1:05
We can see that time was from 12:00 to 12:09 for 9 minutes and 1:00-1:05 for 5 minutes.

This is one good way to do it, but why go through all the trouble when there are tools like Google Analytics?
Re: How To Calculate Total Time A User Spent On My Site by seunthomas: 10:08am On Sep 27, 2016
DanielTheGeek:

This is one good way to do it, but why go through all the trouble when there are tools like Google Analytics?
Correct answer. Don't bother reinventing the wheel except you really need too.

2 Likes

(1) (Reply)

How Beneficial Is Forum4africa? nairaland critique / Irotvmobile App...used In Watching Cinemas Nollywood / For Developers Especially Beginners And Junior Developers.

(Go Up)

Sections: politics (1) business autos (1) jobs (1) career education (1) romance computers phones travel sports fashion health
religion celebs tv-movies music-radio literature webmasters programming techmarket

Links: (1) (2) (3) (4) (5) (6) (7) (8) (9) (10)

Nairaland - Copyright © 2005 - 2024 Oluwaseun Osewa. All rights reserved. See How To Advertise. 55
Disclaimer: Every Nairaland member is solely responsible for anything that he/she posts or uploads on Nairaland.