Created
August 23, 2013 11:29
-
-
Save ifyouseewendy/6318303 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env ruby | |
require 'rubygems' | |
require 'gattica' | |
# Login | |
ga = Gattica.new({ | |
:email => '', | |
:password => '' | |
}) | |
# Get a list of accounts | |
accounts = ga.accounts | |
# Choose the first account | |
ga.profile_id = accounts.last.profile_id | |
puts 'Successful authed...' if !ga.profile_id.nil? | |
COUNT = 150000 | |
STEP = 5000 | |
pos = 1 | |
file = File.new('ga.data1', 'w') | |
while true | |
begin | |
data = ga.get({ | |
:start_date => '2013-08-05', | |
:end_date => '2013-08-11', | |
:dimensions => ['pagePath'], | |
:metrics => ['pageviews', 'avgTimeOnPage'], | |
:sort => ['-pageviews'], | |
:max_results => STEP, | |
:start_index => pos | |
}) | |
data.points.each do |point| | |
path = point.dimensions.first[:pagePath] | |
pageviews = point.metrics.first.values.first | |
avg_time = point.metrics.last.values.first | |
file.puts "#{path}\t#{pageviews}\t#{avg_time}" | |
end | |
puts "got #{(pos/STEP)+1} chunks..." | |
rescue => e | |
puts e.message | |
puts e.backtrace | |
end | |
pos += STEP | |
# break if data.points.count < STEP | |
break if pos > COUNT | |
end | |
file.close |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment