JSPM

  • Created
  • Published
  • Downloads 172296
  • Score
    100M100P100Q167233F
  • License MIT

pluggable analytics library

Package Exports

  • analytics

This package does not declare an exports field, so the exports above have been automatically detected and optimized by JSPM instead. If any package subpath is missing, it is recommended to post an issue to the original package (analytics) to support the "exports" field. If that is not possible, create a JSPM override to customize the exports field for this package.

Readme

Analytics

This is a pluggable event driven analytics library designed to work with any third party analytics tool.

Features

  • Pluggable via middleware. Bring your own third party tool
  • Works on client & server-side
  • (WIP) In client, works offline. Queues events to send when connection resumes

Why

Companies frequently change their analytics requirements and add/remove services to their sites and applications. This can be a time consuming process integrating with N number of third party tools.

This library solves that.

Philosophy

You should never be locked into a tool.

To add or remove an analytics provider simply remove it as middleware.

The provider integrations can be run independently of this library or plugged into other tools.

Install

npm i

Running Demo

cd _example && npm install && npm start

Usage

import analyticsLib from 'analytics'
import googleAnalytics from 'analytics-ga'
import loggerPlugin from 'analytics-logger'

const plugins = [
  loggerPlugin,
  googleAnalytics({
    trackingId: 'UA-121991291',
  })
]

const analytics = analyticsLib({
  app: 'my-app-name',
  version: 100,
  plugins: plugins
})

// Using it:
// page tracking
analytics.page()
// event tracking
analytics.track('userPurchase', {
  price: 20
})
// identifying users
analytics.identify('user-id-xyz', {
  firstName: 'bill',
  lastName: 'murray',
  email: 'da-coolest@aol.com'
})
//...

Adding Analytics providers

The library is designed to work with any third party vendor!

You simply need to supply the below methods (that apply to your integration).

Here is an example:

export default function googleAnalytics(config) {
  return {
    NAMESPACE: 'google-analytics',
    config: {
      whatever: 'youWant',
      googleAnalyticsId: config.id // 'UA-82833-33833'
    },
    initialize: function() {
      // load provider script to page (in browser)
    },
    page: function() {
      // call provider specific page tracking
    },
    track: function() {
      // call provider specific event tracking
    },
    identify: function() {
      // call provider specific user identify method
    },
    loaded: () => {
      // return boolean for loaded analytics library
      return !!window.gaplugins
    }
  }
}

You can also add whatever middleware functionality you'd like via redux middleware!

// logger example
const logger = store => next => action => {
  if (action.type) {
    console.log(`>> dispatching ${action.type}`, JSON.stringify(action))
  }
  let result = next(action)

  return result
}

export default logger

To use the plugins/integrations simply include them when you boot up analytics, see usage example above.