質問編集履歴

1

書式の改善

2017/09/13 08:01

投稿

k7_pgms88
k7_pgms88

スコア8

test CHANGED
@@ -1 +1 @@
1
- 【Googleアナリティクス Reporting API 】Pageviewが0だとレスポンスに含まれない
1
+ 【Googleアナリティクス Reporting API 】データが0の時、レスポンスに含まれない
test CHANGED
@@ -1,6 +1,6 @@
1
1
  GoogleアナリティクスのReporting API v4を利用し分間のPV数を取得するとき、
2
2
 
3
- 数値が0の場合は、レスポンスに含まれないようなのですが、
3
+ 数値が0の場合は、dimensionsがレスポンスに含まれないようなのですが、
4
4
 
5
5
 
6
6
 
@@ -10,105 +10,59 @@
10
10
 
11
11
 
12
12
 
13
+ 例)
14
+
15
+ 2017-09-11 11:01のとき、pageview「2」
16
+
17
+ 2017-09-11 11:02のとき、pageview「0」
18
+
19
+ 2017-09-11 11:03のとき、pageview「1」
20
+
21
+ の時、レスポンスは
22
+
23
+ ```ここに言語を入力
24
+
25
+ 'data': {
26
+
27
+ 'rows': [{
28
+
29
+ 'dimensions': ['201709111101'],
30
+
31
+ 'metrics': [{
32
+
33
+ 'values': ['2']
34
+
35
+ }]
36
+
37
+ }, {
38
+
39
+ 'dimensions': ['201709111103'],
40
+
41
+ 'metrics': [{
42
+
43
+ 'values': ['1']
44
+
45
+ }]
46
+
47
+ }
48
+
49
+ }
50
+
51
+ ```
52
+
53
+ 上記のように2017-09-11 11:02のデータがレポンスに含まれません。
54
+
55
+
56
+
57
+
58
+
13
- また、コードはGoogleのサンプルにあったものを利用しています。
59
+ また、コードはGoogleのサンプルにあったものを利用しており、実際のコードは
60
+
61
+ 下記のようにしています。
14
62
 
15
63
 
16
64
 
17
65
  ```ここに言語を入力
18
-
19
- """Hello Analytics Reporting API V4."""
20
-
21
-
22
-
23
- import argparse
24
-
25
-
26
-
27
- from apiclient.discovery import build
28
-
29
- import httplib2
30
-
31
- from oauth2client import client
32
-
33
- from oauth2client import file
34
-
35
- from oauth2client import tools
36
-
37
-
38
-
39
- SCOPES = ['https://www.googleapis.com/auth/analytics.readonly']
40
-
41
- DISCOVERY_URI = ('https://analyticsreporting.googleapis.com/$discovery/rest')
42
-
43
- CLIENT_SECRETS_PATH = 'client_secrets.json' # Path to client_secrets.json file.
44
-
45
- VIEW_ID = '<REPLACE_WITH_VIEW_ID>'
46
-
47
-
48
-
49
- def initialize_analyticsreporting():
50
-
51
- """Initializes the analyticsreporting service object.
52
-
53
-
54
-
55
- Returns:
56
-
57
- analytics an authorized analyticsreporting service object.
58
-
59
- """
60
-
61
- # Parse command-line arguments.
62
-
63
- parser = argparse.ArgumentParser(
64
-
65
- formatter_class=argparse.RawDescriptionHelpFormatter,
66
-
67
- parents=[tools.argparser])
68
-
69
- flags = parser.parse_args([])
70
-
71
-
72
-
73
- # Set up a Flow object to be used if we need to authenticate.
74
-
75
- flow = client.flow_from_clientsecrets(
76
-
77
- CLIENT_SECRETS_PATH, scope=SCOPES,
78
-
79
- message=tools.message_if_missing(CLIENT_SECRETS_PATH))
80
-
81
-
82
-
83
- # Prepare credentials, and authorize HTTP object with them.
84
-
85
- # If the credentials don't exist or are invalid run through the native client
86
-
87
- # flow. The Storage object will ensure that if successful the good
88
-
89
- # credentials will get written back to a file.
90
-
91
- storage = file.Storage('analyticsreporting.dat')
92
-
93
- credentials = storage.get()
94
-
95
- if credentials is None or credentials.invalid:
96
-
97
- credentials = tools.run_flow(flow, storage, flags)
98
-
99
- http = credentials.authorize(http=httplib2.Http())
100
-
101
-
102
-
103
- # Build the service object.
104
-
105
- analytics = build('analytics', 'v4', http=http, discoveryServiceUrl=DISCOVERY_URI)
106
-
107
-
108
-
109
- return analytics
110
-
111
-
112
66
 
113
67
  def get_report(analytics):
114
68
 
@@ -124,7 +78,11 @@
124
78
 
125
79
  'viewId': VIEW_ID,
126
80
 
81
+ 'pageSize': 10000,
82
+
127
- 'dateRanges': [{'startDate': '7daysAgo', 'endDate': 'today'}],
83
+ 'dateRanges': [{'startDate': '2017-09-11', 'endDate': '2017-09-11'}],
84
+
85
+ 'dimensions':[{'name':'ga:dateHourMinute'}],
128
86
 
129
87
  'metrics': [{'expression': 'ga:pageviews'}]
130
88
 
@@ -134,64 +92,4 @@
134
92
 
135
93
  ).execute()
136
94
 
137
-
138
-
139
- def print_response(response):
140
-
141
- """Parses and prints the Analytics Reporting API V4 response"""
142
-
143
-
144
-
145
- for report in response.get('reports', []):
146
-
147
- columnHeader = report.get('columnHeader', {})
148
-
149
- dimensionHeaders = columnHeader.get('dimensions', [])
150
-
151
- metricHeaders = columnHeader.get('metricHeader', {}).get('metricHeaderEntries', [])
152
-
153
- rows = report.get('data', {}).get('rows', [])
154
-
155
-
156
-
157
- for row in rows:
158
-
159
- dimensions = row.get('dimensions', [])
160
-
161
- dateRangeValues = row.get('metrics', [])
162
-
163
-
164
-
165
- for header, dimension in zip(dimensionHeaders, dimensions):
166
-
167
- print header + ': ' + dimension
168
-
169
-
170
-
171
- for i, values in enumerate(dateRangeValues):
172
-
173
- print 'Date range (' + str(i) + ')'
174
-
175
- for metricHeader, value in zip(metricHeaders, values.get('values')):
176
-
177
- print metricHeader.get('name') + ': ' + value
178
-
179
-
180
-
181
- def main():
182
-
183
-
184
-
185
- analytics = initialize_analyticsreporting()
186
-
187
- response = get_report(analytics)
188
-
189
- print_response(response)
190
-
191
-
192
-
193
- if __name__ == '__main__':
194
-
195
- main()
196
-
197
95
  ```