How to collect visit statistics on a Sinatra web app?

216 views Asked by At

I'm writing a blog application in Sinatra, and I want to collect some visit statistics.

As of now, I'm only thinking of getting more or less reliable visit statistics per user (that is, page visits grouped by users). Maybe later I'll want to get some client-related information (i.e., user agent).

How do I do that?

2

There are 2 answers

3
art-solopov On

My idea to do it:

  • Use Rack sessions to determine the visitor ID;
  • Store the hits in a database table
  • Write a Thor task to unload it into something human-readable.

I'll appreciate any critique of this idea and/or any other ideas to do it.

3
NolanDC On

While you can use Sinatra to do this, the technology has already been implemented in other ways. I think the easiest solution is to put a piece of Javascript on the frontend that records this information for you. The most popular library for doing this is Google Analytics. This will give you far more information than you could easily capture yourself (screen size, device, etc..), and in a very clean format.