Import from google analytics data to csv file. Need your help guru developers!

247 views
Skip to first unread message

Андрей Коваль

unread,
Mar 28, 2013, 6:25:30 AM3/28/13
to google-analytics...@googlegroups.com
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.
I have a problem. My boss gave me task import the data from google analytics into a file in order to make it easier to analyze. Iam not a developer, my first meeting with google api useing python was here( https://developers.google.com/analytics/solutions/articles/hello-analytics-api ) I made it its connects to google and show me data in cmd. Its great. 
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)



James Standen

unread,
Mar 28, 2013, 10:14:15 AM3/28/13
to google-analytics...@googlegroups.com
Hello,

Another possibility is to use one of the many tools available in the application gallery to export the data.

The application gallery for Google Analytics is here -  http://www.google.com/analytics/apps/

I am the founder of one of them, Analytics Canvas, which is well suited to pulling data from hundreds of accounts..


We have a 30 day free trial, it may be you can get the file you need using that, and if this is something you need to do regularly long term, see if our company plan works for you.

Regards,

James

Aaron Toledo

unread,
Mar 28, 2013, 11:37:01 PM3/28/13
to google-analytics...@googlegroups.com
For your second issue, have you trie using "~all" as the value for some of the IDs such as webpropertyid and accountid?

Андрей Коваль

unread,
Mar 29, 2013, 6:38:26 AM3/29/13
to google-analytics...@googlegroups.com


пятница, 29 марта 2013 г., 5:37:01 UTC+2 пользователь Aaron Toledo написал:
For your second issue, have you trie using "~all" as the value for some of the IDs such as webpropertyid and accountid?

Do you meen use code like  get_all_profile_id instead of get_first_profile_id ?

I have progress
I create simple application: that show me data that i need from GA, its look like table : ga:customVarValue1  Visits   Goals    PagViewPervisit  Time on site

Then i have next problems and questions:

First MAIN PROBLEM I enter id of company by hand, i can find whou create code that will gave me all ids from profile in cycle and give me all data, mabe ther is such command as get_all_profile_id ?

SEcond If account have no 'ga:customVarValue1 - python shows error There was error construckting your query: None type object is not iterable

And the last 

def get_api_query(service):
 
  return service.data().ga().get(
      ids=TABLE_ID, ----------------------------- entered by hand MAIN PROBLEM
      start_date='2013-03-23',
      end_date='2013-03-25',
      metrics='ga:visits,ga:goalCompletionsAll,ga:timeOnSite,ga:pageviewsPerVisit',
      dimensions='ga:customVarValue1', --------------------- if company dont have such value  program make error, need to do pass this value if ther is no.
      sort='-ga:visits',
      #filters='ga:medium==organic',
      #start_index='1',
      max_results='25')

Down the whole code of my test application

import sys
import sample_utils
import csv


from apiclient.errors import HttpError
from oauth2client.client import AccessTokenRefreshError

TABLE_ID = 'ga:58600442'


def main(argv):
  sample_utils.process_flags(argv)

  # Authenticate and construct service.
  service = sample_utils.initialize_service()

  # Try to make a request to the API. Print the results or handle errors.
  try:
    results = get_api_query(service).execute()
    print_results(results)

  except TypeError, error:
    # Handle errors in constructing a query.

    print ('There was an error in constructing your query : %s' % error)

  except HttpError, error:
    # Handle API errors.

    print ('Arg, there was an API error : %s : %s' %
           (error.resp.status, error._get_reason()))

  except AccessTokenRefreshError:
    # Handle Auth errors.
    print ('The credentials have been revoked or expired, please re-run '
           'the application to re-authorize')


          
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_api_query(service):
 
  return service.data().ga().get(
      ids=TABLE_ID,
      start_date='2013-03-23',
      end_date='2013-03-25',
      metrics='ga:visits,ga:goalCompletionsAll,ga:timeOnSite,ga:pageviewsPerVisit',
      dimensions='ga:customVarValue1',
      sort='-ga:visits',
      #filters='ga:medium==organic',
      #start_index='1',
      max_results='25')


def print_results(results):
 
  print
  print 'Profile Name: %s' % results.get('profileInfo').get('profileName')
  print
  output = []
  for header in results.get('columnHeaders'):
    output.append('%20s' % header.get('name'))
  print ''.join(output)
  if results:

   for row in results.get('rows'):
      output = []
      for cell in row:
        output.append('%20s' % cell)
      print ''.join(output)
  
   print
  else:
    print 'No results found'

Андрей Коваль

unread,
Mar 29, 2013, 10:40:48 AM3/29/13
to google-analytics...@googlegroups.com

I trued what you meen i make like that

if accounts.get('items'):
   all = accounts.get('items')
  for one in all:
    AccountId = one.get('id')
        webproperties = service.management().webproperties().list(accountId=one).execute()

  if webproperties.get('items'):
      firstWebpropertyId = webproperties.get('items')[0].get('id')
      profiles = service.management().profiles().list(accountId=one,webPropertyId=firstWebpropertyId).execute()

there is error invalid account id , maybe i do something wrong in code ?
Reply all
Reply to author
Forward
0 new messages