• UserSessionMiddleware.Plugin

UserSessionMiddleware.Plugin

Assembly: UserSessionMiddleware.Plugin

Description

The user session middleware plugin module has been designed to manage a user session whilst navigating through a webiste. At its core the UserSession class provides all the details for the user including:

  • GeoIp Data.
  • Pages Visited.
  • Sales Data.
  • Bot identification.
  • User Agent.
  • Culture Information.
  • Initial Referrer

GeoIp Data

If the GeoIp.Plugin or the SieraDeltaGeoIp.Plugin modules are loaded, when the session is created GeoIp data for the session will be loaded.

IUserSessionService

The IUserSessionService provides methods for saving session data into a database or other data store, this is particularly useful in post analysis of user sessions. Please view Web Analytics and User Session Blog for more information on how to manipulate the user session data collected from user sessions. This could provide a multitude of reports including:

  • Visits by Hour.
  • Visits by Day.
  • Visits by Week.
  • Visits by Month.
  • Location - City/Month.
  • Sales - City Month.
  • Page View by Month
  • Bounced Visits.
  • Bot Visits.
  • Conversions.
  • Conversions by Mobile.
  • Referral Data
    1. Direct
    2. Organic
    3. Bing
    4. Google
    5. Yahoo
    6. Facebook
    7. Twitter

There are litterally dozens of reports that can be generated using the Session Data that can be saved.

Saving Sessions

Session data is saved using a background thread so as the user journey is not interupted whilst navigating throught the website.

Sales Data

Sales data is automatically collected if the ShoppingCartPlugin module is used, otherwise the UserSession class contains a method for saving sales data that can later be saved using the IUserSessionService interface and reported on.

Bot Identification

Bot identification is completed when the session is created, this is achieved by inspecting the UserAgent that is used. Most common bots use standard UserAgent strings that make identifying them easy, however, not all bots are honest and the UserAgent can not always be used.

Culture Information

Culture information is loaded into the session when it is initially loaded, and when the user changes the culture themselves. If the Localization.Plugin module is loaded, the culture is automatically used for the current request.

Initial Referrer

Seo is a very important aspect of web design, and the intial referrer plays no small part. I can be used to determine how well advertising is managed or the general spread of the website throughout the website.

User Login

If the LoginPlugin module is loaded, then the user login details are automatically loaded into the session object so that the current user can easily be identified.

Obtaining Session Data

The user session middleware layer sits within the request pipeline and adds the session data to the HttpContext.Items, you can retrieve user session data in several ways:

Direct from the HttpContext.Items:

UserSession userSession = null;
 
if (HttpContext.Items.ContainsKey(Constants.UserSession))
{
    userSession = ((UserSession)HttpContext.Items[Constants.UserSession]);
}

BaseController contains a method called GetUserSession() which can be used to retrieve the user session:

UserSession session = GetUserSession();

BaseMiddleware contains a method called GetUserSession(HttpContext context) which can be used to retrieve the user session:

public async Task Invoke(HttpContext context)
{
    UserSession userSession = GetUserSession(context);
 
    if (userSession != null)
    {
        // Code removed for brevity...
    }
 
    await _next(context);
}

How to use

The UserSession middleware plugin can be used with or without the Plugin Manager. If using with the plugin manager then it will be automatically added, but should be loaded first, or after the ErrorManager.Plugin module if used. You can also load it manually like:

app.UseUserSessionMiddleware();

builder.UseMiddleware<UserSessionMiddleware>();