Plausible API (Work In Progress)
This is a simple wrapper to read the Plausible API with Ruby. It's based on the WIP API guide
Usage
Add this gem to your Gemfile:
gem 'plausible_api'
Then you need to initialize a Client with your site_id (the domain) and your token.
c = PlausibleApi::Client.new(site_id: 'dailytics.com', token: '123123')
Stats > Aggregate
You have all these options to get the aggregate stats
# Use the default parameters (3mo period + the 4 main metrics)
c.aggregate
# Set parameters (period, metrics, filter, date)
c.aggregate({ period: '3d' })
c.aggregate({ period: '3d', metrics: 'visitors,pageviews' })
c.aggregate({ period: '3d', metrics: 'visitors,pageviews', filter: 'event:page==/order/confirmation' })
# You'll get something like this:
=> {"bounce_rate"=>{"value"=>81.0}, "pageviews"=>{"value"=>29}, "visit_duration"=>{"value"=>247.0}, "visitors"=>{"value"=>14}}
Stats > Timeseries
You have all these options to get the timeseries
# Use the default parameters (3mo period)
c.timeseries
# Set parameters (period, metrics, filter, date)
c.timeseries({ period: '3d' })
c.timeseries({ period: '3d', filter: 'event:page==/order/confirmation', date: '2020/02/10' })
# You'll get something like this:
=> [{"date"=>"2021-01-11", "value"=>100}, {"date"=>"2021-01-12", "value"=>120}, {"date"=>"2021-01-13", "value"=>80}]
Realtime >> Visitors
You have a uniq way to call this data
c.realtime_visitors
Development
$ gem build plausible_api.gemspec
$ gem install ./plausible_api-X.X.X.gem
$ irb
irb(main) > require 'plausible_api'
irb(main) > c = PlausibleApi::Client.new(site_id: 'dailytics.com', token: '123123')
irb(main) > c.aggregate(period: '1w', metrics: 'visitors,pageviews,bounce_rate,visit_duration')
Todo
- Tests