ºÝºÝߣ

ºÝºÝߣShare a Scribd company logo
Google Analytics in 6 steps



      omer@vertica.com
Three steps
• Authenticate

• Get your profile id

• Get data
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
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)
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…
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>
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'
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>

More Related Content

Google Analytics API

  • 1. Google Analytics in 6 steps omer@vertica.com
  • 2. Three steps • Authenticate • Get your profile id • Get data
  • 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>
  • 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. 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>