title | description | pageTitle | subTitle |
---|---|---|---|
Analytics API Reference Documentation |
The core analytics API docs for the open-source analytics module |
API Reference |
Core analytics API |
The core analytics
API is exposed once the library is initialized with configuration.
TLDR; read the getting started guide
Using analytics
- Initialize analytics with configuration
- Export the analytics instance with third-party providers (Google Analytics, HubSpot, etc.)
- Use
page
,identify
,track
in your app - Add an analytics provider
- Write custom plugins
Analytics library configuration
After the library is initialized with config, the core API is exposed & ready for use in the application.
Arguments
- config
object
- analytics core config - [config.app] (optional)
string
- Name of site / app - [config.version] (optional)
string
- Version of your app - [config.debug] (optional)
boolean
- Should analytics run in debug mode - [config.plugins] (optional)
Array
.<Object
> - Array of analytics plugins
Example
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
- userId
String
- Unique ID of user - [traits] (optional)
Object
- Object of user traits - [options] (optional)
Object
- Options to pass to identify call - [callback] (optional)
Function
- Callback function after identify completes
Example
// 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
- eventName
String
- Event name - [payload] (optional)
Object
- Event payload - [options] (optional)
Object
- Event options - [callback] (optional)
Function
- Callback to fire after tracking completes
Example
// 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
- [data] (optional) PageData - Page data overrides.
- [options] (optional)
Object
- Page tracking options - [callback] (optional)
Function
- Callback to fire after page view call completes
Example
// 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
- [key] (optional)
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
- [callback] (optional)
Function
- Handler to run after reset
Example
// Reset current visitor
analytics.reset()
Fire callback when analytics library is ready. If the ready
event has already fired, this calls the
callback. If not, it registers the callback to be called once when the ready
event fires.
Arguments
- callback
Function
- function to trigger when all providers have loaded, or immediately if all providers have already loaded
Example
analytics.ready((payload) => {
console.log('all plugins have loaded or were skipped', payload);
})
Attach an event handler function for analytics lifecycle events.
Arguments
- name
String
- Name of event to listen to - callback
Function
- function to fire on event
Example
// 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 only once.
Arguments
- name
String
- Name of event to listen to - callback
Function
- function to fire on event
Example
// Fire function only once 'track'
analytics.once('track', ({ payload }) => {
console.log('This will 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
- [key] (optional)
string
- dot.prop.path value of state
Example
// 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
// Pull storage off analytics instance
const { storage } = analytics
// Get value
storage.getItem('storage_key')
// Set value
storage.setItem('storage_key', 'value')
// Remove value
storage.removeItem('storage_key')
Get value from storage
Arguments
- key
String
- storage key - [options] (optional)
Object
- storage options
Example
analytics.storage.getItem('storage_key')
Set storage value
Arguments
- key
String
- storage key - value any - storage value
- [options] (optional)
Object
- storage options
Example
analytics.storage.setItem('storage_key', 'value')
Remove storage value
Arguments
- key
String
- storage key - [options] (optional)
Object
- storage options
Example
analytics.storage.removeItem('storage_key')
Management methods for plugins. This is also where custom methods are loaded into the instance.
Example
// Enable a plugin by namespace
analytics.plugins.enable('keenio')
// Disable a plugin by namespace
analytics.plugins.disable('google-analytics')
Enable analytics plugin
Arguments
- plugins
String
|Array
- name of plugins(s) to disable - [callback] (optional)
Function
- callback after enable runs
Example
analytics.plugins.enable('google')
// Enable multiple plugins at once
analytics.plugins.enable(['google', 'segment'])
Disable analytics plugin
Arguments
- name
String
|Array
- name of integration(s) to disable - callback
Function
- callback after disable runs
Example
analytics.plugins.disable('google')
analytics.plugins.disable(['google', 'segment'])