How to display time spent by visitor on website? [closed]
-
10-07-2019 - |
Question
Have no idea how to make it :( I think javascript should be used, any suggestions?
Solution
Something like this?
var startDate = new Date();
window.onbeforeunload = function() {
var millSecsOnPage = new Date() - startDate;
// ajax save this value
};
OTHER TIPS
You need to post from the client side two times:
- First window.onload - when the user started viewing the page, tough you might be able to do it internally on your PHP/ASP server code. This might get broken by cache.
- When the user leaves - this is a must window.onclose.
Just post back an empty post - you only need to know the IP and the lookup for the previous time recorded.
... or... use Google Analytics. 2 minutes of work.
Edit: I see d's code. This is the first answer I gave you - but be warned, that without JavaScript - this cannot be done.
Edit2: Instead of using window.load, the post/get should be done in the header or in the . That php "js" can also be an empty file - you just need it to record the event.
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow