-
Notifications
You must be signed in to change notification settings - Fork 9
/
jekyll-ga.rb
81 lines (68 loc) · 2.29 KB
/
jekyll-ga.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
# Adapted from https://code.google.com/p/google-api-ruby-client/
require 'jekyll'
require 'jekyll/post'
require 'rubygems'
require 'google/api_client'
require 'chronic'
module Jekyll
class GoogleAnalytics < Generator
safe true
priority :highest
def generate(site)
if !site.config['jekyll_ga']
return
end
ga = site.config['jekyll_ga']
client = Google::APIClient.new()
# Load our credentials for the service account
key = Google::APIClient::KeyUtils.load_from_pkcs12(ga['key_file'], ga['key_secret'])
client.authorization = Signet::OAuth2::Client.new(
:token_credential_uri => 'https://accounts.google.com/o/oauth2/token',
:audience => 'https://accounts.google.com/o/oauth2/token',
:scope => 'https://www.googleapis.com/auth/analytics.readonly',
:issuer => ga['service_account_email'],
:signing_key => key)
# Request a token for our service account
client.authorization.fetch_access_token!
analytics = client.discovered_api('analytics','v3')
params = {
'ids' => ga['profileID'],
'start-date' => Chronic.parse(ga['start']).strftime("%Y-%m-%d"),
'end-date' => Chronic.parse(ga['end']).strftime("%Y-%m-%d"),
'dimensions' => "ga:pagePath",
'metrics' => ga['metric'],
'max-results' => 10000
}
if ga['segment']
params['segment'] = ga['segment']
end
if ga['filters']
params['filters'] = ga['filters']
end
response = client.execute(:api_method => analytics.data.ga.get, :parameters => params)
results = Hash[response.data.rows]
site.posts.each { |post|
url = (site.config['baseurl'] || '') + post.url + '/'
post.data.merge!("_ga" => (results[url]) ? results[url].to_i : 0)
}
end
end
class Jekyll::Post
alias_method :original, :<=>
# Override comparator to first try _ga value
def <=>(other)
if site.config['jekyll_ga']['sort'] != true
return original(other)
end
if self.data['_ga'] && other.data['_ga']
cmp = self.data['_ga'] <=> other.data['_ga']
end
if !cmp || 0 == cmp
cmp = self.date <=> other.date
elsif 0 == cmp
cmp = self.slug <=> other.slug
end
return cmp
end
end
end