ASP.NET Core 5.0 - Homegrown Analytics

This article introduces a series about collecting and analyzing a web application's usage metrics. I will assume you have downloaded the ASP.NET Core 5.0 - Homegrown Analytics Project or created a new ASP.NET Core 5.0 Razor Pages project. See Tutorial: Get started with Razor Pages in ASP.NET Core.

Homegrown Analytics Project and Article Series

The project is feature complete and will increment the version with updates. The details page includes the version change log. This project is designed and organized to allow easy integration with existing projects. The KenHaggerty. Com. SingleUser NuGet package segregates the user logic. SQL scripts to create the schema, tables, and default data are included in the package. Details, screenshots, and related articles can be found at ASP.NET Core 5.0 - Homegrown Analytics Project.

I always thought of logging page hits as complimentary to Google Analytics. By adding a SessionId cookie which expires when the browsing session ends and ASP.NET Core SignalR for monitoring active connections (online users), I have gotten closer to a Google Analytics alternative. This series will explore some of the issues and considerations for collecting and analyzing usage metrics.

Integration

This project is designed and organized to allow easy integration with existing projects. I have been tweaking the project as I integrate it with my live sites and real-world data. The KenHaggerty. Com. SingleUser NuGet package segregates the user logic. I have developed SQL scripts to create the schema, tables, and default data which are included in the download package.

Cookie Consent

The project implements a cookie policy banner and CheckConsentNeeded from Microsoft. AspNetCore. CookiePolicy to request user consent for non-essential cookies. If granted, I use cookies to capture screen size and time zone. I consider the SessionId cookie which expires when the browsing session ends as essential. Because of the confusion and interpretations of the GDPR and other privacy laws, my code includes an option to set the SessionId cookie's essential state. See The 2020 Guide to Google Analytics and GDPR Compliance

Analytic Data Collection

I capture whether the user is authenticated and the request's method, path, query string, and user agent. I capture the HttpContext. Connection. RemoteIpAddress but I make it anonymous by blanking the Host ID (the last segment) with zero. See How-To Geek - How Do IP Addresses Work? I developed a middleware service to store the data with a PageHit entity. I display the PageHits on a page which filters on properties. By adding a Session entity, I can capture the first or landing page the user browses on my site.

Not Found Errors

I used to send myself an email every time the Error page was requested. See ASP.NET Core 2.2 - Error and Exception Handling After getting too many emails for not found errors generated by probing robots, I developed a NotFoundError entity and log rather than emailing not found errors.

Preferred UserAgents

I have upgraded the PageHit entity's UserAgent property from a string to an int. The UserAgentId relates to a child UserAgent entity with a Preferred property. Rather than trying to detect robot page hits, I can filter on Preferred UserAgents. I developed a UserAgents page which displays the UserAgent's PageHits count and provides the functions to add or remove the preferred status.

Analytic Dashboard

I developed a Dashboard to display Session and PageHit properties with frequency counts filtered by 1, 3, 7, or 30 previous days. The Dashboard implements links to the Sessions and PageHits pages with the filter for the related property applied. The Dashboard includes links to the default PageHits, Sessions, NotFoundErrors, and UserAgents pages.

ASP.NET Core SignalR

I use Google Analytics' Active Users feature mainly to detect if any users are online before I take the site offline for updates. I implemented and deployed an online user feature based on a couple of examples I found which count online users with SignalR. I have been analyzing and comparing the feature with Google's Active Users. The project has an option to enable SignalR and the online user feature. The option is disabled by default.

Google reCAPTCHA

Before I implemented Google reCAPTCHA v3 on the contact and register pages, I was getting emails and users created by robots. Since, I haven't noticed any. You must register your site to receive an API key and secret. You set the secret, key, and a pass/fail threshold in appsettings.json. reCAPTCHA v3 returns a score (1.0 is very likely a good interaction, 0.0 is very likely a bot). The project has an option to enable Google reCAPTCHA. The option is disabled by default.

Hangfire

Online log maintenance like purging old records are usually long running tasks. Hangfire is better than Background tasks with hosted services for fire and forget tasks. Hangfire is easy to implement. The Hangfire dashboard provides detailed status on running, completed, and failed jobs. The project has an option to enable Hangfire. The option is disabled by default.

Ken Haggerty
Created 12/31/20
Updated 01/06/21 05:56 GMT

Log In or Reset Quota to read more.

Article Tags:

Analytics EF Core Model
Successfully completed. Thank you for contributing.
Contribute to enjoy content without advertisments.
Something went wrong. Please try again.

Comments(0)

Loading...
Loading...

Not accepting new comments.

Submit your comment. Comments are moderated.

User Image.
DisplayedName - Member Since ?