Rietveld Code Review Tool
Help | Bug tracker | Discussion group | Source code | Sign in
(258)

Delta Between Two Patch Sets: samples/analytics/hello_analytics_api_v3.py

Issue 5494058: Adding py sample for google analytics core reporting api (Closed)
Left Patch Set: adding new hello world sample, moving to common auth util, cleaning up errors Created 13 years, 2 months ago
Right Patch Set: more typo fixes Created 13 years, 1 month ago
Left:
Right:
Use n/p to move between diff chunks; N/P to move between comments. Please Sign in to add in-line comments.
Jump to:
Left: Side by side diff | Download
Right: Side by side diff | Download
« no previous file with change/comment | « samples/analytics/core_reporting_v3_reference.py ('k') | samples/analytics/management_v3_reference.py » ('j') | no next file with change/comment »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
LEFTRIGHT
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
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.
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.
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
42 """ 42 """
(...skipping 107 matching lines...) Expand 10 before | Expand all | Expand 10 after
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', []):
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)
LEFTRIGHT

Powered by Google App Engine
RSS Feeds Recent Issues | This issue
This is Rietveld f62528b