Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- from googleapiclient.discovery import build
- from oauth2client.service_account import ServiceAccountCredentials
- from datetime import date, timedelta, datetime
- import csv
- def get_service(api_name, api_version, scopes, key_file_location):
- credentials = ServiceAccountCredentials.from_json_keyfile_name(
- key_file_location, scopes=scopes
- )
- # Build the service object.data
- service = build(api_name, api_version, credentials=credentials)
- return service
- def get_results(service, profile_id):
- # Use the Analytics Service Object to query the Core Reporting API
- # for the number of sessions within the past seven days.
- return service.data().mcf().get(
- ids='ga:' + '201492804', # start_date = '2019-10-13',
- start_date='2019-10-01',
- end_date='2019-10-13',
- metrics='mcf:firstInteractionConversions',
- # dimensions = 'mcf:conversionDate,mcf:source,mcf:sourceMedium,mcf:campaignName,mcf:adwordsAdContent,mcf:basicChannelGrouping',
- dimensions='mcf:sourceMediumPath', # sort = 'mcf:conversionDate',
- filters='mcf:conversionGoalNumber==020',
- max_results='5000'
- ).execute()
- def print_results(results):
- if not results.get('rows', []):
- return
- for row in results.get('rows'):
- res = ','.join([get_value(i) for i in row])
- print(res)
- def get_value(obj: dict):
- if 'nodeValue' in obj:
- return obj['nodeValue']
- elif 'primitiveValue' in obj:
- return obj['primitiveValue']
- elif 'conversionPathValue' in obj:
- return ' > '.join([get_value(i) for i in obj['conversionPathValue']])
- else:
- return ''
- def main():
- # Define the auth scopes to request.
- scope = 'https://www.googleapis.com/auth/analytics.readonly'
- key_file_location = 'C:\\Users\\этикет по Китикету\\Downloads\\BBDO-Digital-Console-1074d9efc275.json'
- # Authenticate and construct service.
- service = get_service(
- api_name='analytics',
- api_version='v3',
- scopes=[scope],
- key_file_location=key_file_location)
- profile_id = '201492804'
- print_results(get_results(service, profile_id))
- if __name__ == '__main__':
- main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement