Afshar
Afshar

Reputation: 11483

Implement my own statistics engine and have a record per website visit?

I am supposed to create an internal statistics mechanism for our ASP.NET MVC 4 web application. We are not going to use external ones like Google Analytics or even Glimpse. Because I'm not sure if I can extract needed data from their API.

What we expect this mechanism is very like to Google Analytics including page hit count, referer, keyword, etc. But just for part of pages not all. We want to use these data in our own pages and reports.

Now I have 2 questions. Is it correct to ignore Google Analytics or Glimpse and implement my own? If yes, it is reasonable to save a record in database per each website visit and then use theses record to extract statistics?

Any help is highly appreciated

Upvotes: 1

Views: 1323

Answers (1)

maxs87
maxs87

Reputation: 2284

I think you can implement both this satistic. Its difficult to say without understanding business logic you need. But if you need more detailed information about every request (visited user roles, retrive controller/action name for some specific statistic, log access to specific resources etc.) you can easily implement this by using action filter.

public class StatisticFilter : ActionFilterAttribute
{
    public override void OnActionExecuted(ActionExecutedContext filterContext)
    {
        base.OnActionExecuted(filterContext);
        if (filterContext.IsChildAction) //if action call was from view like @Html.Action do nothing
            return;
        var CurrentUser = filterContext.RequestContext.HttpContext.User;
        if (CurrentUser.IsInRole("some_role"))
            return; //write logic for this role
        string controllerName = filterContext.ActionDescriptor.ControllerDescriptor.ControllerName;            
        string actionNaem = filterContext.ActionDescriptor.ActionName;
        //here the id of the accessed resource - document, sale, page etc. 
        string id = filterContext.RequestContext.RouteData.Values["id"].ToString(); 
    }

}

Thats all. You can extend this by any logic you need. In my project i have the statistic table with filds: Date - timestamp,

Controller - string,

Action - string,

id - bigint

method - string(POST, GET... if post - submited)

user_id - bigint

And insert record for every request executed. So i have most important information about request for any statistic.

Upvotes: 1

Related Questions