Home

Awesome

<a href="https://getanalytics.io"> <img src="https://user-images.githubusercontent.com/532272/110860586-cc927e00-8271-11eb-95c7-44bdda78ab2a.png" width="450" /> </a>

npm npm bundle size GitHub

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

Table of Contents

<!-- AUTO-GENERATED-CONTENT:START (TOC:collapse=true&collapseText=Click to expand) --> <details> <summary>Click to expand</summary> </details> <!-- AUTO-GENERATED-CONTENT:END -->

Features

Why

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.

how analytics works

Driving philosophy:

To add or remove an analytics provider, adjust the plugins you load into analytics during initialization.

Install

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>

Usage

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'
})
<details> <summary>Node.js usage</summary>

For ES6/7 javascript you can import Analytics from 'analytics' for normal node.js usage you can import like so:

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'
})
</details> <details> <summary>Browser usage</summary>

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.

<script src="https://unpkg.com/analytics/dist/analytics.min.js"></script>
<script>
  const Analytics = _analytics.init({
    app: 'my-app-name',
    version: 100,
    plugins: []
  })

  /* 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'
  })
</script>
</details>

Demo

See Analytics Demo for a site example.

API

The core analytics API is exposed once the library is initialized with configuration.

Typical usage:

  1. Initialize with configuration
  2. Export the analytics instance with third-party providers (Google Analytics, HubSpot, etc)
  3. Use page, identify, track in your app
  4. Plugin custom business logic
<!-- AUTO-GENERATED-CONTENT:START (API_DOCS) -->

Configuration

Analytics library configuration

After the library is initialized with config, the core API is exposed & ready for use in the application.

Arguments

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
  ]
})

analytics.identify

Identify a user. This will trigger identify calls in any installed plugins and will set user data in localStorage

Arguments

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
  }
})

analytics.track

Track an analytics event. This will trigger track calls in any installed plugins

Arguments

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
  }
})

analytics.page

Trigger page view. This will trigger page calls in any installed plugins

Arguments

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
  }
})

analytics.user

Get user data

Arguments

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')

analytics.reset

Clear all information about the visitor & reset analytic state.

Arguments

Example

// Reset current visitor
analytics.reset()

analytics.ready

Fire callback on analytics ready event

Arguments

Example

analytics.ready((payload) => {
  console.log('all plugins have loaded or were skipped', payload);
})

analytics.on

Attach an event handler function for analytics lifecycle events.

Arguments

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()

analytics.once

Attach a handler function to an event and only trigger it once.

Arguments

Example

// 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()

analytics.getState

Get data about user, activity, or context. Access sub-keys of state with dot.prop syntax.

Arguments

Example

// Get the current state of analytics
analytics.getState()

// Get a subpath of state
analytics.getState('context.offline')

analytics.storage

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')

analytics.storage.getItem

Get value from storage

Arguments

Example

analytics.storage.getItem('storage_key')

analytics.storage.setItem

Set storage value

Arguments

Example

analytics.storage.setItem('storage_key', 'value')

analytics.storage.removeItem

Remove storage value

Arguments

Example

analytics.storage.removeItem('storage_key')

analytics.plugins

Async 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')

analytics.plugins.enable

Enable analytics plugin

Arguments

Example

analytics.plugins.enable('google-analytics').then(() => {
  console.log('do stuff')
})

// Enable multiple plugins at once
analytics.plugins.enable(['google-analytics', 'segment']).then(() => {
  console.log('do stuff')
})

analytics.plugins.disable

Disable analytics plugin

Arguments

Example

analytics.plugins.disable('google').then(() => {
  console.log('do stuff')
})

analytics.plugins.disable(['google', 'segment']).then(() => {
  console.log('do stuff')
})
<!-- AUTO-GENERATED-CONTENT:END -->

Events

The analytics library comes with a large variety of event listeners that can be used to fire custom functionality when a specific lifecycle event occurs.

These listeners can be fired using analytics.on & analytics.once

const eventName = 'pageEnd'
analytics.on(eventName, ({ payload }) => {
  console.log('payload', payload)
})

Below is a list of the current available events

<!-- AUTO-GENERATED-CONTENT:START (EVENT_DOCS) -->
EventDescription
bootstrapFires when analytics library starts up.<br/>This is the first event fired. '.on/once' listeners are not allowed on bootstrap<br/>Plugins can attach logic to this event
paramsFires when analytics parses URL parameters
campaignFires if params contain "utm" parameters
initializeStartFires before 'initialize', allows for plugins to cancel loading of other plugins
initializeFires when analytics loads plugins
initializeEndFires after initialize, allows for plugins to run logic after initialization methods run
readyFires when all analytic providers are fully loaded. This waits for 'initialize' and 'loaded' to return true
resetStartFires if analytic.reset() is called.<br/>Use this event to cancel reset based on a specific condition
resetFires if analytic.reset() is called.<br/>Use this event to run custom cleanup logic (if needed)
resetEndFires after analytic.reset() is called.<br/>Use this event to run a callback after user data is reset
pageStartFires before 'page' events fire.<br/> This allows for dynamic page view cancellation based on current state of user or options passed in.
pageCore analytics hook for page views.<br/> If your plugin or integration tracks page views, this is the event to fire on.
pageEndFires after all registered 'page' methods fire.
pageAbortedFires if 'page' call is cancelled by a plugin
trackStartCalled before the 'track' events fires.<br/> This allows for dynamic page view cancellation based on current state of user or options passed in.
trackCore analytics hook for event tracking.<br/> If your plugin or integration tracks custom events, this is the event to fire on.
trackEndFires after all registered 'track' events fire from plugins.
trackAbortedFires if 'track' call is cancelled by a plugin
identifyStartCalled before the 'identify' events fires.<br/>This allows for dynamic page view cancellation based on current state of user or options passed in.
identifyCore analytics hook for user identification.<br/> If your plugin or integration identifies users or user traits, this is the event to fire on.
identifyEndFires after all registered 'identify' events fire from plugins.
identifyAbortedFires if 'track' call is cancelled by a plugin
userIdChangedFires when a user id is updated
registerPluginsFires when analytics is registering plugins
enablePluginFires when 'analytics.plugins.enable()' is called
disablePluginFires when 'analytics.plugins.disable()' is called
onlineFires when browser network goes online.<br/>This fires only when coming back online from an offline state.
offlineFires when browser network goes offline.
setItemStartFires when analytics.storage.setItem is initialized.<br/>This event gives plugins the ability to intercept keys & values and alter them before they are persisted.
setItemFires when analytics.storage.setItem is called.<br/>This event gives plugins the ability to intercept keys & values and alter them before they are persisted.
setItemEndFires when setItem storage is complete.
setItemAbortedFires when setItem storage is cancelled by a plugin.
removeItemStartFires when analytics.storage.removeItem is initialized.<br/>This event gives plugins the ability to intercept removeItem calls and abort / alter them.
removeItemFires when analytics.storage.removeItem is called.<br/>This event gives plugins the ability to intercept removeItem calls and abort / alter them.
removeItemEndFires when removeItem storage is complete.
removeItemAbortedFires when removeItem storage is cancelled by a plugin.
<!-- AUTO-GENERATED-CONTENT:END (EVENT_DOCS) -->

Analytic plugins

The analytics has a robust plugin system. Here is a list of currently available plugins:

<!-- AUTO-GENERATED-CONTENT:START (PLUGINS) -->
PluginStatsVersion
@analytics/activity-utils <br/> User activity listener utilities<a href="https://www.npmjs.com/package/@analytics/activity-utils"><img width="360" height="22" src="https://img.shields.io/npm/dm/@analytics/activity-utils.svg"></a>0.1.16
@analytics/amplitude <br/> Amplitude integration for 'analytics' module<a href="https://www.npmjs.com/package/@analytics/amplitude"><img width="360" height="22" src="https://img.shields.io/npm/dm/@analytics/amplitude.svg"></a>0.1.3
@analytics/aws-pinpoint <br/> AWS Pinpoint integration for 'analytics' module<a href="https://www.npmjs.com/package/@analytics/aws-pinpoint"><img width="360" height="22" src="https://img.shields.io/npm/dm/@analytics/aws-pinpoint.svg"></a>0.7.12
@analytics/cookie-utils <br/> Tiny cookie utility library<a href="https://www.npmjs.com/package/@analytics/cookie-utils"><img width="360" height="22" src="https://img.shields.io/npm/dm/@analytics/cookie-utils.svg"></a>0.2.12
@analytics/core <br/> Lightweight analytics library for tracking events, page views, & identifying users. Works with any third party analytics provider via an extendable plugin system.<a href="https://www.npmjs.com/package/@analytics/core"><img width="360" height="22" src="https://img.shields.io/npm/dm/@analytics/core.svg"></a>0.12.9
@analytics/countly <br/> Countly plugin for 'analytics' module<a href="https://www.npmjs.com/package/@analytics/countly"><img width="360" height="22" src="https://img.shields.io/npm/dm/@analytics/countly.svg"></a>0.21.12
@analytics/crazy-egg <br/> Crazy Egg integration for 'analytics' module<a href="https://www.npmjs.com/package/@analytics/crazy-egg"><img width="360" height="22" src="https://img.shields.io/npm/dm/@analytics/crazy-egg.svg"></a>0.1.2
@analytics/custify <br/> Custify integration for 'analytics' module for browser & node<a href="https://www.npmjs.com/package/@analytics/custify"><img width="360" height="22" src="https://img.shields.io/npm/dm/@analytics/custify.svg"></a>0.0.2
@analytics/customerio <br/> Customer.io integration for 'analytics' module<a href="https://www.npmjs.com/package/@analytics/customerio"><img width="360" height="22" src="https://img.shields.io/npm/dm/@analytics/customerio.svg"></a>0.2.2
@analytics/form-utils <br/> Form utility library for managing HTML form submissions & values<a href="https://www.npmjs.com/package/@analytics/form-utils"><img width="360" height="22" src="https://img.shields.io/npm/dm/@analytics/form-utils.svg"></a>0.3.13
@analytics/fullstory <br/> Unofficial FullStory plugin for 'analytics' module<a href="https://www.npmjs.com/package/@analytics/fullstory"><img width="360" height="22" src="https://img.shields.io/npm/dm/@analytics/fullstory.svg"></a>0.2.6
@analytics/global-storage-utils <br/> Tiny global storage utility library<a href="https://www.npmjs.com/package/@analytics/global-storage-utils"><img width="360" height="22" src="https://img.shields.io/npm/dm/@analytics/global-storage-utils.svg"></a>0.1.7
@analytics/google-analytics <br/> Google analytics v4 plugin for 'analytics' module<a href="https://www.npmjs.com/package/@analytics/google-analytics"><img width="360" height="22" src="https://img.shields.io/npm/dm/@analytics/google-analytics.svg"></a>1.0.7
@analytics/google-tag-manager <br/> Google tag manager plugin for 'analytics' module<a href="https://www.npmjs.com/package/@analytics/google-tag-manager"><img width="360" height="22" src="https://img.shields.io/npm/dm/@analytics/google-tag-manager.svg"></a>0.5.5
@analytics/google-analytics-v3 <br/> Google analytics v3 plugin for 'analytics' module<a href="https://www.npmjs.com/package/@analytics/google-analytics-v3"><img width="360" height="22" src="https://img.shields.io/npm/dm/@analytics/google-analytics-v3.svg"></a>0.6.1
@analytics/gosquared <br/> GoSquared integration for 'analytics' module<a href="https://www.npmjs.com/package/@analytics/gosquared"><img width="360" height="22" src="https://img.shields.io/npm/dm/@analytics/gosquared.svg"></a>0.1.3
@analytics/hubspot <br/> HubSpot plugin for 'analytics' module<a href="https://www.npmjs.com/package/@analytics/hubspot"><img width="360" height="22" src="https://img.shields.io/npm/dm/@analytics/hubspot.svg"></a>0.5.1
@analytics/intercom <br/> Intercom integration for 'analytics' module for browser & node<a href="https://www.npmjs.com/package/@analytics/intercom"><img width="360" height="22" src="https://img.shields.io/npm/dm/@analytics/intercom.svg"></a>1.0.2
@analytics/listener-utils <br/> Backward compatible event listener library for attaching & detaching event handlers<a href="https://www.npmjs.com/package/@analytics/listener-utils"><img width="360" height="22" src="https://img.shields.io/npm/dm/@analytics/listener-utils.svg"></a>0.4.0
@analytics/localstorage-utils <br/> Tiny LocalStorage utility library<a href="https://www.npmjs.com/package/@analytics/localstorage-utils"><img width="360" height="22" src="https://img.shields.io/npm/dm/@analytics/localstorage-utils.svg"></a>0.1.10
@analytics/mixpanel <br/> Mixpanel plugin for 'analytics' module<a href="https://www.npmjs.com/package/@analytics/mixpanel"><img width="360" height="22" src="https://img.shields.io/npm/dm/@analytics/mixpanel.svg"></a>0.4.0
@analytics/original-source-plugin <br/> Save original referral source of visitor plugin for 'analytics' pkg<a href="https://www.npmjs.com/package/@analytics/original-source-plugin"><img width="360" height="22" src="https://img.shields.io/npm/dm/@analytics/original-source-plugin.svg"></a>1.0.11
@analytics/ownstats <br/> Ownstats integration for 'analytics' module for browser & node<a href="https://www.npmjs.com/package/@analytics/ownstats"><img width="360" height="22" src="https://img.shields.io/npm/dm/@analytics/ownstats.svg"></a>0.1.2
@analytics/perfumejs <br/> Send browser performance metrics to third-party analytics providers<a href="https://www.npmjs.com/package/@analytics/perfumejs"><img width="360" height="22" src="https://img.shields.io/npm/dm/@analytics/perfumejs.svg"></a>0.2.1
@analytics/queue-utils <br/> Dependency free queue processor<a href="https://www.npmjs.com/package/@analytics/queue-utils"><img width="360" height="22" src="https://img.shields.io/npm/dm/@analytics/queue-utils.svg"></a>0.1.2
@analytics/redact-utils <br/> Utility library for redacting event data<a href="https://www.npmjs.com/package/@analytics/redact-utils"><img width="360" height="22" src="https://img.shields.io/npm/dm/@analytics/redact-utils.svg"></a>0.1.3
@analytics/remote-storage-utils <br/> Storage utilities for cross domain localStorage access, with permissions<a href="https://www.npmjs.com/package/@analytics/remote-storage-utils"><img width="360" height="22" src="https://img.shields.io/npm/dm/@analytics/remote-storage-utils.svg"></a>0.4.20
@analytics/router-utils <br/> Route change utilities for single page apps<a href="https://www.npmjs.com/package/@analytics/router-utils"><img width="360" height="22" src="https://img.shields.io/npm/dm/@analytics/router-utils.svg"></a>0.1.1
@analytics/scroll-utils <br/> Scroll utility library to fire events on scroll<a href="https://www.npmjs.com/package/@analytics/scroll-utils"><img width="360" height="22" src="https://img.shields.io/npm/dm/@analytics/scroll-utils.svg"></a>0.1.22
@analytics/segment <br/> Segment integration for 'analytics' module for browser & node<a href="https://www.npmjs.com/package/@analytics/segment"><img width="360" height="22" src="https://img.shields.io/npm/dm/@analytics/segment.svg"></a>2.1.0
@analytics/session-storage-utils <br/> Tiny SessionStorage utility library<a href="https://www.npmjs.com/package/@analytics/session-storage-utils"><img width="360" height="22" src="https://img.shields.io/npm/dm/@analytics/session-storage-utils.svg"></a>0.0.7
@analytics/session-utils <br/> Tiny session utility library<a href="https://www.npmjs.com/package/@analytics/session-utils"><img width="360" height="22" src="https://img.shields.io/npm/dm/@analytics/session-utils.svg"></a>0.2.0
@analytics/simple-analytics <br/> Simple analytics plugin for 'analytics' module for browser<a href="https://www.npmjs.com/package/@analytics/simple-analytics"><img width="360" height="22" src="https://img.shields.io/npm/dm/@analytics/simple-analytics.svg"></a>0.4.0
@analytics/snowplow <br/> Snowplow integration for 'analytics' module for browser & node<a href="https://www.npmjs.com/package/@analytics/snowplow"><img width="360" height="22" src="https://img.shields.io/npm/dm/@analytics/snowplow.svg"></a>0.3.3
@analytics/storage-utils <br/> Storage utility with fallbacks<a href="https://www.npmjs.com/package/@analytics/storage-utils"><img width="360" height="22" src="https://img.shields.io/npm/dm/@analytics/storage-utils.svg"></a>0.4.2
@analytics/type-utils <br/> Tiny runtime type checking utils<a href="https://www.npmjs.com/package/@analytics/type-utils"><img width="360" height="22" src="https://img.shields.io/npm/dm/@analytics/type-utils.svg"></a>0.6.2
@analytics/url-utils <br/> Url utils<a href="https://www.npmjs.com/package/@analytics/url-utils"><img width="360" height="22" src="https://img.shields.io/npm/dm/@analytics/url-utils.svg"></a>0.2.3
@analytics/visitor-source <br/> Get visitor source<a href="https://www.npmjs.com/package/@analytics/visitor-source"><img width="360" height="22" src="https://img.shields.io/npm/dm/@analytics/visitor-source.svg"></a>0.0.7
analytics-cli <br/> CLI for analytics pkg<a href="https://www.npmjs.com/package/analytics-cli"><img width="360" height="22" src="https://img.shields.io/npm/dm/analytics-cli.svg"></a>0.0.5
analytics-plugin-do-not-track <br/> Disable tracking for opted out visitors plugin for 'analytics' module<a href="https://www.npmjs.com/package/analytics-plugin-do-not-track"><img width="360" height="22" src="https://img.shields.io/npm/dm/analytics-plugin-do-not-track.svg"></a>0.1.5
analytics-plugin-event-validation <br/> Event validation plugin for analytics<a href="https://www.npmjs.com/package/analytics-plugin-event-validation"><img width="360" height="22" src="https://img.shields.io/npm/dm/analytics-plugin-event-validation.svg"></a>0.1.2
gatsby-plugin-analytics <br/> Easily add analytics to your Gatsby site<a href="https://www.npmjs.com/package/gatsby-plugin-analytics"><img width="360" height="22" src="https://img.shields.io/npm/dm/gatsby-plugin-analytics.svg"></a>0.2.0
analytics-plugin-lifecycle-example <br/> Example plugin with lifecycle methods for 'analytics' module<a href="https://www.npmjs.com/package/analytics-plugin-lifecycle-example"><img width="360" height="22" src="https://img.shields.io/npm/dm/analytics-plugin-lifecycle-example.svg"></a>0.1.2
analytics-plugin-tab-events <br/> Expose tab visibility events plugin for 'analytics' module<a href="https://www.npmjs.com/package/analytics-plugin-tab-events"><img width="360" height="22" src="https://img.shields.io/npm/dm/analytics-plugin-tab-events.svg"></a>0.2.1
use-analytics <br/> Analytics hooks for React<a href="https://www.npmjs.com/package/use-analytics"><img width="360" height="22" src="https://img.shields.io/npm/dm/use-analytics.svg"></a>1.1.0
analytics-util-params <br/> Url Parameter helper functions<a href="https://www.npmjs.com/package/analytics-util-params"><img width="360" height="22" src="https://img.shields.io/npm/dm/analytics-util-params.svg"></a>0.1.2
analytics-utils <br/> Analytics utility functions used by 'analytics' module<a href="https://www.npmjs.com/package/analytics-utils"><img width="360" height="22" src="https://img.shields.io/npm/dm/analytics-utils.svg"></a>1.0.12
analytics-plugin-window-events <br/> Expose window events plugin for 'analytics' module<a href="https://www.npmjs.com/package/analytics-plugin-window-events"><img width="360" height="22" src="https://img.shields.io/npm/dm/analytics-plugin-window-events.svg"></a>0.0.7
<!-- AUTO-GENERATED-CONTENT:END -->

Community Plugins

Below are plugins created outside of this repo:

<!-- AUTO-GENERATED-CONTENT:START (EXTERNAL_PLUGINS) --> <!-- AUTO-GENERATED-CONTENT:END -->

Additional examples

Creating analytics plugins

The library is designed to work with any third-party analytics tool.

Plugins are just plain javascript objects that expose methods for analytics to register and call.

Here is a quick example of a plugin:

// plugin-example.js
export default function pluginExample(userConfig) {
  // return object for analytics to use
  return {
    /* All plugins require a name */
    name: 'my-example-plugin',
    /* Everything else below this is optional depending on your plugin requirements */
    config: {
      whatEver: userConfig.whatEver,
      elseYouNeed: userConfig.elseYouNeed
    },
    initialize: ({ config }) => {
      // load provider script to page
    },
    page: ({ payload }) => {
      // call provider specific page tracking
    },
    track: ({ payload }) => {
      // call provider specific event tracking
    },
    identify: ({ payload }) => {
      // call provider specific user identify method
    },
    loaded: () => {
      // return boolean so analytics knows when it can send data to third-party
      return !!window.myPluginLoaded
    }
  }
}

