plausible_api 0.1.5 → 0.1.6

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: 040cbafdc8f270ac3f2bfdcaed7501535f801e93ba1059e6b776be42258714e9
4
+ data.tar.gz: 9e5511a3d7bb752a1b0a89e0f4d3b405eea5a6ca56262cf684626d7cf444dafc
5
5
  SHA512:
6
- metadata.gz: dfa7ec056c52894e489838410ec922d90544e80dca6525718c5c8629fa9fb9faece1fb76dd28f0fc8c6f455187ab46d5bd00e5d60ad7f7a8458c38e77ef60a5e
7
- data.tar.gz: c858040b75e37e69c5d50813b78cf1ccf6708d24982ad865727adeb1a501839ed3b2a0244e8128fb99919dc97b41176c78bf870b1130be332bbfd07b27ee49bc
6
+ metadata.gz: 83077cd7edeb80de8388afbea3fd85d2ba2e8068345e10df04331c6356f7dc5a7c7096da9774bfe2fa6324d76fd68ec5c80cf4c39f1dd499a39dbb8d73ba24c7
7
+ data.tar.gz: fc95a48bf881567cce4f126c899206e18cdfbbc0b0f3ddeba6e66f37553a12f2a77a541472005eafd3c07ddc435d70886fc95d13b751b906056d616bd5604a42
data/Gemfile.lock CHANGED
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- plausible_api (0.1.5)
4
+ plausible_api (0.1.6)
5
5
 
6
6
  GEM
7
7
  remote: https://rubygems.org/
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}]
@@ -3,15 +3,13 @@
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
@@ -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)
@@ -38,45 +33,45 @@ module PlausibleApi
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:source visit:referrer visit:utm_medium
37
+ visit:utm_source visit:utm_campaign visit:device visit:browser visit:browser_version
38
+ 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
@@ -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.6"
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.6
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-02-25 00:00:00.000000000 Z
12
12
  dependencies: []
13
13
  description: A very humble wrapper for the new API by Plausible
14
14
  email: