Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Plausible analytics date range #576

Merged
merged 2 commits into from
Mar 18, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 8 additions & 3 deletions app/models/plausible.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,17 @@
class Plausible
PLAUSIBLE_API_URL = 'https://plausible.io/api/v1'

def self.date_period
"2021-01-01,#{Time.zone.today.strftime('%Y-%m-%d')}"
end

# Fetches pageview counts from Plausible for a given document id.
def self.pageviews(document_id)
return 'X' if ENV['PLAUSIBLE_KEY'].nil?

c = PlausibleApi::Client.new(Rails.configuration.pdc_discovery.plausible_site_id, ENV['PLAUSIBLE_KEY'])
page = "/discovery/catalog/#{document_id}"
response = c.aggregate({ date: "2021-01-01,#{Time.zone.today.strftime('%Y-%m-%d')}", metrics: 'visitors,pageviews', filters: "event:page==#{page}" })
response = c.aggregate({ date: date_period, metrics: 'visitors,pageviews', filters: "event:page==#{page}" })
response["pageviews"]["value"]
rescue => e
Rails.logger.error "PLAUSIBLE ERROR: (Pageviews for document: #{document_id}) #{e.message}"
Expand All @@ -33,13 +37,14 @@ def self.downloads(document_id)

# Plausible API breakdown API: https://plausible.io/docs/stats-api#get-apiv1statsbreakdown
# Notice that the Plausible API uses "==" to filter: https://plausible.io/docs/stats-api#filtering
# Time periods: https://plausible.io/docs/stats-api#time-periods
site_id = Rails.configuration.pdc_discovery.plausible_site_id
property = "event:props:filename"
page = "/discovery/catalog/#{document_id}"
filters = "event:page==#{page}"
metrics = "visitors,pageviews"
period = "12mo"
url = "#{PLAUSIBLE_API_URL}/stats/breakdown?site_id=#{site_id}&property=#{property}&filters=#{filters}&metrics=#{metrics}&period=#{period}"
period = "custom"
url = "#{PLAUSIBLE_API_URL}/stats/breakdown?site_id=#{site_id}&property=#{property}&filters=#{filters}&metrics=#{metrics}&period=#{period}&date=#{date_period}"
authorization = "Bearer #{ENV['PLAUSIBLE_KEY']}"
response = HTTParty.get(url, headers: { 'Authorization' => authorization })
total_downloads = 0
Expand Down
3 changes: 2 additions & 1 deletion spec/models/plausible_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@
RSpec.describe Plausible do
before do
plausible = "https://plausible.io/api/v1"
url = "#{plausible}/stats/breakdown?filters=event:page==/discovery/catalog/88163&metrics=visitors,pageviews&property=event:props:filename&site_id=pdc-discovery-staging.princeton.edu&period=12mo"
date_period = "2021-01-01,#{Time.zone.today.strftime('%Y-%m-%d')}"
url = "#{plausible}/stats/breakdown?filters=event:page==/discovery/catalog/88163&metrics=visitors,pageviews&property=event:props:filename&site_id=pdc-discovery-staging.princeton.edu&period=custom&date=#{date_period}"
stub_request(:get, url)
.with(
headers: {
Expand Down