plausible_api 0.1.5 → 0.1.9

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 22016ef4c32d7f1d0470a619111b0134134e3c272ed62a6630f3f81184e7cd50
4
- data.tar.gz: ef9f7efae941960ca78dca62b4b5f02207ddfb6ebeffb174120edcc0c14b7eec
3
+ metadata.gz: 946cc54d639da26e6d4e835df896ed3aa2237e9abf9c184085c28c7eafff0497
4
+ data.tar.gz: c98ccc5b1060614edc3141282589fa5158d1367544699efadb89c5ff9aecb0f0
5
5
  SHA512:
6
- metadata.gz: dfa7ec056c52894e489838410ec922d90544e80dca6525718c5c8629fa9fb9faece1fb76dd28f0fc8c6f455187ab46d5bd00e5d60ad7f7a8458c38e77ef60a5e
7
- data.tar.gz: c858040b75e37e69c5d50813b78cf1ccf6708d24982ad865727adeb1a501839ed3b2a0244e8128fb99919dc97b41176c78bf870b1130be332bbfd07b27ee49bc
6
+ metadata.gz: 9ce81d99cd4cce297c3cd388af7836e7f1415d6b5a04e83e51da7e44ba3bb042c8a97e7f9c059e1f1c69ad688e306c9cb98ed6c360e6afe26b908075a52e0063
7
+ data.tar.gz: 21f54c76bd3a314748258681f34604e93928c257e663ff0f5df33bad13b5d5189feb9ecad6b1360e276160ad4dd11aa1e91c825ea2e2b8105827a6a63dbfdb17
data/Gemfile.lock CHANGED
@@ -1,12 +1,12 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- plausible_api (0.1.5)
4
+ plausible_api (0.1.9)
5
5
 
6
6
  GEM
7
7
  remote: https://rubygems.org/
8
8
  specs:
9
- minitest (5.14.3)
9
+ minitest (5.14.4)
10
10
  rake (12.3.3)
11
11
 
12
12
  PLATFORMS
data/README.md CHANGED
@@ -1,4 +1,4 @@
1
- # Plausible API (Work In Progress)
1
+ # Plausible API Ruby Gem
2
2
  This is a simple wrapper to read the Plausible API with Ruby.
3
3
  It's based on the WIP [API guide](https://plausible.io/docs/stats-api)
4
4
 
@@ -27,7 +27,7 @@ c.aggregate
27
27
  c.aggregate({ period: '30d' })
28
28
  c.aggregate({ period: '30d', metrics: 'visitors,pageviews' })
29
29
  c.aggregate({ period: '30d', metrics: 'visitors,pageviews', filters: 'event:page==/order/confirmation' })
30
- c.aggregate({ period: 'custom', date: '2021-01-01,2021-02-10' })
30
+ c.aggregate({ date: '2021-01-01,2021-02-10' })
31
31
 
32
32
  # You'll get something like this:
33
33
  => {"bounce_rate"=>{"value"=>81.0}, "pageviews"=>{"value"=>29}, "visit_duration"=>{"value"=>247.0}, "visitors"=>{"value"=>14}}
@@ -43,7 +43,7 @@ c.timeseries
43
43
  # Set parameters (period, filters, interval)
44
44
  c.timeseries({ period: '7d' })
45
45
  c.timeseries({ period: '7d', filters: 'event:page==/order/confirmation' })
46
- c.timeseries({ period: 'custom', date: '2021-01-01,2021-02-15' })
46
+ c.timeseries({ date: '2021-01-01,2021-02-15' })
47
47
 
48
48
  # You'll get something like this:
49
49
  => [{"date"=>"2021-01-11", "value"=>100}, {"date"=>"2021-01-12", "value"=>120}, {"date"=>"2021-01-13", "value"=>80}...]
@@ -58,7 +58,7 @@ c.breakdown
58
58
  c.breakdown({ property: 'visit:source' })
59
59
  c.breakdown({ property: 'visit:source', metrics: 'visitors,pageviews' })
60
60
  c.breakdown({ property: 'visit:source', metrics: 'visitors,pageviews', period: '30d' })
61
- c.breakdown({ property: 'visit:source', metrics: 'visitors,pageviews', period: 'custom', date: '2021-01-01,2021-02-01' })
61
+ c.breakdown({ property: 'visit:source', metrics: 'visitors,pageviews', date: '2021-01-01,2021-02-01' })
62
62
 
63
63
  # You'll get something like this:
