Skip to content

Instantly share code, notes, and snippets.

@henrik
Created March 12, 2009 16:33
Show Gist options
  • Save henrik/78152 to your computer and use it in GitHub Desktop.
Save henrik/78152 to your computer and use it in GitHub Desktop.
Ruby+Google::Base to get "absolute unique visitors" for a given date range from Google Analytics.
# Get "absolute unique visitors" for a given date range from Google Analytics.
# By Henrik Nyh <http://henrik.nyh.se> 2009-03-12. Public domain.
require 'rubygems'
require 'google/base' # sudo gem install googlebase
require 'date'
FROM = Date.today - 10
UNTO = Date.today
SITE_ID = '12345678'
USERNAME = '[email protected]'
PASSWORD = 'sesame'
qs_from, qs_unto = FROM.strftime('%Y%m%d'), UNTO.strftime('%Y%m%d')
Google::Base.establish_connection(USERNAME, PASSWORD)
csv = Google::Base.get("https://www.google.com/analytics/reporting/export?fmt=2&id=#{SITE_ID}&pdr=#{qs_from}-#{qs_unto}&cmp=average&rpt=VisitorsOverviewReport")
data = csv[/^Day,Visitors(.+?)#/m, 1].scan(/"(.*?)","(.*?)"/).map do |date, count|
date = Date.parse(date)
count = count.gsub(/\D/, '').to_i
[date, count]
end
dates_to_visitors = Hash[*data.flatten] # e.g. {tue, 10 mar 2009=>1234}
p dates_to_visitors
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment