Rubyã®ãããã§GoogleAnalyticsã®ãã¼ã¿ãåå¾ããï¼OAuth2å©ç¨ï¼
Garbã使ããªããªã£ãã»ã»ã»
ãã¨ãã¨ã¯Garbã¨ããgemã使ã£ã¦ã¢ã¯ã»ã¹æ°ãåå¾ããããããä½ã£ã¦ããã®ã§ãããæ¥ã«Googleã«ãã°ã¤ã³ãã§ããªããªã£ã¦ãã¾ãã¾ãããã
ããããã¨è¦ã¦ã¿ãã¨ãGoogle APIãOAuth2以å¤ã®èªè¨¼ãåãä»ããªããªã£ã¦ãã¾ã£ã模æ§ã
Garbã§OAuth2ã使ãããã«ãããã試ãã¦ã¿ãã®ã§ãããã¾ãããããGoogle謹製ã®gemã«ä¹ãæãã¾ããã
ä¹ãæããgemã¯ãã¡ã
google/google-api-ruby-client · GitHub
Google Developers Consoleã§éµãã¡ã¤ã«ãåå¾
ã¾ãæºåã¨ãã¦ãGoogle Developers Consoleã«ã¢ã¯ã»ã¹ã https://console.developers.google.com
ããã¸ã§ã¯ããä½æãã¦ããªãå ´åã¯ä½æããã
ããã¸ã§ã¯ããéãããã
ãAPIã¨èªè¨¼ > èªè¨¼æ
å ±
ãéããæ°ããã¯ã©ã¤ã¢ã³ãIDãä½æããã
ã¢ããªã±ã¼ã·ã§ã³ã®ç¨®é¡ã¯ãããµã¼ãã¹ ã¢ã«ã¦ã³ãããé¸æããã
IDãä½æãããããæ°ãã P12 ãã¼ãçæããã¯ãªãã¯ãã¦ãéµãã¡ã¤ã«ããã¦ã³ãã¼ãããã
ä½æããã¯ã©ã¤ã¢ã³ãã®ã¡ã¼ã«ã¢ãã¬ã¹ã«æ¨©éãè¨å®ãã
Analyticsã®ãã¼ã¿ãè¦ããããã«ã権éãè¨å®ãã¾ãã
èªåã¯ãããå¿ãã¦ã ãã¶ãããã¾ããã
Google Analyticsãéãããã¢ããªãã£ã¯ã¹è¨å®ããé¸æãã¾ãã
ã¢ã«ã¦ã³ãâããããã£âãã¥ã¼ ã®é ã«ãåå¾ããããµã¤ãã®é ç®ãé¸æãã¦ããã¾ãã
ãã¥ã¼ã®ä¸ã®ãã¦ã¼ã¶ã¼ç®¡çããã¯ãªãã¯ããGoogle Developers Consoleã®ã¯ã©ã¤ã¢ã³ãIDã®ä¸ã«è¡¨ç¤ºããã¦ããã¡ã¼ã«ã¢ãã¬ã¹ã追å ãã¦ãã ããã
権éã¯ãèªåã¯ç·¨éã¾ã§ã«ãã¾ãããããã£ã¨ä½ãã¦ã大ä¸å¤«ããããã¾ããã
ãã®æããã¥ã¼è¨å®ã®ããã¥ã¼IDããã³ãã£ã¦ããã¾ãã
å¾ã§ä½¿ç¨ãã¾ãã
Gemfileè¨å®
# Gemfile gem 'google-api-client' gem 'signet'
ãè¨å
¥ãã¦ã bundle install
ããã
OAuth2èªè¨¼
client = Google::APIClient.new(:application_name => '') # éµãã¡ã¤ã«ã®å ´æãæå® key = Google::APIClient::PKCS12.load_key(File.join(Rails.root, 'certificates', 'analytics.p12'), 'notasecret') 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', :issuer => '[email protected]', # Google Developers Consoleã«è¡¨ç¤ºããã¦ããã¡ã¼ã«ã¢ãã¬ã¹ãå ¥å :signing_key => key, ) client.authorization.fetch_access_token!
ä»åã¯éµãã¡ã¤ã«ãã RAILS_ROOT/certificates/analytics.p12
ã«é
ç½®ãã¦ãã¾ãã
éµãã¡ã¤ã«ã®å ´æã¯åèªæ¸ãæãã¦ãã ããã
client.authorizationã®ãã©ã¡ã¼ã¿ issuer
ã«ã¯ãå
ç¨æ¨©éã追å ããã¡ã¼ã«ã¢ãã¬ã¹ãå
¥åãã¦ãã ããã
Analytcsãããã¼ã¿ãåå¾ãã
start_date = Date.today - 7 end_date = Date.today - 1 api_method = client.discovered_api('analytics','v3').data.ga.get results = client.execute(:api_method => api_method, :parameters => { 'ids' => 'ga:999999', # ãã¥ã¼IDãè¨å®ãã 'start-date' => start_date.to_s, 'end-date' => end_date.to_s, 'dimensions' => 'ga:pagePath', 'metrics' => 'ga:pageviews' }).data.rows
ãã©ã¡ã¼ã¿ ids
ã«ã¯ã権é追å æã«ã³ãã¼ãã¦ããããã¥ã¼IDãå
¥åãã¾ãã
ãããå®è¡ããã¨ã
results => [["/", 10], ["page1.html", "5"], ["page2.html"], "8"]
ã®ãããªå½¢å¼ã§ãã¼ã¸ãã¥ã¼ãåå¾ã§ãã¾ãã
å ¨ä½ã®ã½ã¼ã¹
require 'google/api_client' require 'google/api_client/client_secrets' require 'google/api_client/auth/installed_app' module Google class Analytics def self.import start_date = Date.today - 7 end_date = Date.today - 1 # OAuth2èªè¨¼ client = Google::APIClient.new(:application_name => '') key = Google::APIClient::PKCS12.load_key(File.join(Rails.root, 'certificates', 'analytics.p12'), 'notasecret') 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', :issuer => '[email protected]', :signing_key => key, ) client.authorization.fetch_access_token! # Analyticsãããã¼ã¿åå¾ api_method = client.discovered_api('analytics','v3').data.ga.get results = client.execute(:api_method => api_method, :parameters => { 'ids' => 'ga:999999', 'start-date' => start_date.to_s, 'end-date' => end_date.to_s, 'dimensions' => 'ga:pagePath', 'metrics' => 'ga:pageviews' }).data.rows # resultsãããã¼ã¿ãåå¾ããå¦ç # ãã» # ãã» # ãã» end end end