简体   繁体   English

在session_End事件中写入数据库

[英]Writing to database on session_End event

I'm writing a HttpModule to track page views for a user based on their current session. 我正在编写一个HttpModule来根据用户当前会话跟踪其页面浏览量。

I have the module working fine except for when the user abandons their session. 我的模块工作正常,除了用户退出会话时。 Example scenario: 示例方案:

  • Session timeout is 20 minutes 会话超时为20分钟
  • User browses site for 15 minutes 用户浏览网站15分钟
  • User navigates away from site 用户离开站点
  • Session end event will now not be triggered for this user as they have left the site 现在,该用户离开站点后将不会触发会话结束事件

I want to hook into the Session End event so that I can bulk update the page views for a user rather than doing this each time they hit a page (not as good performance wise). 我想加入Session End事件,以便我可以为用户批量更新页面视图,而不是每次用户访问页面时都这样做(这并不是很好的性能)。

Is there a way of detecting when this happens and then triggering an event/piece of code to log the views? 有没有一种方法可以检测何时发生这种情况,然后触发一个事件/一段代码来记录视图?

Sample code incase needed: 所需的示例代码:

public void Init(HttpApplication application)
{
    application.PostRequestHandlerExecute += application_PostRequestHandlerExecute;
    SessionStateModule session = (SessionStateModule)application.Modules["Session"];
    session.Start += session_Start;
    session.End += session_End;
}

void session_End(object sender, EventArgs e)
{
    Visitor visitor = HttpContext.Current.Session["visitor"] as Visitor;

    if (visitor != null)
    {
        foreach (PageItem page in visitor.Pages)
        {
            page.UpdatePageViews();
        }
    }
}

I know this probably isn't in line with the solution as it stands, but perhaps you could look at another solution for tracking usage, such as Google Analytics - since they have put a lot of effort into their platform. 我知道这可能与目前的解决方案不符,但也许您可以考虑使用另一种跟踪使用情况的解决方案,例如Google Analytics(分析),因为他们已经在其平台上投入了大量精力。

You can also integrate your solution with Google Analytics to pull summary information back into yours as required. 您还可以将解决方案与Google Analytics(分析)集成,以根据需要将摘要信息提取回您的信息中。 This means you also don't need to store all the tracking information locally, but rather simply make requests via their API as required. 这意味着您也不需要将所有跟踪信息存储在本地,而只需根据需要通过其API发出请求。

I found the API difficult to integrate with but have it working in a solution now. 我发现该API难以集成,但现在可以在解决方案中使用。

If you have to do the tracking locally, why not do this asynchronously so you're not tying up the UI thread - that way the performance impact should be minimal? 如果您必须在本地进行跟踪,为什么不异步进行此操作,以免您不占用UI线程-这样对性能的影响应该最小化?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM