Ruby实时谷歌分析API

Ruby实时谷歌分析API

本文介绍了Ruby实时谷歌分析API的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图用google-api-ruby-client获得activeVisitors。该客户已列在实时Google Analytics(分析)API文档中但是我没有在关于将其用于实时API的文档中看不到任何内容。



我看到函数discovered_api,但是我没有看到API名称的可用参数列表。 p>

常规Analytics API示例:

 #获取analytics API 
analytics = client.discovered_api('analytics','v3')

有谁知道如何使用这个客户端来获得实时活跃的访问者?



以下是我尝试使用的代码:

  require'google / api_client'
require'date'

#更新这些以匹配您自己的应用凭据
service_account_email ='xxxxxxxxxxxxx @ developer .gserviceaccount.com'#服务帐户的电子邮件
key_file ='/path/to/key/privatekey.p12'#Fi le包含您的私钥
key_secret ='notasecret'#解锁私钥的密码$ b $ profileID ='111111111'#Analytics profile ID。

#获取Google API客户端
client = Google :: APIClient.new(:application_name =>'[YOUR APPLICATION NAME]',
:application_version =>' 0.01')

#加载服务帐户的凭证
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 =>密钥)

#启动调度程序
SCHEDULER.every'1m',:first_in => 0 do

#为我们的服务帐户申请一个令牌
client.authorization.fetch_access_token!

#获取分析API
analytics = client.discovered_api('analytics','v3')

#执行查询
visitCount = client .execute(:api_method => analytics.data.ga.get,:parameters => {
'ids'=>ga:+ profileID,
'metrics'=> ga:activeVisitors,
})

#更新仪表板
send_event('current_visitors',{current:visitCount.data.rows [0] [0]})
end

返回错误:

 缺少必需的参数:结束日期,开始日期。 


解决方案

假设ruby客户端lib使用发现服务,该方法实际上是可用的,而不是:

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

试试这个(在api_method中将ga改为实时):

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


I am trying to get activeVisitors with the google-api-ruby-client. The client is listed in the real time google analytics API docs here however I see nothing in the docs about using it for real time api.

I see the function discovered_api however I see no list for posisble parameters for the API name.

Example for Regular Analytics API:

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

Does anyone know how to use this client to get real time active visitors?

Here is the code I am trying to use:

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

Error returned:

Missing required parameters: end-date, start-date.
解决方案

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",

这篇关于Ruby实时谷歌分析API的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-02 01:03