Google Analytics for node.js - Writing an npm Module
Sep 12, 2012

Google Analytics plus Node.js

On its own, Node.js is a powerful platform for developing all sorts of applications for everything from the web to even robots (full disclosure: I'm into robots).

Like all programming languages, the basics of Node are like LEGO® building blocks: you can build anything you can think of (unless Tommy's sister hid that one piece you were going to turn into a cannon).

npm (purposely written in all lowercase) stands for Node Package Manager. These packages are modules that you and other members of the Node community have written to make your life a little easier. Sometimes these packages are rather simple (like a conversion tool), and sometimes they are rather complex (like the express framework).

Writing a module can come across as rather daunting at first, but with a few key steps, it's rather easy

My First npm Module

I recently published my first npm module, called Nodealytics, which is a server-side Google Analytics (GA) tool for Node that is based on the Gabba Ruby gem by The Hybrid Group. The purpose of this module is to help customize events for analysis in GA, so we can track when users visit specific pages/press certain buttons or even dynamically tally who is visiting and when.

In this particular case, I had the luxury of seeing someone else (Gabba) do all the heavy work. I just needed to port the code over from Ruby for use by members of the Node community. (As an aside: the beauty of open source is that you don't have to always make everything from scratch—as long as you attribute where you found the stuff you're using, you're good to go.)

So, I started by looking at Gabba's current source and decoding what was going on.

I'm assuming you have the latest versions of both Node and npm installed. The first step is to initialize your module using npm init. This is an easy way to set up the module's package.json, which will set up your module's name, description, dependencies, and other basics.

The next step is to understand how Node modules work. When adding a node module to an application, the require syntax is used:

The way this works is that when the module is required, it is passed into the module variable.

From the inside of the module, we tell the code that it's destined for module-dom by using the module.exports syntax:

Now we can fill in the code. I won't go into how to translate from Ruby to JavaScript here, nor will I go into the depths of testing dos and don'ts. But suffice it to say, the translation part was rather simple, and if you're not using test- (or behavior-) driven development, then get started pronto.

The difficult part about creating the Nodealytics module is that testing is a bit backwards. I can test to ensure that an appropriate http response occurs when sending data, and I can confirm page-views almost immediately, but events take at least 8 hours to show up on the Google Analytics dashboard. What's more, it's nearly impossible to test using a fake account; Google seems to require an official connection (i.e. attached to a real, live site) before it is willing to show off any analytics data.

That said, the major pieces (customized events and page-views) have successfully been ported over from Gabba to Nodealytics. More work can still be done, however, so feel free to contribute.

Now that the module has been (mostly) written, all tests are passing, and we're ready to share with the world, it's time to publish. Fortunately, publishing an npm module is as easy as starting one, with npm publish—no fuss, no muss. If you don't already have a username and password, you'll be able to create one on the spot.

Resources:

http://npmjs.org

http://cnnr.me/b/2012/05/your-first-node-dot-js-module/

http://howtonode.org/how-to-module

http://anders.janmyr.com/2012/04/writing-node-module.html