Skip to content

Commit

Permalink
fixed an issue with the GA4 Analytics migration
Browse files Browse the repository at this point in the history
  • Loading branch information
mdorf committed Aug 8, 2023
1 parent 7c6c25b commit d88b121
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 4 deletions.
8 changes: 6 additions & 2 deletions controllers/ontology_analytics_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,11 @@ class OntologyAnalyticsController < ApplicationController
expires 86400, :public
ont = Ontology.find(params["acronym"]).first
error 404, "No ontology exists with the acronym: #{params["acronym"]}" if ont.nil?
analytics = ont.analytics
year = year_param(params)
error 400, "The year you supplied is invalid. Valid years start with 2 and contain 4 digits." if params["year"] && !year
month = month_param(params)
error 400, "The month you supplied is invalid. Valid months are 1-12." if params["month"] && !month
analytics = ont.analytics(year, month)

if params["format"].to_s.downcase.eql?("csv")
tf = Tempfile.new("analytics-#{params['acronym']}")
Expand All @@ -39,7 +43,7 @@ class OntologyAnalyticsController < ApplicationController
years.each do |year|
months = analytics[params["acronym"]][year].keys.sort
months.each do |month|
next if now.year == year && now.month <= month || (year == 2013 && month < 10) # we don't have good data going back past Oct 2013
next if year && month && (now.year == year.to_i && now.month <= month.to_i || year.to_i == 2013 && month.to_i < 10) # we don't have good data going back past Oct 2013
visits = analytics[params["acronym"]][year][month]
month = DateTime.parse("#{year}/#{month}").strftime("%b %Y")
csv << [month, visits]
Expand Down
6 changes: 4 additions & 2 deletions helpers/application_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -261,9 +261,10 @@ def month_param(params=nil)
if params["month"]
month = params["month"].strip
if %r{(?<month>^(0[1-9]|[1-9]|1[0-2])$)}x === month
month.to_i
return month
end
end
nil
end

# validates year for starting with 1 or 2 and containing 4 digits
Expand All @@ -272,9 +273,10 @@ def year_param(params=nil)
if params["year"]
year = params["year"].strip
if %r{(?<year>^([1-2]\d{3})$)}x === year
year.to_i
return year
end
end
nil
end

##
Expand Down

0 comments on commit d88b121

Please sign in to comment.