Browse by type
A lightweight analytics abstraction library for tracking page views, custom events, & identify visitors.
Designed to work with any third-party analytics tool or your own backend.
Read the docs or view the live demo app
Click to expand
Companies frequently change analytics requirements based on evolving needs. This results in a lot of complexity, maintenance, & extra code when adding/removing analytic services to a site or application.
This library aims to solves that with a simple pluggable abstraction layer.

Driving philosophy:
To add or remove an analytics provider, adjust the plugins you load into analytics during initialization.
This module is distributed via npm, which is bundled with node and should be installed as one of your project's dependencies.
npm install analytics --save
Or as a script tag:
<script src="https://unpkg.com/analytics/dist/analytics.min.js"></script>
import Analytics from 'analytics'
import googleAnalytics from '@analytics/google-analytics'
import customerIo from '@analytics/customerio'
/* Initialize analytics */
const analytics = Analytics({
app: 'my-app-name',
version: 100,
plugins: [
googleAnalytics({
trackingId: 'UA-121991291',
}),
customerIo({
siteId: '123-xyz'
})
]
})
/* Track a page view */
analytics.page()
/* Track a custom event */
analytics.track('userPurchase', {
price: 20,
item: 'pink socks'
})
/* Identify a visitor */
analytics.identify('user-id-xyz', {
firstName: 'bill',
lastName: 'murray',
email: 'da-coolest@aol.com'
})
Node.js usage
For ES6/7 javascript you can import Analytics from 'analytics' for normal node.js usage you can import like so:
```js const { Analytics } = require('analytics') // or const Analytics = require('analytics').default const googleAnalytics = require('@analytics/google-analytics') const customerIo = require('@analytics/customerio')
const analytics = Analytics({ app: 'my-app-name', version: 100, plugins: [ googleAnalytics({ trackingId: 'UA-121991291', }), customerIo({ siteId: '123-xyz' }) ] })
/ Track a page view / analytics.page()
/ Track a custom event / analytics.track('userPurchase', { price: 20, item: 'pink socks' })
/ Identify a visitor / analytics.identify('user-id-xyz', { firstName: 'bill', lastName: 'murray', email: 'da-coolest@aol.com' }) ```
Browser usage
When importing global analytics into your project from a CDN, the library exposes via a global _analytics variable.
Call _analytics.init to create an analytics instance.
```html
```
See Analytics Demo for a site example.
The core analytics API is exposed once the library is initialized with configuration.
Typical usage:
page, identify, track in your appAnalytics library configuration
After the library is initialized with config, the core API is exposed & ready for use in the application.
Arguments
object - analytics core configstring - Name of site / appstring - Version of your appboolean - Should analytics run in debug modeArray.<AnalyticsPlugin> - Array of analytics pluginsExample
import Analytics from 'analytics'
import pluginABC from 'analytics-plugin-abc'
import pluginXYZ from 'analytics-plugin-xyz'
// initialize analytics
const analytics = Analytics({
app: 'my-awesome-app',
plugins: [
pluginABC,
pluginXYZ
]
})
Identify a user. This will trigger identify calls in any installed plugins and will set user data in localStorage
Arguments
String - Unique ID of userObject - Object of user traitsObject - Options to pass to identify callFunction - Callback function after identify completesExample
// Basic user id identify
analytics.identify('xyz-123')
// Identify with additional traits
analytics.identify('xyz-123', {
name: 'steve',
company: 'hello-clicky'
})
// Fire callback with 2nd or 3rd argument
analytics.identify('xyz-123', () => {
console.log('do this after identify')
})
// Disable sending user data to specific analytic tools
analytics.identify('xyz-123', {}, {
plugins: {
// disable sending this identify call to segment
segment: false
}
})
// Send user data to only to specific analytic tools
analytics.identify('xyz-123', {}, {
plugins: {
// disable this specific identify in all plugins except customerio
all: false,
customerio: true
}
})
Track an analytics event. This will trigger track calls in any installed plugins
Arguments
String - Event nameObject - Event payloadObject - Event optionsFunction - Callback to fire after tracking completesExample
// Basic event tracking
analytics.track('buttonClicked')
// Event tracking with payload
analytics.track('itemPurchased', {
price: 11,
sku: '1234'
})
// Fire callback with 2nd or 3rd argument
analytics.track('newsletterSubscribed', () => {
console.log('do this after track')
})
// Disable sending this event to specific analytic tools
analytics.track('cartAbandoned', {
items: ['xyz', 'abc']
}, {
plugins: {
// disable track event for segment
segment: false
}
})
// Send event to only to specific analytic tools
analytics.track('customerIoOnlyEventExample', {
price: 11,
sku: '1234'
}, {
plugins: {
// disable this specific track call all plugins except customerio
all: false,
customerio: true
}
})
Trigger page view. This will trigger page calls in any installed plugins
Arguments
Object - Page tracking optionsFunction - Callback to fire after page view call completesExample
// Basic page tracking
analytics.page()
// Page tracking with page data overrides
analytics.page({
url: 'https://google.com'
})
// Fire callback with 1st, 2nd or 3rd argument
analytics.page(() => {
console.log('do this after page')
})
// Disable sending this pageview to specific analytic tools
analytics.page({}, {
plugins: {
// disable page tracking event for segment
segment: false
}
})
// Send pageview to only to specific analytic tools
analytics.page({}, {
plugins: {
// disable this specific page in all plugins except customerio
all: false,
customerio: true
}
})
Get user data
Arguments
string - dot.prop.path of user data. Example: 'traits.company.name'Example
// Get all user data
const userData = analytics.user()
// Get user id
const userId = analytics.user('userId')
// Get user company name
const companyName = analytics.user('traits.company.name')
Clear all information about the visitor & reset analytic state.
Arguments
Function - Handler to run after resetExample
// Reset current visitor
analytics.reset()
Fire callback on analytics ready event
Arguments
Function - function to trigger when all providers have loadedExample
analytics.ready((payload) => {
console.log('all plugins have loaded or were skipped', payload);
})
Attach an event handler function for analytics lifecycle events.
Arguments
String - Name of event to listen toFunction - function to fire on eventExample
// Fire function when 'track' calls happen
analytics.on('track', ({ payload }) => {
console.log('track call just happened. Do stuff')
})
// Remove listener before it is called
const removeListener = analytics.on('track', ({ payload }) => {
console.log('This will never get called')
})
// cleanup .on listener
removeListener()
Attach a handler function to an event and only trigger it once.
Arguments
String - Name of event to listen toFunction - function to fire on eventExample
// Fire function only once per 'track'
analytics.once('track', ({ payload }) => {
console.log('This is only triggered once when analytics.track() fires')
})
// Remove listener before it is called
const listener = analytics.once('track', ({ payload }) => {
console.log('This will never get called b/c listener() is called')
})
// cleanup .once listener before it fires
listener()
Get data about user, activity, or context. Access sub-keys of state with dot.prop syntax.
Arguments
string - dot.prop.path value of stateExample
// Get the current state of analytics
analytics.getState()
// Get a subpath of state
analytics.getState('context.offline')
Storage utilities for persisting data. These methods will allow you to save data in localStorage, cookies, or to the window.
Example
```js // Pull storage off
$ claude mcp add analytics \
-- python -m otcore.mcp_server <graph>