Solved

Calculate Average Time Spent on a site via SQL ?

Posted on 2004-03-31
1
837 Views
Last Modified: 2007-12-19
I wrote a script to track my website visitors using a really simple method:

TABLE: Visitors
  Column: URL (varchar)
  Column: IPAddress (numeric)
  Column: Date/Time (DateTime)


Using this I can count how many visits/unique vistors, etc to a specific page or to the entire site. within a date range.  I can also calculate average page views per visitor and other basic web stats.

What I would like to figure out now is how to view the "average visit length" - or the average amount of time each user spent on the site before they went away.  Obviously this won't be 100% accurate, since they could spend 20 minutes on the LAST page they looked at and then just close their browser, but I can get a pretty good idea.

I'm wondering if there is a way to do this through SQL with the data present.  I would basically need to do 3 things:

(1) Count the number of unique visitors (ie; unique numbers in the IPAddress field)

(2) Get the amount of time spent on the site for each visitor.  Sometimes visitors will come to the site 2 or 3 times a day so I'm not sure exactly how to come up with this.  My first thought was to take the DateDiff from Max(Datetime) and Min(datetime) but if the user comes on and 8:00 AM and then again at 8:00 PM this method would show they were on the site for 12 hours, which we know isn't realistic.  Not really sure what to do here - maybe base it on a 15 minute window, where if a particular visitor's hits are more than 15 minutes apart it doesn't count that last 15 minutes?  

In other words... someone has hits at 8:00 AM, 8:05 AM, 8:10 AM, 9:30 AM, 9:35 AM ... we will assume that they were connected from 8:00 AM to 8:10 AM simultaneously since those are close together, but their last two visits at 9:30 AM and 9:35 AM were because they opened their browser later and came back.  So THAT one person's "average visit length" would be 7.5 minutes, NOT 1 hour and 35 minutes.

(I came up with 7.5 by assuming two sessions, one from 8:00 to 8:10 for 10 minutes, and one from 9:30 to 9:35 for 5 minutes.  15 Minutes total, two sessions, = average of 7.5 minutes)

Then of course this leads to the third step ... which is to take the total SUM of the average session for each visitor and divide that by the total number of visitors to create an "Average visit length" for the site.

The question is can this be done through a stored procedure that wouldn't kill the server every time it was run?  I don't even know where to begin coding something like this.
0
Comment
Question by:visionmultimedia
1 Comment
 
LVL 3

Accepted Solution

by:
ctcampbell earned 155 total points
ID: 10727512
Here's one way to do it.  This requires SQL Server 2000 because of a table variable.  I have written it for a 15 minute window (900s).  Change 900 if you want a different window.  This should perform fine up to a couple of hundred thousand rows, based on server memory.  If you need more, just segment the data based on something like an IP range.

--- Begin T-SQL ---
declare @Visits table (
      IPAddress varchar(15) not null,
      LogTime datetime not null,
      VisitID int null,
      LogID int not null identity,
      primary key clustered (IPAddress, LogTime, LogID)
)

insert into @Visits (IPAddress, LogTime)
select IPAddress, LogTime
  from Visitors
 order by IPAddress, LogTime

declare @LastIPAddress varchar(15), @LastLogTime datetime, @VisitID int
set @VisitID = 0
set @LastIPAddress = ''
set @LastLogTime = 0

update @Visits set
      @VisitID = VisitID = case when @LastIPAddress <> IPAddress or datediff(second, @LastLogTime, LogTime) > 900 then @VisitID + 1 else @VisitID end,
      @LastIPAddress = IPAddress,
      @LastLogTime = LogTime

select VisitID, IPAddress, MIN(LogTime) as FirstVisit, MAX(LogTime) AS LastVisit,
      VisitLength = datediff(minute, MIN(LogTime), MAX(LogTime))
  from @Visits
 group by VisitID, IPAddress

select IPAddress, AVG(VisitLength) as AvgVisit
  from (
      select VisitID, IPAddress, VisitLength = cast(datediff(minute, MIN(LogTime), MAX(LogTime)) as smallmoney)
        from @Visits
       group by VisitID, IPAddress
      ) data
 group by IPAddress
0

Featured Post

Enterprise Mobility and BYOD For Dummies

Like “For Dummies” books, you can read this in whatever order you choose and learn about mobility and BYOD; and how to put a competitive mobile infrastructure in place. Developed for SMBs and large enterprises alike, you will find helpful use cases, planning, and implementation.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Slow Connectivity over ODBC 8 36
How to search for strings inside db views 4 28
Update a text value in another table 10 40
Proper Case SQL Command 2 10
Introduction In my previous article (http://www.experts-exchange.com/Microsoft/Development/MS-SQL-Server/SSIS/A_9150-Loading-XML-Using-SSIS.html) I showed you how the XML Source component can be used to load XML files into a SQL Server database, us…
International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
Viewers will learn how to use the INSERT statement to insert data into their tables. It will also introduce the NULL statement, to show them what happens when no value is giving for any given column.
Viewers will learn how to use the UPDATE and DELETE statements to change or remove existing data from their tables. Make a table: Update a specific column given a specific row using the UPDATE statement: Remove a set of values using the DELETE s…

777 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