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

plausible-node-client

v1.0.0

Published

A lightweight Node.js client for sending custom events to Plausible Analytics via the Events API.

Readme

plausible-node-client

A lightweight, zero-dependency Node.js client for sending events to Plausible Analytics from a back-end server using the Events API.

Works with Node.js 18+ (uses the built-in fetch API). Fully typed with TypeScript.


Installation

npm install plausible-node-client

Quick start

import { PlausibleClient } from "plausible-node-client";

const plausible = new PlausibleClient({ domain: "example.com" });

Sending a custom event

await plausible.sendEvent({
  name: "Signup",                        // custom event name
  url: "https://example.com/register",   // page where the event happened
  userAgent: req.headers["user-agent"] ?? "",
  ip: req.ip,                            // visitor's IP for geo + dedup
  props: {
    plan: "pro",
    referral_code: "SUMMER24",
  },
});

Sending a pageview

await plausible.sendPageview({
  url: "https://example.com/blog/my-post",
  userAgent: req.headers["user-agent"] ?? "",
  ip: req.ip,
});

Revenue tracking (e-commerce)

await plausible.sendEvent({
  name: "Purchase",
  url: "https://example.com/checkout/success",
  userAgent: req.headers["user-agent"] ?? "",
  ip: req.ip,
  revenue: { currency: "USD", amount: 49.99 },
  props: { product_id: "sku-42" },
});

Express.js middleware example

import express from "express";
import { PlausibleClient, PlausibleError } from "plausible-node-client";

const app = express();
const plausible = new PlausibleClient({ domain: "example.com" });

app.post("/api/checkout", async (req, res) => {
  // ... process checkout ...

  try {
    await plausible.sendEvent({
      name: "Purchase",
      url: `https://example.com${req.originalUrl}`,
      userAgent: req.headers["user-agent"] ?? "",
      ip: req.ip,
      revenue: { currency: "USD", amount: 99.0 },
    });
  } catch (err) {
    if (err instanceof PlausibleError) {
      console.warn("Plausible tracking failed:", err.message, "status:", err.statusCode);
    }
  }

  res.json({ success: true });
});

API

new PlausibleClient(options)

| Option | Type | Default | Description | |---|---|---|---| | domain | string | required | Domain name as configured in your Plausible dashboard. | | baseUrl | string | "https://plausible.io" | Override for self-hosted Plausible instances. | | timeoutMs | number | 5000 | Request timeout in milliseconds. |


client.sendEvent(options): Promise<void>

| Option | Type | Required | Description | |---|---|---|---| | name | string | ✅ | Event name. Use "pageview" for page views. | | url | string | ✅ | Full URL where the event occurred. | | userAgent | string | ✅ | Visitor's User-Agent (from the incoming request header). | | ip | string | — | Visitor's IP address. Required for correct geo & deduplication when calling from a server. | | referrer | string | — | Referrer URL. | | props | Record<string, string \| number \| boolean> | — | Custom properties (max 30 pairs). | | revenue | { currency: string; amount: number \| string } | — | Revenue data for e-commerce tracking. | | interactive | boolean | — | Set false to exclude from bounce-rate calculations. Default true. |


client.sendPageview(options): Promise<void>

Shorthand for sendEvent({ name: "pageview", ...options }).


PlausibleError

Thrown on HTTP errors or network failures.

| Property | Type | Description | |---|---|---| | message | string | Human-readable error description. | | statusCode | number \| undefined | HTTP status returned by Plausible (absent for network errors). | | cause | unknown | Original caught error for network failures. |


Important notes on visitor tracking

Because events are sent server-side, you must forward the visitor's real User-Agent and IP address from the original HTTP request. Without these, every event will appear to come from a single unique visitor (your server).

// Express example – forward both headers
await plausible.sendEvent({
  name: "Download",
  url: `https://example.com${req.path}`,
  userAgent: req.headers["user-agent"] ?? "",   // forwarded UA
  ip: req.ip,                                    // forwarded IP
});

License

MIT