Module: Sufia::Analytics

Defined in:
app/services/sufia/analytics.rb

Class Method Summary collapse

Class Method Details

.configHash

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`

Returns:

  • (Hash)

    A hash containing five keys: ‘app_name’, ‘app_version’, ‘client_email’, ‘privkey_path’, ‘privkey_secret’



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

.profileObject

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

.tokenOAuth2::AccessToken

Generate an OAuth2 token for Google Analytics

Returns:

  • (OAuth2::AccessToken)

    An OAuth2 access token for GA



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'])
   = Google::APIClient::JWTAsserter.new(config['client_email'], scope,
                                                       key)
  client.authorization = .authorize
  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.authorization.access_token)
end

.userLegato::User

Return a user object linked to a Google Analytics account

Returns:

  • (Legato::User)

    A user account wit GA access



37
38
39
# File 'app/services/sufia/analytics.rb', line 37

def self.user
  Legato::User.new(token)
end