繁体   English   中英

Google Analytics API-Ruby作为代码库

[英]Google Analytics API - Ruby as code base

下面是Google Analytics API的结果。 这是我正在developers.google.com上运行的查询的输出。

"start-date": "2015-04-01",
  "end-date": "today",
  "dimensions": "ga:source",
  "metrics": [
   "ga:users"
  ],
  "sort": [
   "-ga:users"
  ],

{
   "name": "ga:source",
   "columnType": "DIMENSION",
   "dataType": "STRING"
  },
  {
   "name": "ga:users",
   "columnType": "METRIC",
   "dataType": "INTEGER"
  }
 ],
 "totalsForAllResults": {
  "ga:users": "2201795"
 },
 "rows": [
  [
   "(direct)",
   "869648"
  ],
  [
   "sa",
   "591843"
  ],

我想在我的代码(ruby)中做的是从“行”中提取信息。 我需要引用名称“ sa”和用户计数“ 591843”。

这是我尝试执行的操作。

sa_referral = client.execute(:api_method => analytics.data.ga.get, :parameters => {
    'ids' => "ga:" + saprofileID,
    'dimensions' => "ga:fullreferrer",
    'metrics' => "ga:users",
    'sort' => "-ga:users",
    'filters' => "ga:ga:source!=us-mg6.mail.yahoo.com;ga:source!=sa;ga:source!=(direct);ga:source!=mail.google.com;ga:source!=us-mg5.mail.yahoo.com;ga:source!=SA;ga:source!=m.mg.mail.yahoo.com;ga:source!=mail.aol.com;ga:source!=us-mg4.mail.yahoo.com;ga:source!=yahoo;ga:source!=bing;ga:source!=google;ga:source!=weibo;ga:fullreferrer!=eccie.net/showthread.php",
    'start-date' => startDate,
    'end-date' => endDate,
    })

sa_referral_data = sa_referral.rows do |row|
  rows.map{|r| { referrals:r[0], members:r[1] }}
end
puts sa_referral_data

puts sa_referral_data的结果如下

scheduler caught exception:
undefined method `rows' for #<Google::APIClient::Result:0x000000044651f0>

似乎API根本没有从行中获取信息。 有没有一种方法可以对API进行故障排除,以查看其为什么丢失“行”部分的数据?

代替sa_referral.rows ,使用sa_referral.each

我的项目中的示例代码.....它使用相同的方法来查询google并返回结果。

result = client.execute(
  :api_method => api.users.list,
  :parameters => { 'domain' => domain,
          'orderBy' => 'givenName',
        'maxResults' => 200,
        'pageToken'=> pageToken,
        #'fields' => 'users(id,etag,primaryEmail,name,suspended)',
        'fields' => 'nextPageToken,users(primaryEmail,suspended)',
        'query' => "orgUnitPath=#{orgUnitPath}"
   }
)
#pp result   # There should be results here
#pp result.methods #Displays what commands you can use on the object
returnabledata = Array.new;
result.data.users.each do |user|
  if user["suspended"] == false
    returnabledata << user["primaryEmail"]
  end
end

暂无
暂无

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

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