name is required for all plugins. All other methods are optional.

If you don't need to hook into page tracking, just omit the page key from your plugin object.

To use a plugin, import it and pass it into the plugins array when you bootstrap analytics.

import Analytics from 'analytics'
import pluginExample from './plugin-example.js'

const analytics = Analytics({
  app: 'my-app-name',
  plugins: [
    pluginExample({
      whatEver: 'hello',
      elseYouNeed: 'there'
    }),
    ...otherPlugins
  ]
})

React to any event

Plugins can react to any event flowing through the analytics library.

For example, if you wanted to trigger custom logic when analytics bootstraps, you can attach a function handler to the bootstrap event.

For a full list of core events, checkout events.js.

// Example Plugin plugin.js
export default function myPlugin(userConfig) {
  return {
    /* Name is a required field for plugins */
    name: 'my-plugin',
    /* Bootstrap runs when analytics starts */
    bootstrap: ({ payload, config, instance }) => {
      // Do whatever on `bootstrap` event
    },
    pageStart: ({ payload, config, instance }) => {
      // Fire custom logic before analytics.page() calls
    },
    pageEnd: ({ payload, config, instance }) => {
      // Fire custom logic after analytics.page() calls
    },
    trackStart: ({ payload, config, instance }) => {
      // Fire custom logic before analytics.track() calls
    },
    'track:customerio': ({ payload, config, instance }) => {
      // Fire custom logic before customer.io plugin runs.
      // Here you can customize the data sent to individual analytics providers
    },
    trackEnd: ({ payload, config, instance }) => {
      // Fire custom logic after analytics.track() calls
    },
    // ... hook into other events
  }
}

