Analytics Client

A lightweight (2.9k) JavaScript analytics client library with offline support for Hybrid and Progressive Web Apps.
This library provides a simple API for tracking events and user properties, compatible with popular analytics service endpoints. Events are written to localStorage first and are only removed once the analytics API confirms receipt, thus allowing the device to go offline without losing events.
Note: This client library can be used with self-hosted analytics APIs (like TrainerDay's analytics-api) or any compatible analytics service endpoint.
Self-Hosted Integration
To use with TrainerDay's analytics-api:
analytics.init('your-analytics-token', {
trackingUrl: 'https://your-domain.com/track?data=',
engageUrl: 'https://your-domain.com/engage?data='
});
analytics.track('Event Name', { property: 'value' });
analytics.identify('user@email.com');
Demo: The analytics-api project includes a complete demo page at /demo/index.html
showing integration examples.
Usage
NPM Installation (Recommended)
npm install @trainerday/analytics-client
CDN Installation
<script src="https://unpkg.com/@trainerday/analytics-client@latest/dist/analytics-client.min.js"></script>
Manual Installation
Download analytics-client.min.js and add to your project:
<script src="analytics-client.min.js"></script>
At present only the following methods are supported:
var analytics = mixpanel;
analytics.init('your-token-here');
analytics.identify('user@email.com');
analytics.register({'Gender': 'Female'});
analytics.people.set({
$email: 'user@email.com'
});
analytics.track('Your Event Name' {
firstName: 'Optional event property 1',
lastName: 'Optional event property 2'
});
analytics.reset();
analytics.mute();
analytics.unmute();
if (analytics.muted) {
console.log('Analytics is disabled');
}
Contributing
Pull requests are welcomed:
- Fork it!
- Create your feature branch:
git checkout -b my-new-feature
- Commit your changes:
git commit -m 'Add some feature'
- Push to the branch:
git push origin my-new-feature
- Submit a pull request
Dependencies
analytics-client uses window.localStorage
and window.Promise
which should exist in all modern browsers.
Update .min files
To generate a new analytics-client.min.js from source, tweak the version number in package.json
and run:
npm run build
Star the repo
Star the repo if you find this useful as it helps me prioritize which bugs I should tackle first.
History
For change-log, check releases.
License
Licensed under MIT License © John Doherty