|
2 | 2 | require 'net/http'
|
3 | 3 | require 'uri'
|
4 | 4 | require 'google/api_client'
|
| 5 | +require 'json' |
5 | 6 |
|
6 | 7 |
|
7 | 8 | get '/' do
|
|
10 | 11 | get '/post' do
|
11 | 12 | # Update these to match your own apps credentials
|
12 | 13 | service_account_email = ENV['SERVICE_ACCOUNT_EMAIL'] # Email of service account
|
13 |
| - profile_id = ENV['PROFILE_ID'] # Analytics profile ID. |
14 | 14 |
|
15 | 15 | # Get the Google API client
|
16 | 16 | 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' |
19 | 19 | )
|
20 | 20 |
|
21 | 21 | key = OpenSSL::PKey::RSA.new(ENV['GOOGLE_API_KEY'].gsub("\\n", "\n"))
|
22 | 22 | 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, |
28 | 28 | )
|
29 | 29 |
|
30 | 30 | # Request a token for our service account
|
31 | 31 | client.authorization.fetch_access_token!
|
32 | 32 |
|
33 | 33 | # 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}、view_id=#{view_id}" |
| 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 | + |
| 68 | + responses.store("#{site}", :result => {:status => res.code, :body => "#{res.body}"}) |
| 69 | + result_status = [result_status, res.code.to_i].max |
| 70 | + end |
56 | 71 | end
|
57 |
| - res = https.request(req) |
58 |
| - status res.code |
59 |
| - headers 'Content-Type' => 'application/json' |
60 |
| - body "#{res.body}" |
61 | 72 | end
|
| 73 | + |
| 74 | + headers 'Content-Type' => 'application/json' |
| 75 | + status result_status |
| 76 | + resp = { |
| 77 | + body: responses, |
| 78 | + } |
| 79 | + resp.to_json |
| 80 | +end |
| 81 | + |
| 82 | +get '/sites' do |
| 83 | + site_and_view_id_json = JSON.parse(ENV['SITE_AND_VIEW_ID_JSON']) |
| 84 | + |
| 85 | + site_and_view_id_json.each do |site_and_view_id| |
| 86 | + site_and_view_id.each do |site,view_id| |
| 87 | + p "site=#{site}, viewId=#{view_id}" |
| 88 | + end |
| 89 | + end |
| 90 | + |
| 91 | + content_type :json |
| 92 | + response = { |
| 93 | + body: site_and_view_id_json, |
| 94 | + } |
| 95 | + response.to_json |
62 | 96 | end
|
0 commit comments