Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save unavailabl3/e3c096fcb0bcfff1f461a9f76d2072f3 to your computer and use it in GitHub Desktop.
Save unavailabl3/e3c096fcb0bcfff1f461a9f76d2072f3 to your computer and use it in GitHub Desktop.
# Add to Gemfile
gem 'google-analytics-data', require: 'google/analytics/data'
-------------------------------------------------------------
# /path/to/google_analytics/report.rb
module GoogleAnalytics
class Report
DEFAULT_PROPERTY_ID = 'default-property-id' # store in Rails secrets or ENV variable
attr_reader :metrics
attr_reader :property_id
attr_reader :client
def initialize(metrics: nil, property_id: nil, client: nil)
@metrics = metrics
@property_id = property_id || DEFAULT_PROPERTY_ID
@client = client || analytics_data_client
end
def get(page_path:, start_date:, end_date:)
request = GoogleAnalytics::ReportRequest.new(
page_path: page_path,
start_date: start_date,
end_date: end_date,
property_id: property_id
).generate
client.run_report(request)
end
private
def analytics_data_client
Google::Analytics::Data.analytics_data do |config|
config.credentials = 'path/to/credentials.json' # store in Rails secrets or ENV variable
end
end
end
end
# /path/to/google_analytics/report_request.rb
require 'google/analytics/data/v1beta'
module GoogleAnalytics
class ReportRequest
DEFAULT_METRICS = ['screenPageViews'].freeze
BEGINS_WITH_MATCH_TYPE = Google::Analytics::Data::V1beta::Filter::StringFilter::MatchType::BEGINS_WITH
attr_reader :property_id
attr_reader :page_path
attr_reader :start_date
attr_reader :end_date
attr_reader :metrics
def initialize(page_path:, start_date:, end_date:, property_id:, metrics: nil)
@page_path = page_path
@start_date = start_date
@end_date = end_date
@property_id = property_id
@metrics = metrics || DEFAULT_METRICS
end
def generate
Google::Analytics::Data::V1beta::RunReportRequest.new(
property: "properties/#{property_id}",
dimensions: [{ name: 'pagePath' }],
dimension_filter: dimension_filter(page_path),
metrics: report_metrics,
date_ranges: [{ start_date: start_date.to_s, end_date: end_date.to_s }]
)
end
private
def report_metrics
metrics.map { |metric| { name: metric } }
end
def dimension_filter(page_path)
{
filter: {
field_name: 'pagePath',
string_filter: {
match_type: BEGINS_WITH_MATCH_TYPE,
value: page_path,
case_sensitive: false
}
}
}
end
end
end
def extract_page_views(report)
report.rows.each_with_object({}) do |row, memo|
page_path = row.dimension_values.first.value
memo[page_path] = row.metric_values.first.value.to_i
end
end
# USAGE
report_end_date = Time.zone.today
report_start_date = 7.days.before(report_end_date)
report = GoogleAnalytics::Report.new.get(
page_path: '/articles/',
start_date: report_start_date,
end_date: report_end_date
)
extract_page_views(report)
# => {
# '/articles/first-article' => 135,
# '/articles/second-article' => 20,
# ...
# }
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment