2012-03-07 98 views
0

在兩個日期之間獲取網頁瀏覽數據我正在開發使用Google Analytics API的報告工具。我正在使用Garb我能夠提取數據。我面臨的挑戰是如何指定start_date和end_date以獲取兩個日期之間的值。使用Google Analytics(分析)使用Garb gem

class Exits 

    extend Garb::Model 

    metrics :visits,:percentNewVisits 
    dimensions :visitorType 
end 


    def registration 
puts "entering the registration method" 
Garb::Session.login("[email protected]", "password") 

profile = Garb::Management::Profile.all.detect {|p| p.web_property_id == 'UA-xxxxxxxx-x'} 
report = Exits.results(profile, {:start_date =>2.day.ago, :end_date =>1.day.ago, :metrics =>[:visits]}) 
#report = Garb::Report.new(profile, {:start_date => 2.day.ago, :end_date => Time.now}) 
puts "entering the loop" 
report = profile.exits() 
    report.each do |re| 
    puts re 
#puts "#{report.visits}" 
#puts "#{profile.percent_new_visits}" 
#puts "#{profile.visitorType}" 
    end 

end 

請幫助抓取頁面視圖和兩個日期之間的任何其他指標。

回答

2

試試這個:

profile = Garb::Management::Profile.all.detect {|p| p.web_property_id == web_profile} 
    options = { 
        :start_date  => (Date.today - 2), 
        :end_date  => (Date.today - 1), 
       } 

    result = Report.results(profile, options) 
0

在自述文檔方面:

Other Parameters 
start_date: The date of the period you would like this report to start 
end_date: The date to end, inclusive 
limit: The maximum number of results to be returned 
offset: The starting index 

所以,你可以做你想做的東西:

report = Garb::Report.results(profile, {:start_date => 2.day.ago, :end_date => 1.day.ago}) 
+0

我試了一下它不工作對於我而言,我只需一次就能得到一個月的數據 – 2012-03-07 10:07:28

相關問題