@jackdbd/plausible-client
v1.0.2
Published
Unofficial API client for Plausible.io
Downloads
1
Readme
@jackdbd/plausible-client
Unofficial API client for Plausible.io.
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()