Google Analytics Feeds

Allows access to Google Analytics feeds from Ruby.

Does not support any of the OAuth-related authentication methods, only session login.

It tries to be really simple, returning rows as Hashes rather than specific objects, and provides flexibility as to how you want to make HTTP calls via Faraday.

Tested on 1.9, 1.8 & Rubinus; depends on Ox, which has C dependencies so not tested on JRuby.

Installation

gem install google_analytics_feeds

Usage

require 'google_analytics_feeds'

# Define your report. Reports are built up like an ActiveRecord or
# Sequel::Dataset query.
report = GoogleAnalyticsFeeds::DataFeed.new.
  profile(123456).
  dimensions(:visitor_type).
  metrics(:visits)

# Create a session and login. You can optionally pass a
# Faraday::Connection to +new+ to use a different adapter, deal
# with proxies etc.
session = GoogleAnalyticsFeeds::Session.new
session.login("username", "password")

# Fetch a report. Rows are handled with a
# GoogleAnalyticsFeeds::RowHandler - this may just be an anonymous
# block as demonstrated here.
session.fetch_report(report) do
  def row(r)
    puts r.inspect # => outputs the row as a hash.
  end
end

# Or you can pass a RowHandler class
class StdoutRowHandler < GoogleAnalyticsFeeds::RowHandler
  def row(r)
    puts r.inspect
  end
end

# Either takes the class
session.fetch_report(report, StdoutRowHandler)

# Or an instance (if you needed to pass things to the constructor)
session.fetch_report(report, StdoutRowHandler.new)

For detailed documentation on how to use the Google Analytics Data Feeds, see Google’s documentation: developers.google.com/analytics/devguides/reporting/core/v2/gdataReferenceDataFeed

TODO

Versioning

Patch-level releases will not change the API. Minor releases may make breaking changes to the API until a 1.0 release.

Contributing to google_analytics_feeds

Copyright © 2012 Roland Swingler. See LICENSE.txt for further details.