You may or may not have heard that Google has finally opened up their Analytics API to the public. You can read Googles official statement here.
I thought I’d show you all a quick crash introduction on how you can access the API using Ruby.
The following code uses Open-Uri to access the API and Hpricot to parse the response. In this example, we’re going to authenticate with the API, and then we’re going to use it to extract all the keywords (organic and paid) which have brought visitors to our site within the past year (limited to 100 keywords max)
NB: sorry for the shitty formatting. I tried like hell to get this code to display well, but I gave up. Just copy and paste..you’ll be able to sort it out.
require ‘rubygems’
require ‘open-uri’
require ‘hpricot’
USER_EMAIL=”XXXXXX” #Insert your Google Account email address here
USER_PASS=”XXXXXX” #Insert your password here
PROFILE_ID=”XXXXXX” #Insert your profile ID here (Find this in your analytics profile under profile settings)
results = {}
keywords = []
auth_url = “https://www.google.com/accounts/ClientLogin?Email=#{USER_EMAIL}&Passwd=#{USER_PASS}&accountType=GOOGLE&source=open-uri-limbo-v01&service=analytics”
auth_key = (open(auth_url).read).split(”\n”).last
#now query for keywords, you can change the date values as you see fit, as well as the max-results value
response = Hpricot.XML(open(”https://www.google.com/analytics/feeds/data?start-date=2008-10-01&end-date=2009-04-21&dimensions=ga:keyword&max-results=100&ids=ga:#{PROFILE_ID}&prettyprint=true”,
“Authorization” => “GoogleLogin #{auth_key}”).read)
## storing some of the extra information in a “results” hash in case we need it later
results[:total_results] = (response/’openSearch:totalResults’).inner_text
results[:start_index] = (response/’openSearch:startIndex’).inner_text
results[:items_per_page] = (response/’openSearch:itemsPerPage’).inner_text
results[:start_date] = (response/’dxp:startDate’).inner_text
results[:end_date] = (response/’dxp:endDate’).inner_text
results[:aggregates] = (response/’dxp:aggregates’).inner_text
results[:data_source] = (response/’dxp:dataSource’)
results[:entries] = (response/’entry’)
## the entries contain each individual keyword
## for my purposes, I’m not interested in visitors coming from search operators like “site:”
## so we’re scrubbing those out.
for entry in results[:entries]
keyword = (entry/’dxp:dimension’).first[:value]
keywords << keyword unless keyword.include? “site:” or keyword.match(/\(.*\)/)
end
## Now print out our keywords
keywords.each {|kw|
puts kw
}
UPDATE just seconds ago, a new gem was released called Gattica which you can use to access the Google Analytics API. How’s that for up to the minute blogging? Thanks Twitter!

Next time you post code, try using http://pastie.org or http://gist.github.com/