64
64
  => [{"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}]
@@ -38,12 +38,17 @@ module PlausibleApi
38
38
  end
39
39
 
40
40
  def valid?
41
- realtime_visitors.is_a? Integer
41
+ begin
42
+ realtime_visitors
43
+ return true
44
+ rescue
45
+ return false
46
+ end
42
47
  end
43
48
 
44
49
  private
45
50
  def call(api)
46
- raise StandardError.new "There is some invalid parameters." unless api.valid?
51
+ raise StandardError.new api.errors unless api.valid?
47
52
 
48
53
  url = "#{BASE_URL}#{api.request_url.gsub('$SITE_ID', @site_id)}"
49
54
  uri = URI.parse(url)
@@ -54,7 +59,13 @@ module PlausibleApi
54
59
  http = Net::HTTP.new(uri.host, uri.port)
55
60
  http.use_ssl = true
56
61
 
57
- api.parse_response http.request(req).body
62
+ response = http.request(req)
63
+
64
+ if response.code == "200"
65
+ api.parse_response response.body
66
+ else
67
+ raise StandardError.new response.body
68
+ end
58
69
  end
59
70
  end
60
71
  end
@@ -3,21 +3,19 @@
3
3
  module PlausibleApi
4
4
  module Stats
5
5
  class Aggregate < Base
6
+
6
7
  def initialize(options = {})
7
- @period = options[:period] || '30d'
8
- @metrics = options[:metrics] || 'visitors,pageviews,bounce_rate,visit_duration'
9
- @filters = options[:filters]
10
- @compare = options[:compare]
11
- @date = options[:date]
12
- @period = 'custom' if @date
8
+ super({ period: '30d',
9
+ metrics: 'visitors,pageviews,bounce_rate,visit_duration' }
10
+ .merge(options))
13
11
  end
14
-
12
+
15
13
  def request_url_base
16
14
  "/api/v1/stats/aggregate?site_id=$SITE_ID"
17
15
  end
18
16
 
19
17
  def parse_response(body)
20
- JSON.parse body
18
+ JSON.parse(body)['results']
21
19
  end
22
20
  end
23
21
  end
@@ -3,8 +3,12 @@
3
3
  module PlausibleApi
4
4
  module Stats
5
5
  class Base
6
+
6
7
  def initialize(options = {})
7
- raise NotImplementedError
8
+ @options = { compare: nil, date: nil, filters: nil, interval: nil,
9
+ limit: nil, metrics: nil, page: nil, period: nil,
10
+ property: nil }.merge(options)
11
+ @options[:period] = 'custom' if @options[:date]
8
12
  end
9
13
 
10
14
  def request_url_base
@@ -12,17 +16,8 @@ module PlausibleApi
12
16
  end
13
17
 
14
18
  def request_url
15
- url = request_url_base
16
- url += "&period=#{@period}" if defined?(@period) && @period
17
- url += "&metrics=#{@metrics}" if defined?(@metrics) && @metrics
18
- url += "&filters=#{CGI.escape(@filters)}" if defined?(@filters) && @filters
19
- url += "&compare=#{@compare}" if defined?(@compare) && @compare
20
- url += "&interval=#{@interval}" if defined?(@interval) && @interval
21
- url += "&property=#{@property}" if defined?(@property) && @property
22
- url += "&limit=#{@limit}" if defined?(@limit) && @limit
23
- url += "&page=#{@page}" if defined?(@page) && @page
24
- url += "&date=#{@date}" if defined?(@date) && @date
25
- url
19
+ params = @options.select{ |_,v| !v.to_s.empty? }
20
+ [request_url_base, URI.encode_www_form(params)].reject{|e| e.empty?}.join('&')
26
21
  end
27
22
 
28
23
  def parse_response(body)
@@ -33,50 +28,50 @@ module PlausibleApi
33
28
  errors.empty?
34
29
  end
35
30
 
36
- def errors
31
+ def errors
37
32
  allowed_period = %w(12mo 6mo month 30d 7d day custom)
38
33
  allowed_metrics = %w(visitors pageviews bounce_rate visit_duration)
39
34
  allowed_compare = %w(previous_period)
40
35
  allowed_interval = %w(date month)
41
- allowed_property = %w(event:page event:source event:referrer event:utm_medium
42
- event:utm_source event:utm_campaign event:device event:browser event:browser_version
43
- event:os event:os_version event:country)
36
+ allowed_property = %w(event:page visit:entry_page visit:exit_page visit:source visit:referrer
37
+ visit:utm_medium visit:utm_source visit:utm_campaign visit:device visit:browser
38
+ visit:browser_version visit:os visit:os_version visit:country)
44
39
  e = 'Not a valid parameter. Allowed parameters are: '
45
-
40
+
46
41
  errors = []
47
- if defined?(@period) && @period
48
- errors.push({ period: "#{e}#{allowed_period.join(', ')}" }) unless allowed_period.include? @period
42
+ if @options[:period]
43
+ errors.push({ period: "#{e}#{allowed_period.join(', ')}" }) unless allowed_period.include? @options[:period]
49
44
  end
50
- if defined?(@metrics) && @metrics
51
- metrics_array = @metrics.split(',')
45
+ if @options[:metrics]
46
+ metrics_array = @options[:metrics].split(',')
52
47
  errors.push({ metrics: "#{e}#{allowed_metrics.join(', ')}" }) unless metrics_array & allowed_metrics == metrics_array
53
48
  end
54
- if defined?(@compare) && @compare
55
- errors.push({ compare: "#{e}#{allowed_compare.join(', ')}" }) unless allowed_compare.include? @compare
49
+ if @options[:compare]
50
+ errors.push({ compare: "#{e}#{allowed_compare.join(', ')}" }) unless allowed_compare.include? @options[:compare]
56
51
  end
57
- if defined?(@interval) && @interval
58
- errors.push({ interval: "#{e}#{allowed_interval.join(', ')}" }) unless allowed_interval.include? @interval
52
+ if @options[:interval]
53
+ errors.push({ interval: "#{e}#{allowed_interval.join(', ')}" }) unless allowed_interval.include? @options[:interval]
59
54
  end
60
- if defined?(@property) && @property
61
- errors.push({ property: "#{e}#{allowed_property.join(', ')}" }) unless allowed_property.include? @property
55
+ if @options[:property]
56
+ errors.push({ property: "#{e}#{allowed_property.join(', ')}" }) unless allowed_property.include? @options[:property]
62
57
  end
63
- if defined?(@filters) && @filters
64
- filters_array = @filters.to_s.split(';')
58
+ if @options[:filters]
59
+ filters_array = @options[:filters].to_s.split(';')
65
60
  filters_array.each do |f|
66
61
  parts = f.split("==")
67
62
  errors.push({ filters: "Unrecognized filter: #{f}" }) unless parts.length == 2
68
63
  errors.push({ filters: "Unknown metric for filter: #{parts[0]}" }) unless allowed_property.include? parts[0]
69
64
  end
70
65
  end
71
- if defined?(@limit) && @limit
72
- errors.push({ limit: "Limit param must be a positive number" }) unless @limit.is_a? Integer and @limit > 0
66
+ if @options[:limit]
67
+ errors.push({ limit: "Limit param must be a positive number" }) unless @options[:limit].is_a? Integer and @options[:limit] > 0
73
68
  end
74
- if defined?(@page) && @page
75
- errors.push({ page: "Page param must be a positive number" }) unless @page.is_a? Integer and @page > 0
69
+ if @options[:page]
70
+ errors.push({ page: "Page param must be a positive number" }) unless @options[:page].is_a? Integer and @options[:page] > 0
76
71
  end
77
- if defined?(@date) && @date
78
- errors.push({ date: 'You must define the period parameter as custom' }) unless @period == 'custom'
79
- date_array = @date.split(",")
72
+ if @options[:date]
73
+ errors.push({ date: 'You must define the period parameter as custom' }) unless @options[:period] == 'custom'
74
+ date_array = @options[:date].split(",")
80
75
  errors.push({ date: 'You must define start and end dates divided by comma' }) unless date_array.length == 2
81
76
  regex = /\d{4}\-\d{2}\-\d{2}/
82
77
  errors.push({ date: 'Wrong format for the start date' }) unless date_array[0] =~ regex
@@ -86,4 +81,4 @@ module PlausibleApi
86
81
  end
87
82
  end
88
83
  end
89
- end
84
+ end
@@ -3,18 +3,11 @@
3
3
  module PlausibleApi
4
4
  module Stats
5
5
  class Breakdown < Base
6
+
6
7
  def initialize(options = {})
7
- @property = options[:property] || 'event:page' # required
8
- @period = options[:period] || '30d' # required
9
- @metrics = options[:metrics]
10
- @limit = options[:limit]
11
- @page = options[:page]
12
- @filters = options[:filters]
13
- @date = options[:date]
14
- @period = 'custom' if @date
15
-
8
+ super({ period: '30d', property: 'event:page' }.merge(options))
16
9
  end
17
-
10
+
18
11
  def request_url_base
19
12
  "/api/v1/stats/breakdown?site_id=$SITE_ID"
20
13
  end
@@ -4,9 +4,6 @@ module PlausibleApi
4
4
  module Stats
5
5
  module Realtime
6
6
  class Visitors < PlausibleApi::Stats::Base
7
- def initialize
8
- end
9
-
10
7
  def request_url_base
11
8
  "/api/v1/stats/realtime/visitors?site_id=$SITE_ID"
12
9
  end
@@ -3,14 +3,11 @@
3
3
  module PlausibleApi
4
4
  module Stats
5
5
  class Timeseries < Base
6
+
6
7
  def initialize(options = {})
7
- @period = options[:period] || '30d'
8
- @filters = options[:filters]
9
- @interval = options[:interval]
10
- @date = options[:date]
11
- @period = 'custom' if @date
8
+ super({ period: '30d' }.merge(options))
12
9
  end
13
-
10
+
14
11
  def request_url_base
15
12
  "/api/v1/stats/timeseries?site_id=$SITE_ID"
16
13
  end
@@ -1,3 +1,3 @@
1
1
  module PlausibleApi
2
- VERSION = "0.1.5"
2
+ VERSION = "0.1.9"
3
3
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: plausible_api
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.5
4
+ version: 0.1.9
5
5
  platform: ruby
6
6
  authors:
7
7
  - Gustavo Garcia
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2021-02-23 00:00:00.000000000 Z
11
+ date: 2021-10-18 00:00:00.000000000 Z
12
12
  dependencies: []
13
13
  description: A very humble wrapper for the new API by Plausible
14
14
  email: