{"id":14957630,"url":"https://github.com/sebinsua/express-keenio","last_synced_at":"2025-05-02T07:32:49.551Z","repository":{"id":12504196,"uuid":"15173818","full_name":"sebinsua/express-keenio","owner":"sebinsua","description":":bar_chart: Rapidly install analytics with a middleware.","archived":false,"fork":false,"pushed_at":"2017-05-02T11:38:00.000Z","size":183,"stargazers_count":42,"open_issues_count":4,"forks_count":3,"subscribers_count":5,"default_branch":"master","last_synced_at":"2024-10-29T23:14:56.928Z","etag":null,"topics":["analytics","connect","express-js","human","keen-io","middleware"],"latest_commit_sha":null,"homepage":"http://keen.io","language":"JavaScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"bsd-2-clause","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/sebinsua.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2013-12-13T20:51:53.000Z","updated_at":"2023-05-12T16:40:04.000Z","dependencies_parsed_at":"2022-09-05T13:00:13.403Z","dependency_job_id":null,"html_url":"https://github.com/sebinsua/express-keenio","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/sebinsua%2Fexpress-keenio","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/sebinsua%2Fexpress-keenio/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/sebinsua%2Fexpress-keenio/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/sebinsua%2Fexpress-keenio/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/sebinsua","download_url":"https://codeload.github.com/sebinsua/express-keenio/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":224305845,"owners_count":17289446,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["analytics","connect","express-js","human","keen-io","middleware"],"created_at":"2024-09-24T13:15:15.940Z","updated_at":"2024-11-12T16:04:49.074Z","avatar_url":"https://github.com/sebinsua.png","language":"JavaScript","funding_links":[],"categories":["JavaScript"],"sub_categories":[],"readme":"[express-keenio](http://sebinsua.github.io/express-keenio/)\n==============\n\n[![Build Status](https://travis-ci.org/sebinsua/express-keenio.png)](https://travis-ci.org/sebinsua/express-keenio) [![npm version](https://badge.fury.io/js/express-keenio.svg)](https://npmjs.org/package/express-keenio)\n\nInstall [Keen IO analytics](http://keen.io/) support into your Node.JS [Express.js](https://github.com/visionmedia/express) app in mere seconds and instantly begin capturing data.\n\nOnce 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()`.\n\nRead about [why the middleware was made and the use cases it solves here](https://keen.io/blog/78561215787/how-to-install-keen-io-analytics-into-your-node-js-app).\n\nGetting Started\n---------------\n\n**[Sign up to Keen IO for free here](https://keen.io/signup)**. And then install the package from the command line with:\n\n```shell\n$ npm install express-keenio\n```\n\n\nUsage\n-----\n\nIt's possible to use the middleware with specific routes decorator-style, like so:\n\n```javascript\nvar express = require(\"express\"),\n    keenio = require('express-keenio');\n\nvar app = express();\n\nkeenio.configure({ client: { projectId: '\u003ctest\u003e', writeKey: '\u003ctest\u003e'} });\nkeenio.on('error', console.warn); // There are 'error', 'info', 'warning', 'debug', 'track', and 'flush' events which are emitted.\n\napp.get('/test', keenio.trackRoute('testCollection'), function (req, res) {\n  // You code goes here.\n});\n\napp.post('/payment', keenio.trackRoute(\"payments\",\n                                      { query: ['userId', 'itemId', 'type', 'quantity', 'price'],\n                                        reaction: ['receipt.status', 'receipt.tax'] }), function (req, res) {\n  // Your code goes here.\n});\n\napp.listen(3000);\n```\n\nIt's also possible to make the middleware handle all routes by `use`ing it against the `app`:\n\n```javascript\nvar express = require(\"express\"),\n    bodyParser = require(\"body-parser\"),\n    keenio = require('express-keenio');\n\nvar app = express();\n\nkeenio.configure({ client: { projectId: '\u003ctest\u003e', writeKey: '\u003ctest\u003e' } });\napp.configure(function () {\n   app.use(bodyParser.json());\n   app.use(keenio.handleAll());\n});\n\napp.get('/test', function (req, res) {\n   // Your code goes here.\n});\n\napp.listen(3000);\n```\n\nAdditionally, the standard [Keen.IO Node.JS client](https://github.com/keen/keen-js) is exposed on `middleware.keenClient`.\n\nWhat will an event look like?\n-----------------------------\n\nThe middleware will create something that looks sort of like this:\n\n```json\n{\n  \"identity\": {\n    \"user\": {\n      \"name\": \"Joe Bloggs\",\n      \"email\": \"joe@example.com\",\n      \"age\": 17\n    },\n    \"session\": {\n      \"id\": \"some-identifier\"\n    },\n    \"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\"\n  },\n  \"intention\": {\n    \"method\": \"POST\",\n    \"path\": \"/pay-user/5\",\n    \"params\": {\n      \"userId\": 5\n    },\n    \"body\": {\n      \"price\": 5.00\n    },\n    \"query\": {},\n    \"referer\": \"http://keen.io/\"\n  },\n  \"reaction\": {\n    \"success\": true,\n    \"userAddress\": \"...\"\n  },\n  \"httpStatus\": 200,\n  \"environment\": {\n    \"library\": \"express-keenio\"\n  }\n}\n```\n\nConfiguration\n-------------\n\n### Keen.IO Client Configuration\n\nSee [KeenClient-Node#initialization](https://github.com/keenlabs/KeenClient-node#initialization).\n\n```javascript\n{\n  client: {\n    projectId: '\u003ctest\u003e',\n    writeKey: '\u003ctest\u003e'\n  }\n}\n```\n\n### Event Property Limitation\n\nKeen.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.\n\nOnce you are reliant on analytics I **STRONGLY** recommend switching to explicit whitelists.\n\nHowever by default this middleware provides a fallback in the form of [eventually rigid schemas](https://github.com/sebinsua/eventual-schema). 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.\n\n### Whitelist Properties\n\nThere 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.\n\nExample 1:\n\n```javascript\n{\n  client: {\n    projectId: '\u003ctest\u003e',\n    writeKey: '\u003ctest\u003e'\n  },\n  whitelistProperties: {\n    query: ['id', 'userId', 'name', 'type'],\n    body: [],\n    reaction: ['description']\n  }\n}\n```\n\n*NOTE 1: An empty array means nothing is whitelisted while a missing whitelist key means no whitelist should be applied to the data.*\n\n*NOTE 2: `whitelistProperties.query`, `whitelistProperties.body` and `whitelistProperties.reaction` can take deep property identifiers (e.g. 'deep.array[].name' or 'deep.property.value'.)*\n\nExample 2:\n\n```javascript\n\napp.get('/test', keenio.trackRoute(\"testEventCollection\", { query: ['id', 'userId', 'name', 'type'], body: [] }), function (req, res) {\n   // Your code goes here.\n});\n```\n\n### Blacklist Properties\n\nBy 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:\n\n```javascript\n{\n  client: {\n    projectId: '\u003ctest\u003e',\n    writeKey: '\u003ctest\u003e'\n  },\n  blacklistProperties: ['passwordHash', 'apiKey', 'authToken', 'userKey']\n}\n```\n\n*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.*\n\n### Route Configuration\n\nIf 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.\n\n*You must pick either 'routes' or 'excludeRoutes' but not both.*\n\n#### Excluding routes from default middleware operation\n\n```javascript\n{\n  client: {\n    projectId: '\u003ctest\u003e',\n    writeKey: '\u003ctest\u003e'\n  },\n  excludeRoutes: [\n    { method: 'GET', route: 'route-name' }\n  ]\n}\n```\n\n#### Defining route configuration for middleware operation\n\n```javascript\n{\n  client: {\n    projectId: '\u003ctest\u003e',\n    writeKey: '\u003ctest\u003e'\n  }\n  routes: [\n    { method: 'GET', route: 'route-name-1', eventCollectionName: '', whitelistProperties: {} },\n    { method: 'POST', route: 'route-name-2', eventCollectionName: '' }\n  ]\n}\n```\n\n### Defaults\n\nIt's also possible to override some of the default values used by validators, route schemas, etc.\n\n```javascript\n{\n  client: {\n    projectId: '\u003ctest\u003e',\n    writeKey: '\u003ctest\u003e'\n  },\n  defaults: {\n    addons: {\n      ipToGeo: false,\n      userAgentParser: false\n    },\n    MAX_PROPERTY_HIERARCHY_DEPTH: 10,\n    MAX_STRING_LENGTH: 1000,\n    MAX_PROPERTY_QUANTITY: 300,\n    eventualSchemas: {\n      cache: true,\n      cachePath: './route-schemas.cache',\n      query: {\n        MAX_PROPERTIES: 30,\n        NUMBER_OF_INSTANCES: 500,\n        NUMBER_OF_DAYS: 7\n      },\n      body: {\n        MAX_PROPERTIES: 80,\n        NUMBER_OF_INSTANCES: 500,\n        NUMBER_OF_DAYS: 7\n      },\n      reaction: {\n        MAX_PROPERTIES: 120,\n        NUMBER_OF_INSTANCES: 500,\n        NUMBER_OF_DAYS: 7\n      }\n    }\n  }\n}\n```\n\n### Data Enrichment Addons\n\nKeen 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](http://users.keen.io/), [IRC](http://webchat.freenode.net/?channels=keen-io), or at [contact@keen.io](mailto:contact@keen.io).\n\n\n### Middleware Overrides\n\nWhile not recommended it's possible to override some of the internal behaviours of the middleware like so:\n\n```javascript\n{\n  client: {\n    projectId: '\u003ctest\u003e',\n    writeKey: '\u003ctest\u003e'\n  },\n  handlers: {\n    generateIdentity: function (req) {},\n    generateEventCollectionName: function (route) {},\n    parseRequestBody: function (body) {},\n    parseRequestObject: function (req) {},\n    parseResponseBody: function (body) {}\n  }\n}\n```\n\nSupport\n-------\n\nFeel free to submit issues and pull requests.\n\n### Tests\n\n```shell\n$ npm install --dev\n$ npm test\n```\n\n### Contributors\n\n* [Seb Insua](http://github.com/sebinsua) - [@sebinsua](http://twitter.com/sebinsua)\n\nLicense\n-------\n\n[BSD 2-Clause License](https://github.com/sebinsua/express-keenio/blob/master/LICENSE)\n\nCopyright (c) 2014, Seb Insua\nAll rights reserved.\n\nRedistribution and use in source and binary forms, with or without modification,\nare permitted provided that the following conditions are met:\n\n* Redistributions of source code must retain the above copyright notice, this\n  list of conditions and the following disclaimer.\n\n* Redistributions in binary form must reproduce the above copyright notice, this\n  list of conditions and the following disclaimer in the documentation and/or\n  other materials provided with the distribution.\n\nTHIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS \"AS IS\" AND\nANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED\nWARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE\nDISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR\nANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES\n(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;\nLOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON\nANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT\n(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS\nSOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fsebinsua%2Fexpress-keenio","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fsebinsua%2Fexpress-keenio","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fsebinsua%2Fexpress-keenio/lists"}