Skip to content

Instantly share code, notes, and snippets.

@chipoglesby
Last active August 29, 2015 14:19
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save chipoglesby/ae01b7803bfe0d802515 to your computer and use it in GitHub Desktop.
Save chipoglesby/ae01b7803bfe0d802515 to your computer and use it in GitHub Desktop.
Traverse through accounts and return filters for each account
import argparse
import sys
from apiclient.errors import HttpError
from apiclient import sample_tools
from oauth2client.client import AccessTokenRefreshError
def main(argv):
analytics, flags = sample_tools.init(
argv, 'analytics', 'v3', __doc__, __file__,
scope='https://www.googleapis.com/auth/analytics.readonly')
try:
traverse_hiearchy(analytics)
except TypeError, error:
print ('Chip, there was an error in constructing your query : %s' % error)
except HttpError, error:
print ('Arg! there was an API error : %s : %s' %
(error.resp.status, error._get_reason()))
except AccessTokenRefreshError:
print ('Chip, the credentials have been revoked or expired, please re-run'
'the application to re-authorize')
def traverse_hiearchy(analytics):
accounts = analytics.management().accounts().list().execute()
if accounts.get('items'):
firstAccountId = accounts.get('items')[0].get('id')
filters = analytics.management().filters().list(
accountId=firstAccountId).execute()
print_filters(filters)
def print_filters(filters_response):
print '------ List of Filters -------'
print_pagination_info(filters_response)
print
for filter in filters_response.get('items', []):
print 'Account Id = %s' % filter.get('accountId')
print 'Filter Id = %s' % filter.get('id')
print 'Filter Kind = %s' % filter.get('kind')
print 'Filter Name = %s' % filter.get('name')
print 'Filter Created = %s' % filter.get('created')
print 'Filter Updated = %s' % filter.get('updated')
# Get the filter type.
filterType = filter.get('type')
print 'Filter Type = %s' % filterType
# Print the properties if the filter is of type "EXCLUDE".
if filterType == 'EXCLUDE':
details = filter.get('excludeDetails', {})
print 'Exclude field = %s' % details.get('field')
print 'Exclude match type = %s' % details.get('matchType')
print 'Exclude expression value = %s' % details.get('expressionValue')
print 'Exclude case sensitive = %s' % details.get('caseSensitive')
print
# Print the properties if the filter is of type 'INCLUDE'.
if filterType == 'INCLUDE':
details = filter.get('includeDetails', {})
print 'Include field = %s' % details.get('field')
print 'Include match type = %s' % details.get('matchType')
print 'Include expression value = %s' % details.get('expressionValue')
print 'Include case sensitive = %s' % details.get('caseSensitive')
print
# Print the properties if the filter is of type 'LOWERCASE'.
if filterType == 'LOWERCASE':
details = filter.get('lowercaseDetails', {})
print 'Lowercase field = %s' % details.get('field')
print
# Print the properties if the filter is of type 'UPPERCASE'.
if filterType == 'UPPERCASE':
details = filter.get('uppercaseDetails', {})
print 'Uppercase field = %s' % details.get('field')
print
# Print the properties if the filter is of type 'SEARCH_AND_REPLACE'.
if filterType == 'SEARCH_AND_REPLACE':
details = filter.get('searchAndReplaceDetails', {})
print 'Search and replace field = %s' % details.get('field')
print 'Search string = %s' % details.get('searchString')
print 'Replace string = %s' % details.get('replaceString')
print 'Search and replace case sensitive = %s' % details.get('caseSensitive')
print
# Print the properties if the filter is of type 'ADVANCED'.
if filterType == 'ADVANCED':
details = filter.get('advancedDetails', {})
print 'Advanced field A = %s' % details.get('fieldA')
print 'Advanced extract A = %s' % details.get('extractA')
print 'Advanced field B = %s' % details.get('fieldB')
print 'Advanced extract B = %s' % details.get('extractB')
print 'Advanced output field = %s' % details.get('outputToField')
print 'Advanced output constructor = %s' % details.get('outputConstructor')
print 'Advanced field A required = %s' % details.get('fieldARequired')
print 'Advanced field B required = %s' % details.get('fieldBRequired')
print 'Advanced override output field = %s' % details.get('overrideOutputField')
print 'Advanced case sensitive = %s' % details.get('caseSensitive')
print
def print_pagination_info(management_response):
"""Prints common pagination details.
Args:
management_response: The common reponse object for each collection in the
Management API.
"""
print 'Items per page = %s' % management_response.get('itemsPerPage')
print 'Total Results = %s' % management_response.get('totalResults')
print 'Start Index = %s' % management_response.get('startIndex')
if management_response.get('previousLink'):
print 'Previous Link = %s' % management_response.get('previousLink')
if management_response.get('nextLink'):
print 'Next Link = %s' % management_response.get('nextLink')
if __name__ == '__main__':
main(sys.argv)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment