ASP.NET - Count feed subscribers without Feedburner


The most important advantage of Feedburner is the subscriber count. But Feedburner is not an option for everyone, since the address of your feed changes if you move it to Feedburner.
You could redirect requests to your feed to the new address, the disadvantage though is that when someone opens the URL in his browser the browser will show the new address.
In this post I will show an approach how you can get a subscriber count, without moving your feed to Feedburner.

The basics

To count the number of readers we have to differentiate two cases:

  • Requests from a single user: Due to proxy servers or NAT environments we cannot distinguish users by their IP address. Thus we will use the user agent together with the IP address to improve the approximation.
  • Requests from a feed aggregator like Google Feedfetcher: Most aggregators can be identified by their user agent. Many aggregators additionally supply the number of their subscribers. For some sample user agents have a look at this blog post.

Implementation of a subscriber counter in ASP.NET

To count the subscribers of a feed I chose to implement a custom IHttpModule. The module checks at the beginning of each request whether the feed is accessed. If this is the case, statistics are captured depending on the supplied user agent.
A regular expression is used to identify feed aggregators and to determinate the number of readers:

var match = Regex.Match(request.UserAgent, @".+?(\d*).?(?>subscribers|readers|users).?(\d*).*");

string application = request.Browser.Browser + " " + request.Browser.Version;

if (application.StartsWith("Unknown", StringComparison.OrdinalIgnoreCase))
  application = Regex.Match(request.UserAgent, @"^(?>\w|-)*", RegexOptions.Compiled).Value;

if (match.Success)
  string numberOfSubscribers = match.Groups[1].Value;

  if (string.IsNullOrEmpty(numberOfSubscribers))
    numberOfSubscribers = match.Groups[2].Value;

  string identifier = request.UserHostAddress + request.UserAgent;
  this.FeedStatisticsStore.AddSingleUser(application, identifier, DateTime.Now);

The IHttpModule relies on two interfaces (IFeedStatisticsStore and IFeedSubscriberNotifier).
The IFeedStatisticsStore is responsible to store the captured statistics. I provide one implementation which saves the statistics in a file and caches them in memory. You could create your own implementation where you can e.g. use a database as storage.
The IFeedSubscriberNotifier is used to report the statistics. The IHttpModule executes the IFeedSubscriberNotifier when the IFeedStatisticsStore's property 'SendReport' returns true. My provided implementation sends an daily email containing all relevant information. By creating your own implementation you can again customize this behavior.

You can download my source code and use it for your own subscriber counter. The code still contains some TODOs to mark the positions where you have to complete it.
If you use my EmailFeedSubscriberNotifier don't forget to enter your email address or to retrieve it from a config file. Perhaps you want to add some exception handling in case that sending the email fails.


Feedly Feedly Tweet

Related posts