What is analytics?
The 'analytics' npm package is a lightweight, pluggable library for tracking events, user data, and more. It provides a simple API to integrate with various analytics services and manage user data.
What are analytics's main functionalities?
Track Events
This feature allows you to track custom events within your application. The code sample demonstrates how to initialize the analytics instance with a Google Analytics plugin and track a 'buttonClicked' event.
const Analytics = require('analytics').default;
const googleAnalytics = require('@analytics/google-analytics').default;
const analytics = Analytics({
app: 'my-app',
plugins: [
googleAnalytics({
trackingId: 'UA-1234567'
})
]
});
analytics.track('buttonClicked', {
category: 'User Interaction',
label: 'Signup Button'
});
Identify Users
This feature allows you to identify users and associate them with specific data. The code sample shows how to identify a user with a unique ID and additional user information.
const Analytics = require('analytics').default;
const googleAnalytics = require('@analytics/google-analytics').default;
const analytics = Analytics({
app: 'my-app',
plugins: [
googleAnalytics({
trackingId: 'UA-1234567'
})
]
});
analytics.identify('user-id-123', {
name: 'John Doe',
email: 'john.doe@example.com'
});
Page Views
This feature allows you to track page views within your application. The code sample demonstrates how to initialize the analytics instance and track a page view.
const Analytics = require('analytics').default;
const googleAnalytics = require('@analytics/google-analytics').default;
const analytics = Analytics({
app: 'my-app',
plugins: [
googleAnalytics({
trackingId: 'UA-1234567'
})
]
});
analytics.page();
Other packages similar to analytics
segment
Segment is a customer data platform that helps you collect, clean, and control your customer data. It offers a more comprehensive suite of tools and integrations compared to 'analytics', making it suitable for larger enterprises.
mixpanel
Mixpanel is an advanced analytics service that helps you track user interactions with web and mobile applications. It provides more in-depth analysis and visualization tools compared to 'analytics'.
amplitude
Amplitude is a product analytics service that helps you understand user behavior and optimize your product. It offers more robust analytics and reporting features compared to 'analytics'.
Analytics
This is a pluggable event driven analytics library designed to work with any third party analytics tool.
Features
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 install analytics --save
Usage
import analyticsLib from 'analytics'
import googleAnalytics from 'analytics-plugin-ga'
const plugins = [
googleAnalytics({
trackingId: 'UA-121991291',
})
]
const analytics = analyticsLib({
app: 'my-app-name',
version: 100,
plugins: plugins
})
analytics.page()
analytics.track('userPurchase', {
price: 20
})
analytics.identify('user-id-xyz', {
firstName: 'bill',
lastName: 'murray',
email: 'da-coolest@aol.com'
})
Demo
Install the clone the https://github.com/DavidWells/analytics-example repo and give it a spin.
Current plugins
Adding Analytics providers plugins
The library is designed to work with any third party analytics tool.
Here is an example:
export default function googleAnalytics(config) {
return {
NAMESPACE: 'google-analytics',
config: {
whatever: 'youWant',
googleAnalyticsId: config.id
},
initialize: function() {
},
page: function() {
},
track: function() {
},
identify: function() {
},
loaded: () => {
return !!window.gaplugins
}
}
}
Alternatively, you can also add whatever middleware functionality you'd like via redux middleware.
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
Plugin naming conventions
Plugins should follow a naming convention like so:
analytics-plugin-{your-plugin-name}