
usage-stats
A minimal, offline-friendly Google Analytics Measurement Protocol client for tracking usage statistics in node.js applications.
Synopsis
const UsageStats = require('usage-stats')
const usageStats = new UsageStats('UA-98765432-1', {
name: 'sick app',
version: '1.0.0'
})
usageStats.start()
usageStats.event('option', 'verbose-level', 'infinite')
try {
usageStats.screenView('encoding')
beginEncoding(options)
} catch (err) {
usageStats.exception(err.message, true)
}
usageStats.end().send()
List of metrics sent
Beside tracking events, exceptions and screenviews, the follow stats are collected each session.
API Reference
Example
const UsageStats = require('usage-stats')
UsageStats ⏏
Kind: Exported class
new UsageStats(trackingId, [options])
| trackingId | string | Google Analytics tracking ID (required). |
| [options] | object | |
| [options.name] | string | App name |
| [options.version] | string | App version |
| [options.lang] | string | Language. Defaults to process.env.LANG. |
| [options.sr] | string | Screen resolution. Defaults to ${process.stdout.rows}x${process.stdout.columns}. |
| [options.dir] | string | Path of the directory used for persisting clientID and queue. |
| [options.url] | string | Defaults to 'https://www.google-analytics.com/batch'. |
| [options.debugUrl] | string | Defaults to 'https://www.google-analytics.com/debug/collect'. |
Example
const usageStats = new UsageStats('UA-98765432-1', {
name: 'sick app',
version: '1.0.0'
})
usageStats.start() ↩︎
Starts the session.
Kind: instance method of UsageStats
Chainable
usageStats.end() ↩︎
Ends the session.
Kind: instance method of UsageStats
Chainable
usageStats.disable() ↩︎
Disable the module. While disabled, all operations are no-ops.
Kind: instance method of UsageStats
Chainable
usageStats.enable() ↩︎
Re-enable the module.
Kind: instance method of UsageStats
Chainable
usageStats.event(category, action, [label], [value]) ↩︎
Track an event. All event hits are queued until .send() is called.
Kind: instance method of UsageStats
Chainable
| category | string | Event category (required). |
| action | string | Event action (required). |
| [label] | string | Event label |
| [value] | string | Event value |
usageStats.screenView(name) ↩︎
Track a screenview. All screenview hits are queued until .send() is called.
Kind: instance method of UsageStats
Chainable
usageStats.exception(description, isFatal) ↩︎
Track a exception. All screenview hits are queued until .send() is called.
Kind: instance method of UsageStats
Chainable
| description | string | Error message |
| isFatal | boolean | Set true if the exception was fatal |
usageStats.send([options]) ⇒ Promise
Send queued stats using as few requests as possible (typically a single request - a max of 20 events/screenviews may be sent per request). If offline, the stats will be stored and re-tried on next invocation.
Kind: instance method of UsageStats
Fulfil: debug mode: { hits: {hits}, result: {validation result} }
Fulfil: live mode: [{ res: {res}, data: {Buffer} }] - array of responses
| [options] | object | |
| [options.debug] | boolean | Validates hits, fulfilling with the result. |
© 2016 Lloyd Brookes <75pound@gmail.com>. Documented by jsdoc-to-markdown.