3

我正在尝试使用 google-api-ruby-client 获取 activeVisitor。该客户端列在此处的实时谷歌分析 API 文档中,但是我在文档中看不到任何关于将其用于实时 api 的信息。

我看到了函数 found_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.
4

2 回答 2

5

Assuming that the ruby client lib uses the discovery service and the method is actually available, instead of:

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

try this (change ga to realtime in the api_method):

visitCount = client.execute(:api_method => analytics.data.realtime.get, :parameters => {
'ids' => "ga:" + profileID,
'metrics' => "ga:activeVisitors",
于 2013-10-08T06:06:01.990 回答
1

如果您是实时报告产品论坛的成员,这篇文章可能会有所帮助 - https://groups.google.com/forum/m/#!topic/google-analytics-realtime-api/zgAsKFBenV8

你可以试试...

分析 = client.discovered_api('realtime','v3')

或实时,或不带 v3。

如果可行,也请更新您的 get 方法。

希望我能提供更多帮助,但绝对没有这方面的文档。

于 2013-10-05T04:43:35.190 回答