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

segment-protocol

v1.0.1

Published

Segment introduced [Analytics.js in 2012](https://news.ycombinator.com/item?id=4912076). Since then, a lot of companies besides Segment have started to use same API for their own SDKs. Notable examples: Jitsu, Rudderstack, June.so and Hightouch Events

Downloads

26

Readme

Segment Protocol

Segment introduced Analytics.js in 2012. Since then, a lot of companies besides Segment have started to use same API for their own SDKs. Notable examples: Jitsu, Rudderstack, June.so and Hightouch Events

However, Segment protocol was never formally spec'ed. segment-protocols project aims to fill the gap, at least partially, by providing a TypeScript types for Segment protocol. Since the protocol doesn't have an official documentation, each vendor has its own treatment and set of extension. This project aims to cover most of them and be flexible enough.

Along with the types, this project also provides a set of tools to work them.

Install

npm install -S segment-protocols #for npm
yarn add segment-protocols       #for yarn
pnpm add segment-protocols       #for pnpm

Usage

Types overview

AnalyticsInterface is an interface that contains all the methods that are supported by Segment-compatible SDKs: track(), page(), screen() etc.

AnayliticsClientEvent is an interface that contains an event that can be sent to Segment-compatible end-point. The type has a number of subtypes that narrow it down for particular event type: ScreenEvent, TrackEvent, IdentifyEvent etc.

AnalyticsServerEvent is an event that is being processed by server. It's an AnayliticsClientEvent plus certain properties which is inferred from HTTP Request context, such as IP address

Tools

The library provides a number of tools to simplify the implementation of Segment protocol for various use-cases

createAnalyticsSerializer() creates an adjusted implementation of AnalyticsInterface that returns AnalyticsServerEvent on every method call. This function serves as a reference implementation that turns .track(), .page() etc. methods into a JSON message that is being sent to Segment-compatible end-point.

createAnalytics(opts) creates an implementation of AnalyticsInterface that uses createAnalyticsSerializer() to serialize events, and passes it to opts.handler.

inferAnalyticsContextFields(). AnalyticsClientEvent and AnalyticsServerEvent has a number of fields that can be inferred from others. For example, context.page.referring_domain can be inferred from context.page.referred. This function takes an event and returns a new event, with inferred fields if those fields are missing in the original event.

Compile-time type-safety

Segment protocol allows to pass any set of properties and user traits along with event. It's convinient and gives a lot of flexibility, but at the same time it's easy to make a mistake and pass a wrong property name or value type. AnalyticsTypeHelper solves this:


//first, let's define types for properties and user traits
type TrackingProperties = { env: string };
type UserProperties = { name: string; email: string };
//define a type with all possible event names for .track() method
type EventNames = 'Sign Up' | 'Sign In' | 'Sign Out';

type MyAnalytics = AnalyticsInterface<{
  //event names for .track() method
  eventNames: "Sign Up" | "Login" | "Logout";
  //properties for .page() and .track() methods
  page: TrackingProperties;
  track: TrackingProperties;
  //user traits for .identify() method
  traits: UserProperties;
}>;

//now we can use it. No need to create a new instance of analytics, since all validation is done at compile time
const myAnalytics: MyAnalytics = analytics as MyAnalytics;

myAnalytics.page({ env: "prod" }); // ✅OK
myAnalytics.page({ otherProp: "prod" }); //⚠️Error - unknown property
myAnalytics.identify({ name: "John Doe", email: "[email protected]" }); //✅OK
myAnalytics.identify({ name: "John Doe" }); //⚠️Error - email is missing
myAnalytics.track("Sign Up"); //✅OK
myAnalytics.track("sign up"); //⚠️Error - misspelled event name

Maintainers guide

bun (>= 1.0.0) and pnpm (>= 8.1.0) are required. bun is used for building and package management, pnpm is used only for publishing to NPM registry

  • bun build — to build the project (including linting and type-checking);
  • bun test — to run tests
  • bun format:check - to check code formatting; bun format - to reformat all files
  • bun run build && bun release - to publish a new version to NPM registry (dry run); bun build && bun release --publish to actually publish it
  • bun run build && bun release:canary - to publish a new canary version