Class: GoogleAnalyticsPageViewRanking::GoogleAnalytics

Inherits:
Object
  • Object
show all
Defined in:
lib/google_analytics_page_view_ranking/google_analytics.rb

Defined Under Namespace

Classes: Pageviews

Instance Method Summary collapse

Constructor Details

#initializeGoogleAnalytics

Returns a new instance of GoogleAnalytics.



6
7
8
9
10
11
12
13
# File 'lib/google_analytics_page_view_ranking/google_analytics.rb', line 6

def initialize
  
  set_target_property(Settings.google.analytics_property_id)
  if @profile.nil?
    fail('Failed get target property')
  end
  self
end

Instance Method Details

#garb_loginObject



31
32
33
34
35
36
37
# File 'lib/google_analytics_page_view_ranking/google_analytics.rb', line 31

def 
  if (args = [Settings.google.client_id, Settings.google.client_secret, Settings.google.oauth2_access_token, Settings.google.oauth2_refresh_token]).all?(&:present?)
    (*args)
  else
    fail 'NO AUTH ENV is given'
  end
end

#garb_oauth_login(client_id, client_secret, access_token, refresh_token) ⇒ Object



39
40
41
42
43
44
45
46
47
# File 'lib/google_analytics_page_view_ranking/google_analytics.rb', line 39

def (client_id, client_secret, access_token, refresh_token)
  client = OAuth2::Client.new(
    client_id, client_secret,
    site: 'https://accounts.google.com',
    authorize_url: '/o/oauth2/auth',
    token_url: '/o/oauth2/token'
  )
  Garb::Session.access_token = OAuth2::AccessToken.from_hash(client, access_token: access_token, refresh_token: refresh_token).refresh!
end

#ranking(page_path_regex, start_date, end_date, limit) ⇒ Object



21
22
23
24
25
26
27
28
29
# File 'lib/google_analytics_page_view_ranking/google_analytics.rb', line 21

def ranking(page_path_regex, start_date, end_date, limit)
  @profile.pageviews(
    limit: limit,
    sort: :pageviews.desc,
    start_date: start_date,
    end_datd: end_date,
    filters: { :page_path.contains => page_path_regex }
  )
end

#set_target_property(property_id) ⇒ Object



15
16
17
18
19
# File 'lib/google_analytics_page_view_ranking/google_analytics.rb', line 15

def set_target_property(property_id)
  @profile = Garb::Management::Profile.all.detect do |p|
    p.web_property_id == property_id
  end
end