2013-10-04 36 views
3

我正在嘗試使用google-api-ruby-client獲取activeVisitors。該客戶端被列在實時谷歌分析API文檔here中,但是我沒有在關於將其用於實時API的文檔中看到任何內容。Ruby實時谷歌分析API

我看到了函數discovered_api,但是我沒有看到API名稱的posisble參數列表。

用於定期分析API

例子:

# Get the analytics API 
analytics = client.discovered_api('analytics','v3') 

有誰知道如何使用該客戶端來獲取實時的活躍訪客?

這裏是代碼我想使用:

require 'google/api_client' 
require 'date' 

# Update these to match your own apps credentials 
service_account_email = '[email protected]' # Email of service account 
key_file = '/path/to/key/privatekey.p12' # File containing your private key 
key_secret = 'notasecret' # Password to unlock private key 
profileID = '111111111' # Analytics profile ID. 

# Get the Google API client 
client = Google::APIClient.new(:application_name => '[YOUR APPLICATION NAME]', 
:application_version => '0.01') 

# Load your credentials for the service account 
key = Google::APIClient::KeyUtils.load_from_pkcs12(key_file, key_secret) 
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 => service_account_email, 
:signing_key => key) 

# Start the scheduler 
SCHEDULER.every '1m', :first_in => 0 do 

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

# Get the analytics API 
analytics = client.discovered_api('analytics','v3') 

# Execute the query 
visitCount = client.execute(:api_method => analytics.data.ga.get, :parameters => { 
'ids' => "ga:" + profileID, 
'metrics' => "ga:activeVisitors", 
}) 

# Update the dashboard 
send_event('current_visitors', { current: visitCount.data.rows[0][0] }) 
end 

返回錯誤:

Missing required parameters: end-date, start-date. 

回答

5

假設紅寶石客戶端的lib使用發現服務和方法,而不是實際可用,:

visitCount = client.execute(:api_method => analytics.data.ga.get, :parameters => { 
'ids' => "ga:" + profileID, 
'metrics' => "ga:activeVisitors", 

試試這個(變化GA中的api_method到實時):

visitCount = client.execute(:api_method => analytics.data.realtime.get, :parameters => { 
'ids' => "ga:" + profileID, 
'metrics' => "ga:activeVisitors", 
+0

皮特男人!這工作。 – joshmmo

1

如果你的實時報告產品論壇的成員,這個帖子可能是樂於助人 - https://groups.google.com/forum/m/#!topic/google-analytics-realtime-api/zgAsKFBenV8

您可以試試......

分析= client.discove red_api('realtime','v3')

或者是實時的,或者沒有v3。

如果該作品更新你的獲取方法。

希望我能有更多的幫助,但是絕對沒有關於此的文檔。

+0

我已經在測試計劃中被接受。 – joshmmo

+0

仍然沒有運氣嘗試你的建議。一個人如何被邀請加入該Google小組? – joshmmo

+0

當您接受測試版本時應該被邀請。如果您有測試版帳戶,您應該可以請求訪問權限。祝你好運! – Blexy