This repository has been archived by the owner on Apr 4, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 3
/
analytics_interface.rb
102 lines (83 loc) · 2.84 KB
/
analytics_interface.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
require 'oauth'
require 'garb'
require 'active_support/core_ext'
class MainVisits
extend Garb::Model
metrics :pageviews, :unique_pageviews, :avg_time_on_page
dimensions :page_path, :page_title
end
class Overview
extend Garb::Model
metrics :visitors, :new_visits
end
module AnalyticsUtils
def percentage_change(from, to)
change = 100 * (to.to_f - from.to_f) / from.to_f
change = change.round(2)
if change > 0
"+#{change}"
else
change.to_s
end
end
end
class AnalyticsInterface
attr_accessor :profile, :property
def initialize(analytics_id, oauth_token, oauth_secret)
establish_garb_session(oauth_token, oauth_secret)
self.property = Garb::Management::WebProperty.all.detect { |p| p.id == analytics_id }
self.profile = Garb::Management::Profile.all.detect { |p| p.web_property_id == analytics_id }
end
def weekly_stats
yesterday = Date.yesterday
start_of_the_week = yesterday - 7.days
start_of_previous_week = start_of_the_week - 7.days
first_period = overview(start_of_the_week, yesterday)
second_period = overview(start_of_previous_week, start_of_the_week)
describe_period(first_period, second_period, "this week", "last week")
end
def daily_stats
yesterday = Date.yesterday
day_before_yesterday = Date.yesterday - 1.day
first_period = overview(yesterday, yesterday)
second_period = overview(day_before_yesterday, day_before_yesterday)
describe_period(first_period, second_period, "yesterday", "the day before")
end
def main_visits_data
profile.main_visits(
:start_date => Date.yesterday,
:end_date => Date.yesterday,
:limit => 10,
:sort => :unique_pageviews.desc
)
end
private
include AnalyticsUtils
def overview(start_date, end_date)
profile.overview(
:filters => { :page_path.does_not_contain => 'admin' },
:start_date => start_date,
:end_date => end_date
).first
end
def describe_period(current_period, previous_period, this_kind, last_kind)
{
visitors: current_period.visitors,
visitors_change: percentage_change(previous_period.visitors, current_period.visitors),
new_visits: current_period.new_visits,
new_visits_change: percentage_change(previous_period.new_visits, current_period.new_visits)
}
end
def build_oauth_token(oauth_token, oauth_secret)
consumer = OAuth::Consumer.new('anonymous', 'anonymous', {
:site => 'https://www.google.com',
:request_token_path => '/accounts/OAuthGetRequestToken',
:access_token_path => '/accounts/OAuthGetAccessToken',
:authorize_path => '/accounts/OAuthAuthorizeToken'
})
OAuth::AccessToken.new(consumer, oauth_token, oauth_secret)
end
def establish_garb_session(oauth_token, oauth_secret)
Garb::Session.access_token = build_oauth_token(oauth_token, oauth_secret)
end
end