代码之家  ›  专栏  ›  技术社区  ›  joshmmo

Ruby实时谷歌分析API

  •  3
  • joshmmo  · 技术社区  · 11 年前

    我正在尝试使用谷歌api ruby客户端获取activeVisitors。客户端列在实时谷歌分析API文档中 here 然而,我在文档中没有看到任何关于将其用于实时api的内容。

    我看到了函数discover_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 = '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 回复  |  直到 11 年前
        1
  •  5
  •   Pete    11 年前

    假设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",
    
        2
  •  1
  •   Blexy    11 年前

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

    你可以试试。。。

    analytics=客户端发现api('altime','v3')

    或实时,或w/o v3。

    如果有效的话,也更新你的get方法。

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