A type-safe wrapper for the Plausible Analytics API, designed to simplify integration and enhance developer experience with TypeScript support.
To install the package, use npm:
npm install @istorry/plausible
Or with yarn:
yarn add @istorry/plausible
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>",
});
To get the number of realtime visitors, use the realtime
method:
const visitors = await plausible.realtime();
console.log(`Realtime visitors: ${visitors}`);
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);
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);
Fetches the current number of realtime visitors.
Returns:
Promise<number>
: The number of realtime visitors.
Fetches aggregated data based on the specified options.
Parameters:
options
: An object defining aggregation options. See PlausibleAggregateOptions for details.
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:
options
: An object defining timeseries options. See PlausibleTimeseriesOptions for details.
Returns:
Promise<Array<{ date: string } & { [K in T[number]]: PlausibleTimeseries[K] }>>
: Timeseries data for the specified metrics.
Errors are thrown if the API call fails. Ensure proper error handling in your application.
Contributions are welcome! Please open an issue or submit a pull request on GitHub.
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.