Question

Couch has a REST interface. This means that data-updates are exclusive to PUT calls.

I'm inspecting ways to implement a humble analyics counters, and came accross the features of couchdb, sofa and couchapp - which are kin'da cool, having in mind my strong JavaScript orientation.

However, most web-analytics servcies end with making count update calls using requesting some resource, usually in an IMG or SCRIPT tag.

  • Is there a way I can use couchApp to use GET request to perform my counts?
  • Would that be abuse of the architecture? I mean, not everything in couch is REST - i,g, - the administration parts are not.

I'd be very happy to hear what the experts have to say :)

** Editted *

I just noted that CouchDB and Sofa are shipped with a Mochiweb web-server! Maybe there's a way I could hook on that?

Was it helpful?

Solution

Fork or plugin idea

If you are an Erlang programmer (or you're looking for a new project to learn Erlang), then you definitely can write anything you want as a plugin/extension to CouchDB. The smallest example I know of is Die CouchDB, my proof-of-concept which adds one query that will simply stop the server.

https://github.com/iriscouch/die_couchdb

You could in principle write a plugin or fork of CouchDB to handle GET requests and do anything with them.

Note about REST architecture

I am not super familiar with analytics implementations, but the point of REST and HTTP is that GET queries have no side-effects and/or are idempotent (running 50 queries is no different from running one).

The upshot is, proxies can and will cache many GET responses, in both standard and nonstandard ways. That seems incompatible with user tracking and data gathering techniques; however maybe analytics tools still think the benefits outweigh the costs.

For most people, it's probably easier to use external tools.

Log idea

One trick is to GET anything from Couch, and then check the log entry from couch. You can get the couch log by querying /_log as the admin. The log will show users' IP address, request path, and any query parameters.

For example

$ curl -X GET http://localhost:5984/?userid=abcde\&windowsize=1024x768\&color=blue
{"couchdb":"Welcome","version":"1.1.0"}

$ curl localhost:5984/_log | grep userid
[Mon, 23 May 2011 00:34:54 GMT] [info] [<0.1409.0>] 127.0.0.1 - - 'GET' /?userid=abcde&windowsize=1024x768&color=blue 200

Next you can process that log entry and re-insert into your actual analytics database yourself.

Wrapper idea

A final solution is to run a simple reverse-proxy which converts your GET requests into whatever you need. NodeJS is getting popular for tasks like that, but you can use any web platform you prefer: PHP, ASP, JSP, whatever you know already.

You simply respond to the GET request and do whatever you need on the server side, such as inserting the relavant information into your analytics db.

Good luck!

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top