Left: | ||
Right: |
LEFT | RIGHT |
---|---|
1 #!/usr/bin/python | 1 #!/usr/bin/python |
2 # -*- coding: utf-8 -*- | 2 # -*- coding: utf-8 -*- |
3 # | 3 # |
4 # Copyright 2012 Google Inc. All Rights Reserved. | 4 # Copyright 2012 Google Inc. All Rights Reserved. |
5 # | 5 # |
6 # Licensed under the Apache License, Version 2.0 (the "License"); | 6 # Licensed under the Apache License, Version 2.0 (the "License"); |
7 # you may not use this file except in compliance with the License. | 7 # you may not use this file except in compliance with the License. |
8 # You may obtain a copy of the License at | 8 # You may obtain a copy of the License at |
9 # | 9 # |
10 # http://www.apache.org/licenses/LICENSE-2.0 | 10 # http://www.apache.org/licenses/LICENSE-2.0 |
11 # | 11 # |
12 # Unless required by applicable law or agreed to in writing, software | 12 # Unless required by applicable law or agreed to in writing, software |
13 # distributed under the License is distributed on an "AS IS" BASIS, | 13 # distributed under the License is distributed on an "AS IS" BASIS, |
14 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | 14 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
15 # See the License for the specific language governing permissions and | 15 # See the License for the specific language governing permissions and |
16 # limitations under the License. | 16 # limitations under the License. |
17 | 17 |
18 """Simple intro to using the Google Analytics API v3. | 18 """Simple intro to using the Google Analytics API v3. |
19 | 19 |
20 This application demonstrates how to use the python client library to access | 20 This application demonstrates how to use the python client library to access |
21 Google Analytics data. The sample traverses the Management API to obtain the | 21 Google Analytics data. The sample traverses the Management API to obtain the |
22 authorized users first profile ID. Then the sample uses this ID to | 22 authorized user's first profile ID. Then the sample uses this ID to |
jsoneja
2012/03/06 22:33:55
users's
api.nickm1
2012/04/26 06:30:16
Done.
| |
23 contstruct a Core Reporting API query to return the top 25 organic search | 23 contstruct a Core Reporting API query to return the top 25 organic search |
24 terms. | 24 terms. |
25 | 25 |
26 Before you begin, you must sigup for a new project in the Google APIs console: | 26 Before you begin, you must sigup for a new project in the Google APIs console: |
27 https://code.google.com/apis/console | 27 https://code.google.com/apis/console |
28 | 28 |
29 The register the project to use OAuth2.0 for installed applications. | 29 Then register the project to use OAuth2.0 for installed applications. |
jsoneja
2012/03/06 22:33:55
s/The/Then/
api.nickm1
2012/04/26 06:30:16
Done.
| |
30 | 30 |
31 Finally you will need to add the client id, client secret, and redirect URL | 31 Finally you will need to add the client id, client secret, and redirect URL |
32 into the client_secrest.json file that is in the same directory as this sample. | 32 into the client_secrets.json file that is in the same directory as this sample. |
jsoneja
2012/03/06 22:33:55
client_secrets.json
api.nickm1
2012/04/26 06:30:16
Done.
| |
33 | 33 |
34 Sample Usage: | 34 Sample Usage: |
35 | 35 |
36 $ python hello_analytics_api_v3.py | 36 $ python hello_analytics_api_v3.py |
37 | 37 |
38 Also you can also get help on all the command-line flags the program | 38 Also you can also get help on all the command-line flags the program |
39 understands by running: | 39 understands by running: |
40 | 40 |
41 $ python hello_analytics_api_v3.py --help | 41 $ python hello_analytics_api_v3.py --help |
jsoneja
2012/03/06 22:33:55
if we do decide to rename the java sample, we shou
api.nickm1
2012/04/26 06:30:16
lets do in another cl since renaming now is a bit
| |
42 """ | 42 """ |
43 | 43 |
44 __author__ = 'api.nickm@gmail.com (Nick Mihailovski)' | 44 __author__ = 'api.nickm@gmail.com (Nick Mihailovski)' |
45 | 45 |
46 import sys | 46 import sys |
47 import sample_utils | 47 import sample_utils |
48 | 48 |
49 from apiclient.errors import HttpError | 49 from apiclient.errors import HttpError |
50 from oauth2client.client import AccessTokenRefreshError | 50 from oauth2client.client import AccessTokenRefreshError |
51 | 51 |
(...skipping 98 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
150 results: The response returned from the Core Reporting API. | 150 results: The response returned from the Core Reporting API. |
151 """ | 151 """ |
152 | 152 |
153 print | 153 print |
154 print 'Profile Name: %s' % results.get('profileInfo').get('profileName') | 154 print 'Profile Name: %s' % results.get('profileInfo').get('profileName') |
155 print | 155 print |
156 | 156 |
157 # Print header. | 157 # Print header. |
158 output = [] | 158 output = [] |
159 for header in results.get('columnHeaders'): | 159 for header in results.get('columnHeaders'): |
160 output.append(header.get('name')) | 160 output.append('%30s' % header.get('name')) |
161 print ''.join(output) | 161 print ''.join(output) |
162 | 162 |
163 # Print data table. | 163 # Print data table. |
164 if results.get('rows'): | 164 if results.get('rows', []): |
jcgregorio_google
2012/03/06 15:01:59
if 'rows' in results:
or combine these two lines
api.nickm1
2012/04/26 06:30:16
Done.
| |
165 for row in results.get('rows'): | 165 for row in results.get('rows'): |
166 output = [] | 166 output = [] |
167 for cell in row: | 167 for cell in row: |
168 output.append('%30s' % cell) | 168 output.append('%30s' % cell) |
169 print ''.join(output) | 169 print ''.join(output) |
170 | 170 |
171 else: | 171 else: |
172 print 'No Rows Found' | 172 print 'No Rows Found' |
173 | 173 |
174 | 174 |
175 if __name__ == '__main__': | 175 if __name__ == '__main__': |
176 main(sys.argv) | 176 main(sys.argv) |
LEFT | RIGHT |