Using this plugin is the same as any other.

import Analytics from 'analytics'
import customerIoPlugin from '@analytics/customerio'
import myPlugin from './plugin.js'

const analytics = Analytics({
  app: 'my-app-name',
  plugins: [
    // include myPlugin
    myPlugin(),
    customerIoPlugin({
      trackingId: '1234'
    })
    ...otherPlugins
  ]
})

Custom methods

Analytics plugins can provide their own custom functionality via the methods key.

import Analytics from 'analytics'

// Example plugin with custom methods
const pluginOne = {
  name: 'one',
  // ... page, track, etc
  // Custom functions to expose to analytics instance
  methods: {
    myCustomThing(one, two, three) {
      const analyticsInstance = this.instance
      console.log('Use full analytics instance', analyticsInstance)
    },
    otherCustomThing: (one, two, ...args) => {
      // Arrow functions break this.instance context.
      // The instance is instead injected as last arg
      const analyticsInstance = args[args.length - 1]
      console.log('Use full analytics instance', analyticsInstance)
    },
    // Async function examples
    async fireCustomThing(one, two, three) {
      const { track } = this.instance
      track('customThing')
      return 'data'
    },
    triggerSpecial: async (argOne, argTwo, ...args) => {
      // Arrow functions break this.instance context.
      // The instance is instead injected as last arg
      const analyticsInstance = args[args.length - 1]
      return argOne + argTwo
    }
  }
}

