Server side analytics

I wanted to keep trace of visitors and know the usual stuff of web analytics: visitors, source, nationality, behaviour and so on. 
And client side analytics are not so reliable:

  • Ad Blockers interfere with them
  • Using a third party service require to annoy the user with those huge cookie consent banners
  • They drastically increase the loading time of the web application
  • They don't register API calls and any other not-html calls like web api

So i developed by myself a very simple server side analytics system for AspNet Core, which is running is also here my website.
SSA is an AspNet Core middleware that inserted in to the task pipeline of the application will be invoked every time a new request is recieved.
Here you can find a Live Demo of the system.

If you find a bug or you got an idea on to improve the system, please contribute to the GitHub Repository

 

How to use

To insert the middleware into the task pipeline import the NuGet package and invoke the extension method UserServerSideAnalytics  in your application Startup:

// Startup.cs        
// This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
public void Configure(IApplicationBuilder app, IHostingEnvironment env) { app.UseServerSideAnalytics(new AnalyticStore()) }

 

This method requires an IAnalyticStore interface that is gonna be the place where our recieved request will be stored.
Implementing your own store is quite simple but if you're lazy you can use one of mine:

 

Configuration

By default SSA record every request coming to the server, which can produce a lot of bloat data.
You can configure SSA to exclude some request or to limit is recording to specific sets of requests by using fluid extension methods during the assigment in application Startup.


//Startup.cs // This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
public
void Configure(IApplicationBuilder app, IHostingEnvironment env) { app.UseDeveloperExceptionPage(); app.UseBrowserLink(); app.UseDatabaseErrorPage(); app.UseAuthentication(); //Let's create our middleware using Mongo DB to store data app.UseServerSideAnalytics(new MongoAnalyticStore("mongodb://localhost/matteo")) // Request into those url spaces will be not recorded .ExcludePath("/js", "/lib", "/css") // Request ending with this extension will be not recorded .ExcludeExtension(".jpg", ".ico", "robots.txt", "sitemap.xml") // I don't want to track my own activity on the website .Exclude(x => x.UserIdentity() == "matteo") // And also request coming from mmy home wifi .ExcludeIp(IPAddress.Parse("192.168.0.1")) // Request coming from local host will be not recorded .ExcludeLoopBack(); app.UseStaticFiles(); }

 

IP Geocoding

One of the most interesting data of every analytic system is to know where your user come from.
This can be a quite annoying operation due to difficulties of find a reliable an cheap database of countires and their relative ip address ranges.

For this reason i wrote other three analytic stores that acts as wrappers around an existing one to provide fallback geo-IP resolution.
If the first repository doesn't contains a valid IP range ffor the client It will ask to the second one and so on.
If at the end the chain a valid geo-IP has been found this i saved into the main store.
I wrote three of them, if you want to add more, please contribute on GitHub.
You can find those analytic store in ServerSideAnalytics.Extensions

  • IpApiAnalyticStore : add ip-geocoding using Ip Api (ip-api.com)
  • IpInfoAnalyticStore : add ip-geocoding using Ip Stack (ipinfo.io)
  • IpStackAnalyticStore : add ip-geocoding using Ip Stack (ipstack.com)

Usage example:


public IAnalyticStore GetAnalyticStore() { var store = (new MongoAnalyticStore("mongodb://localhost/")) .UseIpStackFailOver("IpStackAPIKey") .UseIpApiFailOver() .UseIpInfoFailOver(); return store; }