Rails ãµã¼ããã Google Analytics API ã§æ å ±ãåå¾ããæé ã¼ã¼ google-api-ruby-client, OAuth
ã¯ããã«
Rails ãµã¼ããã Google Analytics API ããããã¦æ
å ±ãåå¾ããæé ãã¾ã¨ãã¾ãã
google/google-api-ruby-client · GitHub ã使ãã¾ãããã°ã¤ã³ã 㨠Web ã§èªå¯ããã¨ããããï¼å¾è¿°ï¼ã®ã§ OAuth ã使ãã¾ãã
æ¥ã«ã¡ãã¨ã®ã¹ã¯ãªã¼ã³ãã¥ã¼ãåå¾ããã¹ãã¤ã¯ãã¤ããã®ãã´ã¼ã«ã§ãã
OAuth ã«å¿ è¦ãªéµãä¿®å¾
- Google Analytics ã§ä½¿ç¨ããã¢ã«ã¦ã³ã㧠Google Developers Console ã«ã¢ã¯ã»ã¹
- ããã¸ã§ã¯ããä½æ
- ä½æããããã¸ã§ã¯ããã¯ãªãã¯
- APIs & Auth > Credentials ãé¸æ
- Client ID ãæ°è¦ä½æãService account ãé¸æ
- p12 ããã¦ã³ãã¼ã
éµã¯ $RAILS_ROOT/certificates/google_analytics.p12 ã«é
ç½®ããã¨ãã¾ãã
ã©ã¤ãã©ãªãã¤ã³ã¹ãã¼ã«
Gemfile ã«è¿½å ãã¾ãã
gem 'google-api-client' # Google Analytics API ããæ å ±ãåå¾ããããã®ã¯ã©ã¤ã¢ã³ã gem 'signet' # OAuth èªè¨¼
ã¤ã³ã¹ãã¼ã«ãã¾ãã
$ bundle install
ã¯ã©ã¤ã¢ã³ãã®ã¢ãã«ãç¨æ
app/models/ga_client.rb ãæ°è¦ä½æãã¾ãã"EDIT" ã³ã¡ã³ãã®é¨åãã¦ããæ¸ãæãã¦ãã ããã
require 'google/api_client' # Google Analytics Client class GaClient def initialize( application_name: 'My App Google Analytics', # EDIT application_version: '1.0.0' # EDIT ) @client = Google::APIClient.new( application_name: application_name, application_version: application_version ) end def api @api ||= @client.discovered_api('analytics', 'v3') end def signing_key return if @signing_key keyfile = Rails.root.join('certificates', 'google_analytics.p12') # see: http://stackoverflow.com/questions/14525565/not-sufficient-permissions-google-analytics-api-service-account # see: http://stackoverflow.com/questions/11534438/what-is-the-google-api-password-for-the-oauth-pkcs-p12-private-key passphrase = 'notasecret' @signing_key = Google::APIClient::KeyUtils.load_from_pkcs12(keyfile, passphrase) 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: '[email protected]', # EDIT signing_key: signing_key ) @client.authorization.fetch_access_token! end def daily_screen_view(date:, screen_name:) # 管çç»é¢ã® Admin > VIEW > View Settings ã«è¨è¼ããã¦ãã View ID ãæå® ga_id = 'ga:XXX' # EDIT result = @client.execute( api_method: api.data.ga.get, parameters: { ids: ga_id, 'start-date' => date.to_s, 'end-date' => date.to_s, metrics: 'ga:screenviews', filters: "ga:screenName==#{screen_name}", dimensions: 'ga:date', } ) body = JSON.parse(result.response.body) date, screen_view = body['rows'].first screen_view end end
ã¯ã©ã¤ã¢ã³ãã使ã£ã¦æ å ±ãåå¾
å¼ã³åºãå´ãå®è£ ãã¾ããããã¤ãã¼ã¸ãã¨ããã¹ã¯ãªã¼ã³åã®ã¹ã¯ãªã¼ã³ãã¥ã¼ãåå¾ãã¾ãã
client = GaClient.new client.authorize! puts client.daily_screen_view(date: Date.today, screen_name: 'ãã¤ãã¼ã¸') #=> 1024
ä»æ¥ã®ã¹ã¯ãªã¼ã³ãã¥ã¼ã¯ 1024 ã ã¨ãããã¾ããï¼
ä»ã®å¤ãã¨ããã
以ä¸ãåç §ãã¦ãã ããã
åèæç®
- Google Analytics API ã«ããã OAuth ã®å ¨ä½å
- OAuth ã® playground: OAuth 2.0 Playground
ç´ä½æ²æ
æ å ±ãåå¾ããã¾ã§ã«ããããã¨è©¦è¡é¯èª¤ãã¾ããããã®ãã°ãæ®ãã¾ãã
- Sija/garb · GitHub ãè¦ã¤ãã
- ãã°ã¤ã³ï¼ã¡ã¼ã«ã¢ãã¬ã¹ã¨ãã¹ã¯ã¼ãã§èªè¨¼ï¼ã§ãã£ã¦ã¿ãã
- ãã¼ã«ã«ã§åããããµã¼ãã«ãããã¤ãã¦ç¢ºèªãã¦ã¿ãããããâ¦â¦åããªããâ¦â¦
- "Error=BadAuthentication Info=WebLoginRequired" ã¨ã¨ã©ã¼ãåãã¦ãããWebãã°ã¤ã³ãå¿ è¦â¦â¦ï¼ï¼
- æ®æ®µã¤ãããªã端æ«ããã¢ã¯ã»ã¹ããã Google ãä¸å¯©ãªã¢ã¯ãã£ããã£ã¨ãã¦ã¯ããããã
- 端æ«ãã https://accounts.google.com/displayunlockcaptcha ã«ã¢ã¯ã»ã¹ãã¦èªå¯ããã°ããããã
- ã¨ã¯ãããµã¼ããã Webãã©ã¦ã¶ã¯ç«ã¡ãããããªã
- ãããã試ã
- Activity 一覧 ããèªå¯ã§ããªãããªã¨æã£ããã ãªããã ã£ã
- こんなふうに curl で叩いて ã©ãã«ããªããªããã¨æã£ããã©ãã«ããªããªãã£ã
- ã²ã¨ã¾ããã°ã¤ã³ã§ãªã OAuth ã§ãã£ã¦ã¿ãããé·æéããã£ã¦ã解決ããªãããã ã£ãããµã¼ãã« proxy ãç«ã¦ã¦ Webèªè¨¼ããªâ¦â¦ï¼ãããããªãï¼
- garb ã 㨠OAuth ã¤ããã®ããã©ããã ã£ããWeb ãä¸åº¦ä»ããªãã¨ã¢ã¯ã»ã¹ãã¼ã¯ã³ãåå¾ããã®ãã§ããªããã
- ä»ã«ãããããªãããªãomniauth ã¯ããã©ããã ãªã
- PHP にはクライアントあるのかãRuby ã«ããªãããª
- あった!
è¨äºã®ã¯ããã«æ»ãã¾ãã