Module: Sufia::Analytics
- Defined in:
- app/services/sufia/analytics.rb
Class Method Summary collapse
-
.config ⇒ Hash
Loads configuration options from config/analytics.yml.
-
.profile ⇒ Object
Return a Google Analytics profile matching specified ID @ return [Legato::Management::Profile] A user profile associated with GA.
-
.token ⇒ OAuth2::AccessToken
Generate an OAuth2 token for Google Analytics.
-
.user ⇒ Legato::User
Return a user object linked to a Google Analytics account.
Class Method Details
.config ⇒ Hash
Loads configuration options from config/analytics.yml. Expected structure: ‘analytics:` ` app_name: GOOGLE_OAUTH_APP_NAME` ` app_version: GOOGLE_OAUTH_APP_VERSION` ` privkey_path: GOOGLE_OAUTH_PRIVATE_KEY_PATH` ` privkey_secret: GOOGLE_OAUTH_PRIVATE_KEY_SECRET` ` client_email: GOOGLE_OAUTH_CLIENT_EMAIL`
15 16 17 |
# File 'app/services/sufia/analytics.rb', line 15 def self.config @config ||= YAML.load(File.read(File.join(Rails.root, 'config', 'analytics.yml')))['analytics'] end |
.profile ⇒ Object
Return a Google Analytics profile matching specified ID @ return [Legato::Management::Profile] A user profile associated with GA
44 45 46 47 48 |
# File 'app/services/sufia/analytics.rb', line 44 def self.profile self.user.profiles.detect do |profile| profile.web_property_id == Sufia.config.google_analytics_id end end |
.token ⇒ OAuth2::AccessToken
Generate an OAuth2 token for Google Analytics
21 22 23 24 25 26 27 28 29 30 31 32 33 34 |
# File 'app/services/sufia/analytics.rb', line 21 def self.token scope = 'https://www.googleapis.com/auth/analytics.readonly' client = Google::APIClient.new(application_name: self.config['app_name'], application_version: self.config['app_version']) key = Google::APIClient::PKCS12.load_key(self.config['privkey_path'], self.config['privkey_secret']) service_account = Google::APIClient::JWTAsserter.new(self.config['client_email'], scope, key) client. = service_account. oauth_client = OAuth2::Client.new('', '', { authorize_url: 'https://accounts.google.com/o/oauth2/auth', token_url: 'https://accounts.google.com/o/oauth2/token'}) OAuth2::AccessToken.new(oauth_client, client..access_token) end |
.user ⇒ Legato::User
Return a user object linked to a Google Analytics account
38 39 40 |
# File 'app/services/sufia/analytics.rb', line 38 def self.user Legato::User.new(self.token) end |