Class: Spotlight::Analytics::Ga

Inherits:
Object
  • Object
show all
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

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

Returns:

  • (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, options)
  context(exhibit).results(site, Spotlight::Engine.config.ga_analytics_options.merge(options)).to_a.first || exhibit_data_unavailable
end

.exhibit_data_unavailableObject



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_clientObject



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, options)
  options[:sort] ||= '-pageviews'
  query = context(exhibit).results(site, Spotlight::Engine.config.ga_page_analytics_options.merge(options))
  query.dimensions << :page_path
  query.dimensions << :page_title

  query.to_a
end

.signing_keyObject



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

.siteObject



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