Skip to content

Instantly share code, notes, and snippets.

@chadfennell
Last active October 19, 2015 21:17
Show Gist options
  • Select an option

  • Save chadfennell/78b0822028b116fbf35c to your computer and use it in GitHub Desktop.

Select an option

Save chadfennell/78b0822028b116fbf35c to your computer and use it in GitHub Desktop.
require 'googleauth'
require 'google/apis/analytics_v3'
# /via https://github.com/google/google-api-ruby-client/issues/291
class GoogleAnalytics
def self.client
service = new
service.analytics_service
end
def google_api_key
# We /should/ be able to set the GOOGLE_APPLICATION_CREDENTIALS and forget it
# but this doesn't seem to work. So, we pass it in manually.
File.open(ENV['GOOGLE_APPLICATION_CREDENTIALS'], "r")
end
def authorization(scope)
Google::Auth::ServiceAccountCredentials.new(json_key_io: google_api_key, scope: scope)
end
def analytics_service
scope = [
'https://www.googleapis.com/auth/analytics.readonly'
]
client = Google::Apis::AnalyticsV3::AnalyticsService.new
client.authorization = authorization(scope)
client.authorization.fetch_access_token!
client
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment