Skip to content

dailytics/plausible_api

Repository files navigation

Plausible API Ruby Gem

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 api_key. Optionally, you can pass a third parameter in case you are using a self-hosted instance of Plausible (You don't need to add this third parameter if your are using the comercial version of Plausible).

# Using the comercial version:
c = PlausibleApi::Client.new("mysite.com", "MYAPIKEY")

# Using a self hosted instance
c = PlausibleApi::Client.new("mysite.com", "MYAPIKEY", "https://my-hosted-plausible.com")

# Test if the site and token are valid
c.valid?
=> true

If you will always work with the same site, you can set some (or all) of these 3 parameters before initializing the client. On a Ruby on Rails app, you can add this to an initializer like config/initializers/plausible.rb

# Do not include a trailing slash
PlausibleApi.configure do |config|
  config.base_url = "https://your-plausible-instance.com"
  config.site_id = "dailytics.com"
  config.api_key = "123123"
end

And then, initializing the client simply like this:

c = PlausibleApi::Client.new

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, compare)
c.aggregate({ period: '30d' })
c.aggregate({ period: '30d', metrics: 'visitors,pageviews' })
c.aggregate({ period: '30d', metrics: 'visitors,pageviews', filters: 'event:page==/order/confirmation' })
c.aggregate({ date: '2021-01-01,2021-02-10' })

# 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, filters, interval)
c.timeseries({ period: '7d' })
c.timeseries({ period: '7d', filters: 'event:page==/order/confirmation' })
c.timeseries({ date: '2021-01-01,2021-02-15' })

# You'll get something like this:
=> [{"date"=>"2021-01-11", "value"=>100}, {"date"=>"2021-01-12", "value"=>120}, {"date"=>"2021-01-13", "value"=>80}...]

Stats > Breakdown

# Use the default parameters (30d, event:page)
c.breakdown

# Set parameters (property, period, metrics, limit, page, filters, date)
c.breakdown({ property: 'visit:source' })
c.breakdown({ property: 'visit:source', metrics: 'visitors,pageviews' })
c.breakdown({ property: 'visit:source', metrics: 'visitors,pageviews', period: '30d' })
c.breakdown({ property: 'visit:source', metrics: 'visitors,pageviews', date: '2021-01-01,2021-02-01' })

# You'll get something like this:
=> [{"page"=>"/", "visitors"=>41}, {"page"=>"/plans/", "visitors"=>14}, {"page"=>"/agencies/", "visitors"=>8}, {"page"=>"/features/", "visitors"=>8}, {"page"=>"/ready/", "visitors"=>5}, {"page"=>"/contact/", "visitors"=>4}, {"page"=>"/about/", "visitors"=>3}, {"page"=>"/donate/", "visitors"=>3}]

Realtime > Visitors

It's as simple as:

c.realtime_visitors

=> 13

Events

You can send an event like this:

# Example using Rack::Request in Rails for user_agent and ip.
c.event({
  name: "signup",
  url: 'https://dailytics.com/users/new',
  user_agent: "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/120.0.0.0 Safari/537.3",
  ip: "127.0.0.1"
})

Development

After checking out the repo, run bin/setup to install dependencies. Then, run rake test to run the tests. You can also run bin/console for an interactive prompt that will allow you to experiment.

To install this gem onto your local machine, run bundle exec rake install. To release a new version, update the version number in version.rb, and then run bundle exec rake release, which will create a git tag for the version, push git commits and tags, and push the .gem file to rubygems.org.

Contributing

Bug reports and pull requests are welcome on GitHub at https://github.com/dailytics/plausible_api. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the code of conduct.

License

The gem is available as open source under the terms of the MIT License.

Code of Conduct

Everyone interacting in the PlausibleApi project's codebases, issue trackers, chat rooms and mailing lists is expected to follow the code of conduct.