anonlytics-express
Advanced tools
Comparing version 0.1.1 to 0.2.0
{ | ||
"name": "anonlytics-express", | ||
"version": "0.1.1", | ||
"version": "0.2.0", | ||
"description": "Privacy respecting analytics for express apps", | ||
@@ -5,0 +5,0 @@ "main": "src/index.js", |
160
src/index.js
const KeenTracking = require('keen-tracking'); | ||
const responseTime = require('response-time'); | ||
const client = new KeenTracking({ | ||
projectId: process.env.KEEN_PROJECT_ID, | ||
writeKey: process.env.KEEN_WRITE_KEY | ||
}); | ||
module.exports = (options = {}) => { | ||
module.exports = responseTime((req, res, time) => { | ||
// Setup options | ||
const eventCollection = options.eventCollection || 'anonlytics'; | ||
const client = new KeenTracking({ | ||
projectId: options.keenProjectId || process.env.KEEN_PROJECT_ID, | ||
writeKey: options.keenWriteKey || process.env.KEEN_WRITE_KEY | ||
}); | ||
// Setup event data | ||
const eventData = { | ||
http: { | ||
method: req.method, | ||
statusCode: res.statusCode | ||
}, | ||
responseTime: time, | ||
url: { | ||
string: `${req.protocol}://${req.headers.host}${req.originalUrl}` | ||
}, | ||
ip: req.ip.substr(0, 7) == '::ffff:' ? req.ip.substr(7) : req.ip, | ||
userAgent: { | ||
string: req.headers['user-agent'] | ||
}, | ||
referrer: { | ||
url: req.headers.referer | ||
}, | ||
express: { | ||
route: req.route && req.route.path | ||
} | ||
}; | ||
// Return middleware function | ||
return responseTime((req, res, time) => { | ||
// Enable keen date time addon | ||
eventData.keen = { | ||
addons: [ | ||
// Setup event data | ||
const eventData = { | ||
http: { | ||
method: req.method, | ||
statusCode: res.statusCode | ||
}, | ||
responseTime: time, | ||
url: { | ||
string: `${req.protocol}://${req.headers.host}${req.originalUrl}` | ||
}, | ||
ip: req.ip.substr(0, 7) == '::ffff:' ? req.ip.substr(7) : req.ip, | ||
userAgent: { | ||
string: req.headers['user-agent'] | ||
}, | ||
referrer: { | ||
url: req.headers.referer | ||
}, | ||
express: { | ||
route: req.route && req.route.path | ||
} | ||
}; | ||
// Enable keen date time addon | ||
eventData.keen = { | ||
addons: [ | ||
{ | ||
name: 'keen:date_time_parser', | ||
input: { | ||
date_time: 'keen.timestamp', | ||
}, | ||
output: 'timestamp_info' | ||
} | ||
] | ||
}; | ||
// Only enable other keen addons if we have the required input | ||
const keenAddons = [ | ||
{ | ||
name: 'keen:date_time_parser', | ||
name: 'keen:url_parser', | ||
input: { | ||
date_time: 'keen.timestamp', | ||
url: 'url.string' | ||
}, | ||
output: 'timestamp_info' | ||
} | ||
] | ||
}; | ||
// Only enable other keen addons if we have the required input | ||
const keenAddons = [ | ||
{ | ||
name: 'keen:url_parser', | ||
input: { | ||
url: 'url.string' | ||
output: 'url.components' | ||
}, | ||
output: 'url.components' | ||
}, | ||
{ | ||
name: 'keen:ip_to_geo', | ||
input: { | ||
ip: 'ip' | ||
{ | ||
name: 'keen:ip_to_geo', | ||
input: { | ||
ip: 'ip' | ||
}, | ||
output: 'location' | ||
}, | ||
output: 'location' | ||
}, | ||
{ | ||
name: 'keen:ua_parser', | ||
input: { | ||
ua_string: 'userAgent.string' | ||
{ | ||
name: 'keen:ua_parser', | ||
input: { | ||
ua_string: 'userAgent.string' | ||
}, | ||
output: 'userAgent.info' | ||
}, | ||
output: 'userAgent.info' | ||
}, | ||
{ | ||
name: 'keen:referrer_parser', | ||
input: { | ||
referrer_url: 'referrer.url', | ||
page_url: 'url.string' | ||
}, | ||
output: 'referrer.info' | ||
} | ||
]; | ||
keenAddons.forEach(addon => { | ||
const hasRequiredProperties = Object | ||
.keys(addon.input) | ||
.map(key => addon.input[key]) | ||
.every(requiredProp => requiredProp.split('.').reduce((obj, i) => obj[i], eventData)); | ||
{ | ||
name: 'keen:referrer_parser', | ||
input: { | ||
referrer_url: 'referrer.url', | ||
page_url: 'url.string' | ||
}, | ||
output: 'referrer.info' | ||
} | ||
]; | ||
keenAddons.forEach(addon => { | ||
const hasRequiredProperties = Object | ||
.keys(addon.input) | ||
.map(key => addon.input[key]) | ||
.every(requiredProp => requiredProp.split('.').reduce((obj, i) => obj[i], eventData)); | ||
if(hasRequiredProperties) { | ||
eventData.keen.addons.push(addon); | ||
} | ||
if(hasRequiredProperties) { | ||
eventData.keen.addons.push(addon); | ||
} | ||
}); | ||
// Send event data | ||
client.recordEvent(eventCollection, eventData); | ||
}); | ||
// Send event data | ||
client.recordEvent('analytics', eventData); | ||
}); | ||
} |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
5256
89