How to track usage on a node.js RESTful service with Google Analytics?

I've written a RESTful node.js service as a backend for http://www.cross-copy.net and would like to not only track usage of the web-client but also other clients (like commandline or Apps) which use the service for inter-device copy/paste. Is it possible to embed the Google Analytics JavaScript API into a node.js application and do server-side tracking?

You won't be able to just drop ga.js into your Node project. It has to be loaded in a browser to function correctly.

I don't believe there is anything out there for Node yet (correct me if I'm wrong!), but you should be able to easily adapt the existing PHP classes for doing logging server-side:

https://developers.google.com/analytics/devguides/collection/other/mobileWebsites

You can see how the URL to request the tracking GIF is constructed within ga.php. Translate ga.php to JS and you're set.

$utmGifLocation = "http://www.google-analytics.com/__utm.gif";

// Construct the gif hit url.
$utmUrl = $utmGifLocation . "?" .
    "utmwv=" . VERSION .
    "&utmn=" . getRandomNumber() .
    "&utmhn=" . urlencode($domainName) .
    "&utmr=" . urlencode($documentReferer) .
    "&utmp=" . urlencode($documentPath) .
    "&utmac=" . $account .
    "&utmcc=__utma%3D999.999.999.999.999.1%3B" .
    "&utmvid=" . $visitorId .
    "&utmip=" . getIP($_SERVER["REMOTE_ADDR"]);

As Brad rightfully sad, there was nothing for Node... So I wrote a nodejs module tailored for this these last few days and just published it on NPM: node-ga

The module is still really new (barely trying it in production on a pet project), so don't hesitate to give your input :)


Since all of the answers are really old, I will mention a new npm package: https://www.npmjs.com/package/universal-analytics

It seems really great.

I tried out node-ga, but didn't get event tracking to work. nodealytics did the job.