Class: Hyrax::Analytics::Ga4::Base
- Inherits:
-
Object
- Object
- Hyrax::Analytics::Ga4::Base
show all
- Defined in:
- app/services/hyrax/analytics/ga4/base.rb
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
#dimensions ⇒ Object
Returns the value of attribute dimensions.
6
7
8
|
# File 'app/services/hyrax/analytics/ga4/base.rb', line 6
def dimensions
@dimensions
end
|
#end_date ⇒ Object
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
|
#metrics ⇒ Object
Returns the value of attribute metrics.
6
7
8
|
# File 'app/services/hyrax/analytics/ga4/base.rb', line 6
def metrics
@metrics
end
|
#start_date ⇒ Object
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:)
@results = nil
filters[dimension] ||= []
filters[dimension] += values
end
|
#dimension_expressions ⇒ Object
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_filter ⇒ Object
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
|
#filters ⇒ Object
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
|
#report ⇒ Object
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
|
#results ⇒ Object
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 = {}
(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
|