Hello!For the very begining, sorry for my English. I am from Ukraine, and dont speak english fluent. I want to ask you for help guru developers-programmers.
We have about 250 company accounts in our analitic profile, we need to get from them:
1. Visits(in, out)
2. Goals(achieving)
3. Depth session
All this data need to be get and saved in csv.
All this we need to do useing Python.
Useing manuals tutorials, working demo and other stuff on google site i made api that get top key word and source of visits, get webpropertis of account.
Maybe i dont undrestand truly how to develop web aplications and other, iam just begin to study. So help please. to made such application
Questions:
To get top key word i used this
def get_top_keywords(service, profile_id):
return service.data().ga().get(
ids='ga:' + profile_id,
start_date='2013-03-01',
end_date='2013-03-25',
metrics='ga:visits',
dimensions='ga:source,ga:keyword',
sort='-ga:visits',
filters='ga:medium==organic',
start_index='1',
max_results='25').execute()
what comand i need to use to get Visits Goals depth sessions
I have been looking for some sort of ga api coomand list but not result.
Second: my api shows me only one company instead of 250 that we have in account:
to show i use:
def get_first_profile_id(service):
accounts = service.management().accounts().list().execute()
if accounts.get('items'):
firstAccountId = accounts.get('items')[0].get('id')
webproperties = service.management().webproperties().list(
accountId=firstAccountId).execute()
if webproperties.get('items'):
firstWebpropertyId = webproperties.get('items')[0].get('id')
profiles = service.management().profiles().list(
accountId=firstAccountId,
webPropertyId=firstWebpropertyId).execute()
if profiles.get('items'):
return profiles.get('items')[0].get('id')
return None
what i need to use to make programm show others profile information.
Down is whole code of my api Maybe you can help me some how.
Thank you very much for your attention.
import sys
import sample_utils
from apiclient.errors import HttpError
from oauth2client.client import AccessTokenRefreshError
def main(argv):
sample_utils.process_flags(argv)
service = sample_utils.initialize_service()
try:
first_profile_id = get_first_profile_id(service)
if not first_profile_id:
print 'Could not find a valid profile for this user.'
else:
results = get_top_keywords(service, first_profile_id)
print_results(results)
except TypeError, error:
print ('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:
def get_first_profile_id(service):
accounts = service.management().accounts().list().execute()
if accounts.get('items'):
firstAccountId = accounts.get('items')[0].get('id')
webproperties = service.management().webproperties().list(
accountId=firstAccountId).execute()
if webproperties.get('items'):
firstWebpropertyId = webproperties.get('items')[0].get('id')
profiles = service.management().profiles().list(
accountId=firstAccountId,
webPropertyId=firstWebpropertyId).execute()
if profiles.get('items'):
return profiles.get('items')[0].get('id')
return None
def get_top_keywords(service, profile_id):
return service.data().ga().get(
ids='ga:' + profile_id,
start_date='2013-03-01',
end_date='2013-03-25',
metrics='ga:visits',
dimensions='ga:source,ga:keyword',
sort='-ga:visits',
filters='ga:medium==organic',
start_index='1',
max_results='25').execute()
def print_results(results):
print
print 'Profile Name: %s' % results.get('profileInfo').get('profileName')
print
output = []
for header in results.get('columnHeaders'):
output.append('%30s' % header.get('name'))
print ''.join(output)
if results.get('rows', []):
for row in results.get('rows'):
output = []
for cell in row:
output.append('%30s' % cell)
print ''.join(output)
else:
print 'No Rows Found'
if __name__ == '__main__':
main(sys.argv)