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 +4 -4
- data/Gemfile.lock +1 -1
- data/README.md +4 -4
- data/lib/plausible_api/stats/aggregate.rb +5 -7
- data/lib/plausible_api/stats/base.rb +29 -34
- data/lib/plausible_api/stats/breakdown.rb +3 -10
- data/lib/plausible_api/stats/realtime/visitors.rb +0 -3
- data/lib/plausible_api/stats/timeseries.rb +3 -6
- data/lib/plausible_api/version.rb +1 -1
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 040cbafdc8f270ac3f2bfdcaed7501535f801e93ba1059e6b776be42258714e9
|
4
|
+
data.tar.gz: 9e5511a3d7bb752a1b0a89e0f4d3b405eea5a6ca56262cf684626d7cf444dafc
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 83077cd7edeb80de8388afbea3fd85d2ba2e8068345e10df04331c6356f7dc5a7c7096da9774bfe2fa6324d76fd68ec5c80cf4c39f1dd499a39dbb8d73ba24c7
|
7
|
+
data.tar.gz: fc95a48bf881567cce4f126c899206e18cdfbbc0b0f3ddeba6e66f37553a12f2a77a541472005eafd3c07ddc435d70886fc95d13b751b906056d616bd5604a42
|
data/Gemfile.lock
CHANGED
data/README.md
CHANGED
@@ -1,4 +1,4 @@
|
|
1
|
-
# Plausible API
|
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({
|
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({
|
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',
|
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
|
-
|
8
|
-
|
9
|
-
|
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
|
-
|
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
|
-
|
16
|
-
|
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
|
42
|
-
|
43
|
-
|
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
|
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
|
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
|
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
|
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
|
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
|
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
|
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
|
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
|
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
|
-
|
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
|
@@ -3,14 +3,11 @@
|
|
3
3
|
module PlausibleApi
|
4
4
|
module Stats
|
5
5
|
class Timeseries < Base
|
6
|
+
|
6
7
|
def initialize(options = {})
|
7
|
-
|
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
|
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.
|
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-
|
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:
|