Skip to content

Tracking (most viewed/commented/etc.)

Node.js server

The Node.js server for tracking is a web server that collects pageviews from the public facing part of the project, incrementing a counter in Redis. In order to share the same configuration parameters between Symfony2 and node.js, the wf:tracking:nodejs:start command was created to read the Sf parameters and pass them to the node process.

The parameters used to configure the Node.js server are:

yaml
wf_tracking.nodejs.host: 0.0.0.0 #The host that the server should bind to
wf_tracking.nodejs.port: 8080 #The port that the server should bind to
wf_tracking.redis.host: 127.0.0.1 #The IP of the Redis server
wf_tracking.redis.port: 6379 #The port of the Redis server

The Node.js was tested in production with version v0.10.36.

The command above starts the node command, so make sure that node is in the search path. Please note that, since this process should normally be started from supervisor, supervisor does not read the .bashrc or .zshrc of the user. So the node executable should exist (or have a symlink, at least) in a standard directory (/usr/bin, for example)

Once the server is started, it will listen to requests for /track/pageviews/article-ID on the %wf_tracking.nodejs.host%, port %wf_tracking.nodejs.port%. You can either change the %wf_tracking.nodejs.url% parameter to include the port (e.g. http://project.com:8080/, or you can setup forwarding to this server for /track requests.

The simplest check if the node.js server works, open http://__PROJECT_URL__:__NODE_PORT__/track/pageviews/article-1 in the browser and see if it returns an image. To make things as fast and as low on resources as possible (the node.js is hit for every pageview, without caching, and it is very fast 😃 ), the node.js tracker process doesn't check the validity of the article ID (that's done at processing), so it can be anything.

Tracker processor

The node.js server only increments the number of pageviews/comments for each article when a request is made to it. It doesn't compute the list of most read/commented/etc. This processing is done by the wf:tracking:process command, that takes the keys incremented by the node.js server and builds the lists of most viewed/commented/etc. articles.

As this command should run continuously, it accepts the parameters:

  • --time the time (in seconds) to sleep between runs. Default 600 (10 minutes)
  • --iteration To make sure the eventual memory leaks don't end up eating the whole available RAM, restart this command after iteration iterations.

Tracking namespaces

The tracker can track hits on different namespaces. These namespaces are configured through the @wf_tracking.namespaces@ parameter. By default, it defines the following namespaces:

  • pageviews
  • comments
  • shares

To log a hit to one of these namespaces, you must add on the client side an image with the URL http://__PROJECT_URL__:__NODE_PORT__/track/__NAMESPACE__/article-__ARTICLE_ID__

So, for pageviews, you can add on the article page:

html
<img src="/track/pageviews/article-1" />

To track hits that occured following a user event, add some JavaScript code to the function that handles that user event:

var image = new Image();
image.src = '/track/shares/article-' + articleId;

Of course, you'd have to pass that articleId variable from Symfony to the JS.