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 Exhibit 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
81 82 83 84 85 86 87 88 |
# File 'app/models/spotlight/analytics/ga.rb', line 81 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
57 58 59 60 61 62 63 |
# File 'app/models/spotlight/analytics/ga.rb', line 57 def self.context(exhibit) if exhibit.is_a? Spotlight::Exhibit for_exhibit(exhibit) else path(exhibit) end end |
.enabled? ⇒ Boolean
17 18 19 |
# File 'app/models/spotlight/analytics/ga.rb', line 17 def self.enabled? user && site end |
.exhibit_data(exhibit, options) ⇒ Object
40 41 42 |
# File 'app/models/spotlight/analytics/ga.rb', line 40 def self.exhibit_data(exhibit, ) context(exhibit).results(site, Spotlight::Engine.config..merge()).to_a.first || exhibit_data_unavailable end |
.exhibit_data_unavailable ⇒ Object
44 45 46 |
# File 'app/models/spotlight/analytics/ga.rb', line 44 def self.exhibit_data_unavailable OpenStruct.new(pageviews: 'n/a', users: 'n/a', sessions: 'n/a') end |
.for_exhibit(exhibit) ⇒ Object
21 22 23 |
# File 'app/models/spotlight/analytics/ga.rb', line 21 def self.for_exhibit(exhibit) path(Spotlight::Engine.routes.url_helpers.exhibit_path(exhibit)) end |
.oauth_client ⇒ Object
72 73 74 75 |
# File 'app/models/spotlight/analytics/ga.rb', line 72 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
65 66 67 68 69 70 |
# File 'app/models/spotlight/analytics/ga.rb', line 65 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
48 49 50 51 52 53 54 55 |
# File 'app/models/spotlight/analytics/ga.rb', line 48 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
77 78 79 |
# File 'app/models/spotlight/analytics/ga.rb', line 77 def self.signing_key @signing_key ||= OpenSSL::PKCS12.new(File.read(Spotlight::Engine.config.ga_pkcs12_key_path), 'notasecret').key end |
.site ⇒ Object
36 37 38 |
# File 'app/models/spotlight/analytics/ga.rb', line 36 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
27 28 29 30 31 32 33 34 |
# File 'app/models/spotlight/analytics/ga.rb', line 27 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 |