简体   繁体   English

红宝石:如何通过Google Analytics(分析)获取对页面的计数

[英]ruby: how to get count vistor a page with google analytics

i want to count visitor by page 我想按页面统计访问者

opts = YAML.load_file("ga_config.yml")

## Update these to match your own apps credentials in the ga_config.yml file
service_account_email = opts['service_account_email']  # Email of service account
key_file = opts['key_file']                            # File containing your private key
key_secret = opts['key_secret']                        # Password to unlock private key
profile_id = opts['profileID'].to_s                    # Analytics profile ID.


client = Google::APIClient.new(
  :application_name => opts['application_name'],
  :application_version => opts['application_version'])

## Load our credentials for the service account
key = Google::APIClient::KeyUtils.load_from_pkcs12(key_file, key_secret)

visitors = []


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

  # 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
  response = client.execute(:api_method => analytics.data.realtime.get, :parameters => {
    'ids' => "ga:" + profile_id,
    'metrics' => "ga:activeVisitors",
  })

  puts response.data.rows.count

when in run code. 在运行代码中。 response.data.row.count = 0. but i go to https://analytics.google.com/analytics/web/#realtime/rt-content response.data.row.count =0。但是我转到https://analytics.google.com/analytics/web/#realtime/rt-content

in content 在内容上

Right Now : 2 现在:2

apparently there are any mistakes in my code? 显然我的代码中有任何错误? how to fix this? 如何解决这个问题?

and I want to display get visitor by page example: 我想按页面显示获取访问者示例:

ActivePage                         activeUser
/page1                                  1
/page2                                  3

How to get above data ? 如何获得上述数据?

thanks 谢谢

you are requesting data from the Real-time api but you are not using valid real-time api metric. 您正在从实时API请求数据,但未使用有效的实时API指标。 You will also need to add a dimension 您还需要添加一个尺寸

try this 尝试这个

'metrics' => "rt:activeVisitors", 'dimensions' => "rt:pagePath",

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM