Google AnalyticsAPI - Ruby作为代码库

时间:2022-02-23 13:57:17

Below I have the results from google analytics API. This is the output of the query I am running from developers.google.com.

下面我有谷歌分析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"
  ],

What I am trying to do in my code (ruby) is pull out the information from "rows". I need both the referral name "sa" and the user count "591843".

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

Here is how I am attempting to do this.

这是我试图这样做的方式。

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

The result of puts sa_referral_data is the following

puts sa_referral_data的结果如下

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

It seems like the API is not getting the information from rows at all. Is there a way I can troubleshoot the API to see why it is losing the data for the "rows" section?

似乎API根本没有从行获取信息。有没有办法我可以解决API的问题,看看为什么它会丢失“行”部分的数据?

1 个解决方案

#1


instead of sa_referral.rows, use sa_referral.each

而不是sa_referral.rows,使用sa_referral.each

Example code from my project..... It uses the same method to query google and return results.

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

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

#1


instead of sa_referral.rows, use sa_referral.each

而不是sa_referral.rows,使用sa_referral.each

Example code from my project..... It uses the same method to query google and return results.

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

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