Ruby实时谷歌分析API

3
我正在尝试使用google-api-ruby-client获取activeVisitors。客户端在实时google analytics API文档这里中列出,但是我在文档中没有看到有关将其用于实时API的内容。
我看到了discovered_api函数,但是我没有看到API名称的可能参数列表。
常规分析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 = 'xxxxxxxxxxxxx@developer.gserviceaccount.com' # 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.
2个回答

5
假设 Ruby 客户端库使用了发现服务并且该方法确实可用,那么不需要:
visitCount = client.execute(:api_method => analytics.data.ga.get, :parameters => {
'ids' => "ga:" + profileID,
'metrics' => "ga:activeVisitors",

尝试这个(在api_method中将ga更改为realtime):

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

1

仍然没有运行您的建议的好运。一个人如何被邀请加入那个Google群组? - joshmmo
如果您已被接受为测试版用户,那么您应该已经收到了邀请。如果您确实拥有测试版账户,那么您应该可以请求访问权限。祝您好运! - Blexy

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接