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

@flowkey/tracking-pipeline

v1.6.9

Published

Declarative pipeline for tracking events

Downloads

197

Readme

Flowkey Tracking Pipeline

This is a library which helps implementing tracking events and user property changes of an app to multiple tracking services in a declarative and fully typed manner.

It does so by providing a trackEvent function which consumes the app events and can be called from anywhere in the app. Then the different tracking integrations need to implement an eventTransformer which converts the app events into a format that is usable for the tracking services.

The event transformer should be a pure function which has no side effects and does not access any global state or objects (e.g. window, etc.). For each app event it can return one event or multiple events as an array.

To actually transfer the service native events to the service the tracking integration also needs to implement a sendEvent event function which is able to process one tracking service event.

In addition to that a global getCommonEventInfos can be defined to gather global parameters which might be needed for every event (e.g. the userId, ip address, etc.)

Here is an example:

Tracking events

Define the events our app can send

type AppEvent =
  | {
      eventName: "SOMETHING_HAPPENED";
      foo: string;
      test: number;
    }
  | {
      eventName: "I_NEED_TO_GENERATE_TWO_EVENTS";
    };

Define a structure which our service can handle

type MyFancyTrackingServiceEvent = {
  name: string;
  data?: {
    foo: string;
    nested: {
      test: number;
    };
  };
};

Implement getCommonEventInfos

// We want to send the userId with every event so let's grab it from our app service
const getCommonEventInfos = async () => ({
  userId: await AuthService.getUserId()
});

// This automatically derives a type from the async function definition above
type CommonEventInfo = Parameters<
  Parameters<ReturnType<typeof getCommonEventInfos>["then"]>[0]
>[0];

Implement our tracking integration

const myFancyServiceIntegration: EventTrackingIntegration<
  AppEvent,
  MyFancyTrackingServiceEvent,
  CommonEventInfo
> = {
  init: async () => {
    // If needed we can initialize the service e.g. with a token
    await myFancyService.init("MYTOKEN");
  },
  name: "test",
  sendEvent: async (event, commonInfos) => {
    // Here we take the common infos we want and the event and send itthem to our service
    await myFancyService.sendEvent({
      user: commonInfos.userId,
      event: {
        name: event.name,
        data: event.data || {}
      }
    });
  },
  transformEvent: event => {
    switch (event.eventName) {
      case "SOMETHING_HAPPENED":
        return {
          // Our service uses different event names, so we map this here
          name: "it happened",
          data: {
            foo: event.foo,
            nested: {
              test: event.test
            }
          }
        };
      case "I_NEED_TO_GENERATE_TWO_EVENTS":
        return [
          {
            name: "it happened once"
          },
          {
            name: "it happened twice"
          }
        ];
      default:
        return [];
    }
  }
};

Create and initialize our app tracking functions

// Add as many integrations as you need
const integrations = [myFancyServiceIntegration];

const sendEvent = createSendEventFunction<AppEvent, any, CommonEventInfo>({
  integrations,
  // This turns on event logging
  options: { debug: true }
});

const trackEvent = createTrackEventFunction({
  sendEvent,
  getCommonEventInfos
});

const initializeIntegrations = createInitializeIntegrationsFunction({
  integrations
});

// Run the init calls of all integrations
await initializeIntegrations();

Send event from the app

await trackEvent({
  eventName: "SOMETHING_HAPPENED",
  foo: "bar",
  test: 123
});

await trackEvent({
  eventName: "I_NEED_TO_GENERATE_TWO_EVENTS"
});

User Properties

Tracking user property changes works similarly.

Create app user properties type

type UserProperties = {
  name?: string;
  email?: string;
  lastLogin?: Date;
};

// This service is only interested in updating the last login as a unix timestamp
type FancyServiceUserProperties = {
  lastLoginTimestamp?: string;
};

Create the integration

const myFancyServiceIntegration: UserPropertiesTrackingIntegration<
  UserProperties,
  FancyServiceUserProperties
> = {
  transformUserProperties: userProperties =>
    Object.fromEntries(
      Object.entries(userProperties)
        .map(([key, value]) => {
          switch (key) {
            case "lastLogin":
              return ["lastLoginTimestamp", value.getTime()];
            default:
              return [];
          }
        })
        .filter(entry => entry.length)
    ),
  async updateUserProperties(
    userId: string,
    userProperties: FancyServiceUserProperties
  ) {
    await myFancyService.updateUserProperties(userId, userProperties);
  }
};

Wire everything together

const integrations = [myFancyServiceIntegration];

const updateUserProperties = createUpdateUserPropertiesFunction<
  UserProperties,
  FancyServiceUserProperties
>({
  integrations
});

const trackUserProperties = createTrackUserPropertiesFunction({
  updateUserProperties,
  getUserId: () => AuthService.getUserId()
});

Send property updates from the app

await trackUserProperties({
  name: "Foo Bar"
});

await trackUserProperties({
  lastLogin: new Date()
});