@jackdbd/plausible-client
TypeScript icon, indicating that this package has built-in type declarations

1.0.2 • Public • Published

@jackdbd/plausible-client

npm version Snyk Vulnerabilities for npm package

Unofficial API client for Plausible.io.

Table of Contents

Installation

npm install @jackdbd/plausible-client

API

API docs generated with TypeDoc

Usage

import { makeClient } from '@jackdbd/plausible-client'
import { makeClient as makeStatsClient } from '@jackdbd/plausible-client/stats'
import { makeEleventyFetch } from '@jackdbd/plausible-client/fetch-clients/eleventy-fetch'
import { breakdown } from '@jackdbd/plausible-client/stats/api'

const main = async () => {
  const apiKey = 'YOUR-API-KEY'
  const siteId = 'YOUR-SITE-ID (i.e. domain)'

  const credentials = { apiKey, siteId }

  // options for eleventy-fetch (the fetch client used by the default API client)
  // https://www.11ty.dev/docs/plugins/fetch/#options
  const options = {
    // cache of JSON responses from the Plausible API
    directory: '.cache-plausible-json-responses',
    duration: '5s',
    verbose: true
  }

  // client for all Plausible APIs
  const plausible = makeClient(credentials, options)
  const results = await plausible.stats.breakdown()
  console.log('results', results)

  // client just for the Plausible Stats API
  const stats = makeStatsClient(credentials, options)
  const sameResults = await stats.breakdown()
  console.log('sameResults', sameResults)

  // no client, just a fetch
  const fetchClient = makeEleventyFetch(apiKey, options)
  const sameResultsOnceMore = await breakdown({ fetchClient, siteId })
  console.log('sameResultsOnceMore', sameResultsOnceMore)
}

main()

Readme

Keywords

Package Sidebar

Install

npm i @jackdbd/plausible-client

Weekly Downloads

81

Version

1.0.2

License

MIT

Unpacked Size

62.1 kB

Total Files

93

Last publish

Collaborators

  • jackdbd