Entries in Rugalytics (1)

Sunday
Feb152009

Adventures in Code: the Google Analytics API

Google Analytics

Google is huge and well-known, and they swallowed the old Urchin code and company to provide web analytics. You can't beat the introductory cost (free, as in beer), and they offer nice graphs and a pretty analytical interface. What's not to like? Well, it's possible that Google doesn't offer every form of analytics you need -- then you need an API, to do your own analytics-synthesis. You need a Google Analytics API...

The API

What API? It's easy to be glib and say No Google Analytics API? Not Problem!, but that's really not a good enough answer. The problem here is that analytics is kind of a sensitive area for Google -- they like to determine just-how-much you know about your website (not more). I'd love a rich interface with REST access, but that's not Google.

Google doesn't even hesitate -- their own web site proclaims their nothingness, API-wise

How We Get There -- First Try: Rugalytics

The best thing I've found so far to break the barrier is Rugalytics, which appears to give a nice Ruby interface to Google Analytics. Ruby is a bit funky as a language, but it is elegant and is terrific for scripting -- I'll have more on the Ruby language in a later post.

I installed it on my Mac, and proceeded to put it through its paces. Here's what I found out...

OK -- code / session time

First lets get get libraries and get started.

JKR-MBP:rugalytics johnrepko$ irb


>> require 'rubygems'; require 'rugalytics'
=> true

Now we log in -- through Rugalytics to Google

>> Rugalytics.login 'jkrepko', '????????'


=> #<Google::Base:0x18503bc @sid="DQAAAHAAAAAd0EaDrxbSRKo6EjFbdUSltb2I1gjSlvDCfQvHVVVIlBJmRui-
rpbnERXvcbt3eYV25YrJScDgK2foNWMcGmoSoVZnLCMYXS8vAUKXJAX5Kttu
MD-pCYOADKRl18aRbxzJAIUnfXpKSaUIUh-gqWdz", @email="jkrepko", @password="?????????">

Let's link to the site info for my techblog:

>> profile = Rugalytics.find_profile('tmn-techblog.com')


=> #<Rugalytics::Profile:0x17dbaa8 @profile_id=15171077, @account_id=7527070, @name="tmn-techblog.com">

... and make a quick grab of pageview information:

>> profile.pageviews :from=>'2009-02-17', :to=>'2009-02-18'


pageviews_graph
=> 43

43 pageviews? Not so many. Let's look by date:

>> report = profile.pageviews_report :from=>'2009-02-17', :to=>'2009-02-18'


pageviews_graph
=> #<Rugalytics::Report:0x17921dc @base_url="tmn-techblog.com", @pageviews_graph=#<Rugalytics::Graph:0x178dc04 
@points_by_day=[[Tue, 17 Feb 2009, 15], [Wed, 18 Feb 2009, 
28]], @name="Pageviews", @to=Wed, 18 Feb 2009, @from=Tue, 
17 Feb 2009, @points=[15, 28]>, name"Pageviews for all 
visitors", end_dateWed, 18 Feb 2009, start_dateTue, 17 Feb 2009

>> report.pageviews_by_day


=> [[Tue, 17 Feb 2009, 15], [Wed, 18 Feb 2009, 28]]

Not a bad start -- let's see what canned reports are available:

>> profile.report_names


=> ["ad_versions_report", "adwords_report", "all_sources_report", "average_pageviews_report",
"bounce_rate_report", "browsers_report", "campaigns_report", "colors_report", 
"content_by_title_report", "content_drilldown_report", "content_report", "dashboard_report", 
"depth_of_visit_report", "direct_sources_report", "entrances_report", "exits_report", "flash_report", 
"geo_map_report", "hostnames_report", "java_report", "keyword_position_report", "keywords_report", 
"languages_report", "length_of_visit_report", "loyalty_report", "networks_report", "os_browsers_report",
 "pageviews_report", "platforms_report", "recency_report", "referring_sources_report", "resolutions_report",
 "search_engines_report", "speeds_report", "time_on_site_report", "top_content_detail_keywords_report", 
"top_content_detail_navigation_report", 
"top_content_detail_path_report", "top_content_detail_sources_report", "top_content_report",
 "traffic_sources_report", "unique_visitors_report", "visitor_types_report", "visitors_overview_report", 
"visits_report"]

