1.2.0 • Published 1 year ago

simple-track v1.2.0

Weekly downloads
6
License
MIT
Repository
github
Last release
1 year ago

simple-track

A simple client side library for creating and firing off analytics events.

Setup

npm

npm install --save simple-track

script tag

<script type="module" src="https://cdn.jsdelivr.net/npm/simple-track"></script>

Note: The browser global is SimpleTrack

Usage

  1. Create an event generator using createEventGenerator
const appName = '<your-app-name>';
const analyticsApiUrl = '<your-analytics-endpoint-url>';

const eventGenerator = window.SimpleTrack.createEventGenerator({
    appName,
    analyticsApiUrl,
});
  1. Fire off an event using track!
const eventType = '<your-event-type-or-name>';
const eventData = { foo: 'bar' };

eventGenerator.track(eventType, eventData);

Note: Providing event data is optional, if not provided it defaults to null

Parameters/Customization

When creating an event generator, you have the ability to additionally pass in more than an appName, which is already optional, and the analyticsApiUrl, which is required.

export interface IEventGeneratorInfo {
    analyticsApiUrl: string;
    appName?: string;
    storageKey?: string;
    storage?: Pick<Storage, 'getItem' | 'setItem'>;
    generateIdentifier?: () => string;
    doNotTrack?: boolean;
}
  • storageKey represents the key at which to store the analytics id generated by generateIdentifier in the storage implementation.

    The default storage key, if none specified, is analytics-session-id.

  • storage is an implementation of the Storage interface, only requiring getItem and setItem be implemented.

    The default storage, if none specified, is sessionStorage.
    You are also free to not provide a storage implementation at all (null).

  • generateIdentifier is a function that broadly outputs an idenfitier to associate with the client/browser instance/user. You could always no-op (() => '') this if you didn't want to associate an identifier.

    The default generateIdentifier function, simply generates a UUID utilizing an internal implementation.

  • doNotTrack is a boolean indicating whether calls to track should call out to the analyticsApiUrl or not. If true it will, otherwise it won't.

Additional Info

Internally this library utilizes the Beacon API to fire off the event to the analytics endpoint provided from the client.

If the Beacon API is not supported by the client, however, it will fallback to using the Fetch API.

At the end of the day your analytics endpoint service will receive a POST request with event as a JSON object of the following shape:

export interface IEventInfo<T> {
    appName: string;
    analyticsId: string;
    type: string;
    data: T;
}

export interface IEvent<T> extends IEventInfo<T> {
    timeString: string;
    eventId: string;
    version: number;
}
{
	"appName": "your-app-name",
	"analyticsId": "66eacd05-6624-4589-9c9e-9ef4f194d07a",
	"type": "your-event-type-or-name",
	"data": {
		"foo": "bar"
    },
    "timeString": "2020-12-30T23:18:34.191Z",
	"eventId": "1ff1d482-079b-4f2b-85ff-5f93d832f951",
	"version": 1
}

As an added bonus, the library itself also exports a function called generateUUID that allows you to generate a UUID.

1.2.0

1 year ago

1.1.1

2 years ago

1.1.0

3 years ago

1.0.0

3 years ago

0.0.3

3 years ago

0.0.2

3 years ago

0.0.1

3 years ago