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

videoblocks-snoopy

v1.0.13

Published

Front-end analytics library

Downloads

15

Readme

Snoopy

Snoopy

Front-end tracking library for asynchronously sending browser events to an external tracker URL with plugins.

Usage

Include snoopy with:

import Snoopy from 'snoopy';

API

Snoopy(name, trackerURL, options = {})

Instantiates a snoopy object for tracking front end events and sending to a tracker URL

Arguments

  • name (string): name of Snoopy tracker instance
  • trackerURL (string): URL to send tracking data to
  • options (object): object containing Snoopy options (e.g. headers, request method, disabledPlugins)

Options

  • disabledPlugins (object): Names of plugins to be disabled, accepts a k:v object with:
    • { 'nameOfPlugin': true, 'plugin2': true, ...}
  • method (string): HTTP request method (will use 'POST' if not provided)
  • headers (object): HTTP headers to include. Will not include any by default

Returns

  • Snoopy (object)

Example

var snoopy = new Snoopy('tracker', 'mytrackerurl.com');

track(eventName, eventCategory, eventAction, eventOrigin, eventLabel = '', eventValue = '', eventSchema = 'latest', data = {})

Sends event name, payload, and schema to the tracker URL

Arguments

Required
  • eventName (string) - Name of the event being tracked
  • eventCategory (string) - Category of the event being tracked
  • eventAction (string) - Action being performed in the event
  • eventOrigin (string) - Origin from which event is being tracked
Optional
  • eventLabel (string) - Label associated with the event being tracked
  • eventValue (string) - Value associated with the event
  • eventSchema (string) - (Default: 'latest'): Schema version for downstream validation
  • data (object): Object containing event data

Returns

Promise(Array[eventObject, XMLHttpRequest.xhr])

snoopy.track('clickButton', 'myCategory', 'myAction', 'myOrigin', 'myLabel', 'myValue', 'latest', {user_id: '123456'})
// => {eventTimestamp: 1520541978172, eventUuid: "5dc45625-281e-4bd5-ac4b-c625e04f4ceb", eventName: "clickButton", …}

This will send an object to mytrackerurl.com logging that visitor abc123 did a clickSignup event, along with a timestamp and any other global parameters you have set.

Notes:

It is highly recommended that you add an eventSchema, eventLabel and eventValue to each event, this will allow GA to pick these events up as well as the internal tracker you are using, and ensure you are sending valid event schemas.

Read here to understand more about GA Events.

set({ k: v })

Add common payload values to all events emitted by the tracker. This will automatically add them to the payload and overwrite previous values if they already existed.

Arguments

  • (Object) - obj of key-values including any values you want to set. Can be nested.

Returns

  • (Error) - if the argument isn't an object
  • (Boolean) - True if success; Never False;
snoopy.set({ 'user_id': '123456', 'source': 'referral' })
// => true

unset(stringPath)

Remove payload values added with set(). Takes a string path delimited by . and removes node from object, including child nodes.

Arguments

  • (String) - Path of key to be deleted, delimited by . (key1.key2.key3 would remove key3 and all its children)

Returns

  • (Boolean) - True if success; Never False;
snoopy.unset('user.account.userId')
// => true

Plugins

Snoopy is an extensible front-end library. Plugins are loaded automatically from the plugins directory inside of src, and are used to transform the event object in a sequence. Adding a plugin requires a pull-request.

Example

./plugins/cookies.js

class Cookies {
  constructor(tracker) {
    this.tracker = tracker;
    this.cookies = this.getCookies();
    this.order = 'after';
    return this;
  }

  getCookies() {
    const pairs = document.cookie.split(';');
    const cookies = {};
    for (let i = 0; i < pairs.length; i += 1) {
      const pair = pairs[i].split('=');
      cookies[(pair[0]).trim()] = decodeURIComponent(pair[1]);
    }
    return cookies;
  }

  callback(event) {
    event.data.cookies = {
      cookies: this.cookies,
    };
    return event;
  }
}

export default Cookies;

This will be loaded into Snoopy by default

var snoopy = new Snoopy('tracker', 'mytrackerurl.com');

snoopy.track('clickButton', 'myCategory', 'myAction', 'myOrigin', 'myLabel', 'myValue', 'latest', {user_id: '123456'}).then(function(response) {
	console.log(response[0].payload.cookies);
	// => (String) of all your non http-only cookies
});

Disabling Plugins

var snoopy = new Snoopy('tracker', 'mytrackerurl.com', { disabledPlugins: { cookies: true } });

snoopy.track('clickButton', 'myCategory', 'myAction', 'myOrigin', 'myLabel', 'myValue', 'latest', {user_id: '123456'}).then(function(response) {
	console.log(response[0].payload.cookies);
	// => undefined
});

Plugin order

Plugins can include an "order" property with a value of either 'before' or 'after'. Plugins without a valid 'order' will be treated as 'before'.

Plugins tagged 'before' will execute before those marked 'after', thus those marked 'after' can refer to those marked 'before'.

Development

Getting started

  1. Clone the library then
  • Run yarn install (recommended)
  1. Development mode
  • Running npm start will open a webpack dev server
  1. Running the tests
  • Run yarn test or yarn test:watch

Scripts

  • yarn build or npm run build - produces production version of your library under the lib folder
  • yarn test or yarn test:watch - well ... it runs the tests :)
  • npm start - Open a dev server where you can play around with Snoopy