Class: Spotlight::Analytics::Ga
- Inherits:
-
Object
- Object
- Spotlight::Analytics::Ga
- Defined in:
- app/models/spotlight/analytics/ga.rb
Overview
Google Analytics data provider for the Exhibit dashboard
Instance Method Summary collapse
- #client ⇒ Object
- #dimension_filter(path) ⇒ Object
- #dimension_headers ⇒ Object
- #enabled? ⇒ Boolean
- #exhibit_data(path, dates) ⇒ Object
-
#metric_headers ⇒ Object
rubocop:enable Metrics/AbcSize.
- #metric_parsing(report_data) ⇒ Object
- #page_data(path, dates) ⇒ Object
- #page_params(path, dates) ⇒ Object
- #params(path, dates) ⇒ Object
- #parse_data(value) ⇒ Object
- #report(params) ⇒ Object
-
#rows ⇒ Object
rubocop:disable Metrics/AbcSize.
- #search_params(path, dates) ⇒ Object
- #totals ⇒ Object
Instance Method Details
#client ⇒ Object
15 16 17 18 19 20 21 22 |
# File 'app/models/spotlight/analytics/ga.rb', line 15 def client Google::Analytics::Data.analytics_data do |config| config.credentials = Spotlight::Engine.config.ga_json_key_path end rescue StandardError => e Rails.logger.error(e) nil end |
#dimension_filter(path) ⇒ Object
35 36 37 38 39 40 41 42 43 44 45 |
# File 'app/models/spotlight/analytics/ga.rb', line 35 def dimension_filter(path) Google::Analytics::Data::V1beta::FilterExpression.new( filter: Google::Analytics::Data::V1beta::Filter.new( field_name: 'pagePath', string_filter: Google::Analytics::Data::V1beta::Filter::StringFilter.new( match_type: :PARTIAL_REGEXP, value: "^#{path}(/.*)?$" ) ) ) end |
#dimension_headers ⇒ Object
109 110 111 |
# File 'app/models/spotlight/analytics/ga.rb', line 109 def dimension_headers @report_data.dimension_headers.map(&:name) end |
#enabled? ⇒ Boolean
11 12 13 |
# File 'app/models/spotlight/analytics/ga.rb', line 11 def enabled? Spotlight::Engine.config.ga_json_key_path && client end |
#exhibit_data(path, dates) ⇒ Object
83 84 85 |
# File 'app/models/spotlight/analytics/ga.rb', line 83 def exhibit_data(path, dates) metric_parsing(report(search_params(path, dates))) end |
#metric_headers ⇒ Object
rubocop:enable Metrics/AbcSize
105 106 107 |
# File 'app/models/spotlight/analytics/ga.rb', line 105 def metric_headers @report_data.metric_headers.map(&:name) end |
#metric_parsing(report_data) ⇒ Object
113 114 115 116 117 118 119 |
# File 'app/models/spotlight/analytics/ga.rb', line 113 def metric_parsing(report_data) return OpenStruct.new({ totals: [], rows: [] }) unless report_data.rows.any? @report_data = report_data OpenStruct.new({ rows: rows, totals: totals }) end |
#page_data(path, dates) ⇒ Object
69 70 71 |
# File 'app/models/spotlight/analytics/ga.rb', line 69 def page_data(path, dates) metric_parsing(report(page_params(path, dates))) end |
#page_params(path, dates) ⇒ Object
55 56 57 58 59 60 61 62 |
# File 'app/models/spotlight/analytics/ga.rb', line 55 def page_params(path, dates) params(path, dates).merge({ dimensions: [{ name: 'pagePath' }, { name: 'pageTitle' }], order_bys: [{ metric: { metric_name: 'screenPageViews' }, desc: true }], metrics: [{ name: 'totalUsers' }, { name: 'activeUsers' }, { name: 'screenPageViews' }] }).merge(Spotlight::Engine.config.) end |
#params(path, dates) ⇒ Object
24 25 26 27 28 29 30 31 32 33 |
# File 'app/models/spotlight/analytics/ga.rb', line 24 def params(path, dates) { date_ranges: [{ start_date: dates['start_date'], end_date: dates['end_date'] }], metric_aggregations: [ ::Google::Analytics::Data::V1beta::MetricAggregation::TOTAL ], property: "properties/#{ga_property_id}", dimension_filter: dimension_filter(path) } end |
#parse_data(value) ⇒ Object
73 74 75 76 77 78 79 80 81 |
# File 'app/models/spotlight/analytics/ga.rb', line 73 def parse_data(value) if value.to_i.to_s == value value.to_i.to_fs(:delimited) elsif !!(value =~ /\A[-+]?\d*\.?\d+\z/) value.to_f else value end end |
#report(params) ⇒ Object
64 65 66 67 |
# File 'app/models/spotlight/analytics/ga.rb', line 64 def report(params) request = ::Google::Analytics::Data::V1beta::RunReportRequest.new(params) client.run_report request end |
#rows ⇒ Object
rubocop:disable Metrics/AbcSize
94 95 96 97 98 99 100 101 102 |
# File 'app/models/spotlight/analytics/ga.rb', line 94 def rows @report_data.rows.map do |row| OpenStruct.new(row.dimension_values.each_with_index.with_object({}) do |(dv, index), result| result[dimension_headers[index]] = parse_data(dv.value) end.merge(row.metric_values.each_with_index.with_object({}) do |(mv, index), result| result[metric_headers[index]] = parse_data(mv.value) end)) end end |
#search_params(path, dates) ⇒ Object
47 48 49 50 51 52 53 |
# File 'app/models/spotlight/analytics/ga.rb', line 47 def search_params(path, dates) params(path, dates).merge({ dimensions: [{ name: 'searchTerm' }], metrics: [{ name: 'eventCount' }, { name: 'sessions' }, { name: 'screenPageViewsPerSession' }, { name: 'engagementRate' }], order_bys: [{ metric: { metric_name: 'eventCount' }, desc: true }] }).merge(Spotlight::Engine.config.) end |
#totals ⇒ Object
87 88 89 90 91 |
# File 'app/models/spotlight/analytics/ga.rb', line 87 def totals OpenStruct.new(@report_data.totals[0].metric_values.each_with_index.with_object({}) do |(mv, index), result| result[metric_headers[index]] = parse_data(mv.value) end) end |