Class: Spotlight::Analytics::Ga
- Inherits:
-
Object
- Object
- Spotlight::Analytics::Ga
- Extended by:
- Legato::Model
- Defined in:
- app/models/spotlight/analytics/ga.rb
Overview
Google Analytics data provider for the curation dashboard
Class Method Summary collapse
- .auth_client(scope) ⇒ Object
- .context(exhibit) ⇒ Object
- .enabled? ⇒ Boolean
- .exhibit_data(exhibit, options) ⇒ Object
- .exhibit_data_unavailable ⇒ Object
- .for_exhibit(exhibit) ⇒ Object
- .oauth_client ⇒ Object
- .oauth_token(scope) ⇒ Object
- .page_data(exhibit, options) ⇒ Object
- .signing_key ⇒ Object
- .site ⇒ Object
- .user(scope = 'https://www.googleapis.com/auth/analytics.readonly') ⇒ Object
Class Method Details
.auth_client(scope) ⇒ Object
79 80 81 82 83 84 85 86 |
# File 'app/models/spotlight/analytics/ga.rb', line 79 def self.auth_client(scope) Signet::OAuth2::Client.new token_credential_uri: 'https://accounts.google.com/o/oauth2/token', audience: 'https://accounts.google.com/o/oauth2/token', scope: scope, issuer: Spotlight::Engine.config.ga_email, signing_key: signing_key, sub: Spotlight::Engine.config.ga_email end |
.context(exhibit) ⇒ Object
55 56 57 58 59 60 61 |
# File 'app/models/spotlight/analytics/ga.rb', line 55 def self.context(exhibit) if exhibit.is_a? Spotlight::Exhibit for_exhibit(exhibit) else path(exhibit) end end |
.enabled? ⇒ Boolean
15 16 17 |
# File 'app/models/spotlight/analytics/ga.rb', line 15 def self.enabled? user && site end |
.exhibit_data(exhibit, options) ⇒ Object
38 39 40 |
# File 'app/models/spotlight/analytics/ga.rb', line 38 def self.exhibit_data(exhibit, ) context(exhibit).results(site, Spotlight::Engine.config..merge()).to_a.first || exhibit_data_unavailable end |
.exhibit_data_unavailable ⇒ Object
42 43 44 |
# File 'app/models/spotlight/analytics/ga.rb', line 42 def self.exhibit_data_unavailable OpenStruct.new(pageviews: 'n/a', users: 'n/a', sessions: 'n/a') end |
.for_exhibit(exhibit) ⇒ Object
19 20 21 |
# File 'app/models/spotlight/analytics/ga.rb', line 19 def self.for_exhibit(exhibit) path(Spotlight::Engine.routes.url_helpers.exhibit_path(exhibit)) end |
.oauth_client ⇒ Object
70 71 72 73 |
# File 'app/models/spotlight/analytics/ga.rb', line 70 def self.oauth_client OAuth2::Client.new('', '', authorize_url: 'https://accounts.google.com/o/oauth2/auth', token_url: 'https://accounts.google.com/o/oauth2/token') end |
.oauth_token(scope) ⇒ Object
63 64 65 66 67 68 |
# File 'app/models/spotlight/analytics/ga.rb', line 63 def self.oauth_token(scope) require 'oauth2' access_token = auth_client(scope).fetch_access_token! OAuth2::AccessToken.new(oauth_client, access_token['access_token'], expires_in: access_token['expires_in']) end |
.page_data(exhibit, options) ⇒ Object
46 47 48 49 50 51 52 53 |
# File 'app/models/spotlight/analytics/ga.rb', line 46 def self.page_data(exhibit, ) [:sort] ||= '-pageviews' query = context(exhibit).results(site, Spotlight::Engine.config..merge()) query.dimensions << :page_path query.dimensions << :page_title query.to_a end |
.signing_key ⇒ Object
75 76 77 |
# File 'app/models/spotlight/analytics/ga.rb', line 75 def self.signing_key @signing_key ||= OpenSSL::PKCS12.new(File.read(Spotlight::Engine.config.ga_pkcs12_key_path), 'notasecret').key end |
.site ⇒ Object
34 35 36 |
# File 'app/models/spotlight/analytics/ga.rb', line 34 def self.site @site ||= user.accounts.first.profiles.first { |x| x.web_property_id = Spotlight::Engine.config.ga_web_property_id } end |
.user(scope = 'https://www.googleapis.com/auth/analytics.readonly') ⇒ Object
25 26 27 28 29 30 31 32 |
# File 'app/models/spotlight/analytics/ga.rb', line 25 def self.user(scope = 'https://www.googleapis.com/auth/analytics.readonly') @user ||= begin Legato::User.new(oauth_token(scope)) rescue => e Rails.logger.info(e) nil end end |