

User analytics in under five minutes - rywalker
http://blog.differential.com/prowd-plus-astronomer/

======
angersock
Everyone should blow an afternoon writing a simple analytics collection system
for their site. It's an interesting exercise, and makes you appreciate the
offerings in the space much more.

~~~
rywalker
Good suggestion, angersock — though it might take a bit more than an afternoon
to get it done ;)

~~~
angersock
It's not too bad, honest!

Use an events model for analytics--track page loaded, page left, and maybe a
handful of other events (to taste). Spec out that the API (from the client JS)
is something like:

    
    
      emitAnalyticsEvent( eventType, location, browserTime, optionalData )
    

Register some event handlers to call this. browserTime and location (from
navigator) are done to allow handling of single-page routing properly.

On the server side, on receipt of the request, timestamp it again, and insert
wholesale into a database table (in postgres, use the jsonb type to store the
unstructured optionalData ). In optionalData, note the userAgent and IP from
the headers. Additionally, note the session and user associated with the
event.

Congratulations! You've now got a flexible (albeit barebones) setup.

 _Visualizing_ and _analyzing_ the results may take more than an afternoon,
but that's outside the collection scope. Even still, it's trivial to show user
history, visitor IPs, and high-level traffic patterns.

