[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

Measure the time spent by user on a website

Posted on 2007-07-28
16
Medium Priority
?
929 Views
Last Modified: 2013-11-13
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
0
Comment
Question by:Fahdmurtaza
  • 9
  • 7
16 Comments
 
LVL 14

Expert Comment

by:bugs021997
ID: 19584887
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>



0
 
LVL 6

Author Comment

by:Fahdmurtaza
ID: 19584904
I am a looking for a sever side ASP solution!
0
 
LVL 14

Expert Comment

by:bugs021997
ID: 19584917
@Fahdmurtaza

This script can be included in any SERVER SIDE CODE. Javascript/ASP can be blended together.
0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 14

Expert Comment

by:bugs021997
ID: 19584931
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
0
 
LVL 6

Author Comment

by:Fahdmurtaza
ID: 19584988
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
0
 
LVL 14

Expert Comment

by:bugs021997
ID: 19585013
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
0
 
LVL 14

Expert Comment

by:bugs021997
ID: 19585023
@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>
0
 
LVL 14

Accepted Solution

by:
bugs021997 earned 1000 total points
ID: 19585030
You can also use this code.....grabbed it from one of the sites, not my code....It gives you the number of seconds a person has been on that page....should be helpful to you....



<HTML>
<HEAD><TITLE>JavaScript Timer</TITLE>
<SCRIPT>
<!--//hide from old browsers
var counter = 0;
// call Update function in 1 second afte
//     r first load
ID=window.setTimeout("Update();",1000);


    function Update() {
    counter ++;
    //Optional alert after 10 seconds
    //if (counter==10) (alert("Ten seconds i
    //     s long enough!!!!"))
    //Display time in status window
    window.status="Elapsed time = " + counter + " seconds";
    //Display time in text box
    document.form1.input1.value="You have looked at this page for " + counter + " seconds";
    // set another timeout for the next coun
    //     t
    ID=window.setTimeout("Update();",1000);
}

//-->
</SCRIPT>
</HEAD>
<BODY BGCOLOR=aqua>
<FONT FACE=ARIAL COLOR=navy><CENTER><H1>JavaScript Timer</H1></CENTER>
JavaScript can measure time.<P>
The text value below and the status line are being updated every second.<BR>
Press the RESET button to restart the count, or the STOP button to stop it.
<HR></FONT>
<CENTER>
<FORM NAME="form1">
<INPUT TYPE="text" NAME="input1" SIZE="40" Value="Welcome!"><BR>
<INPUT TYPE="button" VALUE="RESET" onClick="counter = 0;">*
<INPUT TYPE="button" VALUE="STOP" onClick="window.clearTimeout(ID);">
</CENTER>
<HR>
</BODY></HTML>
0
 
LVL 6

Author Comment

by:Fahdmurtaza
ID: 19585079
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
0
 
LVL 6

Author Comment

by:Fahdmurtaza
ID: 19585331
Is there a pure vbscript function for this?
0
 
LVL 14

Expert Comment

by:bugs021997
ID: 19585369
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
0
 
LVL 14

Expert Comment

by:bugs021997
ID: 19585374
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..


0
 
LVL 6

Author Comment

by:Fahdmurtaza
ID: 19585375
well any ideas on recording in the database using global.asa?
0
 
LVL 6

Author Comment

by:Fahdmurtaza
ID: 19585385
Thats great. I think this discussion is now going to the right direction. I will soon update about what I achieve!
0
 
LVL 14

Expert Comment

by:bugs021997
ID: 19585396
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>
0
 
LVL 6

Author Comment

by:Fahdmurtaza
ID: 19585567
Thanks bugs, your help is highly appreciated!
0

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Computer science students often experience many of the same frustrations when going through their engineering courses. This article presents seven tips I found useful when completing a bachelors and masters degree in computing which I believe may he…
When you discover the power of the R programming language, you are going to wonder how you ever lived without it! Learn why the language merits a place in your programming arsenal.
Progress
Introduction to Processes

872 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question