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
- .api_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
- .service_account(scope) ⇒ Object
- .site ⇒ Object
- .user(scope = 'https://www.googleapis.com/auth/analytics.readonly') ⇒ Object
Class Method Details
.api_client(scope) ⇒ Object
80 81 82 83 84 85 86 87 88 |
# File 'app/models/spotlight/analytics/ga.rb', line 80 def self.api_client(scope) require 'google/api_client' client = Google::APIClient.new( application_name: 'spotlight', application_version: Spotlight::VERSION ) client. = service_account(scope). client end |
.context(exhibit) ⇒ Object
54 55 56 57 58 59 60 |
# File 'app/models/spotlight/analytics/ga.rb', line 54 def self.context(exhibit) if exhibit.is_a? Spotlight::Exhibit for_exhibit(exhibit) else path(exhibit) end end |
.enabled? ⇒ Boolean
14 15 16 |
# File 'app/models/spotlight/analytics/ga.rb', line 14 def self.enabled? user && site end |
.exhibit_data(exhibit, options) ⇒ Object
37 38 39 |
# File 'app/models/spotlight/analytics/ga.rb', line 37 def self.exhibit_data(exhibit, ) context(exhibit).results(site, Spotlight::Engine.config..merge()).to_a.first || exhibit_data_unavailable end |
.exhibit_data_unavailable ⇒ Object
41 42 43 |
# File 'app/models/spotlight/analytics/ga.rb', line 41 def self.exhibit_data_unavailable OpenStruct.new(pageviews: 'n/a', users: 'n/a', sessions: 'n/a') end |
.for_exhibit(exhibit) ⇒ Object
18 19 20 |
# File 'app/models/spotlight/analytics/ga.rb', line 18 def self.for_exhibit(exhibit) path(Spotlight::Engine.routes.url_helpers.exhibit_path(exhibit)) end |
.oauth_client ⇒ Object
68 69 70 71 |
# File 'app/models/spotlight/analytics/ga.rb', line 68 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
62 63 64 65 66 |
# File 'app/models/spotlight/analytics/ga.rb', line 62 def self.oauth_token(scope) require 'oauth2' OAuth2::AccessToken.new(oauth_client, api_client(scope)..access_token, expires_in: 1.hour) end |
.page_data(exhibit, options) ⇒ Object
45 46 47 48 49 50 51 52 |
# File 'app/models/spotlight/analytics/ga.rb', line 45 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 |
.service_account(scope) ⇒ Object
73 74 75 76 77 78 |
# File 'app/models/spotlight/analytics/ga.rb', line 73 def self.service_account(scope) @service_account ||= begin oauth_key = Google::APIClient::PKCS12.load_key(Spotlight::Engine.config.ga_pkcs12_key_path, 'notasecret') Google::APIClient::JWTAsserter.new(Spotlight::Engine.config.ga_email, scope, oauth_key) end end |
.site ⇒ Object
33 34 35 |
# File 'app/models/spotlight/analytics/ga.rb', line 33 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
24 25 26 27 28 29 30 31 |
# File 'app/models/spotlight/analytics/ga.rb', line 24 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 |