Google Analytics API

1,594 views

Published on

Published in: Technology, News & Politics
0 Comments
0 Likes
Statistics
Notes
  • Be the first to comment

  • Be the first to like this

No Downloads
Views
Total views
1,594
On SlideShare
0
From Embeds
0
Number of Embeds
52
Actions
Shares
0
Downloads
8
Comments
0
Likes
0
Embeds 0
No embeds

No notes for slide

Google Analytics API

  1. 1. Google Analytics in 6 steps omer@vertica.com
  2. 2. Three steps • Authenticate • Get your profile id • Get data
  3. 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. 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. 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. 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>
  7. 7. Get data “/analytics/feeds/data?ids=ga:%s &dimensions=%s &metrics=%s &sort=%s &start-date=%s &end-date=%s“ Ids = this is your profile Id dims, metrics = 'ga:browser', 'ga:pageviews' sort = 'ga:browser,ga:pageviews' start, end = '2008-07-10', '2008-08-10'
  8. 8. Result <feed><entry> <id>…</id> <updated>2008-08-09T17:00:00.001-07:00</updated> <title type=quot;textquot;>ga:browser=Safari</title> <dxp:dimension name=quot;ga:browserquot; value=quot;Safariquot;/> <dxp:metric name=quot;ga:pageviewsquot; value=quot;1493quot; confidenceInterval=quot;0.0quot; type=quot;integerquot;/> </entry></feed>

×