SimpleAnalytics gem provides accessing Google Analytics Export Api. It uses Version 3.0 of the Google Core Reporting API with JSON. You can find the google documentation here.
Install as a gem from rubygems:
gem install simple_analytics
Authentication using the ClientLogin:
analytics = SimpleAnalytics::Api.authenticate('user@gmail.com', 'password')
The authenticate
method sets an auth_token
in the analytics service object. Then you can fetch the report data:
analytics.fetch('ids' => 'ga:id',
'metrics' => 'ga:visitors',
'dimensions' => 'ga:country',
'start-date' => '2012-01-01',
'end-date' => '2012-01-10')
# => [["United States","24451"], ["Brazil","15616"], ["Spain","3966"]]
The fetch
method sets and returns rows. Required query parameters are used to configure which data to return from the Google Analytics:
- ids — The profile IDs from which to access data.
- start-date — The beginning of the date range.
- end-date — The end of the date range.
- metrics — The numeric values to return.
For more detailed information about the parameters see google REST docs.
For the authentication it uses gem google_client_login based on the ClientLogin. You can also pass extra parameters which allows the gem (see the gem's README):
options = { :accountType => 'GOOGLE', :source => 'company-app-version' }
analytics = SimpleAnalytics::Api.authenticate('user@gmail.com', 'password', options)
You can set yourself the auth_token
to use it in the fetching:
analytics.auth_token = 'your-token-from-oauth'
All parameters are escaped before a request. For date format you can use the next tip: Date.today.strftime("%Y-%m-%d")
.
analytics = SimpleAnalytics::Api.authenticate('user@gmail.com', 'password')
analytics.fetch('ids' => 'ga:id',
'metrics' => 'ga:visitors',
'dimensions' => 'ga:country',
'start-date' => '2012-01-01',
'end-date' => '2012-01-10')
# => [["United States","24451"], ["Brazil","15616"], ["Spain","3966"]]
analytics.rows
# => [["United States","24451"], ["Brazil","15616"], ["Spain","3966"]]
analytics.body
# => returns the parsed response body (hash), where you can get other info, see google docs
analytics.fetch('ids' => 'ga:another-id',
'metrics' => 'ga:visitors,ga:newVisits',
'dimensions' => 'ga:pagePath',
'filters' => 'ga:pagepath=~/articles/[\w-]+\z'
'start-date' => '2012-01-01',
'end-date' => '2012-01-10')
# => [["/articles/first-post","12", "2"], ["/articles/second-post","2", "1"]]
analytics.rows
# => [["/articles/first-post","12", "2"], ["/articles/second-post","2", "1"]]
- Ruby 1.8.7 or later