Package Exports
- usage-stats
This package does not declare an exports field, so the exports above have been automatically detected and optimized by JSPM instead. If any package subpath is missing, it is recommended to post an issue to the original package (usage-stats) to support the "exports" field. If that is not possible, create a JSPM override to customize the exports field for this package.
Readme
usage-stats
A minimal, offline-friendly Google Analytics Measurement Protocol client for tracking usage statistics in node.js applications.
Synopsis
Simple
The most trivial example.
const UsageStats = require('usage-stats')
const usageStats = new UsageStats('UA-98765432-1')
// track a hit on the 'main' screen with 'simple' mode set.
usageStats
.screenView('main')
.event('option', 'mode', 'simple')
.send()
Typical
More realistic usage.
const UsageStats = require('usage-stats')
const usageStats = new UsageStats('UA-98765432-1', {
name: 'sick app',
version: '1.0.0'
})
// start a new session
usageStats.start()
// user sets an option..
usageStats.event('option', 'verbose-level', 'infinite')
try {
// register a hit on 'encoding mode'
usageStats.screenView('encoding')
beginEncoding(options)
} catch (err) {
// exception tracking
usageStats.exception(err.message, true)
}
// finished - mark the session as complete
// and send stats (or store until later, if offline).
usageStats.end().send()
List of metrics sent
Beside tracking events, exceptions and screenviews, the follow stats are collected each session.
- App name
- App version
- Operating System version (sent in the UserAgent)
- Client ID (a random UUID, generated once per OS user and stored)
- Language (
process.env.LANG
, if set) - Screen resolution (terminal rows by columns, by default)
API Reference
Example
const UsageStats = require('usage-stats')
- usage-stats
- UsageStats ⏏
- new UsageStats(trackingId, [options])
- .dir :
string
- .defaults :
Map
- .start([sessionParams]) ↩︎
- .end() ↩︎
- .disable() ↩︎
- .enable() ↩︎
- .event(category, action, [options]) ↩︎
- .screenView(name, [options]) ↩︎
- .exception(description, isFatal) ↩︎
- .send([options]) ⇒
Promise
- .abort() ↩︎
- .save() ↩︎
- .hitsQueued() ⇒
number
- UsageStats ⏏
UsageStats ⏏
new UsageStats(trackingId, [options])
Param | Type | Description |
---|---|---|
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.ua] | string |
User Agent string to use. |
[options.dir] | string |
Path of the directory used for persisting clientID and queue. Defaults to ~/.usage-stats . |
[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.dir : string
Cache directory where the queue and client ID is kept. Defaults to ~/.usage-stats
.
Kind: instance property of UsageStats
usageStats.defaults : Map
Set parameters on this map to send them with every hit.
Kind: instance property of UsageStats
Example
usageStats.defaults
.set('cd1', process.version)
.set('cd2', os.type())
.set('cd3', os.release())
.set('cd4', 'api')
usageStats.start([sessionParams]) ↩︎
Starts the session.
Kind: instance method of UsageStats
Chainable
Param | Type | Description |
---|---|---|
[sessionParams] | Array.<Map> |
An option map of paramaters to send with each hit in the sesison. |
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, [options]) ↩︎
Track an event. All event hits are queued until .send()
is called.
Kind: instance method of UsageStats
Chainable
Param | Type | Description |
---|---|---|
category | string |
Event category (required). |
action | string |
Event action (required). |
[options] | option |
|
[options.label] | string |
Event label |
[options.value] | string |
Event value |
[options.hitParams] | Array.<map> |
One or more additional params to send with the hit. |
usageStats.screenView(name, [options]) ↩︎
Track a screenview. All screenview hits are queued until .send()
is called.
Kind: instance method of UsageStats
Chainable
Param | Type | Description |
---|---|---|
name | string |
Screen name |
[options] | object |
|
[options.hitParams] | Array.<map> |
One or more additional params to set on the hit. |
usageStats.exception(description, isFatal) ↩︎
Track a exception. All exception hits are queued until .send()
is called.
Kind: instance method of UsageStats
Chainable
Param | Type | Description |
---|---|---|
description | string |
Error message |
isFatal | boolean |
Set true if the exception was fatal |
[options.hitParams] | Array.<map> |
One or more additional params to set on the hit. |
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: response[]
- array of responses
Param | Type | Description |
---|---|---|
[options] | object |
|
[options.debug] | boolean |
Validates hits, fulfilling with the result. |
usageStats.abort() ↩︎
Aborts the in-progress .send() operation, queuing any unsent hits.
Kind: instance method of UsageStats
Chainable
usageStats.save() ↩︎
Dumps unsent hits to the queue. They will dequeued and sent on next invocation of .send()
.
Kind: instance method of UsageStats
Chainable
usageStats.hitsQueued() ⇒ number
Return the total hits stored on the queue.
Kind: instance method of UsageStats
© 2016 Lloyd Brookes <75pound@gmail.com>. Documented by jsdoc-to-markdown.