Alex Rivera | Logout

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

Asked 2012-05-08T08:15:20.483
13

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?

Edit
Report

2 Answers

2

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"]);
answered 2012-06-16T14:35:44.450
2

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 :)


answered 2013-04-03T10:29:04.003

Your Answer