Click here to Skip to main content
15,888,286 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Every time a visitor to my website performs an action, I am logging that action to a database, along with the time the action occurred. I am in the process of building a dashboard to provide a better overview of what users are doing on the site.

One of the metrics I would like to track is session length.

How can I work out how long the user has been active?

Ideally I would like to create an SQL query that pulls out all the users and groups them into sessions, where the gap between any two actions is never greater than a configured timeout, say, 30 minutes. If the gap is greater than the timeout, then it should be counted as two separate sessions.

Any suggestions on how to do this - either in SQL or C#/Linq?

What I have tried:

I haven't tried anything yet - i'm struggling to think how to begin tackling this.
Posted
Updated 20-Apr-16 3:00am
Comments
Richard Deeming 20-Apr-16 8:51am    
Which version of SQL?
Gavin Coates 28-Apr-16 11:53am    
SQL Server 2012.

1 solution

I'd be very tempted to add info to the DB - a value which identifies a session, so they are "automatically" grouped.
If you add a GUID field to the DB, then when you record an action you can do a trivial search for the previous session entry: it's the same user, and with a time stamp less than 30 minutes ago. If it exists, use it's GUID value for your new entry. If it doesn't, create a new GUID and insert that.
Then when you want to run your metrics, it's a relatively simple GROUP BY clause, either in SQL or Linq.

The reason I'd do it like that is that it's a tiny bit of extra processing as a result of a human-scale event, making the data processing trivial. If you don't, then first you have to work out the groups, and then process them to get the info - which is going to be a lot more complex a query (though the SQL Server LAG[^] and LEAD[^] statements introduced to SQL 2012 would help).
[edit]Typo: "woudl" for "would" - OriginalGriff[/edit]
 
Share this answer
 
v2
Comments
Richard Deeming 20-Apr-16 9:08am    
+5
There's a small typo in the last sentence - "woudl" instead of "would". :)
OriginalGriff 20-Apr-16 9:16am    
Thanks - fixed.
My keyboard types ahead of my brain from time to time...:laugh:
F-ES Sitecore 20-Apr-16 9:38am    
Just to add to that, the Session ID isn't a suitable way of grouping the users (in case the OP was thinking of going that route) as Session IDs are recycled, so create a new GUID for the user and store it in the Session on Session Start and use that to identify subsequent events.
OriginalGriff 20-Apr-16 9:54am    
I considered that, but the default Session length is 20 minutes and it's recommended that it not be extended. (Some hosting services enforce a five minute Session and ignore web.config extension values).
So you'd have a new "user session" every half hour anyway!
F-ES Sitecore 20-Apr-16 10:00am    
From reading the question that seems to be what he wants anyway. He might get more value from his data if he gave each user a GUID stored in a cookie, that way he gets to know the user is a returning user and be able to show all that user's sessions, that's how most tracking apps work.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900