Search code examples
ruby-on-railsrubyruby-on-rails-3rubygemsgarb-gem

To fetch page view data between two dates using in Google Analytics using Garb gem


I am developing a reporting tool using Google Analytics API. I am using Garb I am able to pull the data. The challenge I am facing is how do I specif icy the start_date and end_date to fetch the between two dates.

   class Exits

  extend Garb::Model

  metrics :visits,:percentNewVisits
  dimensions :visitorType
end


  def registration
puts "entering the registration method"  
Garb::Session.login("email@email.com", "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 

end

Kindly help to fetch the page view and any other metrics between two dates.


Solution

  • Try this:

        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)