plausible_api 0.0.1 → 0.0.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: b58fa3210ca9873840e437b5a892002428841db0f3f5783ebe17d908e21c4796
4
- data.tar.gz: a08c015aaae05008413710e4cc7d367f15fa37385ef5db4d751d072c56255637
3
+ metadata.gz: 8be1224dad0283128a3e21c2d4e53061845ddc2424e2a89655eb4a129925cb4d
4
+ data.tar.gz: b665e6e0dea74c2fe9e1f1309a418cbfe2a5f9be55a4ac0f14845ba6a207fc70
5
5
  SHA512:
6
- metadata.gz: c794f02c31e7bedfd56b461bfc0a66ec8a106583fd71eca4ce2a8c71abce521d69088b93a73b012f8ad6201211b02462778b76d6c876d1084431214d6da1afdd
7
- data.tar.gz: 27efd9f3d37cd91c2952800690b5bc5a998f38fc7ef4c40652f56b6c6d843468ef016896f8af945e0a2b11c33d8ed789e8f0f7b99a65a6fff52a8fda7d19a919
6
+ metadata.gz: 645bd976b0dc10c743232daa6752bee9bb5d9e29ee96235ac0d6efa541ffa1d3fe8e3518e510701de377dd3621e44a4c9b0c54cb99a7b50ffe8b85879036ce8c
7
+ data.tar.gz: 312e92ef37577f2e58e7ffabf72320bc8c9fc431c651779f0ccd2d264d0aa144e08a10ac8a4c71375ceaed5a17d0814b7d36e8d4446cbcc41278d7b775f3d43b
data/README.md CHANGED
@@ -4,13 +4,50 @@ It's based on the WIP [API guide](https://plausible.io/docs/stats-api)
4
4
 
5
5
  ## Usage
6
6
  Add this gem to your Gemfile:
7
- ```
7
+ ```rb
8
8
  gem 'plausible_api'
9
9
  ```
10
- Then you need to initialize a Client and then call one of the available stats:
11
- ```
10
+ Then you need to initialize a Client with your `site_id` (the domain) and your `token`.
11
+ ```rb
12
12
  c = PlausibleApi::Client.new(site_id: 'dailytics.com', token: '123123')
13
- c.aggregate(period: '1w', metrics: 'visitors,pageviews,bounce_rate,visit_duration')
13
+ ```
14
+
15
+ ### Stats > Aggregate
16
+
17
+ You have all these options to get the aggregate stats
18
+ ```rb
19
+ # Use the default parameters (3mo period + the 4 main metrics)
20
+ c.aggregate
21
+
22
+ # Set parameters (period, metrics, filter, date)
23
+ c.aggregate({ period: '3d' })
24
+ c.aggregate({ period: '3d', metrics: 'visitors,pageviews' })
25
+ c.aggregate({ period: '3d', metrics: 'visitors,pageviews', filter: 'event:page==/order/confirmation' })
26
+
27
+ # You'll get something like this:
28
+ => {"bounce_rate"=>{"value"=>81.0}, "pageviews"=>{"value"=>29}, "visit_duration"=>{"value"=>247.0}, "visitors"=>{"value"=>14}}
29
+ ```
30
+
31
+ ### Stats > Timeseries
32
+
33
+ You have all these options to get the timeseries
34
+ ```rb
35
+ # Use the default parameters (3mo period)
36
+ c.timeseries
37
+
38
+ # Set parameters (period, metrics, filter, date)
39
+ c.timeseries({ period: '3d' })
40
+ c.timeseries({ period: '3d', filter: 'event:page==/order/confirmation', date: '2020/02/10' })
41
+
42
+ # You'll get something like this:
43
+ => [{"date"=>"2021-01-11", "value"=>100}, {"date"=>"2021-01-12", "value"=>120}, {"date"=>"2021-01-13", "value"=>80}]
44
+ ```
45
+
46
+ ### Realtime >> Visitors
47
+
48
+ You have a uniq way to call this data
49
+ ```rb
50
+ c.realtime_visitors
14
51
  ```
15
52
 
16
53
  ## Development
