I’m currently building out a metrics dashboard and I wanted to incorporate Google Analytics data along with my own data. Google maintains a Python client so I figured this would be easy. Grab some OAuth 2 keys and away we go.
Sadly that’s not the case. It took a bit of digging and piecing together numerous articles and blog posts to get everything working.
So here’s how to do it…
1) First you’ll need to login to the Google Developers Console and create a new project. If you’ve already got a project to use, you can skip this step.
2) Next, activate the Google Analytics API for your project.
3) Then create your credentials by creating a new service account.
4) When you create the credentials, you will automatically download a P12 key file (.p12). Save this with your Python application as you’ll be using it shortly.
5) The next step is SUPER important – even though you’ve created API credentials, you still need to give them permission to access your Google Analytics account. Follow the steps outlined here to add a new user to your Google Analytics account, using the email address generated as part of the service account above.
6) Now that we’ve got all of that set up, we’re ready to move over to Python. First step is to install the requirements:
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# this will install the python client along with these others: | |
# httplib2 oauth2client uritemplate pyasn1 pyasn1-modules rsa simplejson | |
pip install google-api-python-client |
7) Since we’re going to use the P12 key, we’ll also need to install pyOpenSSL:
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# first ensure you've got libffi installed | |
# macports | |
port install libffi | |
# homebrew | |
brew install libffi | |
# ubuntu | |
apt-get install libffi-dev | |
# this will install pyOpenSSL along with these others: | |
# cryptography cffi six pycparser | |
# if you encounter the 'flat namespace' error while installing cryptography, see this blog post: | |
# https://chriskief.com/2014/03/25/installing-cryptography-via-pip-with-macports-or-homebrew/ | |
pip install pyOpenSSL |
8) Finally, it’s time for the code…
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import httplib2 | |
from googleapiclient.discovery import build | |
from googleapiclient.http import HttpError | |
from oauth2client.client import SignedJwtAssertionCredentials | |
def get_metrics(): | |
# load the service account key | |
# this key is managed here – https://console.developers.google.com/project | |
filename = 'YOUR-KEY-FILE.p12' | |
f = file(filename, 'rb') | |
key = f.read() | |
f.close() | |
# create the credentials | |
credentials = SignedJwtAssertionCredentials('SERVICE-ACCOUNT-EMAIL-ADDRESS@developer.gserviceaccount.com', key, scope='https://www.googleapis.com/auth/analytics.readonly') | |
# authorize the http instance with these credentials | |
http = httplib2.Http() | |
http = credentials.authorize(http) | |
# construct a resource object for interacting with an api | |
service = build('analytics', 'v3', http=http) | |
# build the query | |
# your profile id can be found by heading to google analytics, selecting your profile, clicking the admin button, | |
# and then clicking view settings under the view column, the id is labelled 'View ID' | |
# you can see the available metrics here: | |
# https://developers.google.com/analytics/devguides/reporting/core/dimsmets | |
api_query = service.data().ga().get( | |
ids='ga:YOUR_PROFILE_ID_NOT_UA', | |
metrics='ga:users, ga:sessions, ga:avgSessionDuration, ga:pageviews, ga:pageviewsPerSession, ga:percentNewSessions, ga:bounceRate', | |
start_date='2014-01-01', | |
end_date='2015-01-01' | |
) | |
# default value | |
metrics = None | |
# run it | |
try: | |
result = api_query.execute() | |
values = result['rows'][0] | |
# covert the average session to minutes and seconds | |
minutes, seconds = divmod(int(float(values[2])), 60) | |
# the order below is the same as the order the metrics were listed above | |
metrics = { | |
'users': values[0], | |
'sessions': values[1], | |
'avg_session': '%d:%02d' % (minutes, seconds), | |
'pageviews': values[3], | |
'pages_session': '%.2f' % float(values[4]), | |
'new_sessions': '%.2f' % float(values[5]) + '%', | |
'returning_sessions': '%.2f' % (100 – float(values[5])) + '%', | |
'bounce_rate': '%.2f' % float(values[6]) + '%' | |
} | |
# handle errors in constructing a query | |
except TypeError, error: | |
print ('There was an error in constructing your query : %s' % error) | |
# handle api service errors | |
except HttpError, error: | |
print ('There was an API error : %s : %s' % (error.resp.status, error._get_reason())) | |
return metrics |
Reblogged this on SutoCom Solutions.
This is a great post, thanks. Wish I had found it before I spent 6 hours banging my head against the wall/wading through Google’s API “documentation”.
I ran into a mysterious/frustrating problem with the p12 key. It worked just fine on one installation, but on another installation I kept getting errors about how pyCrypto could not handle a p12 so I should convert to PEM. I had pyOpenSSL installed via pip, but the oauth call kept trying to use pyCrypto instead.
It turned out that the .14 release of pyOpenSSL is missing a hard dependency – py-cryptography. ‘pip install cryptography’ solved the problem.
At some point, I also hit problems with ‘six’. Run ‘pip upgrade’ on six solved the problem, I think.
Glad you found it helpful.
I couldn’t recall if cryptography was installed automatically along with pyOpenSSL. Even if it is, you may encounter the ‘flat namespace’ error during installation.
The fix for that issue is here: https://chriskief.com/2014/03/25/installing-cryptography-via-pip-with-macports-or-homebrew/