Link to home
Start Free TrialLog in
Avatar of Fahdmurtaza
FahdmurtazaFlag for Pakistan

asked on

Measure the time spent by user on a website

Hi Experts
I am working on an application that counts the time a user spends on a website. What I believe is that this can be done by logging in to a site and then browsing the web through that site. If you have any ideas, just let me know.

Regards,
Fahd Murtaza
Avatar of bugs021997
bugs021997
Flag of India image

Try using below code....here for your purpose i am alerting the time spent, you can document.write to display it on the webpage.


<html>
<title>BUGS</title>
<script language="javascript">
var time=1;

function timeHere() {
  time = time + 1;
  finalTime = time / 10;
/* Remove the "//" below to display in the title bar
  the amount of time the visitor has been on the site.
  Be aware though, that it does tend to be a bit distracting. */
// document.title = finalTime+" seconds you been here for!";
}

function sayTime() {
  finalTime = time / 10;
  alert("Thank you for coming to my site! \n You have been here " + finalTime + " seconds!");
}

</script>
</head>

<body onload='window.setInterval("timeHere()", 100)' onunload="sayTime()"></body>
</html>



Avatar of Fahdmurtaza

ASKER

I am a looking for a sever side ASP solution!
@Fahdmurtaza

This script can be included in any SERVER SIDE CODE. Javascript/ASP can be blended together.
You can check this site which has got lot of ASP Based SITE STATISTICS which can be used...

http://www.aspin.com/home/webapps/sitestat
can you give me an idea on how to do this. Here is my idea. I will present a user with a login for my site. After he logs in, his/her session is stored in a variable and he/she is redirected to a framed page. Top frame contains links for the bottom frame. Now this frameset is a part of my site and the bottom frame is an external link. What I want to do is, onchange of the bottom frame, a script in the top frame calculates the time spent on that link and logs it down.

Something like.

http://b3u.net/

Regards,
Fahd Murtaza
if u want to display the time
use the following code :

<BODY .... OnLoad="timefn()">
<head>
<Script>
 var dt=new date();
 function timefn()
  {
  document.formname.text1.text=dt.value
  docuemnt.setTimeout("timefn()",1000);
  }
</Script>
</head>


It will display the time in text field every second
@Fahdmurtaza

Use javascript on the client .. this is cool. you could use a div or layer to display the time if you wish.

What you can do is make the textbox hidden on everypage. As soon as the user reaches a new page record the current time and when the user leaves the page and goes to the next page record the time there and CALCULATE the total time spent by doing DATEDIFF()

<HTML>
<HEAD>
<TITLE> Date and Time </TITLE>
</HEAD>
<head>

<script>
// Global variables for timer.
var timerID = null;
var timerRunning = false;
var timevalue;

function startclock ()
   {
   // Make sure the clock is stopped
   stopclock();
   time();
   }

function stopclock ()
   {
   if(timerRunning)
      clearTimeout(timerID);
   timerRunning = false;
   }

function time ()
   {
   var now = new Date();
   var ampm = (now.getHours() >= 12) ? " P.M." : " A.M."
   var hours = now.getHours();
   hours = ((hours > 12) ? hours - 12 : hours);
   var minutes = ((now.getMinutes() < 10) ? ":0" : ":") + now.getMinutes();
   var seconds = ((now.getSeconds() < 10) ? ":0" : ":") + now.getSeconds();

   timevalue =(" " + hours + minutes + seconds + " " + ampm);
   // displays the current time.
   document.forms[0].local.value = timevalue;

   timerid = setTimeout("time()",1000);
   timerrunning = true;
   }
document.write(timevalue);
</script>
</head>
<body onload="startclock ()">
</center>
        <script>
       
        var now = new Date()
        var sec = now.getSeconds()
   
function MakeArray(n) {
        this.length = n;
        for (var i = 1; i <= n; i++) {
                this[i] = 0 }
            return this        
     }
     // end script -->
      </script>
   
<form>
<table width=180><tr>
<td>
 <input type="text" name="local" size=12 value=""></td><td align=left>
</td></tr></table><p>
</form>

<LAYER id=timelayer>
     
</LAYER>

</body>
</html>

</BODY>
</HTML>
ASKER CERTIFIED SOLUTION
Avatar of bugs021997
bugs021997
Flag of India image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Thanks to all contributors.

 I will start its development in next 3 hours. Already developed the back-end and login etc. Will integrate the best solution presented in the next 3 hours!

Regards,
Fahd Murtaza
Is there a pure vbscript function for this?
To have it completely in VB SCRIPT/ASP i would suggest the best option is to work with sessions in combination with an database.

In the global.asa you can specify on which time the visitor entered the site (i.e. session on start) and you can also specify the time the user left the site (session on end). At the session on end event you can insert the data form the user in an database or a textfile. But working with an database is the best solution.

You can record the unique session_ID and IP


BUGS
In the global.asa file you put something like:

Public Sub Session_OnStart()
   Session("timestart") = Now
End Sub

Public Sub Session_OnEnd()
    Application.Lock
    Application("totaltime") = dateDiff("s", "Session("timestart"), Now)    
    Application.UnLock
End Sub



now for time spent on each page can be recorded as below....this way you'll get the time spent in minutes shown..


<%=(Datediff("n",session("timestart"),Now())%>

use h for hours or s for seconds instead of n for minutes..


well any ideas on recording in the database using global.asa?
Thats great. I think this discussion is now going to the right direction. I will soon update about what I achieve!
Check http://www.w3schools.com/asp/asp_globalasa.asp for ideas on recording in the database using global.asa

Also check the code below....

<script language=vbscript runat=server>
Sub Session_OnEnd
set conn = Server.CreateObject("ADODB.Connection")
conn.Open "DSN=....."

query = "YOUR SQL QUERY TO UPDATE"

conn.Execute(query)
conn.Close
set conn = Nothing
End Sub
</script>
Thanks bugs, your help is highly appreciated!