@@ -24,5 +61,4 @@ irb(main) > c.aggregate(period: '1w', metrics: 'visitors,pageviews,bounce_rate,v
24
61
  ```
25
62
 
26
63
  ## Todo
27
- - Add support for other endpoints
28
64
  - Tests
@@ -14,16 +14,26 @@ module PlausibleApi
14
14
 
15
15
  def initialize(site_id:, token:)
16
16
  @site_id = site_id.to_s
17
- @token = token
17
+ @token = token.to_s
18
18
  end
19
19
 
20
- def aggregate(period:, metrics:)
21
- call PlausibleApi::Stats::Aggregate.new(period: period, metrics: metrics)
20
+ def aggregate(options = {})
21
+ call PlausibleApi::Stats::Aggregate.new(options)
22
+ end
23
+
24
+ def timeseries(options = {})
25
+ call PlausibleApi::Stats::Timeseries.new(options)
26
+ end
27
+
28
+ def realtime_visitors
29
+ call PlausibleApi::Realtime::Visitors.new
22
30
  end
23
31
 
24
32
  private
25
- def call(resource)
26
- res = Faraday.get("#{BASE_URL}/#{resource.request_url.gsub('$SITE_ID', @site_id)}") do |req|
33
+ def call(api)
34
+ url = "#{BASE_URL}#{api.request_url.gsub('$SITE_ID', @site_id)}"
35
+ puts url
36
+ res = Faraday.get(url) do |req|
27
37
  req.headers['Authorization'] = "Bearer #{@token}"
28
38
  end
29
39
  JSON.parse res.body
@@ -1,11 +1,14 @@
1
1
  # frozen_string_literal: true
2
2
 
3
- require 'plausible_api/request'
4
-
5
3
  module PlausibleApi
6
4
  module Realtime
7
5
  class Visitors
6
+ def initialize
7
+ end
8
8
 
9
+ def request_url
10
+ "/api/v1/realtime/visitors?site_id=$SITE_ID"
11
+ end
9
12
  end
10
13
  end
11
14
  end
@@ -1,18 +1,24 @@
1
1
  # frozen_string_literal: true
2
2
 
3
- require 'plausible_api/request'
4
-
5
3
  module PlausibleApi
6
4
  module Stats
7
5
  class Aggregate
8
-
9
- def initialize(period: nil, metrics: nil)
10
- @period = period || '3m'
11
- @metrics = metrics || 'visitors,pageviews,bounce_rate,visit_duration'
6
+ def initialize(options = {})
7
+ @period = options[:period] || '3mo'
8
+ @metrics = options[:metrics] || 'visitors,pageviews,bounce_rate,visit_duration'
9
+ @filter = options[:filter]
10
+ @date = options[:date]
12
11
  end
13
12
 
14
13
  def request_url
15
- "api/v1/stats/aggregate?site_id=$SITE_ID&period=#{@period}&metrics=#{@metrics}"
14
+ url = "/api/v1/stats/aggregate?site_id=$SITE_ID&period=#{@period}&metrics=#{@metrics}"
15
+ if @filter
16
+ url += "&filter=#{CGI.escape(@filter)}"
17
+ end
18
+ if @date
19
+ url += "&date=#{@date}"
20
+ end
21
+ url
16
22
  end
17
23
  end
18
24
  end
@@ -1,11 +1,24 @@
1
1
  # frozen_string_literal: true
2
2
 
3
- require 'plausible_api/request'
4
-
5
3
  module PlausibleApi
6
4
  module Stats
7
5
  class Timeseries
6
+ def initialize(options = {})
7
+ @period = options[:period] || '3mo'
8
+ @filter = options[:filter]
9
+ @date = options[:date]
10
+ end
8
11
 
12
+ def request_url
13
+ url = "/api/v1/stats/timeseries?site_id=$SITE_ID&period=#{@period}"
14
+ if @filter
15
+ url += "&filter=#{CGI.escape(@filter)}"
16
+ end
17
+ if @date
18
+ url += "&date=#{@date}"
19
+ end
20
+ url
21
+ end
9
22
  end
10
23
  end
11
24
  end
@@ -1,3 +1,3 @@
1
1
  module PlausibleApi
2
- VERSION = '0.0.1'
2
+ VERSION = '0.0.2'
3
3
  end
@@ -12,10 +12,11 @@ Gem::Specification.new do |s|
12
12
  s.description = 'A very humble wrapper for the new API by Plausible'
13
13
  s.authors = ['Gustavo Garcia']
14
14
  s.email = 'gustavo@dailytics.com'
15
- s.homepage = 'https://rubygems.org/gems/plausible_api'
15
+ s.homepage = 'https://github.com/dailytics/plausible-api'
16
16
  s.license = 'MIT'
17
17
  s.files = `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(spec)/}) }
18
18
  s.require_paths = ['lib']
19
19
 
20
- s.add_dependency 'faraday'
20
+ s.add_dependency 'faraday', '~> 1.0'
21
+ s.required_ruby_version = '>= 2.4'
21
22
  end
metadata CHANGED
@@ -1,29 +1,29 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: plausible_api
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.1
4
+ version: 0.0.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - Gustavo Garcia
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2021-02-09 00:00:00.000000000 Z
11
+ date: 2021-02-10 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: faraday
15
15
  requirement: !ruby/object:Gem::Requirement
16
16
  requirements:
17
- - - ">="
17
+ - - "~>"
18
18
  - !ruby/object:Gem::Version
19
- version: '0'
19
+ version: '1.0'
20
20
  type: :runtime
21
21
  prerelease: false
22
22
  version_requirements: !ruby/object:Gem::Requirement
23
23
  requirements:
24
- - - ">="
24
+ - - "~>"
25
25
  - !ruby/object:Gem::Version
26
- version: '0'
26
+ version: '1.0'
27
27
  description: A very humble wrapper for the new API by Plausible
28
28
  email: gustavo@dailytics.com
29
29
  executables: []
@@ -37,10 +37,9 @@ files:
37
37
  - lib/plausible_api/api/realtime/visitors.rb
38
38
  - lib/plausible_api/api/stats/aggregate.rb
39
39
  - lib/plausible_api/api/stats/timeseries.rb
40
- - lib/plausible_api/request.rb
41
40
  - lib/plausible_api/version.rb
42
41
  - plausible_api.gemspec
43
- homepage: https://rubygems.org/gems/plausible_api
42
+ homepage: https://github.com/dailytics/plausible-api
44
43
  licenses:
45
44
  - MIT
46
45
  metadata: {}
@@ -52,7 +51,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
52
51
  requirements:
53
52
  - - ">="
54
53
  - !ruby/object:Gem::Version
55
- version: '0'
54
+ version: '2.4'
56
55
  required_rubygems_version: !ruby/object:Gem::Requirement
57
56
  requirements:
58
57
  - - ">="
@@ -1,5 +0,0 @@
1
- module PlausibleApi
2
- class Request
3
-
4
- end
5
- end