// Example plugin with custom methods
const pluginTwo = {
  name: 'two',
  page: () => { console.log('page view fired') }
  // Custom functions to expose to analytics instance
  methods: {
    cookieBanner(one, two, three) {
      const analyticsInstance = this.instance
      console.log('Use full analytics instance', analyticsInstance)
      const cookieSettings = analyticsInstance.storage.getItem('preferences-set')
      if (!cookieSettings) {
        // Show cookie settings
      }
    },
  }
}

// Initialize analytics instance with plugins
const analytics = Analytics({
  app: 'your-app-name',
  plugins: [
    pluginOne,
    pluginTwo
  ]
})

// Using custom methods in your code
analytics.plugins.one.myCustomThing()
analytics.plugins.two.cookieBanner()

Plugin Naming Conventions

Plugins should follow this naming convention before being published to npm

analytics-plugin-{your-plugin-name}

E.g. An analytics plugin that does awesome-stuff should be named

npm install analytics-plugin-awesome-stuff

Then submit to the list above

Debugging analytics

During development, you can turn on debug mode. This will connect the dev tools for you to see the analytics events passing through your application visually.

analytics-debug-tools

import Analytics from 'analytics'

const analytics = Analytics({
  app: 'my-app',
  debug: true
})

TypeScript support

Types for analytics and plugins are generated from JSDoc blocks in the code base via the tsd-jsdoc package.

We are always looking to improve type support & improve the DX of users. If you see something that can be improved let us know in an issue!

Contributing

Contributions are always welcome, no matter how large or small. Before contributing, please read the code of conduct.

Setup & Install dependencies

Clone the repo and run

$ git clone https://github.com/davidwells/analytics
$ cd analytics
$ npm install && npm run setup

The above command will set up all the packages and their dependencies.

Development

You can watch and rebuild packages with the npm run watch command.

npm run watch

While watch mode is activated, you can work against the demo site in examples to test out your changes on a live application.