質問編集履歴

2

書式の改善

2019/01/11 02:16

投稿

peipei1993
peipei1993

スコア12

test CHANGED
File without changes
test CHANGED
@@ -20,192 +20,194 @@
20
20
 
21
21
  ### 参考ソース
22
22
 
23
+
24
+
25
+ ```python
26
+
27
+ """A simple example of how to access the Google Analytics API."""
28
+
29
+
30
+
31
+ from apiclient.discovery import build
32
+
33
+ from oauth2client.service_account import ServiceAccountCredentials
34
+
35
+
36
+
37
+ def get_service(api_name, api_version, scopes, key_file_location):
38
+
39
+ """Get a service that communicates to a Google API.
40
+
41
+
42
+
43
+ Args:
44
+
45
+ api_name: The name of the api to connect to.
46
+
47
+ api_version: The api version to connect to.
48
+
49
+ scopes: A list auth scopes to authorize for the application.
50
+
51
+ key_file_location: The path to a valid service account JSON key file.
52
+
53
+
54
+
55
+ Returns:
56
+
57
+ A service that is connected to the specified API.
58
+
59
+ """
60
+
61
+
62
+
63
+ credentials = ServiceAccountCredentials.from_json_keyfile_name(
64
+
65
+ key_file_location, scopes=scopes)
66
+
67
+
68
+
69
+ # Build the service object.
70
+
71
+ service = build(api_name, api_version, credentials=credentials)
72
+
73
+
74
+
75
+ return service
76
+
77
+
78
+
79
+ def get_first_profile_id(service):
80
+
81
+ # Use the Analytics service object to get the first profile id.
82
+
83
+
84
+
85
+ # Get a list of all Google Analytics accounts for this user
86
+
87
+ accounts = service.management().accounts().list().execute()
88
+
89
+
90
+
91
+ if accounts.get('items'):
92
+
93
+ # Get the first Google Analytics account.
94
+
95
+ account = accounts.get('items')[0].get('id')
96
+
97
+
98
+
99
+ # Get a list of all the properties for the first account.
100
+
101
+ properties = service.management().webproperties().list(
102
+
103
+ accountId=account).execute()
104
+
105
+
106
+
107
+ if properties.get('items'):
108
+
109
+ # Get the first property id.
110
+
111
+ property = properties.get('items')[0].get('id')
112
+
113
+
114
+
115
+ # Get a list of all views (profiles) for the first property.
116
+
117
+ profiles = service.management().profiles().list(
118
+
119
+ accountId=account,
120
+
121
+ webPropertyId=property).execute()
122
+
123
+
124
+
125
+ if profiles.get('items'):
126
+
127
+ # return the first view (profile) id.
128
+
129
+ return profiles.get('items')[0].get('id')
130
+
131
+
132
+
133
+ return None
134
+
135
+
136
+
137
+ def get_results(service, profile_id):
138
+
139
+ # Use the Analytics Service Object to query the Core Reporting API
140
+
141
+ # for the number of sessions within the past seven days.
142
+
143
+ return service.data().ga().get(
144
+
145
+ ids='ga:' + profile_id,
146
+
147
+ start_date='7daysAgo',
148
+
149
+ end_date='today',
150
+
151
+ metrics='ga:sessions').execute()
152
+
153
+
154
+
155
+ def print_results(results):
156
+
157
+ # Print data nicely for the user.
158
+
159
+ if results:
160
+
161
+ print 'View (Profile):', results.get('profileInfo').get('profileName')
162
+
163
+ print 'Total Sessions:', results.get('rows')[0][0]
164
+
165
+
166
+
167
+ else:
168
+
169
+ print 'No results found'
170
+
171
+
172
+
173
+ def main():
174
+
175
+ # Define the auth scopes to request.
176
+
177
+ scope = 'https://www.googleapis.com/auth/analytics.readonly'
178
+
179
+ key_file_location = '<REPLACE_WITH_JSON_FILE>'
180
+
181
+
182
+
183
+ # Authenticate and construct service.
184
+
185
+ service = get_service(
186
+
187
+ api_name='analytics',
188
+
189
+ api_version='v3',
190
+
191
+ scopes=[scope],
192
+
193
+ key_file_location=key_file_location)
194
+
195
+
196
+
197
+ profile_id = get_first_profile_id(service)
198
+
199
+ print_results(get_results(service, profile_id))
200
+
201
+
202
+
203
+ if __name__ == '__main__':
204
+
205
+ main()```
206
+
207
+
208
+
23
209
  ```
24
210
 
25
- """A simple example of how to access the Google Analytics API."""
26
-
27
-
28
-
29
- from apiclient.discovery import build
30
-
31
- from oauth2client.service_account import ServiceAccountCredentials
32
-
33
-
34
-
35
- def get_service(api_name, api_version, scopes, key_file_location):
36
-
37
- """Get a service that communicates to a Google API.
38
-
39
-
40
-
41
- Args:
42
-
43
- api_name: The name of the api to connect to.
44
-
45
- api_version: The api version to connect to.
46
-
47
- scopes: A list auth scopes to authorize for the application.
48
-
49
- key_file_location: The path to a valid service account JSON key file.
50
-
51
-
52
-
53
- Returns:
54
-
55
- A service that is connected to the specified API.
56
-
57
- """
58
-
59
-
60
-
61
- credentials = ServiceAccountCredentials.from_json_keyfile_name(
62
-
63
- key_file_location, scopes=scopes)
64
-
65
-
66
-
67
- # Build the service object.
68
-
69
- service = build(api_name, api_version, credentials=credentials)
70
-
71
-
72
-
73
- return service
74
-
75
-
76
-
77
- def get_first_profile_id(service):
78
-
79
- # Use the Analytics service object to get the first profile id.
80
-
81
-
82
-
83
- # Get a list of all Google Analytics accounts for this user
84
-
85
- accounts = service.management().accounts().list().execute()
86
-
87
-
88
-
89
- if accounts.get('items'):
90
-
91
- # Get the first Google Analytics account.
92
-
93
- account = accounts.get('items')[0].get('id')
94
-
95
-
96
-
97
- # Get a list of all the properties for the first account.
98
-
99
- properties = service.management().webproperties().list(
100
-
101
- accountId=account).execute()
102
-
103
-
104
-
105
- if properties.get('items'):
106
-
107
- # Get the first property id.
108
-
109
- property = properties.get('items')[0].get('id')
110
-
111
-
112
-
113
- # Get a list of all views (profiles) for the first property.
114
-
115
- profiles = service.management().profiles().list(
116
-
117
- accountId=account,
118
-
119
- webPropertyId=property).execute()
120
-
121
-
122
-
123
- if profiles.get('items'):
124
-
125
- # return the first view (profile) id.
126
-
127
- return profiles.get('items')[0].get('id')
128
-
129
-
130
-
131
- return None
132
-
133
-
134
-
135
- def get_results(service, profile_id):
136
-
137
- # Use the Analytics Service Object to query the Core Reporting API
138
-
139
- # for the number of sessions within the past seven days.
140
-
141
- return service.data().ga().get(
142
-
143
- ids='ga:' + profile_id,
144
-
145
- start_date='7daysAgo',
146
-
147
- end_date='today',
148
-
149
- metrics='ga:sessions').execute()
150
-
151
-
152
-
153
- def print_results(results):
154
-
155
- # Print data nicely for the user.
156
-
157
- if results:
158
-
159
- print 'View (Profile):', results.get('profileInfo').get('profileName')
160
-
161
- print 'Total Sessions:', results.get('rows')[0][0]
162
-
163
-
164
-
165
- else:
166
-
167
- print 'No results found'
168
-
169
-
170
-
171
- def main():
172
-
173
- # Define the auth scopes to request.
174
-
175
- scope = 'https://www.googleapis.com/auth/analytics.readonly'
176
-
177
- key_file_location = '<REPLACE_WITH_JSON_FILE>'
178
-
179
-
180
-
181
- # Authenticate and construct service.
182
-
183
- service = get_service(
184
-
185
- api_name='analytics',
186
-
187
- api_version='v3',
188
-
189
- scopes=[scope],
190
-
191
- key_file_location=key_file_location)
192
-
193
-
194
-
195
- profile_id = get_first_profile_id(service)
196
-
197
- print_results(get_results(service, profile_id))
198
-
199
-
200
-
201
- if __name__ == '__main__':
202
-
203
- main()```
204
-
205
-
206
-
207
- ```
208
-
209
211
 
210
212
 
211
213
  ### 試したこと

1

書式の改善

2019/01/11 02:16

投稿

peipei1993
peipei1993

スコア12

test CHANGED
File without changes
test CHANGED
@@ -20,6 +20,8 @@
20
20
 
21
21
  ### 参考ソース
22
22
 
23
+ ```
24
+
23
25
  """A simple example of how to access the Google Analytics API."""
24
26
 
25
27
 
@@ -202,6 +204,10 @@
202
204
 
203
205
 
204
206
 
207
+ ```
208
+
209
+
210
+
205
211
  ### 試したこと
206
212
 
207
213