-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathanalytics-reporter.rb
executable file
·321 lines (249 loc) · 8.99 KB
/
analytics-reporter.rb
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
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
#!/usr/bin/env ruby
#
# Author: [email protected]
require "google/analytics/admin/v1alpha"
require "google/analytics/data/v1beta/analytics_data"
require 'fileutils'
require 'google/apis/analytics_v3'
require 'googleauth'
require 'googleauth/stores/file_token_store'
require 'json'
require 'pp'
require 'thor'
require_relative './config'
require_relative './util'
require_relative './writer'
BASEURL = "https://analytics.google.com/analytics/web/"
ENV['GOOGLE_APPLICATION_CREDENTIALS'] = File.join(
Dir.home, '.analytics', 'analytics-ga4.json')
def sum_array(*arr)
# pp arr
return arr.transpose.map {|x| x.reduce(:+)}
end
def get_properties
# Create a client object. The client can be reused for multiple calls.
client = Google::Analytics::Admin::V1alpha::AnalyticsAdminService::Client.new
# Create a request. To set request fields, pass in keyword arguments.
request = Google::Analytics::Admin::V1alpha::ListAccountSummariesRequest.new
# Call the list_account_summaries method.
result = client.list_account_summaries request
properties = {}
# The returned object is of type Gapic::PagedEnumerable. You can iterate
# over elements, and API calls will be issued to fetch pages as needed.
result.each do |account|
# Each element is of type ::Google::Analytics::Admin::V1alpha::AccountSummary.
# p account
account.property_summaries.each do |property_summary|
# puts "#{account.display_name}: #{property_summary.display_name}"
name = property_summary.display_name.sub(/\s+-\s+GA4$/, "")
if name =~ /^Finding Aids/
name = name.sub(/\s+Hosted at New York University$/, "")
end
properties[account.display_name + ":" + name] = property_summary.property
end
end
return properties
end
def get_profiles(service)
profiles = {}
service.list_accounts.items.each do |account|
service.list_profiles(account.id, '~all').items.each do |profile|
name = profile.name.dup
# puts "#{account.name} #{name}"
if name.sub!(' (master view)', '')
profiles[account.name + ":" + name] = profile
end
end
end
return profiles
end
# https://stackoverflow.com/questions/72254647/can-ga4-api-fetch-the-data-from-requests-made-with-a-combination-of-minute-and-r
def get_results(property, start_date, end_date)
client = ::Google::Analytics::Data::V1beta::AnalyticsData::Client.new
request = Google::Analytics::Data::V1beta::RunReportRequest.new(
property: property,
date_ranges: [
{ start_date: start_date, end_date: end_date }
],
metrics: %w(sessions totalUsers screenPageViews).map { |m| { name: m } },
keep_empty_rows: true,
)
ret = client.run_report(request)
row = ret.rows[0]
return row.metric_values.map(&:value).map(&:to_i)
end
##
# Ensure valid credentials, either by restoring from the saved credentials
# files or intitiating an OAuth2 authorization. If authorization is required,
# the user's default browser will be launched to approve the request.
#
# @return [Google::Auth::UserRefreshCredentials] OAuth2 credentials
def authorize
oob_uri = 'urn:ietf:wg:oauth:2.0:oob'
client_secrets_path = File.join(Dir.home, '.analytics',
'client_secret.json')
credentials_path = File.join(Dir.home, '.analytics',
'analytics-reporter.yaml')
scope = Google::Apis::AnalyticsV3::AUTH_ANALYTICS_READONLY
FileUtils.mkdir_p(File.dirname(credentials_path))
client_id = Google::Auth::ClientId.from_file(client_secrets_path)
token_store = Google::Auth::Stores::FileTokenStore.new(
file: credentials_path)
authorizer = Google::Auth::UserAuthorizer.new(
client_id, scope, token_store)
user_id = 'default'
credentials = authorizer.get_credentials(user_id)
if credentials.nil?
url = authorizer.get_authorization_url(
base_url: oob_uri)
puts "Open the following URL in the browser and enter the " +
"resulting code after authorization"
puts url
code = gets
credentials = authorizer.get_and_store_credentials_from_code(
user_id: user_id, code: code, base_url: oob_uri)
end
credentials
end
def fmt_date(date)
date.strftime('%Y-%m-%d')
end
def fmt_date_qry(date)
date.strftime('%Y%m%d')
end
def calc_percent(r1, r2, col_num)
val1 = r1[col_num].to_f
val2 = r2[col_num].to_f
# puts "val1=#{val1}"
# puts "val2=#{val2}"
if !val1.zero?
((val2 - val1) / val1).to_s + '%'
else
''
end
end
def get_analytics(service, profiles, properties, config)
metrics = %w(ga:sessions ga:users ga:pageviews)
metrics_str = metrics.join(',')
all_csv_rows = []
all_csv_rows.push(Array.new)
prev_totals = Array.new(3, 0)
totals = Array.new(3, 0)
names = (profiles.keys() + properties.keys())
puts "Sites that don't have both v3 and v4 properties:"
names.select{|i| names.count(i) == 1}.sort.each do |n|
if profiles.key?(n)
puts "V3 #{n}"
else
puts "V4 #{n}"
end
end
names = names.sort.uniq
skip_list = config[:skip_list].to_h { |name| [name, 1] }
names.each do |name|
if skip_list.key?(name)
puts "Skipping #{name} ..."
next
end
account_name, site_name = name.split(":")
ga_url = "https://analytics.google.com"
prev_result_row = Array.new(3, 0)
result_row = Array.new(3, 0)
if profiles.key?(name)
profile = profiles[name]
# puts profile.inspect
query = "?_u.date00=#{fmt_date_qry(config[:start])}" +
"&_u.date01=#{fmt_date_qry(config[:end])}"
ga_url = BASEURL +
"?authuser=1#report/defaultid/a#{profile.account_id}" +
"w#{profile.internal_web_property_id}p#{profile.id}/" +
CGI.escape(query) + "/"
puts "Querying master view: #{name}"
prev_result = service.get_ga_data("ga:#{profile.id}",
fmt_date(config[:prev_start]),
fmt_date(config[:prev_end]),
metrics_str)
result = service.get_ga_data("ga:#{profile.id}",
fmt_date(config[:start]),
fmt_date(config[:end]),
metrics_str)
# puts "prev_result: ", prev_result.inspect
# puts "result: ", result.inspect
prev_result_rows = prev_result.rows || [["0", "0", "0"]]
result_rows = result.rows || [["0", "0", "0"]]
prev_result_row = sum_array(prev_result_row, prev_result_rows[0].map(&:to_i))
result_row = sum_array(result_row, result_rows[0].map(&:to_i))
end
if properties.key?(name)
property = properties[name]
prefix, prop_num = property.split("/")
puts "Querying GA4 property: #{name} #{property}"
query = "_u.date00=#{fmt_date_qry(config[:start])}" +
"&_u.date01=#{fmt_date_qry(config[:end])}"
# '&_u..comparisons=[{"name":"sessions"}]'
ga_url = BASEURL +
"#/p#{prop_num}/reports/reportinghub?params=" +
CGI.escape(query)
prev_result_row =
sum_array(
prev_result_row,
get_results(
property,
fmt_date(config[:prev_start]),
fmt_date(config[:prev_end])
)
)
result_row =
sum_array(
result_row,
get_results(
property,
fmt_date(config[:start]),
fmt_date(config[:end])
)
)
end
csv_row = []
csv_row.push(account_name)
csv_row.push([ga_url, site_name])
(0..2).each do |n|
csv_row.push(result_row[n])
csv_row.push(calc_percent(prev_result_row, result_row, n))
prev_totals[n] += prev_result_row[n].to_i
totals[n] += result_row[n].to_i
end
all_csv_rows.push(csv_row)
end
all_csv_rows[0].push('All', 'All')
(0..2).each do |n|
all_csv_rows[0].push(totals[n])
all_csv_rows[0].push(calc_percent(prev_totals, totals, n))
end
# pp all_csv_rows
return all_csv_rows
end
config = ReportConfig.get_config
# Initialize the API
ga_service = Google::Apis::AnalyticsV3::AnalyticsService.new
ga_service.client_options.application_name = 'Analytics Reporter'
ga_service.authorization = authorize
profiles = get_profiles(ga_service)
properties = get_properties
file_prefix = 'analytics_report'
Dir.mktmpdir(file_prefix) do |tmpdir|
writer = ReportWriter.new(config, tmpdir, file_prefix)
writer.add_row(['DLTS collections quarterly report - analytics'])
writer.add_row(['Year:', "FY#{config[:report_year]}"])
writer.add_row(['Quarter:', config[:report_qtr]])
writer.add_row(['Account', 'Property',
'# of sessions', 'Chg from prev qtr',
'# of users', 'Chg from prev qtr',
'# of pageviews', 'Chg from prev qtr'])
all_csv_rows = get_analytics(ga_service, profiles, properties, config)
all_csv_rows.sort_by { |x| x[2].to_i }.reverse.each do |val|
writer.add_row(val)
end
Thor.new.print_table(all_csv_rows)
writer.close
Util.mail_and_copy(config, writer.files, 'Google Analytics')
end