Skip to content

Instantly share code, notes, and snippets.

@ifyouseewendy
Created August 23, 2013 11:29
Show Gist options
  • Save ifyouseewendy/6318303 to your computer and use it in GitHub Desktop.
Save ifyouseewendy/6318303 to your computer and use it in GitHub Desktop.
#!/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