User Session Management and Website Analytic Data

User Session Management

To help manage user sessions and collect data some type of user session management object must collect and store data.

UserSessionManager was developed to manage a user session when visiting a website and provides a good base for collecting analytical data.

The UserSessionManager is optimized for performance and is able to be serialized for use within a web garden or web farm or when using global or custom state managers.

Analytics Data

Understanding how people use a website can greatly benefit a business in terms of improved search engine optimization (SEO), market research, effectiveness of campaigns and understanding how customers use a website.

Collecting analytic data is traditionally done using two methods:

  • Log File Analysis. Analyzing a websites log file data.
  • Page Tagging. Using server or client side engines such as PHP, ASP.NET or java script.

The UserSessionManager class uses the Page Tagging method, a lot of the data that is required for analytical research is already used as part of the users web experience.

Once the data is collected and stored, it can be broken down and used for analysis.

Events

The user session manager has several events which are raised as and when required.

  • OnSessionCreated – Event raised after a user session has been created and initialized.
  • OnSessionClosing – Event raised prior to a user session being closed and finalized.
  • OnSessionSave – Event raised when session data (full / partial) can be saved.
  • OnSessionRetrieve – Event raised when a session needs to be retrieved from the database.
  • OnSavePage – Event raised when page view save is required.
  • IPAddressDetails – Event raised when Geo IP details are required.

A background thread is used to monitor the session to raise the appropriate events to save the session and page data, without interfering with the UI.

The Session Manager is not linked to any specific database, instead developers should save the data using the OnSessionSave and OnSavePage events, to their preferred data store.

User Session Initialization

The session is initialized in the Session_Start method within the global class


protected void Session_Start(Object sender, EventArgs e)
{
UserSession newSession = new UserSession(Session, Request);
UserSessionManager.Add(newSession);
Session["USER_SESSION"] = newSession;
UserSessionManager.Instance.InitialiseSession(newSession);
}

The session is created and saved in the Session state class for easy retrieval, calling InitializeSession will force the session to load Geo IP location data and intialize other properties including:

  • IsMobileDevice – Indicates weather the session is on a mobile device or not.
  • MobileRedirect – Indicates whether the session is currently directed to a mobile site or not
  • Referal – Type of referral (direct, organic, Facebook, Twitter, Google, Bing, Yahoo etc)
  • InitialReferrer – The page where the user came from.
  • Bounced – Whether the user has bounced off the website (visited one page and left)
  • IsBot – Whether the session is an internet robot.

The user session data can now be used to:

  • Redirect users to a mobile web site if required.
  • Obtain country or initial language for the user.
  • Correctly determine tax rates used for purchasing products based on Geo location
  • Location based advertising.

Registering Page Views

The page data can be recorded in the page’s OnLoad method:


protected override void OnLoad(EventArgs e)
{
UserSession currentSession = (UserSession)Session["USER_SESSION"];
currentSession.PageView(Request.Url.ToString(),
Request.UrlReferrer == null ? String.Empty :
Request.UrlReferrer.ToString(),
IsPostBack);
base.OnLoad(e);
}

User Login

The user session has 3 properties for storing logged in user details, they are:

  • UserID – Int64 generic user identifier
  • Username – Used to store a users name
  • Useremail – Used to store a users email address

After a user is logged in, via whatever method is used, simply call the login method:


UserSessionManager.Login(Session.SessionID, "Joe Bloggs", "joe.bloggs@website.com", 321);

Sales Data

There are two properties within the user session which are used to record sales, they are:

  • CurrentSale – The value of products/services sold.
  • CurrentSaleCurrency – The currency used.

To record the sale, you can call the Sale method for the user session:

UserSession currentSession = (UserSession)Session["USER_SESSION"];
currentSession.Sale(_basket.TotalAmount, _basket.Currency.CurrencyCode);

Database Tables

As a minimum, there should be four tables to store the data required for later analysis, they are:

  • Initial Referrer – Used to store page referral data.
  • User Agent – Used to store user agent data.
  • Session Data – Used to store session status data.
  • Session Page Data – Used to store page view data.

Initial Referrer

The initial referrer table should, at a minimum, record the referral page and usage count:

CREATE TABLE INITIAL_REFERRER
(
ID BIGINT NOT NULL,
URL VARCHAR(1000),
USAGE_COUNT BIGINT ,
CONSTRAINT PK_INITIAL_REFERRER_ID PRIMARY KEY (ID)
);

User Agent

The user agent table should, at a minimum, record the user agent and usage count:


CREATE TABLE USER_AGENT
(
ID BIGINT NOT NULL,
USER_AGENT VARCHAR(1000),
USAGE_COUNT BIGINT ,
CONSTRAINT PK_USER_AGENT_ID PRIMARY KEY (ID)
);

Session Data

The session data collected would depend on the final reports required.

CREATE TABLE SESSION_DATA
(
ID BIGINT NOT NULL,
CREATED Timestamp NOT NULL,
SESSION_ID Varchar(100) NOT NULL,
USER_AGENT_ID BIGINT NOT NULL,
INITIAL_REFERRER_ID BIGINT NOT NULL,
IP_ADDRESS Varchar(15) NOT NULL,
HOST_NAME Varchar(150),
IS_MOBILE_DEVICE Char(1),
IS_BROWSER_MOBILE Char(1),
MOBILE_REDIRECT Char(1),
REFERRAL_TYPE Integer,
BOUNCED Char(1),
IS_BOT Char(1),
CITY_ID BIGINT,
MOBILE_MANUFACTURER Varchar(100),
MOBILE_MODEL Varchar(100),
USER_ID BIGINT,
SALE_CURRENCY Varchar(3),
SALE_AMOUNT Decimal(18,2),
CONSTRAINT PK_SESSION_DATA_ID PRIMARY KEY (ID)
);
ALTER TABLE SESSION_DATA ADD CONSTRAINT FK_SESSION_DATA_INITIAL_REFERRER
FOREIGN KEY (INITIAL_REFERRER_ID) REFERENCES INITIAL_REFERRER (ID) ON UPDATE CASCADE;
ALTER TABLE SESSION_DATA ADD CONSTRAINT FK_SESSION_DATA_USER_AGENT_ID
FOREIGN KEY (USER_AGENT_ID) REFERENCES USER_AGENT (ID) ON UPDATE CASCADE;

Page Data

Page view data should record the page viewed, the referring page as well as information on how long the user remained on the page.

CREATE TABLE DATA_PAGE_VIEWS
(
ID Bigint NOT NULL,
SEO_DATA_ID Bigint NOT NULL,
URL Varchar(1000),
VISITED Timestamp,
SECONDS Bigint,
REFERRER Varchar(1000),
POST_BACK Char(1),
URL_HASH Bigint NOT NULL,
CONSTRAINT PK_DATA_PAGE_VIEWS_ID PRIMARY KEY (ID)
);
ALTER TABLE DATA_PAGE_VIEWS ADD CONSTRAINT FK_DATA_ID
FOREIGN KEY (DATA_ID) REFERENCES SESSION_DATA (ID) ON UPDATE CASCADE ON DELETE CASCADE;
CREATE INDEX IDX_DATA_PAGE_VIEWS_DATE ON DATA_PAGE_VIEWS (VISITED);
CREATE INDEX IDX_DATA_PAGE_VIEWS_HASH ON DATA_PAGE_VIEWS (URL_HASH);

Summary

Session management is required as part of the user experience on an interactive website, it can provide users with a more personalized experience, if used Geo IP location data can further enhance and personalize the website.

Storing and reporting the session data can help provide essential information for market research, SEO and understanding how end users interact online.

The next step is to process the session data!