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 🙏

© 2024 – Pkg Stats / Ryan Hefner

express-keenio

v0.4.6

Published

Express middleware for creating events from request-responses.

Downloads

79

Readme

express-keenio

Build Status npm version

Install Keen IO analytics support into your Node.JS Express.js app in mere seconds and instantly begin capturing data.

Once installed it creates Keen.IO events from HTTP requests based on data intercepted from the calls res.json(), res.jsonp(), res.send(), res.render(), res.redirect(), res.sendfile() and res.download().

Read about why the middleware was made and the use cases it solves here.

Getting Started

Sign up to Keen IO for free here. And then install the package from the command line with:

$ npm install express-keenio

Usage

It's possible to use the middleware with specific routes decorator-style, like so:

var express = require("express"),
    keenio = require('express-keenio');

var app = express();

keenio.configure({ client: { projectId: '<test>', writeKey: '<test>'} });
keenio.on('error', console.warn); // There are 'error', 'info', 'warning', 'debug', 'track', and 'flush' events which are emitted.

app.get('/test', keenio.trackRoute('testCollection'), function (req, res) {
  // You code goes here.
});

app.post('/payment', keenio.trackRoute("payments",
                                      { query: ['userId', 'itemId', 'type', 'quantity', 'price'],
                                        reaction: ['receipt.status', 'receipt.tax'] }), function (req, res) {
  // Your code goes here.
});

app.listen(3000);

It's also possible to make the middleware handle all routes by useing it against the app:

var express = require("express"),
    bodyParser = require("body-parser"),
    keenio = require('express-keenio');

var app = express();

keenio.configure({ client: { projectId: '<test>', writeKey: '<test>' } });
app.configure(function () {
   app.use(bodyParser.json());
   app.use(keenio.handleAll());
});

app.get('/test', function (req, res) {
   // Your code goes here.
});

app.listen(3000);

Additionally, the standard Keen.IO Node.JS client is exposed on middleware.keenClient.

What will an event look like?

The middleware will create something that looks sort of like this:

{
  "identity": {
    "user": {
      "name": "Joe Bloggs",
      "email": "[email protected]",
      "age": 17
    },
    "session": {
      "id": "some-identifier"
    },
    "userAgent": "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_9_1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/32.0.1700.77 Safari/537.36"
  },
  "intention": {
    "method": "POST",
    "path": "/pay-user/5",
    "params": {
      "userId": 5
    },
    "body": {
      "price": 5.00
    },
    "query": {},
    "referer": "http://keen.io/"
  },
  "reaction": {
    "success": true,
    "userAddress": "..."
  },
  "httpStatus": 200,
  "environment": {
    "library": "express-keenio"
  }
}

Configuration

Keen.IO Client Configuration

See KeenClient-Node#initialization.

{
  client: {
    projectId: '<test>',
    writeKey: '<test>'
  }
}

Event Property Limitation

Keen.IO has a set limit of 1000 on the number of event properties belonging to an Event Collection and after this it will begin to drop events.

Once you are reliant on analytics I STRONGLY recommend switching to explicit whitelists.

However by default this middleware provides a fallback in the form of eventually rigid schemas. Firstly, by default we accept up to 30 properties in the intention.query, 80 properties in a intention.body, and 120 properties in a reaction. Secondly, after a route receives 500 requests or exists for a week it stops accepting new event properties. Once these properties are discovered we cache them in a file given by options.defaults.eventualSchemas.cachePath (normally, './route-schemas.cache') however this feature can be switched off by giving options.defaults.eventualSchemas.cache the value false or specifying a complete explicit whitelist against a route.

Whitelist Properties

There are some default property whitelists in the form of whitelistProperties.query, whitelistProperties.body, whitelistProperties.reaction. Whitelists can also exist against each route definition or be passed into the second argument of the keenio.trackRoute() function.

Example 1:

