Tracking Specific Domains in Google Analytics/Google Docs
From the archive — written 17 years ago. About old posts.
Say you want to keep an eye on a given set of referrers in Google Analytics:
[sourcecode language=‘ruby’]
!/usr/bin/ruby
require 'rubygems'
require "google_spreadsheet"
require 'rugalytics'
session = GoogleSpreadsheet.login("GOOGLE USERNAME", "GOOGLE PASSWORD")
Rugalytics.login 'ANALYTICS USERNAME', 'ANALYTICS PASSWORD'
the key is derived from the 'key' param in the URL, e.g.
http://spreadsheets.google.com/ccc?key=THE KEY&hl=en
ws = session.spreadsheet_by_key("SPREADSHEET KEY").worksheets
domains = [ARRAY OF DOMAINS]
start_cell = 2
@profiles = {'ACCOUNT' => 'PROFILE',
'ACCOUNT2' => 'PROFILE2',
}
@profiles.each do | a,r |
sheet_title = a
sheet = ws.find { |s| s.title == sheet_title }
profile = Rugalytics.find_profile(a,r)
i = 12
while i > 0
range_start = Date.today - i.months
range_end = range_start + 1.months - 1.days
report = profile.referring_sources_report :rows=>1000, :from => range_start, :to => range_end
col = sheet.num_cols + 1
report.items.each do |r|
if social_domains.include?(r.source)
row = social_domains.index(r.source) + 2
sheet[1, col] = "{range_start} - {range_end}"
sheet[row, col] = r.visits.to_i
sheet.save
end
end
i = i - 1
end
end
[/sourcecode]
It needs a little setup work up front: You need a Google Spreadsheet, and the first column needs to be populated with the domains you populate the domains array with. If you have more than one profile to track in Analytics, you need to create a worksheet with the name of each profile in your spreadsheet.
I’d rather be using the more full-featured roo to handle the spreadsheet part, but it isn’t playing nice with rugalytics at the moment. I wrote both the authors, so maybe something good will come of that.
#google analytics #ruby #rugalytics #spreadsheet