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
43 44 45 46 47 |
# File 'app/services/sufia/analytics.rb', line 43 def self.profile 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 |
# 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: config['app_name'], application_version: config['app_version']) key = Google::APIClient::PKCS12.load_key(config['privkey_path'], config['privkey_secret']) service_account = Google::APIClient::JWTAsserter.new(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
37 38 39 |
# File 'app/services/sufia/analytics.rb', line 37 def self.user Legato::User.new(token) end |