Skip to content

Commit 89de82b

Browse files
committed
First commit, 0.1.1
0 parents  commit 89de82b

18 files changed

+667
-0
lines changed

History.txt

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
== 0.1.1 / 2009-04-22
2+
* When outputting as CSV, surround each piece of data with double quotes (appears pretty common for various properties (like Browser name) to contain commas
3+
4+
== 0.1.0 / 2009-03-26
5+
* Basic functionality working good. Can't use filters yet.

LICENSE

+22
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
(The MIT License)
2+
3+
Copyright (c) 2009 Rob Cameron
4+
5+
Permission is hereby granted, free of charge, to any person obtaining
6+
a copy of this software and associated documentation files (the
7+
'Software'), to deal in the Software without restriction, including
8+
without limitation the rights to use, copy, modify, merge, publish,
9+
distribute, sublicense, and/or sell copies of the Software, and to
10+
permit persons to whom the Software is furnished to do so, subject to
11+
the following conditions:
12+
13+
The above copyright notice and this permission notice shall be
14+
included in all copies or substantial portions of the Software.
15+
16+
THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND,
17+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
19+
IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
20+
CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
21+
TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
22+
SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

README.rdoc

+40
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
Gattica is a Ruby library for talking to the Google Analytics API.
2+
3+
= Introduction
4+
There are generally three steps to getting info from the GA API:
5+
6+
1. Authenticate
7+
2. Get a profile id
8+
3. Get the data you really want
9+
10+
= Usage
11+
This library does all three. A typical transaction will look like this:
12+
13+
gs = Gattica.new('[email protected]','password',123456)
14+
results = gs.get({ :start_date => '2008-01-01',
15+
:end_date => '2008-02-01',
16+
:dimensions => 'browser',
17+
:metrics => 'pageviews',
18+
:sort => 'pageviews'})
19+
20+
So we instantiate a copy of Gattica and pass it a Google Account email address and password.
21+
The third parameter is the profile_id that we want to access data for.
22+
23+
Then we call +get+ with the parameters we want to shape our data with. In this case we want
24+
total page views, broken down by browser, from Jan 1 2008 to Feb 1 2008, sorted by page views.
25+
26+
If you don't know the profile_id you want to get data for, call +accounts+
27+
28+
gs = Gattica.new('[email protected]','password')
29+
accounts = gs.accounts
30+
31+
This returns all of the accounts and profiles that the user has access to. Note that if you
32+
use this method to get profiles, you need to manually set the profile before you can call +get+
33+
34+
gs.profile_id = 123456
35+
results = gs.get({ :start_date => '2008-01-01',
36+
:end_date => '2008-02-01',
37+
:dimensions => 'browser',
38+
:metrics => 'pageviews',
39+
:sort => 'pageviews'})
40+

VERSION.yml

+4
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
---
2+
:major: 0
3+
:minor: 1
4+
:patch: 1

examples/example.rb

+23
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
require '../lib/gattica'
2+
3+
# authenticate with the API
4+
ga = Gattica.new('[email protected]','password')
5+
6+
# get the list of accounts you have access to with that username and password
7+
accounts = ga.accounts
8+
9+
# for this example we just use the first account's profile_id, but you'll probably want to look
10+
# at this list and choose the profile_id of the account you want (the web_property_id is the
11+
# property you're most used to seeing in GA, looks like UA123456-1)
12+
ga.profile_id = accounts.first.profile_id
13+
14+
# now get the number of page views by browser for Janurary 2009
15+
# note that as of right now, Gattica does not support filtering
16+
data = ga.get({ :start_date => '2009-01-01',
17+
:end_date => '2009-01-31',
18+
:dimensions => ['browser'],
19+
:metrics => ['pageviews'],
20+
:sort => ['-pageviews'] })
21+
22+
# write the data out as CSV
23+
puts data.to_csv

gattica.gemspec

+36
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
# -*- encoding: utf-8 -*-
2+
3+
Gem::Specification.new do |s|
4+
s.name = %q{gattica}
5+
s.version = "0.1.1"
6+
7+
s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
8+
s.authors = ["Rob Cameron"]
9+
s.date = %q{2009-02-13}
10+
s.description = %q{Gattica is a Ruby library for extracting data from the Google Analytics API.}
11+
s.email = %q{[email protected]}
12+
s.files = ["History.txt", "README.rdoc", "LICENSE", "VERSION.yml", "examples/example.rb", "lib/gattica", "lib/gattica.rb", "lib/gattica/account.rb", "lib/gattica/auth.rb", "lib/gattica/convertible.rb", "lib/gattica/core_extensions.rb", "lib/gattica/data_point.rb", "lib/gattica/data_set.rb", "lib/gattica/exceptions.rb", "lib/gattica/user.rb", "test/fixtures", "test/helper.rb", "test/suite.rb", "test/test_sample.rb"]
13+
s.has_rdoc = true
14+
s.homepage = %q{http://github.com/cannikin/gattica}
15+
s.rdoc_options = ["--inline-source", "--charset=UTF-8"]
16+
s.require_paths = ["lib"]
17+
s.rubyforge_project = %q{gattica}
18+
s.rubygems_version = %q{0.1.1}
19+
s.summary = %q{Gattica is a Ruby library for extracting data from the Google Analytics API.}
20+
21+
#if s.respond_to? :specification_version then
22+
# current_version = Gem::Specification::CURRENT_SPECIFICATION_VERSION
23+
# s.specification_version = 2
24+
#
25+
# if Gem::Version.new(Gem::RubyGemsVersion) >= Gem::Version.new('1.2.0') then
26+
# s.add_runtime_dependency(%q<mime-types>, [">= 1.15"])
27+
# s.add_runtime_dependency(%q<diff-lcs>, [">= 1.1.2"])
28+
# else
29+
# s.add_dependency(%q<mime-types>, [">= 1.15"])
30+
# s.add_dependency(%q<diff-lcs>, [">= 1.1.2"])
31+
# end
32+
#else
33+
# s.add_dependency(%q<mime-types>, [">= 1.15"])
34+
# s.add_dependency(%q<diff-lcs>, [">= 1.1.2"])
35+
#end
36+
end

lib/gattica.rb

+236
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,236 @@
1+
$:.unshift File.dirname(__FILE__) # for use/testing when no gem is installed
2+
3+
# external
4+
require 'net/http'
5+
require 'net/https'
6+
require 'uri'
7+
require 'logger'
8+
require 'rubygems'
9+
require 'hpricot'
10+
11+
# internal
12+
require 'gattica/core_extensions'
13+
require 'gattica/convertible'
14+
require 'gattica/exceptions'
15+
require 'gattica/user'
16+
require 'gattica/auth'
17+
require 'gattica/account'
18+
require 'gattica/data_set'
19+
require 'gattica/data_point'
20+
21+
# Gattica is a Ruby library for talking to the Google Analytics API.
22+
#
23+
# = Introduction
24+
# There are generally three steps to getting info from the GA API:
25+
#
26+
# 1. Authenticate
27+
# 2. Get a profile id
28+
# 3. Get the data you really want
29+
#
30+
# = Usage
31+
# This library does all three. A typical transaction will look like this:
32+
#
33+
# gs = Gattica.new('[email protected]','password',123456)
34+
# results = gs.get({ :start_date => '2008-01-01',
35+
# :end_date => '2008-02-01',
36+
# :dimensions => 'browser',
37+
# :metrics => 'pageviews',
38+
# :sort => 'pageviews'})
39+
#
40+
# So we instantiate a copy of Gattica and pass it a Google Account email address and password.
41+
# The third parameter is the profile_id that we want to access data for. (If you don't know what
42+
# your profile_id is [and you probably don't since GA doesn't tell you except through this API]
43+
# then check out Gattica::Engine#accounts).
44+
#
45+
# Then we call +get+ with the parameters we want to shape our data with. In this case we want
46+
# total page views, broken down by browser, from Jan 1 2008 to Feb 1 2008, sorted by page views.
47+
#
48+
# If you don't know the profile_id you want to get data for, call +accounts+
49+
#
50+
# gs = Gattica.new('[email protected]','password')
51+
# accounts = gs.accounts
52+
#
53+
# This returns all of the accounts and profiles that the user has access to. Note that if you
54+
# use this method to get profiles, you need to manually set the profile before you can call +get+
55+
#
56+
# gs.profile_id = 123456
57+
# results = gs.get({ :start_date => '2008-01-01',
58+
# :end_date => '2008-02-01',
59+
# :dimensions => 'browser',
60+
# :metrics => 'pageviews',
61+
# :sort => 'pageviews'})
62+
63+
64+
module Gattica
65+
66+
VERSION = '0.1.1'
67+
LOGGER = Logger.new(STDOUT)
68+
69+
def self.new(*args)
70+
Engine.new(*args)
71+
end
72+
73+
# The real meat of Gattica, deals with talking to GA, returning and parsing results.
74+
75+
class Engine
76+
77+
SERVER = 'www.google.com'
78+
PORT = 443
79+
SECURE = true
80+
DEFAULT_ARGS = { :start_date => nil, :end_date => nil, :dimensions => [], :metrics => [], :filters => [], :sort => [] }
81+
82+
attr_reader :user, :token
83+
attr_accessor :profile_id
84+
85+
# Create a user, and get them authorized.
86+
# If you're making a web app you're going to want to save the token that's returned by this
87+
# method so that you can use it later (without having to re-authenticate the user each time)
88+
#
89+
# ga = Gattica.new('[email protected]','password',123456)
90+
# ga.token => 'DW9N00wenl23R0...' (really long string)
91+
92+
def initialize(email,password,profile_id=0,debug=false)
93+
LOGGER.datetime_format = '' if LOGGER.respond_to? 'datetime_format'
94+
95+
@profile_id = profile_id
96+
@user_accounts = nil
97+
98+
# save an http connection for everyone to use
99+
@http = Net::HTTP.new(SERVER, PORT)
100+
@http.use_ssl = SECURE
101+
@http.set_debug_output $stdout if debug
102+
103+
# create a user and authenticate them
104+
@user = User.new(email, password)
105+
@auth = Auth.new(@http, user, { :source => 'active-gattica-0.1' }, { 'User-Agent' => 'ruby 1.8.6 (2008-03-03 patchlevel 114) [universal-darwin9.0] Net::HTTP' })
106+
@token = @auth.tokens[:auth]
107+
@headers = { 'Authorization' => "GoogleLogin auth=#{@token}" }
108+
109+
# TODO: check that the user has access to the specified profile and show an error here rather than wait for Google to respond with a message
110+
end
111+
112+
113+
# Returns the list of accounts the user has access to. A user may have multiple accounts on Google Analytics
114+
# and each account may have multiple profiles. You need the profile_id in order to get info from GA. If you
115+
# don't know the profile_id then use this method to get a list of all them. Then set the profile_id of your
116+
# instance and you can make regular calls from then on.
117+
#
118+
# ga = Gattica.new('[email protected]','password')
119+
# ga.get_accounts
120+
# # you parse through the accounts to find the profile_id you need
121+
# ga.profile_id = 12345678
122+
# # now you can perform a regular search, see Gattica::Engine#get
123+
#
124+
# If you pass in a profile id when you instantiate Gattica::Search then you won't need to
125+
# get the accounts and find a profile_id - you apparently already know it!
126+
#
127+
# See Gattica::Engine#get to see how to get some data.
128+
129+
def accounts
130+
# if we haven't retrieved the user's accounts yet, get them now and save them
131+
if @accts.nil?
132+
response, data = @http.get('/analytics/feeds/accounts/default', @headers)
133+
xml = Hpricot(data)
134+
@user_accounts = xml.search(:entry).collect { |entry| Account.new(entry) }
135+
end
136+
return @user_accounts
137+
end
138+
139+
140+
# This is the method that performs the actual request to get data.
141+
#
142+
# == Usage
143+
#
144+
# gs = Gattica.new('[email protected]','password',123456)
145+
# gs.get({ :start_date => '2008-01-01',
146+
# :end_date => '2008-02-01',
147+
# :dimensions => 'browser',
148+
# :metrics => 'pageviews',
149+
# :sort => 'pageviews'})
150+
#
151+
# == Input
152+
#
153+
# When calling +get+ you'll pass in a hash of options. For a description of what these mean to
154+
# Google Analytics, see http://code.google.com/apis/analytics/docs
155+
#
156+
# Required values are:
157+
#
158+
# * +start_date+ => Beginning of the date range to search within
159+
# * +end_date+ => End of the date range to search within
160+
#
161+
# Optional values are:
162+
#
163+
# * +dimensions+ => an array of GA dimensions (without the ga: prefix)
164+
# * +metrics+ => an array of GA metrics (without the ga: prefix)
165+
# * +filter+ => an array of GA dimensions/metrics you want to filter by (without the ga: prefix)
166+
# * +sort+ => an array of GA dimensions/metrics you want to sort by (without the ga: prefix)
167+
#
168+
# == Exceptions
169+
#
170+
# If a user doesn't have access to the +profile_id+ you specified, you'll receive an error.
171+
# Likewise, if you attempt to access a dimension or metric that doesn't exist, you'll get an
172+
# error back from Google Analytics telling you so.
173+
174+
def get(args={})
175+
args = validate_and_clean(DEFAULT_ARGS.merge(args))
176+
query_string = build_query_string(args,@profile_id)
177+
LOGGER.debug(query_string)
178+
response, data = @http.get("/analytics/feeds/data?#{query_string}", @headers)
179+
begin
180+
response.value
181+
rescue Net::HTTPServerException => e
182+
raise GatticaError::AnalyticsError, data.to_s + " (status code: #{e.message})"
183+
end
184+
return DataSet.new(Hpricot.XML(data))
185+
end
186+
187+
188+
private
189+
# Creates a valid query string for GA
190+
def build_query_string(args,profile)
191+
output = "ids=ga:#{profile}&start-date=#{args[:start_date]}&end-date=#{args[:end_date]}"
192+
unless args[:dimensions].empty?
193+
output += '&dimensions=' + args[:dimensions].collect do |dimension|
194+
"ga:#{dimension}"
195+
end.join(',')
196+
end
197+
unless args[:metrics].empty?
198+
output += '&metrics=' + args[:metrics].collect do |metric|
199+
"ga:#{metric}"
200+
end.join(',')
201+
end
202+
unless args[:sort].empty?
203+
output += '&sort=' + args[:sort].collect do |sort|
204+
sort[0..0] == '-' ? "-ga:#{sort[1..-1]}" : "ga:#{sort}" # if the first character is a dash, move it before the ga:
205+
end.join(',')
206+
end
207+
unless args[:filters].empty? # filters are a little more complicated because they can have all kinds of modifiers
208+
209+
end
210+
return output
211+
end
212+
213+
214+
# Validates that the args passed to +get+ are valid
215+
def validate_and_clean(args)
216+
217+
raise GatticaError::MissingStartDate, ':start_date is required' if args[:start_date].nil? || args[:start_date].empty?
218+
raise GatticaError::MissingEndDate, ':end_date is required' if args[:end_date].nil? || args[:end_date].empty?
219+
raise GatticaError::TooManyDimensions, 'You can only have a maximum of 7 dimensions' if args[:dimensions] && (args[:dimensions].is_a?(Array) && args[:dimensions].length > 7)
220+
raise GatticaError::TooManyMetrics, 'You can only have a maximum of 10 metrics' if args[:metrics] && (args[:metrics].is_a?(Array) && args[:metrics].length > 10)
221+
222+
# make sure that the user is only trying to sort fields that they've previously included with dimensions and metrics
223+
if args[:sort]
224+
possible = args[:dimensions] + args[:metrics]
225+
missing = args[:sort].find_all do |arg|
226+
!possible.include? arg.gsub(/^-/,'') # remove possible minuses from any sort params
227+
end
228+
raise GatticaError::InvalidSort, "You are trying to sort by fields that are not in the available dimensions or metrics: #{missing.join(', ')}" unless missing.empty?
229+
end
230+
231+
return args
232+
end
233+
234+
235+
end
236+
end

0 commit comments

Comments
 (0)