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

@peeranat-dan/analytics

v0.0.1

Published

A centralized analytics library supporting Google Analytics, and PostHog

Readme

Centralized Analytics Library

A TypeScript ESModule library that provides a unified interface for analytics providers including Google Analytics and PostHog.

Features

  • Universal Interface: Single API for multiple analytics providers
  • TypeScript Support: Full type safety and IntelliSense
  • ESModule: Modern ES modules support
  • Async/Await: Promise-based APIs
  • Error Handling: Built-in error handling and logging
  • Flexible Configuration: Easy to configure and extend

Installation

npm install @peeranat-dan/analytics

Install the peer dependencies for the analytics providers you want to use:

# For Google Analytics
npm install gtag

# For PostHog
npm install posthog-js

Usage

Basic Setup

import {
  createAnalyticsManager,
  GoogleAnalyticsAdapter,
  PostHogAdapter,
} from "@analytics/analytics";

// Create adapters
const googleAnalytics = new GoogleAnalyticsAdapter({
  measurementId: "G-XXXXXXXXXX",
  config: {
    send_page_view: false, // We'll handle page views manually
  },
});

const posthog = new PostHogAdapter({
  apiKey: "your-posthog-api-key",
  options: {
    api_host: "https://app.posthog.com",
  },
});

// Create analytics manager
const analytics = createAnalyticsManager({
  adapters: [googleAnalytics, posthog],
  enableConsoleLogging: true,
  enableErrorHandling: true,
});

// Initialize
await analytics.initialize();

Tracking Events

// Track a simple event
await analytics.track({
  name: "button_clicked",
  properties: {
    button_id: "signup",
    page: "homepage",
  },
});

// Track with user context
await analytics.track({
  name: "purchase_completed",
  properties: {
    product_id: "prod_123",
    price: 29.99,
    currency: "USD",
  },
  userId: "user_456",
});

Identifying Users

await analytics.identify({
  id: "user_123",
  properties: {
    email: "[email protected]",
    name: "John Doe",
    subscription_tier: "premium",
  },
});

Page Views

// Track current page
await analytics.page();

// Track specific page
await analytics.page("Homepage", {
  section: "landing",
  experiment: "variant_a",
});

Resetting User Data

// Reset user data (useful for logout)
await analytics.reset();

Managing Adapters

// Add adapter dynamically
const newAdapter = new PostHogAdapter({ apiKey: "another-key" });
analytics.addAdapter(newAdapter);

// Remove adapter
analytics.removeAdapter(posthog);

// Get all adapters
const adapters = analytics.getAdapters();

Cleanup

// Clean up resources when done
await analytics.cleanup();

Configuration Options

AnalyticsConfig

interface AnalyticsConfig {
  adapters: AnalyticsAdapter[];
  enableConsoleLogging?: boolean; // Default: false
  enableErrorHandling?: boolean; // Default: false
}

Google Analytics Config

interface GoogleAnalyticsConfig {
  measurementId: string;
  config?: {
    custom_map?: Record<string, string>;
    send_page_view?: boolean;
    [key: string]: unknown;
  };
}

PostHog Config

interface PostHogConfig {
  apiKey: string;
  options?: {
    api_host?: string;
    ui_host?: string;
    loaded?: (posthog: unknown) => void;
    [key: string]: unknown;
  };
}

Event Structure

interface AnalyticsEvent {
  name: string;
  properties?: Record<string, unknown>;
  userId?: string;
  timestamp?: Date;
}

interface AnalyticsUser {
  id: string;
  properties?: Record<string, unknown>;
}

Error Handling

The library includes built-in error handling. When enableErrorHandling is true, individual adapter failures won't crash the entire analytics system:

const analytics = createAnalyticsManager({
  adapters: [googleAnalytics, posthog],
  enableErrorHandling: true, // Prevents individual adapter failures from throwing
  enableConsoleLogging: true, // Logs errors to console
});

Building

npm run build

Development

npm run dev  # Watch mode

License

MIT