-
Notifications
You must be signed in to change notification settings - Fork 0
/
docs_feedback_metrics.py
256 lines (219 loc) · 7.91 KB
/
docs_feedback_metrics.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
from bokeh.plotting import figure, output_file, show, ColumnDataSource
from bokeh.transform import jitter
from googleapiclient.discovery import build
from oauth2client.service_account import ServiceAccountCredentials
SCOPES = ['https://www.googleapis.com/auth/analytics.readonly']
KEY_FILE_LOCATION = '<REPLACE_WITH_JSON_FILE>'
VIEW_ID = '<REPLACE_WITH_VIEW_ID>'
PAGE_METRICS = []
PAGE_TITLES = []
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_no_votes(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': '60daysAgo', 'endDate': 'today'}],
'metrics': [
{'expression': 'ga:totalEvents'}
],
'dimensions': [
{'name': 'ga:pageTitle'}
# {'name': 'ga:eventAction'}
],
'dimensionFilterClauses': [
{
'filters': [
{
'dimensionName': 'ga:eventAction',
'operator': 'REGEXP',
'expressions': ['/docs.*#no-feedback']
}
]
}
]
}
]
}
).execute()
def get_yes_votes(analytics):
return analytics.reports().batchGet(
body={
'reportRequests': [
{
'viewId': VIEW_ID,
'dateRanges': [{'startDate': '60daysAgo', 'endDate': 'today'}],
'metrics': [
{'expression': 'ga:totalEvents'}
],
'dimensions': [
{'name': 'ga:pageTitle'}
# {'name': 'ga:eventAction'}
],
'dimensionFilterClauses': [
{
'filters': [
{
'dimensionName': 'ga:eventAction',
'operator': 'REGEXP',
'expressions': ['/docs.*#yes-feedback']
}
]
}
]
}
]
}
).execute()
def get_unique_pageviews(analytics):
titles = '|'.join(PAGE_TITLES)
# print(titles)
return analytics.reports().batchGet(
body= {
'reportRequests': [
{
'viewId': VIEW_ID,
'dateRanges': [{'startDate': '60daysAgo', 'endDate': 'today'}],
'metrics': [
{'expression': 'ga:uniquePageviews'},
],
'dimensions': [
{'name': 'ga:pageTitle'},
],
'dimensionFilterClauses': [
{
'filters': [
{
'dimensionName': 'ga:pageTitle',
# 'dimensionName': 'ga:pagePath',
'operator': 'REGEXP',
# 'expressions': ['/docs']
'expressions': [titles]
}
]
}
]
}
]
}
).execute()
def no_response(response):
for report in response.get('reports', []):
# print(report)
for row in report.get('data', {}).get('rows', []):
# print(row)
PAGE = {}
TITLES = ''
for dimension in row.get('dimensions', {}):
PAGE['page'] = dimension.replace(' | CockroachDB Docs','')
PAGE_TITLES.append(dimension.replace(' | CockroachDB Docs',''))
for metric in row.get('metrics', {}):
for item in metric.values():
PAGE['no_votes'] = item[0]
PAGE_METRICS.append(PAGE)
def yes_response(response):
for report in response.get('reports', []):
# print(report)
for row in report.get('data', {}).get('rows', []):
# print(row)
for n in PAGE_METRICS:
for dimension in row.get('dimensions', {}):
if dimension.replace(' | CockroachDB Docs','') in n.values():
for metric in row.get('metrics', {}):
for item in metric.values():
n['yes_votes'] = item[0]
def pageviews_response(response):
for report in response.get('reports', []):
# print(report)
for row in report.get('data', {}).get('rows', []):
# print(row)
for n in PAGE_METRICS:
for dimension in row.get('dimensions', {}):
if dimension.replace(' | CockroachDB Docs','') in n.values():
for metric in row.get('metrics', {}):
for item in metric.values():
n['unique_pageviews'] = item[0]
def build_graph():
x = []
y = []
pageviews = []
pages = []
for n in PAGE_METRICS:
# print(n)
x.append(int(n['no_votes']))
pages.append(n['page'])
try:
pageviews.append(int(n['unique_pageviews']))
except:
pageviews.append(int('0'))
try:
y.append(int(n['yes_votes']))
except:
y.append(int('0'))
# print(x, y, pageviews, pages)
data = dict(
x = x,
y = y,
pageviews = pageviews,
pages = pages,
)
output_file("toolbar.html")
# Dynamically size markers based on pageviews.
size = list()
for n in data['pageviews']:
size.append(10*round(n/1000))
# if n < 1000:
# size.append(10)
# elif n < 2000:
# size.append(20)
# elif n < 3000:
# size.append(30)
# elif n < 4000:
# size.append(40)
# else:
# size.append(50)
# Add size markers to data source.
data['size'] = size
source = ColumnDataSource(data=data)
TOOLTIPS = [
# ("index", "$index"),
("Page", "@pages"),
("Unique Pageviews", "@pageviews"),
("Negative Votes", "$x"),
("Positive Votes", "$y"),
]
p = figure(plot_width=700, plot_height=700, tooltips=TOOLTIPS,
title="Hover to see page details")
p.circle(x='x', fill_alpha=0.2, y=jitter('y', width=0.6), size='size', source=source)
p.xaxis.axis_label = "Negative Votes"
p.xaxis.axis_label_text_color = "#aa6666"
p.xaxis.axis_label_standoff = 30
p.yaxis.axis_label = "Positive Votes"
p.yaxis.axis_label_text_color = "#aa6666"
p.yaxis.axis_label_standoff = 30
show(p)
def main():
analytics = initialize_analyticsreporting()
no_response(get_no_votes(analytics))
yes_response(get_yes_votes(analytics))
pageviews_response(get_unique_pageviews(analytics))
build_graph()
print(PAGE_METRICS)
if __name__ == '__main__':
main()