0

Is it possible to report the time spend on a page back to server when the visitor leaves the page by closing the tab or the browser? Using Javascript.

And it will work on Firefox, Google Chrome, Safari, Opera, and IE8 (IE7 and 6 are not important).

But it will work in any case; even while the browser is closing, or it is typed another address.

tshepang
  • 12,111
  • 21
  • 91
  • 136
ilhan
  • 8,700
  • 35
  • 117
  • 201
  • Why is this community wiki? :( – Andreas Bonini Dec 20 '09 at 02:35
  • Andreas Bonini, in case I have grammer errors. – ilhan Dec 20 '09 at 02:41
  • Well, yes but the people who answer (me in this case) also get no rep, and this makes us sad :) You should use community wiki only if your question has no "correct" answer, or it is more of a discussion. See http://meta.stackexchange.com/questions/11740/what-are-community-wiki-posts-on-stack-overflow – Andreas Bonini Dec 20 '09 at 03:00

4 Answers4

4

You can try windows.onbeforeunload but there is no guarantee that it will work in all cases.

Anyway I suggest you use google analytics which has that feature among many others (I doubt you can do better than google!). It's free.

Andreas Bonini
  • 44,018
  • 30
  • 122
  • 156
2

If you want to implement this yourself, you need to decide when to get the start time--you can put a script tag at the top of the page, which may execute before the content has rendered, on the onload event, which will wait until all images and scripts have loaded, or something like the JQuery ready event, which occurs when the DOM is ready, but doesn't wait for resources to load. At this point, you'd want to record a start time:

<script>var startTime = new Date().getTime();</script>

Then you can listen for the onunload or onbeforeunload event and use an image to send a GET request with the info:

<script>
  window.onunload = function() {
    var i = new Image();
    var timeSpentMilliseconds = new Date().getTime() - startTime;
    i.src = '/pagetime?timespent=' + timeSpentMilliseconds;
  }
</script>

You'll have to do some testing to see whether the requests are always sent--I'm sure sometimes the browser closes or the tab switches before they finish firing.

HoldOffHunger
  • 18,769
  • 10
  • 104
  • 133
Annie
  • 6,621
  • 22
  • 27
1

You could also try PiWik which offers you similar statistics about the visitor of you website.

TheHippo
  • 61,720
  • 15
  • 75
  • 100
1

Why not use Google Analytics directly? Besides what you want, it provides much more data for your analytics. And, it is free. Regards, freezea.

Greg Beech
  • 133,383
  • 43
  • 204
  • 250
freezea
  • 1
  • 1
  • Google Analytics do not provide IP based information. I'll make this code for my coisin's web site, and for fun for myself. He is has just got system-network certifcates from microsoft exam. and we (i actually) made an web site for him. he is waching every visit to his site (1 per 3 days) wheter it is from a corporotian IP or not, which corporation. and we want to learn how much time a "worker seeker" spends in his website. and and and i'm statistcs student, i might use it for statistical purposes. – ilhan Dec 20 '09 at 05:06