Export google analytics data via API with Python


August 4, 2013

Fun weekend hacking project: export google analytics data using the google APIs.

Clone the latest version of the API client from:


there is an example for accessing analytics APIs in the samples/analytics folder,
but you need to fill in client_secrets.json.

You can get the credentials from the APIs console:


In SERVICES: activate google analytics
In API Access: Create a “Client ID for installed applications” choosing “Other” as a platform

Copy the client id and the client secret to client_secrets.json.

Now you only need the profile ID of the google analytics account, it is in the google analytics web interface, just choose the website, then click on Admin, then on the profile name in the profile tab, and then on profile settings.

You can then run:

python core_reporting_v3_reference.py ga:PROFILEID
The first time you run it, it will open a browser for authentication, but then the auth token is saved and used for future requests.

This retrieves from the APIs the visits to the website from search, with keywords and the number of visits, for example for my blog:

Total Metrics For All Results:
This query returned 25 rows.
But the query matched 30 total results.
Here are the metric totals for the matched total results.
Metric Name  = ga:visits
Metric Total = 174
google (not provided) 121
google andrea zonca 17
google butterworth filter python 4
google andrea zonca blog 2
google healpix for ubuntu 2
google healpy install ubuntu 2
google python butterworth filter 2
google zonca andrea 2
google andrea zonca buchrain luzern 1
google andrea zonca it 1
google astrofisica in pillole 1
google bin data healpy 1
google ellipticity fwhm 1
google enthought and healpy 1
google fwhm 1
google healpix apt-get 1
google healpix repository ubuntu 1
google healpix ubuntu 12.04 install 1
google healpy ubuntu 1
google install healpix ubuntu 1
google ipython cluster task output 1
google numpy pink noise 1
google pink noise numpy 1
google python 1/f noise 1
google python apply mixin 1