webdevRefinery Forum: How to time how long a user has pages open - webdevRefinery Forum

Jump to content

Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

Rate Topic: -----

User is online Sole_Wolf 

  • Group: Members
  • Posts: 209
  • Joined: 24-March 10
  • Expertise:HTML,PHP,Java

Posted 04 February 2012 - 01:08 PM (#1)

How to time how long a user has pages open


Can javascript time how much time a user spends on multiple pages until they either close the window or tab?

Let's say a user spends 1 minute at index.html and then they click a link which takes them to another page in the same site.

Could javascript keep track of how much time a user spends on a few pages if they are in the same directory?

I am trying to make a total game time played variable for a PHP game.

Thanks.
--Sole_Wolf
0


User is online Hyde 

  • Totally Ubermensch
  • Group: Members
  • Posts: 1272
  • Joined: 08-March 10
  • Expertise:HTML,CSS,PHP

Posted 04 February 2012 - 01:44 PM (#2)

I don't have a lot of experience with JavaScript so someone will probably have a better idea than mine, but there are many timer scripts for JavaScript. You could start one every time the page loads and pass the time to PHP by putting a variable in each link and updating it every time the time increments. I'm not sure how that would be in terms of efficiency though?
Hyde | HTML | CSS | PHP & SQL | Basic JavaScript | Objective-C
0


User is online callumacrae 

  • {{ post.author }}
  • Group: Members
  • Posts: 2170
  • Joined: 20-January 11
  • LocationWarwickshire, England
  • Expertise:HTML,CSS,PHP,Javascript,Node.js,SQL

Posted 04 February 2012 - 01:46 PM (#3)

Save the time the page was opened to a var on page load, and on unload save it to a cookie (I don't think you can send AJAX requests on unload). Next time the page is loaded, send the cookie.
Front-end developer and writer
Twitter | GitHub | phpBB Contributor and Website Team Member | lynxphp
0


User is online Hyde 

  • Totally Ubermensch
  • Group: Members
  • Posts: 1272
  • Joined: 08-March 10
  • Expertise:HTML,CSS,PHP

Posted 04 February 2012 - 01:48 PM (#4)

View Postcallumacrae, on 04 February 2012 - 01:46 PM, said:

Save the time the page was opened to a var on page load, and on unload save it to a cookie (I don't think you can send AJAX requests on unload). Next time the page is loaded, send the cookie.

Ah, see, more efficient.
Hyde | HTML | CSS | PHP & SQL | Basic JavaScript | Objective-C
0


User is online Qasim 

  • Group: Members
  • Posts: 428
  • Joined: 25-October 10
  • LocationOntario, Canada
  • Expertise:HTML,CSS,PHP,Java,Javascript,Graphics

Posted 04 February 2012 - 01:49 PM (#5)

when the page loads, you would, in PHP, set a session variable to the current time.

session_start();
$_SESSION['load_time'] = time();


Then, when the other page loads, set another variable to the current time, then subtract the new time with the old time, to get how long they were on that previous page.

$previous_time = $_SESSION['load_time'];
$current_time = time();
$time_elapsed = $current_time - $previous_time;


And time_elapsed variable will now hold the seconds that have passed from the previous page load to the current one.
Qasim Iqbal
[HTML] [CSS] [JS] [PHP] [JAVA]
[ANDROID DEVELOPER] [CHROME WEB STORE DEVELOPER]
[WEBSITE] [FACEBOOK] [FORRST]
0


User is online Sole_Wolf 

  • Group: Members
  • Posts: 209
  • Joined: 24-March 10
  • Expertise:HTML,PHP,Java

Posted 04 February 2012 - 02:10 PM (#6)

View PostQasim, on 04 February 2012 - 01:49 PM, said:

when the page loads, you would, in PHP, set a session variable to the current time.

session_start();
$_SESSION['load_time'] = time();


Then, when the other page loads, set another variable to the current time, then subtract the new time with the old time, to get how long they were on that previous page.

$previous_time = $_SESSION['load_time'];
$current_time = time();
$time_elapsed = $current_time - $previous_time;


And time_elapsed variable will now hold the seconds that have passed from the previous page load to the current one.


I was thinking about doing something like this but the only problem is how would I detect if the user closed the page? Also, I want to store this value into a mysql database and since PHP is server side, I can't get the new elapsed time value unless the user were to click the logout button.
--Sole_Wolf
0


User is offline NeilHanlon 

  • Group: Members
  • Posts: 669
  • Joined: 08-July 10
  • LocationRowley, Massachusetts
  • Expertise:HTML,CSS,PHP,Java,Graphics

Posted 04 February 2012 - 02:23 PM (#7)

You're gonna want to figure out how to send ajax on unload... or, maybe onBeforeUnload.
Thanks,
兄ニール

Website | Blog | @NeilHanlon | About.Me | Facebook | LinkedIn
0


User is online callumacrae 

  • {{ post.author }}
  • Group: Members
  • Posts: 2170
  • Joined: 20-January 11
  • LocationWarwickshire, England
  • Expertise:HTML,CSS,PHP,Javascript,Node.js,SQL

Posted 04 February 2012 - 04:33 PM (#8)

View PostQasim, on 04 February 2012 - 01:49 PM, said:

when the page loads, you would, in PHP, set a session variable to the current time.

session_start();
$_SESSION['load_time'] = time();


Then, when the other page loads, set another variable to the current time, then subtract the new time with the old time, to get how long they were on that previous page.

$previous_time = $_SESSION['load_time'];
$current_time = time();
$time_elapsed = $current_time - $previous_time;


And time_elapsed variable will now hold the seconds that have passed from the previous page load to the current one.

It isn't worth starting a session for this.


How to send AJAX on unload: http://stackoverflow...-ajax-on-unload
Front-end developer and writer
Twitter | GitHub | phpBB Contributor and Website Team Member | lynxphp
0


User is online Qasim 

  • Group: Members
  • Posts: 428
  • Joined: 25-October 10
  • LocationOntario, Canada
  • Expertise:HTML,CSS,PHP,Java,Javascript,Graphics

Posted 04 February 2012 - 04:59 PM (#9)

View Postcallumacrae, on 04 February 2012 - 04:33 PM, said:

It isn't worth starting a session for this.


Could you please explain what you mean by saying that? Store it in an HTML form instead if starting a session is really that much of a problem. I haven't done research on the bad things about sessions so I wouldnt know any better.
Qasim Iqbal
[HTML] [CSS] [JS] [PHP] [JAVA]
[ANDROID DEVELOPER] [CHROME WEB STORE DEVELOPER]
[WEBSITE] [FACEBOOK] [FORRST]
0


User is offline Daniel15 

  • dan.cx
  • Group: Moderators
  • Posts: 3038
  • Joined: 17-April 10
  • LocationMelbourne, Australia
  • Expertise:HTML,CSS,PHP,Java,Javascript,SQL

Posted 04 February 2012 - 06:15 PM (#10)

Google Analytics does exactly this - Tracks how long people are on the site for, and the "bounce rate" (number of people that just hit one page and leave). They do it using JavaScript, so it can't be too hard. Not 100% sure how it does it though. It's not AJAX as you can't do cross-domain AJAX. I think it inserts images with tracking stuff in the querystring.

var image = document.createElement('img');
image.src = 'track.png?closed=true&other=stuff_here';
document.body.appendChild(image);


You may need to call
ignore_user_abort()
in your PHP script, as the connection will be cut off a bit after the request is sent (if you do it onclose)
Daniel15! :D
Repeat after me: jQuery is not JavaScript. It is not the answer to every JavaScript-related question. When you have to write some JavaScript, do not instantly react with "Oh, I'll do that with jQuery!"

javascript:alert((''+[][[]])[!+[]+!+[]]+(![]+[])[+!+[]]+(''+!+[]/[])[+!+[]]+([![]]+[][[]])[+!+[]+[+[]]]+(''+!![])[+!+[]+!![]+!![]]+(![]+[])[!+[]+!+[]]+(+!+[])+(!+[]+!+[]+!+[]+!+[]+!+[]))

View PostKyek, on 16 November 2011 - 11:14 AM, said:

Daniel15 is ruining my life D:

View Postmorrison_levi, on 30 September 2011 - 04:10 PM, said:

They added more features to tables because. . . oh, yeah, they do have valid uses! Ever heard of data? We do still use that. :)
0


User is online callumacrae 

  • {{ post.author }}
  • Group: Members
  • Posts: 2170
  • Joined: 20-January 11
  • LocationWarwickshire, England
  • Expertise:HTML,CSS,PHP,Javascript,Node.js,SQL

Posted 08 February 2012 - 03:25 AM (#11)

View PostQasim, on 04 February 2012 - 04:59 PM, said:

Could you please explain what you mean by saying that? Store it in an HTML form instead if starting a session is really that much of a problem. I haven't done research on the bad things about sessions so I wouldnt know any better.

session_start() actually uses a fairly significant amount of resources, and it isn't worth starting a session when there are other, just as easy, solutions that don't require the use of sessions.
Front-end developer and writer
Twitter | GitHub | phpBB Contributor and Website Team Member | lynxphp
0


Share this topic:


Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

1 User(s) are reading this topic
0 members, 1 guests, 0 anonymous users


Enter your sign in name and password


Sign in options
  Or sign in with these services