Created
December 28, 2009 20:03
-
-
Save mitchellhislop/264889 to your computer and use it in GitHub Desktop.
This file contains 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
require 'rubygems' | |
require 'gattica' | |
class ExtractKeywords | |
def initialize(email,password) | |
@gs = Gattica.new(email,password) | |
end | |
def get_accounts | |
results = [] | |
@gs.accounts.each{|account| | |
profile = {} | |
profile[:site_title] = account.title | |
profile[:profile_id] = account.profile_id ## this is the id required for requests to the API | |
profile[:account_name] = account.account_name | |
profile[:account_id] = account.account_id | |
profile[:web_property_id] = account.web_property_id | |
results << profile | |
} | |
return results | |
end | |
def connect_profile(profile) | |
@gs.profile_id = profile[:profile_id] | |
end | |
def get_keywords(start_date = nil, end_date = Date.today) | |
results = [] | |
response = @gs.get({:start_date => (start_date || (end_date - 365)).to_s, | |
:end_date => end_date.to_s, | |
:dimensions => "keyword", | |
:metrics => "entrances", | |
:sort => "-entrances"}).to_csv.split("\n") | |
response.delete_at(0) | |
response.each{|result| | |
result = result.split(%Q{","}) | |
resulthash = {:keyword => result[3], :hits => result[4]} | |
results << resulthash unless result[0].include? "site:" or result[0].match(/\(.*\)/) | |
} | |
return results | |
end | |
end | |
gs = ExtractKeywords.new("[email protected]","password") | |
accounts = gs.get_accounts | |
#p accounts.last | |
gs.connect_profile(accounts.last) #Im using the last of my many analytics accounts, just for testing | |
keywords = gs.get_keywords | |
keywords.each{|row| | |
p row | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment