-
Notifications
You must be signed in to change notification settings - Fork 67
Expand file tree
/
Copy pathgoogle_analytics.rb
More file actions
151 lines (132 loc) · 5.72 KB
/
Copy pathgoogle_analytics.rb
File metadata and controls
151 lines (132 loc) · 5.72 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
require 'google/apis/analyticsdata_v1beta'
module Plugins
class GoogleAnalytics < Base
def locals
{ histogram:, metrics: }
end
class << self
def redirect_url
client = Signet::OAuth2::Client.new(client_options)
client.authorization_uri.to_s
end
def fetch_access_token(code)
client = Signet::OAuth2::Client.new(client_options)
client.code = code
client.fetch_access_token!
end
def client_options
{
client_id: Rails.application.credentials.plugins[:google][:client_id],
client_secret: Rails.application.credentials.plugins[:google][:client_secret],
authorization_uri: 'https://accounts.google.com/o/oauth2/auth', # may require change to: '/oauth2/v2/auth'
token_credential_uri: 'https://accounts.google.com/o/oauth2/token', # may require change to: '/oauth2/v2/token'
access_type: 'offline',
scope: [Google::Apis::AnalyticsdataV1beta::AUTH_ANALYTICS_READONLY],
redirect_uri: "#{Rails.application.credentials.base_url}/plugin_settings/google_analytics/redirect",
additional_parameters: {
prompt: 'consent select_account'
}
}
end
end
# Helpful hidden gem https://ga-dev-tools.google/query-explorer/
def metrics
retry_count = 0
begin
date_range = Google::Apis::AnalyticsdataV1beta::DateRange.new(start_date: timestamp_from, end_date: timestamp_to)
pageviews = Google::Apis::AnalyticsdataV1beta::Metric.new(name: 'screenPageViews')
sessions = Google::Apis::AnalyticsdataV1beta::Metric.new(name: 'sessions')
visitors = Google::Apis::AnalyticsdataV1beta::Metric.new(name: 'activeUsers')
mins_on_page = Google::Apis::AnalyticsdataV1beta::Metric.new(name: 'averageSessionDuration')
report = Google::Apis::AnalyticsdataV1beta::RunReportRequest.new(
metrics: [pageviews, sessions, visitors, mins_on_page],
date_ranges: [date_range]
)
service.authorization = client
response = service.run_property_report(property_id, report)
return { pageviews: 0, visitors: 0, sessions: 0, mins_on_page: 0 } if response.rows.nil?
{
pageviews: response.rows[0].metric_values[0].value.to_i,
sessions: response.rows[0].metric_values[1].value.to_i,
visitors: response.rows[0].metric_values[2].value.to_i,
mins_on_page: (response.rows[0].metric_values[3].value.to_f / 60).round(2)
}
rescue Google::Apis::AuthorizationError
refresh!
retry_count += 1
if retry_count <= 1 # Retry just once, otherwise retry next day.
sleep 2
retry
else
plugin_settings.refresh_in_24hr
{}
end
rescue Signet::AuthorizationError, Google::Apis::ClientError => e
handle_erroring_state(e.message)
{}
end
end
def histogram
retry_count = 0
begin
date_range = Google::Apis::AnalyticsdataV1beta::DateRange.new(start_date: timestamp_from, end_date: timestamp_to)
metric = Google::Apis::AnalyticsdataV1beta::Metric.new(name: 'screenPageViews')
dimension = Google::Apis::AnalyticsdataV1beta::Dimension.new(name: 'date')
report = Google::Apis::AnalyticsdataV1beta::RunReportRequest.new(dimensions: [dimension],
metrics: [metric],
date_ranges: [date_range],
filters_expression: {})
service.authorization = client
response = service.run_property_report(property_id, report)
return [] if response.rows.nil?
response.rows
.map { |m| { date: Date.parse(m.dimension_values[0].value), pageviews: m.metric_values[0].value } }
.sort! { |a, b| a[:date] <=> b[:date] }
.each { |m| m[:date] = m[:date].strftime('%Y-%m-%d') }
rescue Google::Apis::AuthorizationError
refresh!
retry_count += 1
if retry_count <= 1 # Retry just once, otherwise retry next day.
sleep 2
retry
else
handle_erroring_state('Google::Apis::AuthorizationError')
{}
end
rescue Signet::AuthorizationError, Google::Apis::ClientError => e
handle_erroring_state(e.message)
{}
end
end
def service
@service ||= Google::Apis::AnalyticsdataV1beta::AnalyticsDataService.new
end
def client
credentials = settings['google_analytics']
oauth_client = Signet::OAuth2::Client.new(Plugins::GoogleAnalytics.client_options)
oauth_client.update!(credentials)
oauth_client
end
def refresh!
response = client.refresh!
credentials = plugin_settings.encrypted_settings
credentials['google_analytics']['access_token'] = response['access_token']
plugin_settings.update(encrypted_settings: credentials)
self.settings = plugin_settings.settings.merge(plugin_settings.encrypted_settings)
rescue Signet::AuthorizationError
handle_erroring_state('Signet::AuthorizationError')
end
# user suggestion to prevent the graph always 'dropping off' on current day
# leveraging case() to ensure no funny business / non permitted string injection
def timestamp_to
case settings["lookback_until"]
when '', nil
Date.today.to_s
when 'yesterday', 'today'
Date.send(settings["lookback_until"]).to_s
end
end
def timestamp_from = lookback_period.days.ago.beginning_of_day.strftime('%Y-%m-%d')
def property_id = "properties/#{settings['property_id']}"
end
end