This document outlines the 6 steps to access Google Analytics data through their API:
1. Authenticate by using the ClientLogin API to get an auth token
2. Get your profile ID by making a request to the accounts feed
3. Make requests to the data feed to retrieve analytics data, specifying the profile ID, dimensions, metrics, date range, and other parameters
3. Imports you need
import base64, httplib, urllib, XMLData
• XMLData is a small XML wraper that uses
minidom and creates dict like objects.
• You can use you favorite XML parser
4. Authentication
• ClientLogin is simple and useful for other google services
cl = httplib.HTTPSConnection('www.google.com')
data = urllib.urlencode({
'accountType':'GOOGLE', ‘service’:’analytics’,
'Email':email, 'Passwd':password, 'source':'My Program Name'})
headers = {
'Content-Type':'application/x-www-form-urlencoded',
'Content-Length':'%d' % len(data)}
cl.request(quot;POSTquot;, quot;/accounts/ClientLoginquot;, data, headers)
5. The Auth Token
clr = cl.getresponse()
r = dict([x.split('=') for x in clr.read().split('n') if
x.find('=') != -1])
headers = {'Authorization':'GoogleLogin auth=%s' %
r['Auth']}
Auth=DQAAAIEAAADnyggwWY_9lTKCTVEG0wE…
6. Profiles and ProfileId
ga = httplib.HTTPSConnection('www.google.com')
ga.request(quot;GETquot;, quot;/analytics/feeds/accounts/defaultquot;, headers = headers)
<feed><entry>
<id>…</id>
<updated>2008-11-24T12:08:33.000-08:00</updated>
<title type=quot;textquot;>name of my website</title>
<dxp:tableId>ga:tableId</dxp:tableId>
<dxp:property name=quot;ga:accountIdquot; value=“accountIdquot;/>
<dxp:property name=quot;ga:accountNamequot; value=“accountNamequot;/>
<dxp:property name=quot;ga:profileIdquot; value=“profileIdquot;/>
<dxp:property name=quot;ga:webPropertyIdquot; value=“webPropertyIdquot;/>
</entry></feed>