1.1.0 • Published 5 months ago

performantrics v1.1.0

Weekly downloads
-
License
MIT
Repository
github
Last release
5 months ago

Performance Metrics SDK

Overview

Performance Metrics SDK is a lightweight, extensible JavaScript library for collecting and analyzing web performance metrics with minimal setup.

Features

  • 🚀 Automatic Performance Tracking
  • 🔍 Custom Metric Measurement
  • 🔌 Plugin System
  • 🛡️ Error Tracking
  • 📊 Configurable Metric Collection
  • 🌐 Browser Compatibility Checks

Installation

npm install performantrics

Quick Start

Basic Usage

import Performantrics from 'performantrics';

// Initialize with basic configuration
const tracker = new Performantrics({
  projectId: 'my-project',
  endpoint: 'https://metrics-collector.com/track',
  debugMode: true
});

// Track custom performance measurement
tracker.startCustomMeasure('login-process');
// ... login logic
tracker.endCustomMeasure('login-process');

Advanced Configuration

const tracker = new Performantrics({
  projectId: 'my-project',
  endpoint: 'https://metrics-collector.com/track',
  debugMode: true,
  sampleRate: 0.5, // Only track 50% of metrics
  disabledMetrics: ['resource'], // Disable specific metric types
  sensitiveDataFilter: (metric) => {
    // Remove sensitive metadata
    delete metric.metadata?.sensitiveInfo;
    return metric;
  }
});

Plugin System

Creating a Custom Plugin

Custom Plugin Example

API Reference

Constructor Options

OptionTypeDescriptionDefault
projectIdstringUnique identifier for your projectundefined
endpointstringMetrics collection endpointundefined
debugModebooleanEnable console loggingfalse
sampleRatenumberPercentage of metrics to collect1.0
disabledMetricsstring[]Metrics types to ignore[]
sensitiveDataFilterFunctionCustom metric filteringundefined

Methods

  • startCustomMeasure(name: string, metadata?: object): Begin custom performance tracking
  • endCustomMeasure(name: string, metadata?: object): End custom performance tracking
  • recordMetric(metric: object): Manually record a performance metric
  • getMetrics(): Retrieve collected metrics
  • clearMetrics(): Clear all collected metrics
  • use(plugin: PerformancePlugin): Add a plugin to the SDK

Browser Compatibility

The SDK checks for essential browser APIs:

  • PerformanceObserver
  • performance
  • fetch

Incompatible browsers will log warnings but won't break your application.

Error Tracking

Automatically captures:

  • Global error events
  • Unhandled Promise rejections

Performance Types Tracked

  • Paint metrics
  • Navigation metrics
  • Resource loading
  • Custom measurements
  • Long tasks

Contributing

  1. Fork the repository
  2. Create your feature branch
  3. Commit changes
  4. Push to the branch
  5. Create a Pull Request

Support

Open an issue on GitHub for bug reports or feature requests.