/gadispatcher

Highly simplified, resilient API for dispatching events to Google Analytics

Primary LanguageJavaScriptMIT LicenseMIT

gadispatcher npm

GADispatcher is a highly simplified API for sending any type of event to Google Analytics. The intended usage was originally for applications built with Electron, but you can use this in any Node.js codebase if you so desire.

(WIP: Any requests made with this library that fail are gracefully queued in the background to resend later.)

⚠️ There are currently no tests for this library. ⚠️

Usage

API

GADispatcher(trackingId, [identifier], [options])

Calling new GADispatcher() instantiates the GADispatcher instance.

  • trackingId - A Google Analytics tracking ID.
  • [identifier] - An optional V4 UUID. One is generated and stored between sessions if no UUID is passed.
  • [options] - See the universal-analytics documentation for available options.
send(eventType, [metadata])
  • eventType - Kind of event to send. Currently available events are pageview, event, transaction, exception and timing.
  • [metadata] - Optional metadata as an object. universal-analytics documentation for available options.
Examples

If you prefer examples, I got you.

Instantiating an instance:
// Set up some vars
const GA_ID = 'UA-123456-78'
const UUID = '123e4567-e89b-12d3-a456-426655440000'

const GA_OPTS = {
  https: true // on by default
}

// Ready
const ga = new GADispatcher(GA_ID, UUID)
Sending a request:
// Configure some data about an event
const recordingEvent = {
  eventCategory: 'User action',
  eventAction: 'Started recording'
}

// Sends an event type request to Google Analytics
ga.send('event', recordingEvent)