Reputation: 10604
I'm starting looking at Google Analytics core reporting API, which is now in version 3.
According to the documentation, I could use one of the client libraries listed in the link http://code.google.com/apis/analytics/docs/gdata/v3/gdataLibraries.html.
I'm using python, so I was looking for an example of using the core reporting API in python, but I could not find one using this library. None of the examples at http://code.google.com/p/google-api-python-client/wiki/SampleApps include an example of the Core Reporting API.
One other option seems to be using the library at http://code.google.com/p/gdata-python-client/ but I'm not sure this library is using the lastest version of the core reporting API (v3.0).
I'm looking for a python library (with documentation / examples) that is compliant to http://code.google.com/apis/analytics/docs/gdata/v3/reference.html
Thanks
Upvotes: 1
Views: 1664
Reputation: 10604
I did not find any example or good documentation, but I was able to mix general oauth2 authentication with the JAVA example and the python library source code to find an answer. So, here it goes:
Authentication:
from oauth2client.file import Storage
from oauth2client.client import AccessTokenRefreshError
from oauth2client.client import OAuth2WebServerFlow
from oauth2client.tools import run
import httplib2
FLOW = OAuth2WebServerFlow(
client_id=CLIENT_ID,
client_secret=CLIENT_SECRET,
scope='https://www.googleapis.com/auth/analytics.readonly')
storage = Storage('file_name.dat')
credentials = storage.get()
if credentials is None or credentials.invalid:
credentials = run(FLOW, storage)
http = credentials.authorize(httplib2.Http())
Connecting to the Core Reporting API (I'm not sure the verb "connect" is adequate)
from apiclient.discovery import build
service = build('analytics', 'v3', http=http)
Making a query:
query = service.data().ga().get(ids='ga:%d' % PROFILE_ID, start_date=START_DATE, end_date=END_DATE,metrics='ga:pageviews')
results = query.execute()
The full list of parameters to pass to the get method when creating the query can be found at http://api-python-client-doc.appspot.com/analytics/v3/data/ga.
The results come in a python dict exactly as described in http://code.google.com/apis/analytics/docs/gdata/v3/reference.html#data_response
Upvotes: 5