-
Notifications
You must be signed in to change notification settings - Fork 0
/
query_google_analytics.py
107 lines (77 loc) · 3.44 KB
/
query_google_analytics.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
import json
import sys
from apiclient.discovery import build
from oauth2client.service_account import ServiceAccountCredentials
SCOPES = ['https://www.googleapis.com/auth/analytics.readonly']
KEY_FILE_LOCATION = '/users/ferper/Private/dotted-embassy-271317-3adc0dd0d7db.json'
VIEW_ID = '187453103'
START_DATE = '30daysAgo'
if len(sys.argv) > 1:
START_DATE = sys.argv[1]
def initialize_analyticsreporting():
"""Initializes an Analytics Reporting API V4 service object.
Returns:
An authorized Analytics Reporting API V4 service object.
"""
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(analytics):
"""Queries the Analytics Reporting API V4.
Args:
analytics: An authorized Analytics Reporting API V4 service object.
Returns:
The Analytics Reporting API V4 response.
"""
return analytics.reports().batchGet(
body={
'reportRequests': [
{
'viewId': VIEW_ID,
'dateRanges': [{'startDate': START_DATE, 'endDate': 'today'}],
'metrics': [{'expression': 'ga:users'}, {'expression': 'ga:sessions'}],
'dimensions': [{'name': 'ga:country'}]
}]
}
).execute()
def print_response(response):
"""Parses and prints the Analytics Reporting API V4 response.
Args:
response: An Analytics Reporting API V4 response.
"""
json_data = {}
for report in response.get('reports', []):
column_header = report.get('columnHeader', {})
dimension_headers = column_header.get('dimensions', [])
metric_headers = column_header.get('metricHeader', {}).get('metricHeaderEntries', [])
for metricHeader, total in zip(metric_headers, report.get('data', {}).get('totals', [])[0].get('values', [])):
if metricHeader.get('name') == 'ga:users':
json_data['user_count'] = total
elif metricHeader.get('name') == 'ga:sessions':
json_data['session_count'] = total
json_data['country_count'] = len(report.get('data', {}).get('rows', []))
json_data['users_by_country'] = {}
json_data['sessions_by_country'] = {}
for row in report.get('data', {}).get('rows', []):
json_country = ""
dimensions = row.get('dimensions', [])
date_range_values = row.get('metrics', [])
for header, dimension in zip(dimension_headers, dimensions):
json_country = dimension
for i, values in enumerate(date_range_values):
for metricHeader, value in zip(metric_headers, values.get('values')):
json_value = value
if metricHeader.get('name') == 'ga:users':
json_data['users_by_country'][json_country] = json_value
elif metricHeader.get('name') == 'ga:sessions':
json_data['sessions_by_country'][json_country] = json_value
with open(f'/fs/website/people/fergus.cooper/google_analytics_data_{START_DATE}.json', 'w') as outfile:
json.dump(json_data, outfile)
def main():
analytics = initialize_analyticsreporting()
response = get_report(analytics)
print_response(response)
if __name__ == '__main__':
main()