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

@istorry/plausible

v1.0.2

Published

Typesafe wrapper for the Plausible analytics API

Downloads

24

Readme

@istorry/plausible

A type-safe wrapper for the Plausible Analytics API, designed to simplify integration and enhance developer experience with TypeScript support.

Installation

To install the package, use npm:

npm install @istorry/plausible

Or with yarn:

yarn add @istorry/plausible

Usage

Importing and Configuration

First, import the PlausibleAPI class and instantiate it with your Plausible site ID and access token:

import { PlausibleAPI } from "@istorry/plausible";

const plausible = new PlausibleAPI({
  siteId: "<your-site-id>",
  accessToken: "<your-access-token>",
});

Fetching Realtime Visitors

To get the number of realtime visitors, use the realtime method:

const visitors = await plausible.realtime();
console.log(`Realtime visitors: ${visitors}`);

Aggregated Data

To get aggregated metrics data, use the aggregate method. You can specify the period, metrics, and optional filters:

const aggregate = await plausible.aggregate({
  period: "30d", // Time period for aggregation, e.g., "30d" for 30 days
  metrics: ["visitors", "pageviews"], // Metrics to aggregate
  filters: [
    {
      property: "event:goal", // Property to filter by
      operator: "==", // Comparison operator
      value: "Signup", // Value to filter by
    },
  ],
});

console.log("Aggregated data:", aggregate);

Timeseries Data

To get timeseries data, use the timeseries method. You can specify the period, metrics, interval, and optional filters:

const timeseries = await plausible.timeseries({
  period: "30d", // Time period for aggregation, e.g., "30d" for 30 days
  metrics: ["visitors", "pageviews"], // Metrics to aggregate (same as in aggregate method)
  interval: "date", // Interval for aggregation, e.g., "month" for monthly data (default is "month")
  filters: [
    {
      property: "visit:device",
      operator: "==",
      value: "Desktop",
    },
  ],
});

console.log("Timeseries data:", timeseries);

API Methods

realtime()

Fetches the current number of realtime visitors.

Returns:

  • Promise<number>: The number of realtime visitors.

aggregate<T extends readonly MetricEnum[]>(options: PlausibleAggregateOptions<T>)

Fetches aggregated data based on the specified options.

Parameters:

Returns:

  • Promise<{ [K in T[number]]: PlausibleAggregate[K] }>: Aggregated data for the specified metrics.

timeseries<T extends readonly (keyof PlausibleTimeseries)[]>(options: PlausibleTimeseriesOptions<T>)

Fetches timeseries data based on the specified options.

Parameters:

Returns:

  • Promise<Array<{ date: string } & { [K in T[number]]: PlausibleTimeseries[K] }>>: Timeseries data for the specified metrics.

Error Handling

Errors are thrown if the API call fails. Ensure proper error handling in your application.

Contributing

Contributions are welcome! Please open an issue or submit a pull request on GitHub.

License

This project is licensed under the MIT License. See the LICENSE file for details.


Feel free to adjust the "Contributing" and "License" sections according to your project's specifics. This revised README includes installation instructions, usage examples, method details, and additional sections for contributing and licensing.