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 🙏

© 2026 – Pkg Stats / Ryan Hefner

@sarfarajey/analytics-logger

v1.0.0

Published

GA4 analytics abstraction with debouncing, traffic source detection, and user ID stitching

Readme

@sarfarajey/analytics-logger

GA4 analytics abstraction for browser apps. Configuration-gated dispatch, debouncing for high-frequency events, traffic source detection, and user ID stitching.

Install

npm install @sarfarajey/analytics-logger

Setup

Call configure() once at app bootstrap (e.g. in your layout component or <script> tag):

import { configure } from '@sarfarajey/analytics-logger';

configure({
  // Only dispatch on production
  isEnabled: () => window.location.hostname === 'example.com',

  // Optional second provider (Plausible, PostHog, custom endpoint, etc.)
  secondaryDispatch: (name, props) => {
    window.plausible?.(name, { props });
  },

  // Events to debounce (600ms, only last call fires)
  debouncedEvents: ['slider_change', 'simulation_run'],
});

Usage

import { logEvent, logPageView, setAnalyticsUserId, detectTrafficSource } from '@sarfarajey/analytics-logger';

// Page view (auto-attaches traffic_source)
logPageView({ page: '/dashboard/', title: 'Dashboard' });

// Custom event
logEvent('button_click', { label: 'Export PDF', format: 'A4' });

// Debounced event (configured above) — only last call within 600ms fires
logEvent('slider_change', { value: 42 });

// Stitch an authenticated user to anonymous GA4 events
setAnalyticsUserId('user-abc-123');

// Clear on sign-out
setAnalyticsUserId(null);

// Inspect traffic source directly
const source = detectTrafficSource();
// → 'direct' | 'seo' | 'linkedin' | 'referral:github.com' | 'utm_campaign_value' | 'unknown'

API

configure(config)

Configure the logger. Must be called before logEvent.

interface AnalyticsConfig {
  isEnabled: () => boolean;
  secondaryDispatch?: (name: string, props: EventProps) => void;
  debouncedEvents?: string[]; // default: ['simulation_run', 'cost_override', 'calculate_click']
}

logEvent(name, props?)

Dispatch an event. Debounced events are coalesced over 600ms.

logPageView(props?)

Shorthand for logEvent('page_view', { traffic_source, ...props }).

setAnalyticsUserId(uid)

Set or clear GA4 user_id for anonymous → registered stitching.

detectTrafficSource()

Returns the current traffic source as a string: 'direct', 'seo', 'linkedin', 'internal', 'referral:<host>', UTM source value, or 'unknown'.

License

MIT