2015-05-04 50 views
0

下面我從谷歌分析API的結果。這是我從developers.google.com運行的查詢的輸出。谷歌分析API - 紅寶石作爲代碼基

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

我想在我的代碼(紅寶石)中做的是從「行」中提取信息。我需要推薦人姓名「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 

看跌sa_referral_data的結果是以下

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

這似乎是API完全不從行獲得的信息。有沒有一種方法可以排除API故障,看看它爲什麼會丟失「行」部分的數據?

回答

0

代替sa_referral.rows,使用來自我的項目sa_referral.each

示例代碼.....它使用查詢谷歌和返回結果的方法相同。

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 
+0

當我將其更改爲.each from .rows時,同樣的錯誤消息也適用。 調度程序發生異常: 未定義的方法'each'for# Johnathon22

+0

'pp sa_referral'的結果是什麼 – joegyoung

+0

另外,您是如何生成客戶端對象的?有兩種方法。通過基於用戶的身份驗證或其他身份驗證通過服務器到Google身份驗你在做什麼? – joegyoung