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

mini-umami

v0.5.3

Published

A tiny Umami helper for Node.js

Downloads

15

Readme

Mini Umami npm install mini-umami test badge gzip size

A tiny universal JS library for tracking with Umami API:

import umami from "mini-umami";

// Optional, otherwise will read process.env.UMAMI_ID
umami.id = "50429a93-8479-4073-be80-d5d29c09c2ec"; // Your website id

// Optional, otherwise will read process.env.UMAMI_TRACKER, or default to Umami Cloud
umami.tracker = "https://cloud.umami.is/"; // The Umami Tracker host URL, NOT your site URL

// Track page views or events
await umami.view("/exercises", { ...options });
await umami.event("event-name", { ...options });

// If you use Express and want to track page views:
app.use(umami.express);

Compared to @umami/node:

  • Allows you to pass the client's User Agent as agent.
  • Allows you to pass the client's IP as ip.
  • Has a Express connector that automates all of the above with a simple middleware.

Options

You can set two options as the environment variables, with these names:

# .env
UMAMI_ID=50429a93-8479-4073-be80-d5d29c09c2ec
UMAMI_TRACKER=https://cloud.umami.is/

Otherwise, you can set those on the umami instance as:

  • umami.id*: the ID of the website you are tracking.
  • umami.tracker: the url of the domain where the Umami instance is running, defaults to Umami Cloud https://cloud.umami.is/.

Finally, the argument options available are:

  • id*: the ID of the website you are tracking.
  • tracker: the url of the domain where the Umami instance is running, defaults to Umami Cloud https://cloud.umami.is/.
  • ip: the IP of the user/client. For Express, usually use ip: req.headers['x-forwarded-for'] || req.socket.remoteAddress.
  • agent: the user agent of the user visiting. It will default to Node.js.
  • hostname: Hostname of server, e.g. "practice.cards".
  • language: Client language, e.g. "en-US".
  • referrer: Page referrer, e.g. https://x.com/.
  • screen: Screen dimensions (eg. 1920x1080)
  • title: Page title
  • data: Event data properties

*The "id" is mandatory, so it needs to be set up either as an env variable UMAMI_ID, on the instance as umami.id or as the argument option id.

They go, in order of preference: argument option, or instance option, or environment variable.

API

All of the methods are async, but they can be awaited for or just not awaited for, in which case it will be deferred and complete eventually (for persistent servers, if you are in a lambda you might want to wait for it):

umami.view("/hello"); // Will resolve later
await umami.view("/hello"); // Will block the rest of execution until resolution

.view(path, options)

Track a pageview by passing the path to the function:

umami.view("/hello");

You can configure the rest of the options:

umami.view("/hello", {
  title: "Welcome Screen",
  agent: req.headers["user-agent"],
});

.event(name, options)

Track a pageview by passing the path to the function:

await umami.event("signup");

You can configure the rest of the options:

await umami.event("signup", {
  title: "User Sign Up",
  agent: req.headers["user-agent"],
  data: { userId: 324 },
});

.express(req, res, next)

Automatically track page views with Express:

import umami from "mini-umami";

// Assuming your .env is properly setup

const app = express();
app.use(umami.express);

// ... other express routes

This method will automatically get extra information from Express like the referrer, language, user-agent, etc. However note that the screen size will be set as "1920x1080" for desktop and "360x720" for mobile since in the backend we cannot get the device size.

You can then still track other things as usual:

import umami from "mini-umami";

const app = express();
app.use(umami.express);

app.post("/signup", (req, res) => {
  umami.event("Signup");
  // ...
});