{
  client: {
    projectId: '<test>',
    writeKey: '<test>'
  },
  whitelistProperties: {
    query: ['id', 'userId', 'name', 'type'],
    body: [],
    reaction: ['description']
  }
}

NOTE 1: An empty array means nothing is whitelisted while a missing whitelist key means no whitelist should be applied to the data.

NOTE 2: whitelistProperties.query, whitelistProperties.body and whitelistProperties.reaction can take deep property identifiers (e.g. 'deep.array[].name' or 'deep.property.value'.)

Example 2:


app.get('/test', keenio.trackRoute("testEventCollection", { query: ['id', 'userId', 'name', 'type'], body: [] }), function (req, res) {
   // Your code goes here.
});

Blacklist Properties

By default we delete any 'password' properties. If you wish you can pass in a list of other properties you wish to explicitly blacklist as shown below:

{
  client: {
    projectId: '<test>',
    writeKey: '<test>'
  },
  blacklistProperties: ['passwordHash', 'apiKey', 'authToken', 'userKey']
}

NOTE: blacklistProperties takes a property name that can be found anywhere inside an object. This means that 'passwordHash' would delete properties like intention.query.passwordHash and reaction.passwordHash. It does not allow you to specify exact properties at a particular depth like whitelistProperties.query, whitelistProperties.body and whitelistProperties.reaction each allow.

Route Configuration

If you are not using the decorator-style version of the middleware, and would like either (a) more control over which event collections exist or (b) the ability to disable specific event collections you may configure the routes upfront.

You must pick either 'routes' or 'excludeRoutes' but not both.

Excluding routes from default middleware operation

{
  client: {
    projectId: '<test>',
    writeKey: '<test>'
  },
  excludeRoutes: [
    { method: 'GET', route: 'route-name' }
  ]
}

Defining route configuration for middleware operation

{
  client: {
    projectId: '<test>',
    writeKey: '<test>'
  }
  routes: [
    { method: 'GET', route: 'route-name-1', eventCollectionName: '', whitelistProperties: {} },
    { method: 'POST', route: 'route-name-2', eventCollectionName: '' }
  ]
}

Defaults

It's also possible to override some of the default values used by validators, route schemas, etc.

{
  client: {
    projectId: '<test>',
    writeKey: '<test>'
  },
  defaults: {
    addons: {
      ipToGeo: false,
      userAgentParser: false
    },
    MAX_PROPERTY_HIERARCHY_DEPTH: 10,
    MAX_STRING_LENGTH: 1000,
    MAX_PROPERTY_QUANTITY: 300,
    eventualSchemas: {
      cache: true,
      cachePath: './route-schemas.cache',
      query: {
        MAX_PROPERTIES: 30,
        NUMBER_OF_INSTANCES: 500,
        NUMBER_OF_DAYS: 7
      },
      body: {
        MAX_PROPERTIES: 80,
        NUMBER_OF_INSTANCES: 500,
        NUMBER_OF_DAYS: 7
      },
      reaction: {
        MAX_PROPERTIES: 120,
        NUMBER_OF_INSTANCES: 500,
        NUMBER_OF_DAYS: 7
      }
    }
  }
}

Data Enrichment Addons

Keen IO supports two data enrichment addons: IP-to-GEO conversion and UserAgent parsing. If you would like to activate these addons for your project, just ask! The team is available in HipChat, IRC, or at [email protected].

Middleware Overrides

While not recommended it's possible to override some of the internal behaviours of the middleware like so:

{
  client: {
    projectId: '<test>',
    writeKey: '<test>'
  },
  handlers: {
    generateIdentity: function (req) {},
    generateEventCollectionName: function (route) {},
    parseRequestBody: function (body) {},
    parseRequestObject: function (req) {},
    parseResponseBody: function (body) {}
  }
}

Support

Feel free to submit issues and pull requests.

Tests

$ npm install --dev
$ npm test

Contributors

License

BSD 2-Clause License

Copyright (c) 2014, Seb Insua All rights reserved.

Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:

  • Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.

  • Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution.

THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.