simple-track
v1.2.0
Published
A simple client side library for creating and firing off analytics events.
Downloads
17
Readme
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
- Create an event generator using
createEventGenerator
const appName = '<your-app-name>';
const analyticsApiUrl = '<your-analytics-endpoint-url>';
const eventGenerator = window.SimpleTrack.createEventGenerator({
appName,
analyticsApiUrl,
});
- 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 bygenerateIdentifier
in thestorage
implementation.The default storage key, if none specified, is
analytics-session-id
.storage
is an implementation of the Storage interface, only requiringgetItem
andsetItem
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 totrack
should call out to theanalyticsApiUrl
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.