class GoogleAnalyticsFeeds::Session

A Google Analytics session, used to retrieve reports. @api public

Constants

CLIENT_LOGIN_URI

@api private

Public Class Methods

new(connection=Faraday.default_connection) click to toggle source

Creates a new session.

Optionally pass a Faraday connection, otherwise uses the default Faraday connection.

# File lib/google_analytics_feeds.rb, line 28
def initialize(connection=Faraday.default_connection)
  @connection = connection
end

Public Instance Methods

fetch_report(report, handler=nil, &block) click to toggle source

Retrieve a report from Google Analytics.

Rows are yielded to a RowHandler, provided either as a class, instance or a block.

# File lib/google_analytics_feeds.rb, line 55
def fetch_report(report, handler=nil, &block)
  handler  = block if handler.nil?
  response = report.retrieve(@token, @connection)

  if response.success?
    DataFeedParser.new(handler).parse_rows(StringIO.new(response.body))
  else
    raise HttpError.new
  end
end
login(username, password) click to toggle source

Log in to Google Analytics with username and password

This should be done before attempting to fetch any reports.

# File lib/google_analytics_feeds.rb, line 35
def login(username, password)
  return @token if @token
  response = @connection.post(CLIENT_LOGIN_URI,
                              'Email'       => username,
                              'Passwd'      => password,
                              'accountType' => 'HOSTED_OR_GOOGLE',
                              'service'     => 'analytics',
                              'source'      => 'ruby-google-analytics-feeds')

  if response.success?
    @token = response.body.match(/^Auth=(.*)$/)[1]
  else
    raise AuthenticationError
  end
end