npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

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

  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.