ProPublica

Journalism in the Public Interest

Cancel

The ProPublica Nerd Blog

Pixel Ping: A node.js Stats Tracker

.

Since the day we launched, ProPublica has encouraged people to republish our stories for free. We even license our stories under Creative Commons (CC). However, in the past we've had trouble knowing precisely which stories had been republished where, and we had no way of knowing how many people were reading our stories on sites that republished them under our CC license.

Shortly after the redesign of our site, we started working on a system that would help us solve this problem. When we found out that Jeremy Ashkenas, a developer at DocumentCloud, was working on a similar problem, we joined forces, and finished work on a lightweight stats tracker, which we are open sourcing today.

World, meet Pixel Ping.

Pixel Ping is a minimalist pixel tracker -- written in CoffeeScript and compiled into JavaScript -- that runs on top of a tremendously fast server-side JavaScript framework called node.js. Pixel Ping tracks each request for a small 1x1 image and periodically flushes aggregated totals of those requests to an endpoint (in our case, a MySQL database).

Pixel Ping is not a particularly new idea, but other solutions (Google Analytics, Omniture, etc) are designed for a different environment and purpose, and can require a fair amount of resources up front. Also, one of the requirements in our use case is that our pixel tracker count only what’s necessary, and not create any privacy worries on the part of sites that republish our material. So our internal implementation of Pixel Ping just tracks page impressions. It doesn’t know anything about individual users, and it doesn’t set or read any cookies.

Pixel Ping is damn fast: an ApacheBench test averaged 3ms per request for 1,000 requests (yadda-yadda, local requests, grain of salt, etc.), and it also only uses around 8 megabytes of memory while it is running.

Along with Pixel Ping we're open sourcing a simple backend for Pixel Ping, written in Ruby on Rails, called Pixel Pong, which we use at ProPublica to provide a reporting interface on the collected data.

The Setup

Pixel Ping Only

Before you install Pixel Ping you'll need node.js, and npm. Once you've installed both, you can run:

npm install pixel-ping

and then run:

pixel-ping path/to/config.json

where config.json looks something like this:

{
    "host": "127.0.0.1",
    "port": "9187",
    "interval": 600,
    "endpoint": "http://www.example.com/save_hits"
  }

You can test that it works by going to

http://127.0.0.1:9187/pixel.gif?key=test

in your browser.

Pixel Ping with Pixel Pong

The easiest way to get up and running with Pixel Ping is to install Pixel Pong with

git clone http://github.com/propublica/pixel-pong.git

then install DocumentCloud's jammit with:

cd pixel-pong; rake gems:install

You'll also need a database.yml in the config directory then run

rake db:create; rake db:migrate

Then install Pixel Ping with:

rake pixel:install

and then add a file in the config directory called config/pixel-ping.development.json with the following:

{
    "port": "9187",
    "host": "127.0.0.1",
    "interval": 5,
    "endpoint": "http://127.0.0.1:3000/stats.json",
    "secret": "one very good secret"
  }

Once you're all set you can start the Rails app with:

script/server

then in a new shell go to the Pixel Pong directory and start Pixel Ping with:

rake pixel:start

Now if you go to the Pixel Ping tracking gif url:

http://127.0.0.1:9187/pixel.gif?key=test☃http://www.example.com

a couple of times you'll see hits logged every second in the reporting interface.

Pixel Pong accepts flushes from Pixel Ping that are delimited by a ☃, in this case the title would be "test" and the url would be "http://www.example.com".

If you point your web browser to:

http://127.0.0.1:9187/pixel.js

You'll see a small bit of javascript that you can include on your own pages. It will insert the pixel and form the request properly so you won't have to find the ☃ on your keyboard.

Both DocumentCloud and ProPublica have been running Pixel Ping instances in production for over a month without problems, but as with any open source project we expect there will be bugs. If you find any you can use the issue trackers (Pixel Ping and Pixel Pong) or send an email to Jeff.Larson@propublica.org.

To quote John Hodgman in his fabulous address to President Obama (at Radio & TV correspondents dinner):

“Hello nerds!”

http://www.youtube.com/watch?v=yW7OPByRGDY

Of course, you’ve already seen it.

Very cool. I’ll be looking to modify this so it tracks unique impressions -thanks!

This blog seems to have started and quickly stopped… is it going to be continued?

There’s an alpha-version Drupal integration module here:

http://github.com/mshmsh5000/pixel_pusher

Discussion here:

http://groups.drupal.org/node/94609

alex kessinger

Dec. 17, 2010, 1:31 a.m.

Just wanted to let everyone know that I wrote a blog post about how you can use Mix Panel to do almost the same thing as above but you don’t need to run a node server.

http://alexkessinger.net/story/doing-pixel-ping-without-server

Any way for run pixel-ping as a service?. I’m trying to do that.

i’m trying with creating some ^.sh file, and some monit configuration.

Add a comment

Email me when someone responds to this article.