Rails / Google Analyticsã®ãã¼ã¿ã使ã£ã¦åæã管çç»é¢ã®ããã®ã°ã©ããã¤ãã
ã¦ã§ããµã¼ãã¹ãéå¶ããã¨ããã£ã¨ããããã®äººã«ä½¿ã£ã¦ãããããï¼ãã¨ãã¡ããæã訳ã§ããããµã¼ãã¹ã®ã©ããæ¹åããã°ããã®ãåã§æ±ºãã¦ãã¾ãããã«ã¯ãããªã⦠ã£ã¦ãã¨ã§ãåæããæã«ä¾¿å©ãªã°ã©ãåã®ã¡ã¢ã
ãã
æµ·å¤ã«è¡ã人å°ç¨ã®ããã°ãµã¼ãã¹CANPATHãé¡æã§ãã
ãããªã°ã©ãã¤ããã¾ãã
欲ããã®ã¯æ¬¡ã®ãã¼ã¿ã¨ãã¾ãã
- æ°è¦ã«è¨ªãã人
- ç»é²ãã人
- èªåå²ï¼ããã°ï¼ãä½ã£ã人
- ã¹ãã¼ãªã¼ï¼è¨äºï¼ãæ¸ãã人
Google Analytics ã¯ã©ã¤ã¢ã³ã
ãã使ãã¾ãã
google-api-ruby-client
OAuthèªè¨¼ã®ããã«signetã¨ããGemã使ãã¾ãã
1
2
3
#Gemfile
gem 'google-api-client'
gem 'signet'
ä¸æºå
ãã¡ããGoogle Analyticsã®ã¢ã«ã¦ã³ãã¯å¿ é ã
Developers Consoleã§OAuthç¨ã®éµãåå¾ã
Developers Consoleã§æ°è¦ããã¸ã§ã¯ããä½ã£ãããAPIsã«ãã£ã¦ãAnalytics APIããONã«ããã
ãã
Credentials / Create new Client ID / Service account ã¨ã¯ãªãã¯ããã¨P12keyããã¦ã³ãã¼ãã§ãã¾ããP12keyã¯ã¢ããªç´ä¸ã«æ°ãããã£ã¬ã¯ããªãã¤ãã£ã¦ä¿åãã¾ããã
ãããªæã â
Railsã«ã¼ã/certificate/xxxxxxxxxxx.p12
ãã
Service Accountã®è©³ç´°ã«EMAIL ADDRESSãããã®ã§ããããGoogle Analyticsã®ç®¡çè
ã¨ãã¦ç»é²ãã¦ãã ãããããããªãã¨ã¨ã©ã¼ã«ãªãã®ã§ã¡ãã£ã¨ããã£ãã
ãã¼ã¿åå¾ç¨ã®ã¯ã©ã¹
ã©ãã§ãããã¨æãã¾ãããã¨ãã«ãã¼ã¿ãã¼ã¹ã¨ãçµã³ã¤ããªãã®ã§ lib/ga_api.rb ã¨ãããã¡ã¤ã«ãã¤ããã¾ããã
ruby
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
#lib/ga_api.rb
require 'google/api_client'
class GaApi
KEY_FILE = "#{Rails.root}/certificate/xxxxxxxxxxx.p12"
ACCOUNT_EMAIL = "[email protected]"
KEY_SECRET = "xxxxxxxxxxx"
VIEW_ID = "xxxxxxxxxxx"
VERSION = "v3"
CACHED_API_FILE = "#{Rails.root}/certificate/analytics-#{VERSION}.cache"
def initialize
@client = Google::APIClient.new(
application_name: 'xxxxxxxxxxx',
application_name: '1.0'
)
end
# Cache api file to avoide round-trip
def api
analytics = nil
if File.exists? CACHED_API_FILE
File.open(CACHED_API_FILE) do |file|
analytics = Marshal.load(file)
end
else
analytics = @client.discovered_api('analytics', VERSION)
File.open(CACHED_API_FILE, 'w') do |file|
Marshal.dump(analytics, file)
end
end
analytics
end
def signing_key
return if @signing_key
@signing_key = Google::APIClient::KeyUtils.load_from_pkcs12(KEY_FILE, KEY_SECRET)
end
def authorize!
@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: ACCOUNT_EMAIL,
signing_key: signing_key
)
@client.authorization.fetch_access_token!
end
def get_data(options = {})
@client.execute(
api_method: api.data.ga.get,
parameters: {
"ids" => "ga:#{VIEW_ID}",
"start-date" => options[:start_date].to_s,
"end-date" => options[:end_date].to_s,
"metrics" => options[:metrics],
"dimensions" => "ga:year,ga:month,ga:day",
"sort" => "ga:year,ga:month,ga:day"
}
)
end
end
KEY_FILE
Developer Consoleã§åå¾ãããã¡ã¤ã«ã®ä¿åå ´æ
ACCOUNT_EMAIL
Developer Consoleã§åå¾ããEã¡ã¼ã«ã¢ãã¬ã¹
KEY_SECRET
Developer Consoleã§keyãä½æããã¨ä¸åã ã表示ããã¾ã
VIEW_ID
ä¸ã®ç»ååè
VERSION
APIã®ãã¼ã¸ã§ã³ãGoogleã¯v3使ãã¨ãã£ã¦ã¾ãã`
CACHED_API_FILE
ãã£ãã·ã¥ãã¡ã¤ã«ã®ä¿åå ´æï¼certificateã®ä¸ã«å
¥ãã¦ããã®ãæ°æã¡æªãã§ãããã©ãã§ãããã¨æãã®ã§é©å®å¤ãã¦ãã ãããï¼
ãã
VIEW_IDã¯analyticsã®ç®¡çç»é¢ããç¥ãäºãã§ãã¾ããä¸ã®ç»åã®ãããªãã¤ã§ãã
ãã¼ã¿æ´å½¢
管çç¨ã¨ãã¦admins_controller.rbãããã¨æ³å®ãã¾ãã
ruby
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
require 'ga_client'
class AdminsController < ApplicationController
def show
api = GaApi.new
api.authorize!
analytics = api.get_data(
start_data: 1.month.ago(Date.today),
end_data: Date.today,
metrics: "ga:newUsers"
)
analytics = JSON.parse(analytics.response.body)
end
end
ãªãã¦ããã¨ãanalytics["row"]ã®ãªãã«ãã¼ã¿ãå
¥ã£ã¦ã¾ãã
metricsã®ãªã¹ãã¯ã³ã³ã«ããã®ã§ããããã試ãã¨ãããããã¼ã¿ãåå¾ã§ãã¾ããã²ã¨ã¤ããæå®ãã¦ã¾ããããã«ã³ããããã§è¤æ°ã¾ã¨ãã¦åå¾ãããã¨ãå¯è½ã
ä¸ã®ä¾ã§ãanalyticsã«ã¯ã
["2014", "10", "05", "123"]
["2014", "10", "06", "456"]
["2014", "10", "07", "352"]
ãªãã¦ãã¼ã¿ãå
¥ã£ã¦ãã®ã§ããã¨ã¯å¥½ããªããã«æ´å½¢ããã
ä»åã¯Chartkickã¨ããGemã使ãããã®ã§ãããããã«æ´å½¢ãã¾ããã
ãããªæã â
ruby
1
2
3
4
5
6
@new_visitor =
{ "2014-10-05" => 1234 }
{ "2014-10-06" => 1234 }
{ "2014-10-07" => 1234 }
ãã
ä¼å¡ç»é²æ°ã¨ãã¯DBããç´æ¥ã¨ã£ã¦æ´å½¢ã
ç»é²ãç¡ãæ¥ãããã¨ããã®æ¥ã®ãã¼ã¿èªä½ãå
¥ããå°ãã®ã§0ãæ¿å
¥ãã¦ã¾ãã
ruby
1
2
3
4
5
6
7
8
9
start_date = 1.month.ago(Date.today)
end_date = Date.today
@new_signup = User.where(created_at: [start_date..end_date]).group("DATE(created_at)").count
start_date.upto(end_date).each do |date|
@new_signup[date] = 0 unless @new_signup.has_key?(date)
end
@new_signup = @new_signup.map { |key, val| [key.strftime('%Y-%m-%d'), val] }
@new_signup.sort!
ãã
ãã¡ãããããªæãã®ãã¼ã¿ã«ãªãã°ããã¨æãã¾ãã â
ruby
1
2
3
4
5
6
@new_signup =
{ "2014-10-05" => 123 }
{ "2014-10-06" => 123 }
{ "2014-10-07" => 123 }
表示
Chartkick 使ãã¾ãã
1
gem "chartkick"
ããã¯ãã¡ããã¡ãç°¡åã
ã¡ããã¨ãã¼ã¿ãæ´å½¢ããã¦ããã°æ¸¡ãã¦ãããã ããã¹ããã¼ã
erb
1
2
3
4
5
6
7
8
9
<%= javascript_include_tag "//www.google.com/jsapi", "chartkick" %>
<%= line_chart [
{ name: "æ°è¦ã«è¨ªãã", data: @new_visit },
{ name: "ç»é²ãã", data: @new_signup },
{ name: "èªåå²ãä½ã£ã", data: @has_timeline },
{ name: "ã¹ãã¼ãªã¼ãæ¸ãã", data: @has_story }
]
%>
以ä¸ã§ãã
ãã¼ã»ã³ãã¨ãã¯åç´ã«è¨ç®ããã°ããã ããªã®ã§çç¥ã
ãã¼ã¿åæã ãã§ãªããã¦ã¼ã¶ã¼ã®ç®¡çç»é¢ã¨ãã«ãè¯ãããã§ããã
ä»åã¯ä½¿ããªãã£ããã©ãRailscastã«ããã£ã¼ãã®ä½ãæ¹ãããã¤ãããããã§ãã
#223 Charts & Graphs (revised) - RailsCasts
#378 FnordMetric - RailsCasts
#223 Charts - RailsCasts
åè
google-api-ruby-client
google-api-ruby-client-samples/service_account at master · google/google-api-ruby-client-samples
google/signet
Chartkick
Dimensions & Metrics Reference - Google Analytics â Google Developers
Railsããã¸ã§ã¯ãã§ã°ã©ãæç»ã©ã¤ãã©ãªChartkickã使ç¨ããæé
Rails ãµã¼ããã Google Analytics API ã§æ
å ±ãåå¾ããæé ã¼ã¼ google-api-ruby-client, OAuth - bekkou68ã®æ¥è¨
ruby on rails - Getting count of elements by created_at
by day in a given month - Stack Overflow
ruby on rails 3 - Grouping created_at by date only? - Stack Overflow