Pregunta

I would like to access data about particular website thru Google Analytics API. Currently I request many ids before I actually request the data I need:

accounts = service.management().accounts().list().execute()
firstAccountId = accounts.get('items')[0].get('id') # returns 45112345
webproperties = service.management().webproperties().list(accountId=firstAccountId).execute()
firstWebpropertyId = webproperties.get('items')[0].get('id') # returns UA-45112345-1
profiles = service.management().profiles().list(accountId=firstAccountId,webPropertyId=firstWebpropertyId).execute()
profile_id = profiles.get('items')[0].get('id') # returns 78212345

Do I really need to request this ids each time I work with Google Analytics API? Or, can I just hardcode them? Can I hardcode profile id 78212345?

Upd. looking at the doc, seems they could be hardcoded.

¿Fue útil?

Solución

I am no python expert but: Tutorial: Hello Analytics API

def get_results(service, profile_id):
# Use the Analytics Service Object to query the Core Reporting API
return service.data().ga().get(
  ids='ga:' + profile_id,
  start_date='2012-03-03',
  end_date='2012-03-03',
  metrics='ga:visits').execute()

The profile id has to start with a ga: so you should just be able to add your profile id there. You still need to be authenticated but you don't need to go though the Management API to get the profile information.

Oh and after spying on your old question: This should still work even though you are using a service account its the same your still authenticated. ( sorry for spying )

Hope this helps.

Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top