Class: Hyrax::Analytics::Ga4::Base

Inherits:
Object
  • Object
show all
Defined in:
app/services/hyrax/analytics/ga4/base.rb

Direct Known Subclasses

Events, EventsDaily, Visits, VisitsDaily

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(start_date:, end_date:, dimensions: [], metrics: []) ⇒ Base

Returns a new instance of Base.



8
9
10
11
12
13
14
15
16
# File 'app/services/hyrax/analytics/ga4/base.rb', line 8

def initialize(start_date:,
               end_date:,
               dimensions: [],
               metrics: [])
  @start_date = start_date.to_date
  @end_date = end_date.to_date
  @dimensions = dimensions
  @metrics = metrics
end

Instance Attribute Details

#dimensionsObject (readonly)

Returns the value of attribute dimensions.



6
7
8
# File 'app/services/hyrax/analytics/ga4/base.rb', line 6

def dimensions
  @dimensions
end

#end_dateObject (readonly)

Returns the value of attribute end_date.



6
7
8
# File 'app/services/hyrax/analytics/ga4/base.rb', line 6

def end_date
  @end_date
end

#metricsObject (readonly)

Returns the value of attribute metrics.



6
7
8
# File 'app/services/hyrax/analytics/ga4/base.rb', line 6

def metrics
  @metrics
end

#start_dateObject (readonly)

Returns the value of attribute start_date.



6
7
8
# File 'app/services/hyrax/analytics/ga4/base.rb', line 6

def start_date
  @start_date
end

Instance Method Details

#add_filter(dimension:, values:) ⇒ Object



26
27
28
29
30
31
# File 'app/services/hyrax/analytics/ga4/base.rb', line 26

def add_filter(dimension:, values:)
  # reset any cached results
  @results = nil
  filters[dimension] ||= []
  filters[dimension] += values
end

#dimension_expressionsObject



56
57
58
59
60
61
62
63
64
65
# File 'app/services/hyrax/analytics/ga4/base.rb', line 56

def dimension_expressions
  filters.map do |dimension, values|
    {
      filter: {
        field_name: dimension,
        in_list_filter: { values: values.uniq }
      }
    }
  end
end

#dimension_filterObject



47
48
49
50
51
52
53
54
# File 'app/services/hyrax/analytics/ga4/base.rb', line 47

def dimension_filter
  return nil if filters.blank?
  {
    and_group: {
      expressions: dimension_expressions
    }
  }
end

#filtersObject



18
19
20
# File 'app/services/hyrax/analytics/ga4/base.rb', line 18

def filters
  @filters ||= {}
end

#filters=(value) ⇒ Object



22
23
24
# File 'app/services/hyrax/analytics/ga4/base.rb', line 22

def filters=(value)
  value
end

#reportObject



37
38
39
40
41
42
43
44
45
# File 'app/services/hyrax/analytics/ga4/base.rb', line 37

def report
  ::Google::Analytics::Data::V1beta::RunReportRequest.new(
    property: Hyrax::Analytics.property,
    metrics: metrics,
    date_ranges: [{ start_date: start_date.iso8601, end_date: end_date.iso8601 }],
    dimensions: dimensions,
    dimension_filter: dimension_filter
  )
end

#resultsObject



33
34
35
# File 'app/services/hyrax/analytics/ga4/base.rb', line 33

def results
  @results ||= Hyrax::Analytics.client.run_report(report).rows
end

#results_array(target_type = nil) ⇒ Object



67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
# File 'app/services/hyrax/analytics/ga4/base.rb', line 67

def results_array(target_type = nil)
  r = {}
  # prefill dates so that all dates at least have 0
  (start_date..end_date).each do |date|
    r[date] = 0
  end
  results.each do |result|
    date = unwrap_dimension(metric: result, dimension: 0)
    type = unwrap_dimension(metric: result, dimension: 1)
    next if date.nil? || type.nil?
    next if target_type && type != target_type
    date = date.to_date
    r[date] += unwrap_metric(result)
  end
  Hyrax::Analytics::Results.new(r.to_a)
end