Skip to content

Commit 9c99529

Browse files
committed
[WIP] Get visitors of plural sites
1 parent c9fb357 commit 9c99529

File tree

2 files changed

+78
-38
lines changed

2 files changed

+78
-38
lines changed

app.json

+8-4
Original file line numberDiff line numberDiff line change
@@ -6,14 +6,18 @@
66
"description": "Google Cloud Platform Service Account Email Address.",
77
"value": "[email protected]"
88
},
9-
"PROFILE_ID": {
10-
"description": "Google Analytics Profile ID.",
11-
"value": "12345678"
12-
},
139
"GOOGLE_API_KEY": {
1410
"description": "Google Cloud Platform Service Account's RSA private key.",
1511
"value": "-----BEGIN RSA PRIVATE KEY-----\nxxxx\n-----END RSA PRIVATE KEY-----"
1612
},
13+
"SITE_AND_VIEW_ID_JSON": {
14+
"description": "Website name (Google Analytics embedded) & Google Analytics View ID.",
15+
"value": "[{\"hoge-blog\":12345678},{\"fuga-blog\":23456789}]"
16+
},
17+
"PROFILE_ID": {
18+
"description": "Google Analytics Profile ID.",
19+
"value": "12345678"
20+
},
1721
"WEBSITE_NAME": {
1822
"description": "Website name Google Analytics embedded.",
1923
"value": "hoge-blog"

app.rb

+70-34
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
require 'net/http'
33
require 'uri'
44
require 'google/api_client'
5+
require 'json'
56

67

78
get '/' do
@@ -10,53 +11,88 @@
1011
get '/post' do
1112
# Update these to match your own apps credentials
1213
service_account_email = ENV['SERVICE_ACCOUNT_EMAIL'] # Email of service account
13-
profile_id = ENV['PROFILE_ID'] # Analytics profile ID.
1414

1515
# Get the Google API client
1616
client = Google::APIClient.new(
17-
:application_name => 'current visitor of Google Analytics post to mackerel.io',
18-
:application_version => '0.0.3'
17+
:application_name => 'current visitor of Google Analytics post to mackerel.io',
18+
:application_version => '0.0.4'
1919
)
2020

2121
key = OpenSSL::PKey::RSA.new(ENV['GOOGLE_API_KEY'].gsub("\\n", "\n"))
2222
client.authorization = Signet::OAuth2::Client.new(
23-
:token_credential_uri => 'https://accounts.google.com/o/oauth2/token',
24-
:audience => 'https://accounts.google.com/o/oauth2/token',
25-
:scope => 'https://www.googleapis.com/auth/analytics.readonly',
26-
:issuer => service_account_email,
27-
:signing_key => key,
23+
:token_credential_uri => 'https://accounts.google.com/o/oauth2/token',
24+
:audience => 'https://accounts.google.com/o/oauth2/token',
25+
:scope => 'https://www.googleapis.com/auth/analytics.readonly',
26+
:issuer => service_account_email,
27+
:signing_key => key,
2828
)
2929

3030
# Request a token for our service account
3131
client.authorization.fetch_access_token!
3232

3333
# Get the analytics API
34-
analytics = client.discovered_api('analytics','v3')
35-
36-
# Execute the query, get the value like `[["1"]]`
37-
response = client.execute(:api_method => analytics.data.realtime.get, :parameters => {
38-
'ids' => "ga:" + profile_id,
39-
'metrics' => "ga:activeVisitors",
40-
}).data.rows
41-
42-
number = response.empty? ? 0 : response.first.first.to_i
43-
payload = [ {
44-
name: "current_visitors.#{ENV['WEBSITE_NAME']}",
45-
time: Time.now.to_i,
46-
value: number,
47-
} ].to_json
48-
49-
uri = URI.parse("https://mackerel.io/api/v0/services/#{ENV['MACKEREL_SERVICE_NAME']}/tsdb")
50-
Net::HTTP.new(uri.host, uri.port).tap do |https|
51-
https.use_ssl = true
52-
req = Net::HTTP::Post.new(uri.request_uri).tap do |q|
53-
q['Content-Type'] = 'application/json'
54-
q['X-Api-Key'] = ENV['MACKEREL_API_KEY']
55-
q.body = payload
34+
analytics = client.discovered_api('analytics', 'v3')
35+
36+
# Get visitors of plural sites
37+
site_and_view_id_json = JSON.parse(ENV['SITE_AND_VIEW_ID_JSON'])
38+
39+
responses = {}
40+
result_status = 0
41+
site_and_view_id_json.each do |site_and_view_id|
42+
site_and_view_id.each do |site, view_id|
43+
#p "site=#{site}、viewId=#{viewId}"
44+
45+
# Execute the query, get the value like `[["1"]]`
46+
response = client.execute(:api_method => analytics.data.realtime.get, :parameters => {
47+
'ids' => "ga:" + "#{view_id}", # Analytics view ID.
48+
'metrics' => "ga:activeVisitors",
49+
}).data.rows
50+
51+
number = response.empty? ? 0 : response.first.first.to_i
52+
payload = [{
53+
name: "current_visitors.#{site}",
54+
time: Time.now.to_i,
55+
value: number,
56+
}].to_json
57+
58+
uri = URI.parse("https://mackerel.io/api/v0/services/#{ENV['MACKEREL_SERVICE_NAME']}/tsdb")
59+
Net::HTTP.new(uri.host, uri.port).tap do |https|
60+
https.use_ssl = true
61+
req = Net::HTTP::Post.new(uri.request_uri).tap do |q|
62+
q['Content-Type'] = 'application/json'
63+
q['X-Api-Key'] = ENV['MACKEREL_API_KEY']
64+
q.body = payload
65+
end
66+
res = https.request(req)
67+
#headers 'Content-Type' => 'application/json'
68+
#status res.code
69+
#body "#{res.body}"
70+
responses.store("#{site}", :result => {:status => res.code, :body => "#{res.body}"})
71+
#result_status = [result_status, res.code].max
72+
end
73+
end
74+
end
75+
76+
content_type :json
77+
status result_status
78+
resp = {
79+
body: responses,
80+
}
81+
resp.to_json
82+
end
83+
84+
get '/test' do
85+
site_and_view_id_json = JSON.parse(ENV['SITE_AND_VIEW_ID_JSON'])
86+
87+
site_and_view_id_json.each do |site_and_view_id|
88+
site_and_view_id.each do |site,view_id|
89+
p "site=#{site}、viewId=#{view_id}"
5690
end
57-
res = https.request(req)
58-
status res.code
59-
headers 'Content-Type' => 'application/json'
60-
body "#{res.body}"
6191
end
92+
93+
content_type :json
94+
response = {
95+
body: site_and_view_id_json,
96+
}
97+
response.to_json
6298
end

0 commit comments

Comments
 (0)