tags:

views:

166

answers:

2

How to implement a counter of users visiting the site in asp.net mvc

+3  A: 

If your question is about "online" users:

Set a counter on application start, on session start increase the counter, on session end decrease it. (in global.asax)

CD
I had never thought of this, that would be a great place for that info .
Kyle LeNeau
+1  A: 

If you want to track views on specific pages, you could increment a counter before you finish executing an action:

public ActionResult Index() { TrafficService.TrackPage("My Index page on controller XYZ", "path"); return View(); }

I am doing something like this on detail pages of blog entries, that way when I show a list of entries to edit I can show the current viewcount. I would not suggest this to heavily though as you are hitting the DB everything your page is viewed. I would suggest using Google Analytics for serious page tracking, including visitors and visits. Much more reliable and faster. Plus it is free and takes two minutes to set up.

You could go the standard page counter like you see on ebay pages but those are not very appealing.

All depends on what your need is and who is going to see the data, some people want a lot of info and others don't. Good luck!

Kyle LeNeau
thanks but i read that googel analytuc is slower
MVCGUY
It is not that slow, just put it at the end of the page so it doesn't block anything else from loading right away.
Kyle LeNeau