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

Side by Side Diff: samples/analytics/management_v3_reference.py

Issue 5494058: Adding py sample for google analytics core reporting api (Closed)
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:
View unified diff | Download patch
« no previous file with comments | « samples/analytics/hello_analytics_api_v3.py ('k') | samples/analytics/sample_utils.py » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 #!/usr/bin/python 1 #!/usr/bin/python
2 # -*- coding: utf-8 -*- 2 # -*- coding: utf-8 -*-
3 # 3 #
4 # Copyright 2011 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.
(...skipping 10 matching lines...) Expand all
25 Google Analytics Management hiearchy. It first retrieves and prints all the 25 Google Analytics Management hiearchy. It first retrieves and prints all the
26 authorized user's accounts, next it prints all the web properties for the 26 authorized user's accounts, next it prints all the web properties for the
27 first account, then all the profiles for the first web property and finally 27 first account, then all the profiles for the first web property and finally
28 all the goals for the first profile. The sample then prints all the 28 all the goals for the first profile. The sample then prints all the
29 user's advanced segments. 29 user's advanced segments.
30 30
31 To read an indepth discussion on how this file works, check out the Management 31 To read an indepth discussion on how this file works, check out the Management
32 API Python Getting Started guide here: 32 API Python Getting Started guide here:
33 http://code.google.com/apis/analytics/docs/mgmt/v3/mgmtPython.html 33 http://code.google.com/apis/analytics/docs/mgmt/v3/mgmtPython.html
34 34
35 Usage: 35 Before You Begin:
36 36
37 Before you begin, you should register your application as an installed 37 Update the client_secrets.json file
38 application to get your own Project / OAUth2 Client ID / Secret:
39 https://code.google.com/apis/console
40 38
41 Learn more about registering your Analytics Application here: 39 You must update the clients_secrets.json file with a client id, client
42 http://code.google.com/apis/analytics/docs/mgmt/v3/mgmtPython.html#authorize 40 secret, and the redirect uri. You get these values by creating a new project
41 in the Google APIs console and registering for OAuth2.0 for installed
42 applications: https://code.google.com/apis/console
43 43
44 $ python analytics.py 44 Learn more about registering your analytics application here:
45 http://code.google.com/apis/analytics/docs/gdata/v3/gdataAuthorization.html
46
47 Sample Usage:
48
49 $ python management_v3_reference.py
45 50
46 Also you can also get help on all the command-line flags the program 51 Also you can also get help on all the command-line flags the program
47 understands by running: 52 understands by running:
48 53
49 $ python analytics.py --help 54 $ python management_v3_reference.py --help
50 """ 55 """
51 56
52 __author__ = 'api.nickm@ (Nick Mihailovski)' 57 __author__ = 'api.nickm@gmail.com (Nick Mihailovski)'
53 58
54 import sys 59 import sys
60 import sample_utils
55 61
56 from apiclient.discovery import build
57 from apiclient.errors import HttpError 62 from apiclient.errors import HttpError
58
59 import gflags
60 import httplib2
61
62 from oauth2client.client import AccessTokenRefreshError 63 from oauth2client.client import AccessTokenRefreshError
63 from oauth2client.client import OAuth2WebServerFlow
64 from oauth2client.file import Storage
65 from oauth2client.tools import run
66
67 FLAGS = gflags.FLAGS
68
69
70 # Remember to get your own client_id / client_secret in the
71 # Google API developer console: https://code.google.com/apis/console
72 FLOW = OAuth2WebServerFlow(
73 client_id='INSERT_YOUR_CLIENT_ID_HERE',
74 client_secret='INSERT_YOUR_CLIENT_SECRET_HERE',
75 scope='https://www.googleapis.com/auth/analytics.readonly',
76 user_agent='analytics-api-v3-awesomeness')
77
78 TOKEN_FILE_NAME = 'analytics.dat'
79 64
80 65
81 def main(argv): 66 def main(argv):
82 # Let the gflags module process the command-line arguments 67 sample_utils.process_flags(argv)
83 try:
84 argv = FLAGS(argv)
85 except gflags.FlagsError, e:
86 print '%s\\nUsage: %s ARGS\\n%s' % (e, argv[0], FLAGS)
87 sys.exit(1)
88 68
89 # Manage re-using tokens. 69 # Authenticate and construct service.
90 storage = Storage(TOKEN_FILE_NAME) 70 service = sample_utils.initialize_service()
91 credentials = storage.get()
92 if not credentials or credentials.invalid:
93 # Get a new token.
94 credentials = run(FLOW, storage)
95 71
96 # Build an authorized service object. 72 # Traverse the Management hiearchy and print results or handle errors.
97 http = httplib2.Http()
98 http = credentials.authorize(http)
99 service = build('analytics', 'v3', http=http)
100
101 # Traverse the Management hiearchy and print results.
102 try: 73 try:
103 traverse_hiearchy(service) 74 traverse_hiearchy(service)
104 75
76 except TypeError, error:
77 # Handle errors in constructing a query.
78 print ('There was an error in constructing your query : %s' % error)
79
105 except HttpError, error: 80 except HttpError, error:
106 print ('Arg, there was an API error : %s %s : %s' % 81 # Handle API errors.
107 (error.resp.status, error.resp.reason, error._get_reason())) 82 print ('Arg, there was an API error : %s : %s' %
83 (error.resp.status, error._get_reason()))
108 84
109 except AccessTokenRefreshError: 85 except AccessTokenRefreshError:
110 print ('The credentials have been revoked or expired, please re-run' 86 print ('The credentials have been revoked or expired, please re-run'
111 'the application to re-authorize') 87 'the application to re-authorize')
112 88
113 89
114 def traverse_hiearchy(service): 90 def traverse_hiearchy(service):
115 """Traverses the management API hiearchy and prints results. 91 """Traverses the management API hiearchy and prints results.
116 92
117 This retrieves and prints the authorized user's accounts. It then 93 This retrieves and prints the authorized user's accounts. It then
118 retrieves and prints all the web properties for the first account, 94 retrieves and prints all the web properties for the first account,
119 retrieves and prints all the profiles for the first web property, 95 retrieves and prints all the profiles for the first web property,
120 and retrieves and prints all the goals for the first profile. 96 and retrieves and prints all the goals for the first profile.
121 97
122 Args: 98 Args:
123 service: The service object built by the Google API Python client library. 99 service: The service object built by the Google API Python client library.
124 100
125 Raises: 101 Raises:
126 HttpError: If an error occured when accessing the API. 102 HttpError: If an error occured when accessing the API.
127 AccessTokenRefreshError: If the current token was invalid. 103 AccessTokenRefreshError: If the current token was invalid.
128 """ 104 """
129 view = View()
130 105
131 accounts = service.management().accounts().list().execute() 106 accounts = service.management().accounts().list().execute()
132 107 print_accounts(accounts)
133 view.print_accounts(accounts)
134 108
135 if accounts.get('items'): 109 if accounts.get('items'):
136 firstAccountId = accounts.get('items')[0].get('id') 110 firstAccountId = accounts.get('items')[0].get('id')
137 webproperties = service.management().webproperties().list( 111 webproperties = service.management().webproperties().list(
138 accountId=firstAccountId).execute() 112 accountId=firstAccountId).execute()
139 113
140 view.print_webproperties(webproperties) 114 print_webproperties(webproperties)
141 115
142 if webproperties.get('items'): 116 if webproperties.get('items'):
143 firstWebpropertyId = webproperties.get('items')[0].get('id') 117 firstWebpropertyId = webproperties.get('items')[0].get('id')
144 profiles = service.management().profiles().list( 118 profiles = service.management().profiles().list(
145 accountId=firstAccountId, 119 accountId=firstAccountId,
146 webPropertyId=firstWebpropertyId).execute() 120 webPropertyId=firstWebpropertyId).execute()
147 121
148 view.print_profiles(profiles) 122 print_profiles(profiles)
149 123
150 if profiles.get('items'): 124 if profiles.get('items'):
151 firstProfileId = profiles.get('items')[0].get('id') 125 firstProfileId = profiles.get('items')[0].get('id')
152 goals = service.management().goals().list( 126 goals = service.management().goals().list(
153 accountId=firstAccountId, 127 accountId=firstAccountId,
154 webPropertyId=firstWebpropertyId, 128 webPropertyId=firstWebpropertyId,
155 profileId=firstProfileId).execute() 129 profileId=firstProfileId).execute()
156 130
157 view.print_goals(goals) 131 print_goals(goals)
158 132
159 view.print_segments(service.management().segments().list().execute()) 133 print_segments(service.management().segments().list().execute())
160 134
161 135
162 class View(object): 136 def print_accounts(accounts_response):
163 """Utility class to print various Management API collections.""" 137 """Prints all the account info in the Accounts Collection.
164 138
165 def print_accounts(self, accounts_list): 139 Args:
166 """Prints all the account info in the Accounts Collection.""" 140 accounts_response: The response object returned from querying the Accounts
167 141 collection.
168 print '------ Account Collection -------' 142 """
169 self.print_pagination_info(accounts_list) 143
170 print 144 print '------ Account Collection -------'
171 145 print_pagination_info(accounts_response)
172 for account in accounts_list.get('items'): 146 print
173 print 'Account ID = %s' % account.get('id') 147
174 print 'Kind = %s' % account.get('kind') 148 for account in accounts_response.get('items', []):
175 print 'Self Link = %s' % account.get('selfLink') 149 print 'Account ID = %s' % account.get('id')
176 print 'Account Name = %s' % account.get('name') 150 print 'Kind = %s' % account.get('kind')
177 print 'Created = %s' % account.get('created') 151 print 'Self Link = %s' % account.get('selfLink')
178 print 'Updated = %s' % account.get('updated') 152 print 'Account Name = %s' % account.get('name')
179 153 print 'Created = %s' % account.get('created')
180 child_link = account.get('childLink') 154 print 'Updated = %s' % account.get('updated')
181 print 'Child link href = %s' % child_link.get('href') 155
182 print 'Child link type = %s' % child_link.get('type') 156 child_link = account.get('childLink')
183 print 157 print 'Child link href = %s' % child_link.get('href')
184 158 print 'Child link type = %s' % child_link.get('type')
185 def print_webproperties(self, webproperties_list): 159 print
186 """Prints all the web property info in the WebProperties Collection.""" 160 else:
187 161 print 'No accounts found.\n'
188 print '------ Web Properties Collection -------' 162
189 self.print_pagination_info(webproperties_list) 163
190 print 164 def print_webproperties(webproperties_response):
191 165 """Prints all the web property info in the WebProperties collection.
192 for webproperty in webproperties_list.get('items'): 166
193 print 'Kind = %s' % webproperty.get('kind') 167 Args:
194 print 'Account ID = %s' % webproperty.get('accountId') 168 webproperties_response: The response object returned from querying the
195 print 'Web Property ID = %s' % webproperty.get('id') 169 Webproperties collection.
196 print ('Internal Web Property ID = %s' % 170 """
197 webproperty.get('internalWebPropertyId')) 171
198 172 print '------ Web Properties Collection -------'
199 print 'Website URL = %s' % webproperty.get('websiteUrl') 173 print_pagination_info(webproperties_response)
200 print 'Created = %s' % webproperty.get('created') 174 print
201 print 'Updated = %s' % webproperty.get('updated') 175
202 176 for webproperty in webproperties_response.get('items', []):
203 print 'Self Link = %s' % webproperty.get('selfLink') 177 print 'Kind = %s' % webproperty.get('kind')
204 parent_link = webproperty.get('parentLink') 178 print 'Account ID = %s' % webproperty.get('accountId')
205 print 'Parent link href = %s' % parent_link.get('href') 179 print 'Web Property ID = %s' % webproperty.get('id')
206 print 'Parent link type = %s' % parent_link.get('type') 180 print ('Internal Web Property ID = %s' %
207 child_link = webproperty.get('childLink') 181 webproperty.get('internalWebPropertyId'))
208 print 'Child link href = %s' % child_link.get('href') 182
209 print 'Child link type = %s' % child_link.get('type') 183 print 'Website URL = %s' % webproperty.get('websiteUrl')
210 print 184 print 'Created = %s' % webproperty.get('created')
211 185 print 'Updated = %s' % webproperty.get('updated')
212 def print_profiles(self, profiles_list): 186
213 """Prints all the profile info in the Profiles Collection.""" 187 print 'Self Link = %s' % webproperty.get('selfLink')
214 188 parent_link = webproperty.get('parentLink')
215 print '------ Profiles Collection -------' 189 print 'Parent link href = %s' % parent_link.get('href')
216 self.print_pagination_info(profiles_list) 190 print 'Parent link type = %s' % parent_link.get('type')
217 print 191 child_link = webproperty.get('childLink')
218 192 print 'Child link href = %s' % child_link.get('href')
219 for profile in profiles_list.get('items'): 193 print 'Child link type = %s' % child_link.get('type')
220 print 'Kind = %s' % profile.get('kind') 194 print
221 print 'Account ID = %s' % profile.get('accountId') 195 else:
222 print 'Web Property ID = %s' % profile.get('webPropertyId') 196 print 'No webproperties found.\n'
223 print ('Internal Web Property ID = %s' % 197
224 profile.get('internalWebPropertyId')) 198
225 print 'Profile ID = %s' % profile.get('id') 199 def print_profiles(profiles_response):
226 print 'Profile Name = %s' % profile.get('name') 200 """Prints all the profile info in the Profiles Collection.
227 201
228 print 'Currency = %s' % profile.get('currency') 202 Args:
229 print 'Timezone = %s' % profile.get('timezone') 203 profiles_response: The response object returned from querying the
230 print 'Default Page = %s' % profile.get('defaultPage') 204 Profiles collection.
231 205 """
232 print ('Exclude Query Parameters = %s' % 206
233 profile.get('excludeQueryParameters')) 207 print '------ Profiles Collection -------'
234 print ('Site Search Category Parameters = %s' % 208 print_pagination_info(profiles_response)
235 profile.get('siteSearchCategoryParameters')) 209 print
236 print ('Site Search Query Parameters = %s' % 210
237 profile.get('siteSearchQueryParameters')) 211 for profile in profiles_response.get('items', []):
238 212 print 'Kind = %s' % profile.get('kind')
239 print 'Created = %s' % profile.get('created') 213 print 'Account ID = %s' % profile.get('accountId')
240 print 'Updated = %s' % profile.get('updated') 214 print 'Web Property ID = %s' % profile.get('webPropertyId')
241 215 print ('Internal Web Property ID = %s' %
242 print 'Self Link = %s' % profile.get('selfLink') 216 profile.get('internalWebPropertyId'))
243 parent_link = profile.get('parentLink') 217 print 'Profile ID = %s' % profile.get('id')
244 print 'Parent link href = %s' % parent_link.get('href') 218 print 'Profile Name = %s' % profile.get('name')
245 print 'Parent link type = %s' % parent_link.get('type') 219
246 child_link = profile.get('childLink') 220 print 'Currency = %s' % profile.get('currency')
247 print 'Child link href = %s' % child_link.get('href') 221 print 'Timezone = %s' % profile.get('timezone')
248 print 'Child link type = %s' % child_link.get('type') 222 print 'Default Page = %s' % profile.get('defaultPage')
249 print 223
250 224 print ('Exclude Query Parameters = %s' %
251 def print_goals(self, goals_list): 225 profile.get('excludeQueryParameters'))
252 """Prints all the goal info in the Goals Collection.""" 226 print ('Site Search Category Parameters = %s' %
253 227 profile.get('siteSearchCategoryParameters'))
254 print '------ Goals Collection -------' 228 print ('Site Search Query Parameters = %s' %
255 self.print_pagination_info(goals_list) 229 profile.get('siteSearchQueryParameters'))
256 print 230
257 231 print 'Created = %s' % profile.get('created')
258 for goal in goals_list.get('items'): 232 print 'Updated = %s' % profile.get('updated')
259 print 'Goal ID = %s' % goal.get('id') 233
260 print 'Kind = %s' % goal.get('kind') 234 print 'Self Link = %s' % profile.get('selfLink')
261 print 'Self Link = %s' % goal.get('selfLink') 235 parent_link = profile.get('parentLink')
262 236 print 'Parent link href = %s' % parent_link.get('href')
263 print 'Account ID = %s' % goal.get('accountId') 237 print 'Parent link type = %s' % parent_link.get('type')
264 print 'Web Property ID = %s' % goal.get('webPropertyId') 238 child_link = profile.get('childLink')
265 print ('Internal Web Property ID = %s' % 239 print 'Child link href = %s' % child_link.get('href')
266 goal.get('internalWebPropertyId')) 240 print 'Child link type = %s' % child_link.get('type')
267 print 'Profile ID = %s' % goal.get('profileId') 241 print
268 242 else:
269 print 'Goal Name = %s' % goal.get('name') 243 print 'No profiles found.\n'
270 print 'Goal Value = %s' % goal.get('value') 244
271 print 'Goal Active = %s' % goal.get('active') 245
272 print 'Goal Type = %s' % goal.get('type') 246 def print_goals(goals_response):
273 247 """Prints all the goal info in the Goals collection.
274 print 'Created = %s' % goal.get('created') 248
275 print 'Updated = %s' % goal.get('updated') 249 Args:
276 250 goals_response: The response object returned from querying the Goals
277 parent_link = goal.get('parentLink') 251 collection
278 print 'Parent link href = %s' % parent_link.get('href') 252 """
279 print 'Parent link type = %s' % parent_link.get('type') 253
280 254 print '------ Goals Collection -------'
281 # Print the goal details depending on the type of goal. 255 print_pagination_info(goals_response)
282 if goal.get('urlDestinationDetails'): 256 print
283 self.print_url_destination_goal_details( 257
284 goal.get('urlDestinationDetails')) 258 for goal in goals_response.get('items', []):
285 259 print 'Goal ID = %s' % goal.get('id')
286 elif goal.get('visitTimeOnSiteDetails'): 260 print 'Kind = %s' % goal.get('kind')
287 self.print_visit_time_on_site_goal_details( 261 print 'Self Link = %s' % goal.get('selfLink')
288 goal.get('visitTimeOnSiteDetails')) 262
289 263 print 'Account ID = %s' % goal.get('accountId')
290 elif goal.get('visitNumPagesDetails'): 264 print 'Web Property ID = %s' % goal.get('webPropertyId')
291 self.print_visit_num_pages_goal_details( 265 print ('Internal Web Property ID = %s' %
292 goal.get('visitNumPagesDetails')) 266 goal.get('internalWebPropertyId'))
293 267 print 'Profile ID = %s' % goal.get('profileId')
294 elif goal.get('eventDetails'): 268
295 self.print_event_goal_details(goal.get('eventDetails')) 269 print 'Goal Name = %s' % goal.get('name')
296 270 print 'Goal Value = %s' % goal.get('value')
297 print 271 print 'Goal Active = %s' % goal.get('active')
298 272 print 'Goal Type = %s' % goal.get('type')
299 def print_url_destination_goal_details(self, goal_details): 273
300 """Prints all the URL Destination goal type info.""" 274 print 'Created = %s' % goal.get('created')
301 275 print 'Updated = %s' % goal.get('updated')
302 print '------ Url Destination Goal -------' 276
303 print 'Goal URL = %s' % goal_details.get('url') 277 parent_link = goal.get('parentLink')
304 print 'Case Sensitive = %s' % goal_details.get('caseSensitive') 278 print 'Parent link href = %s' % parent_link.get('href')
305 print 'Match Type = %s' % goal_details.get('matchType') 279 print 'Parent link type = %s' % parent_link.get('type')
306 print 'First Step Required = %s' % goal_details.get('firstStepRequired') 280
307 281 # Print the goal details depending on the type of goal.
308 print '------ Url Destination Goal Steps -------' 282 if goal.get('urlDestinationDetails'):
309 if goal_details.get('steps'): 283 print_url_destination_goal_details(
310 for goal_step in goal_details.get('steps'): 284 goal.get('urlDestinationDetails'))
311 print 'Step Number = %s' % goal_step.get('number') 285
312 print 'Step Name = %s' % goal_step.get('name') 286 elif goal.get('visitTimeOnSiteDetails'):
313 print 'Step URL = %s' % goal_step.get('url') 287 print_visit_time_on_site_goal_details(
314 else: 288 goal.get('visitTimeOnSiteDetails'))
315 print 'No Steps Configured' 289
316 290 elif goal.get('visitNumPagesDetails'):
317 def print_visit_time_on_site_goal_details(self, goal_details): 291 print_visit_num_pages_goal_details(
318 """Prints all the Visit Time On Site goal type info.""" 292 goal.get('visitNumPagesDetails'))
319 293
320 print '------ Visit Time On Site Goal -------' 294 elif goal.get('eventDetails'):
321 print 'Comparison Type = %s' % goal_details.get('comparisonType') 295 print_event_goal_details(goal.get('eventDetails'))
322 print 'comparison Value = %s' % goal_details.get('comparisonValue') 296
323 297 print
324 def print_visit_num_pages_goal_details(self, goal_details): 298 else:
325 """Prints all the Visit Num Pages goal type info.""" 299 print 'No goals found.\n'
326 300
327 print '------ Visit Num Pages Goal -------' 301
328 print 'Comparison Type = %s' % goal_details.get('comparisonType') 302 def print_url_destination_goal_details(goal_details):
329 print 'comparison Value = %s' % goal_details.get('comparisonValue') 303 """Prints all the URL Destination goal type info.
330 304
331 def print_event_goal_details(self, goal_details): 305 Args:
332 """Prints all the Event goal type info.""" 306 goal_details: The details portion of the goal response.
333 307 """
334 print '------ Event Goal -------' 308
335 print 'Use Event Value = %s' % goal_details.get('useEventValue') 309 print '------ Url Destination Goal -------'
336 310 print 'Goal URL = %s' % goal_details.get('url')
337 for event_condition in goal_details.get('eventConditions'): 311 print 'Case Sensitive = %s' % goal_details.get('caseSensitive')
338 event_type = event_condition.get('type') 312 print 'Match Type = %s' % goal_details.get('matchType')
339 print 'Type = %s' % event_type 313 print 'First Step Required = %s' % goal_details.get('firstStepRequired')
340 314
341 if event_type in ('CATEGORY', 'ACTION', 'LABEL'): 315 print '------ Url Destination Goal Steps -------'
342 print 'Match Type = %s' % event_condition.get('matchType') 316 for goal_step in goal_details.get('steps', []):
343 print 'Expression = %s' % event_condition.get('expression') 317 print 'Step Number = %s' % goal_step.get('number')
344 else: # VALUE type. 318 print 'Step Name = %s' % goal_step.get('name')
345 print 'Comparison Type = %s' % event_condition.get('comparisonType') 319 print 'Step URL = %s' % goal_step.get('url')
346 print 'Comparison Value = %s' % event_condition.get('comparisonValue') 320 else:
347 321 print 'No Steps Configured'
348 def print_segments(self, segments_list): 322
349 """Prints all the segment info in the Segments Collection.""" 323
350 324 def print_visit_time_on_site_goal_details(goal_details):
351 print '------ Segments Collection -------' 325 """Prints all the Visit Time On Site goal type info.
352 self.print_pagination_info(segments_list) 326
353 print 327 Args:
354 328 goal_details: The details portion of the goal response.
355 for segment in segments_list.get('items'): 329 """
356 print 'Segment ID = %s' % segment.get('id') 330
357 print 'Kind = %s' % segment.get('kind') 331 print '------ Visit Time On Site Goal -------'
358 print 'Self Link = %s' % segment.get('selfLink') 332 print 'Comparison Type = %s' % goal_details.get('comparisonType')
359 print 'Name = %s' % segment.get('name') 333 print 'comparison Value = %s' % goal_details.get('comparisonValue')
360 print 'Definition = %s' % segment.get('definition') 334
361 print 'Created = %s' % segment.get('created') 335
362 print 'Updated = %s' % segment.get('updated') 336 def print_visit_num_pages_goal_details(goal_details):
363 print 337 """Prints all the Visit Num Pages goal type info.
364 338
365 def print_pagination_info(self, mgmt_list): 339 Args:
366 """Prints common pagination details.""" 340 goal_details: The details portion of the goal response.
367 341 """
368 print 'Items per page = %s' % mgmt_list.get('itemsPerPage') 342
369 print 'Total Results = %s' % mgmt_list.get('totalResults') 343 print '------ Visit Num Pages Goal -------'
370 print 'Start Index = %s' % mgmt_list.get('startIndex') 344 print 'Comparison Type = %s' % goal_details.get('comparisonType')
371 345 print 'comparison Value = %s' % goal_details.get('comparisonValue')
372 # These only have values if other result pages exist. 346
373 if mgmt_list.get('previousLink'): 347
374 print 'Previous Link = %s' % mgmt_list.get('previousLink') 348 def print_event_goal_details(goal_details):
375 if mgmt_list.get('nextLink'): 349 """Prints all the Event goal type info.
376 print 'Next Link = %s' % mgmt_list.get('nextLink') 350
351 Args:
352 goal_details: The details portion of the goal response.
353 """
354
355 print '------ Event Goal -------'
356 print 'Use Event Value = %s' % goal_details.get('useEventValue')
357
358 for event_condition in goal_details.get('eventConditions', []):
359 event_type = event_condition.get('type')
360 print 'Type = %s' % event_type
361
362 if event_type in ('CATEGORY', 'ACTION', 'LABEL'):
363 print 'Match Type = %s' % event_condition.get('matchType')
364 print 'Expression = %s' % event_condition.get('expression')
365 else: # VALUE type.
366 print 'Comparison Type = %s' % event_condition.get('comparisonType')
367 print 'Comparison Value = %s' % event_condition.get('comparisonValue')
368
369
370 def print_segments(segments_response):
371 """Prints all the segment info in the Segments collection.
372
373 Args:
374 segments_response: The response object returned from querying the
375 Segments collection.
376 """
377
378 print '------ Segments Collection -------'
379 print_pagination_info(segments_response)
380 print
381
382 for segment in segments_response.get('items', []):
383 print 'Segment ID = %s' % segment.get('id')
384 print 'Kind = %s' % segment.get('kind')
385 print 'Self Link = %s' % segment.get('selfLink')
386 print 'Name = %s' % segment.get('name')
387 print 'Definition = %s' % segment.get('definition')
388 print 'Created = %s' % segment.get('created')
389 print 'Updated = %s' % segment.get('updated')
390 print
391
392
393 def print_pagination_info(management_response):
394 """Prints common pagination details.
395
396 Args:
397 management_response: The common reponse object for each collection in the
398 Management API.
399 """
400
401 print 'Items per page = %s' % management_response.get('itemsPerPage')
402 print 'Total Results = %s' % management_response.get('totalResults')
403 print 'Start Index = %s' % management_response.get('startIndex')
404
405 # These only have values if other result pages exist.
406 if management_response.get('previousLink'):
407 print 'Previous Link = %s' % management_response.get('previousLink')
408 if management_response.get('nextLink'):
409 print 'Next Link = %s' % management_response.get('nextLink')
377 410
378 411
379 if __name__ == '__main__': 412 if __name__ == '__main__':
380 main(sys.argv) 413 main(sys.argv)
381 414
OLDNEW
« no previous file with comments | « samples/analytics/hello_analytics_api_v3.py ('k') | samples/analytics/sample_utils.py » ('j') | no next file with comments »

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