-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathextractor.py
396 lines (337 loc) · 16.6 KB
/
extractor.py
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
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
import json
import uuid
import typer
import validators
from googleapiclient.discovery import build
from google.oauth2 import service_account
import yaml
from datetime import datetime, timedelta
from pathlib import Path
from enum import Enum
from typing import Optional, NamedTuple
extractor = typer.Typer()
APP_NAME = "ga-extractor"
class SamplingLevel(str, Enum):
SAMPLING_UNSPECIFIED = "SAMPLING_UNSPECIFIED"
DEFAULT = "DEFAULT"
SMALL = "SMALL"
LARGE = "LARGE"
class OutputFormat(str, Enum):
JSON = "JSON"
CSV = "CSV"
UMAMI = "UMAMI"
@staticmethod
def file_suffix(f):
format_mapping = {
OutputFormat.JSON: "json",
OutputFormat.CSV: "csv",
OutputFormat.UMAMI: "sql",
}
return format_mapping[f]
class Preset(str, Enum):
NONE = "NONE"
FULL = "FULL"
BASIC = "BASIC"
@staticmethod
def metrics(p):
metrics_mapping = {
Preset.NONE: [],
Preset.FULL: ["ga:pageviews", "ga:sessions"],
Preset.BASIC: ["ga:pageviews"],
}
return metrics_mapping[p]
@staticmethod
def dims(p):
dims_mapping = {
Preset.NONE: [],
Preset.FULL: ["ga:pagePath", "ga:browser", "ga:operatingSystem", "ga:deviceCategory", "ga:browserSize",
"ga:language", "ga:country", "ga:fullReferrer"],
Preset.BASIC: ["ga:pagePath"],
}
return dims_mapping[p]
@extractor.command()
def setup(metrics: str = typer.Option(None, "--metrics"),
dimensions: str = typer.Option(None, "--dimensions"),
sa_key_path: str = typer.Option(..., "--sa-key-path"),
table_id: int = typer.Option(..., "--table-id"),
sampling_level: SamplingLevel = typer.Option(SamplingLevel.DEFAULT, "--sampling-level"),
preset: Preset = typer.Option(Preset.NONE, "--preset",
help="Use metrics and dimension preset (can't be specified with '--dimensions' or '--metrics')"),
start_date: datetime = typer.Option(..., formats=["%Y-%m-%d"]),
end_date: datetime = typer.Option(..., formats=["%Y-%m-%d"]),
dry_run: bool = typer.Option(False, "--dry-run", help="Outputs config to terminal instead of config file")):
"""
Generate configuration file from arguments
"""
if (
(preset is Preset.NONE and dimensions is None and metrics is None) or
(dimensions is None and metrics is not None) or (dimensions is not None and metrics is None)
):
typer.echo("Dimensions and Metrics or Preset must be specified.")
typer.Exit(2)
config = {
"serviceAccountKeyPath": sa_key_path,
"table": table_id,
"metrics": "" if not metrics else metrics.split(","),
"dimensions": "" if not dimensions else dimensions.split(","),
"samplingLevel": sampling_level.value,
"startDate": f"{start_date:%Y-%m-%d}",
"endDate": f"{end_date:%Y-%m-%d}",
}
if preset is not Preset.NONE:
config["metrics"] = Preset.metrics(preset)
config["dimensions"] = Preset.dims(preset)
output = yaml.dump(config)
if dry_run:
typer.echo(output)
else:
app_dir = typer.get_app_dir(APP_NAME)
config_path: Path = Path(app_dir) / "config.yaml"
config_path.parent.mkdir(parents=True, exist_ok=True)
with open(config_path, 'w') as outfile:
outfile.write(output)
@extractor.command()
def auth():
"""
Test authentication using generated configuration
"""
app_dir = typer.get_app_dir(APP_NAME)
config_path: Path = Path(app_dir) / "config.yaml"
if not config_path.is_file():
typer.echo("Config file doesn't exist yet. Please run 'setup' command first.")
return
try:
with config_path.open() as config:
credentials = service_account.Credentials.from_service_account_file(yaml.safe_load(config)["serviceAccountKeyPath"])
scoped_credentials = credentials.with_scopes(['openid'])
with build('oauth2', 'v2', credentials=scoped_credentials) as service:
user_info = service.userinfo().v2().me().get().execute()
typer.echo(f"Successfully authenticated with user: {user_info['id']}")
except BaseException as e:
typer.echo(f"Authenticated failed with error: '{e}'")
@extractor.command()
def extract(report: Optional[Path] = typer.Option("report.json", dir_okay=True)):
"""
Extracts data based on the config
"""
# https://developers.google.com/analytics/devguides/reporting/core/v4/rest/v4/reports/batchGet
app_dir = typer.get_app_dir(APP_NAME)
config_path: Path = Path(app_dir) / "config.yaml"
output_path: Path = Path(app_dir) / report
if not config_path.is_file():
typer.echo("Config file doesn't exist yet. Please run 'setup' command first.")
typer.Exit(2)
with config_path.open() as file:
config = yaml.safe_load(file)
credentials = service_account.Credentials.from_service_account_file(config["serviceAccountKeyPath"])
scoped_credentials = credentials.with_scopes(['https://www.googleapis.com/auth/analytics.readonly'])
dimensions = [{"name": d} for d in config['dimensions']]
metrics = [{"expression": m} for m in config['metrics']]
body = {"reportRequests": [
{
# "pageSize": 2, # Use this to test paging
"viewId": f"{config['table']}",
"dateRanges": [
{
"startDate": f"{config['startDate']}",
"endDate": f"{config['endDate']}"
}],
"dimensions": [dimensions],
"metrics": [metrics],
"samplingLevel": config['samplingLevel']
}]}
rows = []
with build('analyticsreporting', 'v4', credentials=scoped_credentials) as service:
response = service.reports().batchGet(body=body).execute()
if not "rows" in response.values():
raise Exception("There were no rows in the response.")
rows.extend(response["reports"][0]["data"]["rows"])
while "nextPageToken" in response["reports"][0]: # Paging...
body["reportRequests"][0]["pageToken"] = response["reports"][0]["nextPageToken"]
response = service.reports().batchGet(body=body).execute()
rows.extend(response["reports"][0]["data"]["rows"])
output_path.write_text(json.dumps(rows))
typer.echo(f"Report written to {output_path.absolute()}")
@extractor.command()
def migrate(output_format: OutputFormat = typer.Option(OutputFormat.JSON, "--format"),
umami_website_id: int = typer.Argument(1, help="Website ID, used if migrating data for Umami Analytics"),
umami_hostname: str = typer.Argument("localhost", help="Hostname website being migrated, used if migrating data for Umami Analytics")):
"""
Export necessary data and transform it to format for target environment (Umami, ...)
Old sessions won't be preserved because session can span multiple days, but extraction is done on daily level.
Bounce rate and session duration won't be accurate.
Views and visitors on day-level granularity will be accurate.
Exact visit time is (hour and minute) is not preserved.
"""
app_dir = typer.get_app_dir(APP_NAME)
config_path: Path = Path(app_dir) / "config.yaml"
output_path: Path = Path(app_dir) / f"{uuid.uuid4()}_extract.{OutputFormat.file_suffix(output_format)}"
if not config_path.is_file():
typer.echo("Config file doesn't exist yet. Please run 'setup' command first.")
typer.Exit(2)
with config_path.open() as file:
config = yaml.safe_load(file)
credentials = service_account.Credentials.from_service_account_file(config["serviceAccountKeyPath"])
scoped_credentials = credentials.with_scopes(['https://www.googleapis.com/auth/analytics.readonly'])
date_ranges = __migrate_date_ranges(config['startDate'], config['endDate'])
rows = __migrate_extract(scoped_credentials, config['table'], date_ranges)
if output_format == OutputFormat.UMAMI:
data = __migrate_transform_umami(rows, umami_website_id, umami_hostname)
with output_path.open(mode="w") as f:
for insert in data:
f.write(f"{insert}\n")
elif output_format == OutputFormat.JSON:
output_path.write_text(json.dumps(rows))
elif output_format == OutputFormat.CSV:
data = __migrate_transform_csv(rows)
with output_path.open(mode="w") as f:
for row in data:
f.write(f"{row}\n")
typer.echo(f"Report written to {output_path.absolute()}")
def __migrate_date_ranges(start_date, end_date):
start_date = datetime.strptime(start_date, '%Y-%m-%d')
end_date = datetime.strptime(end_date, '%Y-%m-%d')
date_ranges = [{"startDate": f"{start_date + timedelta(days=d):%Y-%m-%d}",
"endDate": f"{start_date + timedelta(days=d):%Y-%m-%d}"} for d in
range(((end_date.date() - start_date.date()).days + 1))]
return date_ranges
def __migrate_extract(credentials, table_id, date_ranges):
dimensions = ["ga:pagePath", "ga:browser", "ga:operatingSystem", "ga:deviceCategory", "ga:browserSize", "ga:language", "ga:country", "ga:fullReferrer"]
metrics = ["ga:pageviews", "ga:sessions"]
body = {"reportRequests": [
{
"viewId": f"{table_id}",
"dimensions": [{"name": d} for d in dimensions],
"metrics": [{"expression": m} for m in metrics]
}]}
rows = {}
for r in date_ranges:
with build('analyticsreporting', 'v4', credentials=credentials) as service:
body["reportRequests"][0]["dateRanges"] = [r]
response = service.reports().batchGet(body=body).execute()
num_rows = response["reports"][0]["data"]["totals"][0]["values"]
if len(list(filter(lambda x: x != '0', num_rows))):
rows[r["startDate"]] = response["reports"][0]["data"]["rows"]
return rows
class Session(NamedTuple):
session_id: int
session_uuid: uuid.UUID
website_id: int
created_at: str
hostname: str
browser: str
os: str
device: str
screen: str
language: str
def sql(self):
session_insert = (
f"INSERT INTO public.session (session_id, session_uuid, website_id, created_at, hostname, browser, os, device, screen, language, country) "
f"VALUES ({self.session_id}, '{self.session_uuid}', {self.website_id}, '{self.created_at}', '{self.hostname}', '{self.browser[:20]}', '{self.os}', '{self.device}', '{self.screen}', '{self.language}', NULL);"
)
return session_insert
class PageView(NamedTuple):
id: int
website_id: int
session_id: int
created_at: str
url: str
referral_path: str
def sql(self):
return f"INSERT INTO public.pageview (view_id, website_id, session_id, created_at, url, referrer) VALUES ({self.id}, {self.website_id}, {self.session_id}, '{self.created_at}', '{self.url}', '{self.referral_path}');"
def __migrate_transform_umami(rows, website_id, hostname):
# Sample row:
# {'dimensions': ['/', 'Chrome', 'Windows', 'desktop', '1350x610', 'en-us', 'India', '(direct)'], 'metrics': [{'values': ['1', '1']}]}
#
# Notes: there can be 0 sessions in the record; there's always more or equal number of views
# - treat zero sessions as one
# - if sessions is non-zero and page views are > 1, then divide, e.g.:
# - 5, 5 - 5 sessions, 1 view each
# - 4, 2 - 2 sessions, 2 views each
# - 5, 3 - 3 sessions, 2x1 view, 1x3 views
page_view_id = 1
session_id = 1
sql_inserts = []
for day, value in rows.items():
for row in value:
timestamp = f"{day} 00:00:00.000+00" # PostgreSQL-style "timestamp with timezone"
referrer = f"https://{row['dimensions'][7]}"
if not validators.url(referrer):
referrer = ""
elif referrer == "google":
referrer = "https://google.com"
language = row["dimensions"][5][:2]
page_views, sessions = map(int, row["metrics"][0]["values"])
sessions = max(sessions, 1) # in case it's zero
if page_views == sessions: # One page view for each session
for i in range(sessions):
s = Session(session_uuid=uuid.uuid4(), session_id=session_id, website_id=website_id, created_at=timestamp, hostname=hostname,
browser=row["dimensions"][1], os=row["dimensions"][2], device=row["dimensions"][3], screen=row["dimensions"][4],
language=language)
p = PageView(id=page_view_id, website_id=website_id, session_id=session_id, created_at=timestamp, url=row["dimensions"][0], referral_path=referrer)
sql_inserts.extend([s.sql(), p.sql()])
session_id += 1
page_view_id += 1
elif page_views % sessions == 0: # Split equally
for i in range(sessions):
s = Session(session_uuid=uuid.uuid4(), session_id=session_id, website_id=website_id, created_at=timestamp, hostname=hostname,
browser=row["dimensions"][1], os=row["dimensions"][2], device=row["dimensions"][3], screen=row["dimensions"][4],
language=language)
sql_inserts.append(s.sql())
for j in range(page_views // sessions):
p = PageView(id=page_view_id, website_id=website_id, session_id=session_id, created_at=timestamp, url=row["dimensions"][0], referral_path=referrer)
sql_inserts.append(p.sql())
page_view_id += 1
session_id += 1
else: # One page view for each, rest for the last session
for i in range(sessions):
s = Session(session_uuid=uuid.uuid4(), session_id=session_id, website_id=website_id, created_at=timestamp, hostname=hostname,
browser=row["dimensions"][1], os=row["dimensions"][2], device=row["dimensions"][3], screen=row["dimensions"][4],
language=language)
p = PageView(id=page_view_id, website_id=website_id, session_id=session_id, created_at=timestamp, url=row["dimensions"][0], referral_path=referrer)
sql_inserts.extend([s.sql(), p.sql()])
session_id += 1
page_view_id += 1
last_session_id = session_id - 1
for i in range(page_views - sessions):
p = PageView(id=page_view_id, website_id=website_id, session_id=last_session_id, created_at=timestamp, url=row["dimensions"][0], referral_path=referrer)
page_view_id += 1
sql_inserts.append(p.sql())
sql_inserts.extend([
f"SELECT pg_catalog.setval('public.pageview_view_id_seq', {page_view_id}, true);",
f"SELECT pg_catalog.setval('public.session_session_id_seq', {session_id}, true);"
])
return sql_inserts
class CSVRow(NamedTuple):
path: str
browser: str
os: str
device: str
screen: str
language: str
country: str
referral_path: str
count: str
date: datetime.date
@staticmethod
def header():
return f"path,browser,os,device,screen,language,country,referral_path,count,date"
def csv(self):
return f"{self.path},{self.browser},{self.os},{self.device},{self.screen},{self.language},{self.country},{self.referral_path},{self.count},{self.date}"
def __migrate_transform_csv(rows):
csv_rows = [CSVRow.header()]
for day, value in rows.items():
for row in value:
page_views, _ = map(int, row["metrics"][0]["values"])
row = CSVRow(path=row["dimensions"][0],
browser=row["dimensions"][1],
os=row["dimensions"][2],
device=row["dimensions"][3],
screen=row["dimensions"][4],
language=row["dimensions"][5],
country=row["dimensions"][6],
referral_path=row["dimensions"][7],
count=page_views,
date=day)
csv_rows.append(row.csv())
return csv_rows