Terrific -- let's see what browsers have been used on the blog site

>> report = profile.browsers_report :from => '2009-02-17', :to=>'2009-02-20'
>> for r in report.items
>> print r.visits, ' ', r.browser, '\n'
>> end


17 Safari
7 Internet Explorer
4 Mozilla
3 Firefox

Great - not a bad set -- now let's dig into a report:

>> report = profile.top_content_detail_keywords_report(:url=>'tmn-techblog.com/archives')
items


pageviews_graph
=> #<Rugalytics::Report:0x18756e4 @base_url="tmn-techblog.com", @pageviews_graph=#
<Rugalytics::Graph:0x1863ae8 @points_by_day=[[Mon, 19 Jan 2009, 0], [Tue, 20 Jan 2009, 0], [Wed, 21 Jan 2009, 0], 
[Thu, 22 Jan 2009, 0], [Fri, 23 Jan 2009, 0], [Sat, 24 Jan 2009, 0], [Sun, 25 Jan 2009, 0], [Mon, 26 Jan 2009, 0], 
[Tue, 27 Jan 2009, 0], [Wed, 28 Jan 2009, 0], [Thu, 29 Jan 2009, 0], [Fri, 30 Jan 2009, 0], [Sat, 31 Jan 2009, 0], 
[Sun, 01 Feb 2009, 0], [Mon, 02 Feb 2009, 0], [Tue, 03 Feb 2009, 0], [Wed, 04 Feb 2009, 0], [Thu, 05 Feb 2009, 0], 
[Fri, 06 Feb 2009, 0], [Sat, 07 Feb 2009, 0], [Sun, 08 Feb 2009, 0], [Mon, 09 Feb 2009, 0], [Tue, 10 Feb 2009, 0], 
[Wed, 11 Feb 2009, 0], [Thu, 12 Feb 2009, 0], [Fri, 13 Feb 2009, 0], [Sat, 14 Feb 2009, 0], [Sun, 15 Feb 2009, 0], 
[Mon, 16 Feb 2009, 0], [Tue, 17 Feb 2009, 0], [Wed, 18 Feb 2009, 0], [Thu, 19 Feb 2009, 0]], @name="Pageviews", 
@to=Thu, 19 Feb 2009, @from=Mon, 19 Jan 2009, @points=[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]>, name"Entrance Keywords:", end_dateThu, 19 Feb 2009, items[], 
start_dateMon, 19 Jan 2009

Let's see what the top items this week are:

>> top_items_over_week = report.items.sort_by{|i| i.unique_pageviews.to_i}.reverse


=> [#<Rugalytics::Item:0x16343f8 @path="/", @dollar_index="0.0", @bounce_rate="0.3125", 
@unique_pageviews="16", @pageviews="38", @percentage_exit="0.42105263157894735", 
@avg_time_on_page="186.95454545454547", @url="http://tmn-techblog.com/", @page="/">, #<Rugalytics::Item:0x1632940 
@path="/past/2008/12/16/welcome_to_simplelog_this_is/", @dollar_index="0.0", @bounce_rate="0.0", 
@unique_pageviews="4", @pageviews="4", @percentage_exit="0.0", @avg_time_on_page="8.5", 
@url="http://tmn-techblog.com/past/2008/12/16
/welcome_to_simplelog_this_is/", @page="/past/2008/12/16/welcome_to_simplelog_this_is/">, 
#<Rugalytics::Item:0x1630f00 @path="/past/tags/AWS", @dollar_index="0.0", @bounce_rate="0.0", 
@unique_pageviews="1", @pageviews="1", @percentage_exit="0.0", @avg_time_on_page="617.0", 
@url="http://tmn-techblog.com/past/tags/AWS", @page="/past/tags/AWS">]

Enough for now -- let me run with a bit more data, and I'll dig deeper into the reports and check on performance and data transfer / transformation options.

dynamic language scripting Google analytics Ruby API Rugalytics