-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathGAReport.py
executable file
·107 lines (79 loc) · 3.15 KB
/
GAReport.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
#!/usr/bin/env python3
# A class to set up Google Analytics API v4 queries and create Pandas dataframes
import pandas as pd
# import pygsheets
from apiclient.discovery import build
from oauth2client.service_account import ServiceAccountCredentials
from DimensionsAndMetrics import DimensionsAndMetrics
KEY_FILE_LOCATION = 'PutAPIKeyFileHere.json'
SCOPES = ['https://www.googleapis.com/auth/analytics.readonly']
class GAReport():
"""docstring for GAReport"""
def __init__(self, startdate, enddate, viewID, dimensions, metrics, filters):
super(GAReport, self).__init__()
self.VIEW_ID = viewID
self.startdate = startdate
self.enddate = enddate
# For the full list of dimensions & metrics, check https://developers.google.com/analytics/devguides/reporting/core/dimsmets
columns = DimensionsAndMetrics()
self.columnids = columns.ids
self.columnnames = columns.names
self.DIMENSIONS = self.setColumnIDs(dimensions)
self.METRICS = self.setColumnIDs(metrics)
self.FILTERS = filters
self.analytics = self.initialize_analyticsreporting()
response = self.get_report(self.analytics)
self.df = self.convert_to_dataframe(response)
self.setColumnNames()
def initialize_analyticsreporting(self):
credentials = ServiceAccountCredentials.from_json_keyfile_name(
KEY_FILE_LOCATION, SCOPES)
# Build the service object.
analytics = build('analyticsreporting', 'v4', credentials=credentials)
return analytics
def get_report(self, analytics):
return analytics.reports().batchGet(
body={
'reportRequests': [
{
'viewId': self.VIEW_ID,
'dateRanges': [{'startDate': self.startdate, 'endDate': self.enddate}],
'metrics': [{'expression':i} for i in self.METRICS],
'dimensions': [{'name':j} for j in self.DIMENSIONS],
"filtersExpression":self.FILTERS,
}]
}
).execute()
def convert_to_dataframe(self, response):
for report in response.get('reports', []):
columnHeader = report.get('columnHeader', {})
dimensionHeaders = columnHeader.get('dimensions', [])
metricHeaders = [i.get('name',{}) for i in columnHeader.get('metricHeader', {}).get('metricHeaderEntries', [])]
finalRows = []
for row in report.get('data', {}).get('rows', []):
dimensions = row.get('dimensions', [])
metrics = row.get('metrics', [])[0].get('values', {})
rowObject = {}
for header, dimension in zip(dimensionHeaders, dimensions):
rowObject[header] = dimension
for metricHeader, metric in zip(metricHeaders, metrics):
rowObject[metricHeader] = metric
finalRows.append(rowObject)
dataFrameFormat = pd.DataFrame(finalRows)
return dataFrameFormat
def setColumnNames(self):
"""docstring for setColumnNames"""
keys = self.df.keys()
for key in keys:
self.df.rename(columns={key : self.columnids[key]}, inplace=True)
def setColumnIDs(self, keynamess):
"""docstring for setColumnIDs"""
keyids = []
for key in keynamess:
keyids.append(self.columnnames[key])
return keyids
def export_to_sheets(self):
gc = pygsheets.authorize(service_file='client_secrets.json')
sht = gc.open_by_key(self.SHEET_ID)
wks = sht.worksheet_by_title('Sheet1')
wks.set_dataframe(self.df,'A1')