0.9.5 • Published 1 year ago

usage-stats v0.9.5

Weekly downloads
1,742
License
MIT
Repository
github
Last release
1 year ago

view on npm npm module downloads Gihub repo dependents Gihub package dependents Node.js CI js-standard-style

usage-stats

A minimal, offline-friendly Google Analytics Measurement Protocol client for tracking usage statistics in shell and javascript applications.

This is a low-level API client, it doesn't hold any opinion of how usage tracking should be done. If you're looking for a convention which leverages the power and flexibility of Custom Metrics and Dimensions, take a look at app-usage-stats. For the command line client see usage-stats-cli.

Synopsis

The most trivial example.

const UsageStats = require('usage-stats')
const usageStats = new UsageStats('UA-98765432-1', { an: 'example' })

usageStats.screenView('screen name')
usageStats.event('category', 'action')
usageStats.send()

More realistic usage in a server application:

const UsageStats = require('usage-stats')
const usageStats = new UsageStats('UA-98765432-1', {
  an: 'encode-video',
  av: '1.0.0'
})

// start a new session
usageStats.start()

// user set two options..
usageStats.event('option', 'verbose-level', 'infinite')
usageStats.event('option', 'preset', 'iPod')

try {
  // Begin. Track as a screenView.
  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 if offline).
usageStats.end().send()

Protocol Parameters

See here for the full list of Google Analytics Measurement Protocol parameters.

Sent by default

All parameters are send on demand, beside this list.

  • 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

Kind: Exported class

new UsageStats(trackingId, options)

ParamTypeDescription
trackingIdstringGoogle Analytics tracking ID (required).
optionsobject
options.anstringApp name
options.avstringApp version
options.langstringLanguage. Defaults to process.env.LANG.
options.srstringScreen resolution. Defaults to ${process.stdout.rows}x${process.stdout.columns}.
options.uastringUser Agent string to use.
options.dirstringPath of the directory used for persisting clientID and queue. Defaults to ~/.usage-stats.
options.urlstringDefaults to 'https://www.google-analytics.com/batch'.
options.debugUrlstringDefaults to 'https://www.google-analytics.com/debug/collect'.

Example

const usageStats = new UsageStats('UA-98765432-1', {
  an: 'sick app',
  av: '1.0.0'
})

usageStats.dir : string

Cache directory. Defaults to ~/.usage-stats.

Kind: instance property of UsageStats

usageStats.defaults : Map

A list of parameters to be to sent 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

ParamTypeDescription
sessionParamsArray.<Map>An optional map of paramaters to send with each hit in the sesison.

usageStats.end(sessionParams) ↩︎

Ends the session.

Kind: instance method of UsageStats
Chainable

ParamTypeDescription
sessionParamsArray.<Map>An optional map of paramaters to send with the final hit of this sesison.

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) ⇒ Map

Track an event. All event hits are queued until .send() is called.

Kind: instance method of UsageStats

ParamTypeDescription
categorystringEvent category (required).
actionstringEvent action (required).
optionsoption
options.elstringEvent label
options.evstringEvent value
options.hitParamsArray.<map>One or more additional params to send with the hit.

usageStats.screenView(name, options) ⇒ Map

Track a screenview. All screenview hits are queued until .send() is called. Returns the hit instance.

Kind: instance method of UsageStats

ParamTypeDescription
namestringScreen name
optionsobject
options.hitParamsArray.<map>One or more additional params to set on the hit.

usageStats.exception(options) ⇒ Map

Track a exception. All exception hits are queued until .send() is called.

Kind: instance method of UsageStats

ParamTypeDescription
optionsobjectoptional params
options.exdstringError message
options.exfbooleanSet true if the exception was fatal
options.hitParamsArray.<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. Each response has data and the original node res.
Reject: Error - Rejects with the first error encountered. The error is a standard node http error with a name of request-fail and a hits property showing what failed to send.

ParamType
optionsobject
options.timeoutnumber

usageStats.debug() ⇒ Promise

Send any hits (including queued) to the GA validation server, fulfilling with the result.

Kind: instance method of UsageStats
Fulfil: Response[]
Reject: Error - Error instance includes hits.

usageStats.abort() ↩︎

Aborts the in-progress .send() operation, queuing any unsent hits.

Kind: instance method of UsageStats
Chainable


© 2016-23 Lloyd Brookes \75pound@gmail.com\. Documented by jsdoc-to-markdown.

0.9.5

1 year ago

0.9.4

7 years ago

0.9.3

7 years ago

0.9.2

7 years ago

0.8.6

7 years ago

0.9.1

7 years ago

0.8.5

7 years ago

0.8.4

7 years ago

0.8.3

7 years ago

0.9.0

7 years ago

0.8.2

7 years ago

0.8.1

7 years ago

0.8.0

7 years ago

0.7.0

8 years ago

0.6.0

8 years ago

0.5.0

8 years ago

0.4.4

8 years ago

0.4.3

8 years ago

0.4.2

8 years ago

0.4.1

8 years ago

0.4.0

8 years ago

0.3.2

8 years ago

0.3.1

8 years ago

0.3.0

8 years ago

0.2.1

8 years ago

0.2.0

8 years ago

0.1.5

8 years ago

0.1.4

8 years ago

0.1.3

8 years ago

0.1.2

8 years ago

0.1.1

8 years ago

0.1.0

8 years ago

0.0.2

8 years ago

0.0.1

8 years ago

0.0.0

8 years ago