Jeevan Dongre
Jeevan Dongre

Reputation: 4649

Iterating the Time for 24 hours for 30 days in ruby

I am working on google analytics API using the ruby gem. It accepts the parameters and generates the results, I am fetching the data from the seven metrics. It accepts the start time and end time and generates the data according to the Time we pass.

Here is the sample of that

Visits.results(pro1, {:start_date => Time.now - 86400, :end_date => Time.now})

Which gives me total number of visits for the last 24 hours.

My requirement is little complicated. There are several other metrics also. I need to get the data from the google analytics of previous 30 days. Suppose if I take the dump today it should get me previous 30 days of data. like 28th 27th 26th 25th.... which gets the data of 28th march 2012 00:00:00 to 28th March 2012 59:59:59 , 27th march 2012 00:00:00 to 27th March 2012 59:59:59 and ......I have to trying to achieve this since 2-3 days inruby. I am newbie to ruby. Kindly help me out. TO make the things I need to take data from google analytic s iterating for 24 hours for the past 30 days.

Upvotes: 0

Views: 447

Answers (2)

Soundar Rathinasamy
Soundar Rathinasamy

Reputation: 6728

Try

Visits.results(pro1, {:start_date => Date.today.last_month , :end_date => Date.today})

Upvotes: 0

Shaun
Shaun

Reputation: 549

If you want results for individual days

results = []
30.downto(1) do |n|
  results << Visits.results(pro1, {:start_date => Date.today - n.days, :end_date => Date.today - (n-1).days})
end

or if you want them all at once

Visits.results(pro1, {:start_date => Date.today - 30.days, :end_date => Date.today})

If you want to include today as well then set the end date to Date.tomorrow

Upvotes: 1

Related Questions