npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2025 – Pkg Stats / Ryan Hefner

node-analytics

v1.2.15

Published

NodeJS web analytics

Readme

#node-analytics

node-analytics is an ExpressJS visitor analytic middleware.

Installation

$ npm install --save node-analytics

Prerequisites

Complete node-analytics functionality relies upon the following:

  • MongoDB: a mongod instance must be running on the server for data storage
  • a MaxMind GeoIP database for location data; the free GeoLite2 City database is available here
  • an open WebSocket for client behaviour data collection; ensure port is open (default is 8080) or pass in server object

Though it will function without one, some, or all of the above.

Basic usage

app.js

In its current iteration node-analytics will process every server request, including static files. To restrict it to page loads call the middleware after the express.static middleware.

var express = require('express')
,   analytics = require('node-analytics')

var app = express();

// app middleware //
app.use(express.static(path.join(__dirname, 'public')));
app.use(analytics());   // beneath express.static

Client

The client script node-analytics-client.js must be included on the served webpage beneath the socket.io client script. Specify the client JS directory client_dir as an option and the file will be automatically copied, or disable this behaviour by client_copy: false.

<script type='text/javascript' src="https://cdn.socket.io/socket.io-1.4.5.js"></script>
<script type='text/javascript' src='/path/to/node-analytics-client.js'></script>

User behaviour

On the client side, node-analytics logs clicks, reaches, and reads of elements, each assigned their own class:

Event | Logged when element | Element class | Note --- | --- | --- | --- click | clicked (e.g. link) | na_click | Emits for every click reach | scrolled to | na_reach | Emits once per page load read | paused at and, presumptively, read | na_read | Tallies time spent paused within element bounds

Events will be associated with an element's id property, or will otherwise be assigned an index.

<section id="contact" class="na_read">      //id: contact
  
  <a href="mailto:me" class="na_click">     //id: reach_point_0
    Contact us
  </a>
  
  <span="fine_print" class="na_reach">      //id: read_section_0
    in exchange for your marbles
  </span>
  
</section>

Options

node-analytics may be adapted by the following options:

app.js

Key | Description | Default --- | --- | --- db_host | MongoDB host | 'localhost' db_port | MongoDB port | 27017 db_name | MongoDB database name | 'node_analytics_db' ws_port | WebSocket port; disabled if ws_server or s_io is set | 8080 ws_server | Express server object; disabled if s_io is set | null s_io | socket.io server object | null geo_ip | Use GeoIP boolean | true mmdb | MaxMind DB path | 'GeoLite2-City.mmdb' log | Output log boolean | true error_log | Error log boolean | true secure | Cookies: HTTPS only boolean | true secret | Cookies: AES encryption key | 'changeMe'

Example use with WebSocket server instead of port (see necessary client edit below):

var server = require('http').createServer(app);
server.listen(80);

app.use(analytics({
  ws_server:  server
}));

Example use with socket.io server:

var server = require('http').createServer(app);
server.listen(80);

var io = require('socket.io').listen(server);
// OR:
var io = require('socket.io').listen(8080);

app.use(analytics({
  s_io: io
}));

Client

Key | Description | Default --- | --- | --- ws_host | Websocket host | location.hostname ws_port | Websocket port; disable if using ws_server or s_io in app.js | 8080 click_class | Click-log class | 'na_click' reach_class | Reach-log class | 'na_reach' read_class | Read-log class | 'na_read' force_protocol | Force 'http' or 'https' socket connection | null

Example use including server support (editing node-analytics-client.js):

var na_obj = {
    ws_port:        null          // must be disabled if server object is being used
  , click_class:    'clicked_me'
  , reach_class:    'reached_me'
  , read_class:     'read_me'
};