cannikin-gattica 0.1.4
Sign up to get free protection for your applications and to get access to all the features.
- data/History.txt +14 -0
- data/LICENSE +22 -0
- data/README.rdoc +137 -0
- data/VERSION.yml +4 -0
- data/examples/example.rb +23 -0
- data/lib/gattica.rb +236 -0
- data/lib/gattica/account.rb +26 -0
- data/lib/gattica/auth.rb +39 -0
- data/lib/gattica/convertible.rb +28 -0
- data/lib/gattica/core_extensions.rb +18 -0
- data/lib/gattica/data_point.rb +52 -0
- data/lib/gattica/data_set.rb +54 -0
- data/lib/gattica/exceptions.rb +18 -0
- data/lib/gattica/user.rb +32 -0
- data/test/helper.rb +15 -0
- data/test/suite.rb +6 -0
- data/test/test_sample.rb +13 -0
- metadata +81 -0
data/History.txt
ADDED
@@ -0,0 +1,14 @@
|
|
1
|
+
== 0.1.4 / 2009-04-22
|
2
|
+
* Another attempt at getting the gem to build on github
|
3
|
+
|
4
|
+
== 0.1.3 / 2009-04-22
|
5
|
+
* Getting gem to build on github
|
6
|
+
|
7
|
+
== 0.1.2 / 2009-04-22
|
8
|
+
* Updated readme and examples, better documentation throughout
|
9
|
+
|
10
|
+
== 0.1.1 / 2009-04-22
|
11
|
+
* When outputting as CSV, surround each piece of data with double quotes (appears pretty common for various properties (like Browser name) to contain commas
|
12
|
+
|
13
|
+
== 0.1.0 / 2009-03-26
|
14
|
+
* Basic functionality working good. Can't use filters yet.
|
data/LICENSE
ADDED
@@ -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.
|
data/README.rdoc
ADDED
@@ -0,0 +1,137 @@
|
|
1
|
+
Gattica is a Ruby library for talking to the Google Analytics API.
|
2
|
+
|
3
|
+
= Installation
|
4
|
+
Install the gattica gem using github as the source:
|
5
|
+
|
6
|
+
gem install cannikin-gattica -s http://gems.github.com
|
7
|
+
|
8
|
+
When you want to require, you just use 'gattica' as the gem name:
|
9
|
+
|
10
|
+
require 'rubygems'
|
11
|
+
require 'gattica'
|
12
|
+
|
13
|
+
= Introduction
|
14
|
+
There are generally three steps to getting info from the GA API:
|
15
|
+
|
16
|
+
1. Authenticate
|
17
|
+
2. Get a profile id
|
18
|
+
3. Get the data you really want
|
19
|
+
|
20
|
+
It's a good idea to familiarize yourself with the Google API docs:
|
21
|
+
|
22
|
+
http://code.google.com/apis/analytics/docs/gdata/gdataDeveloperGuide.html
|
23
|
+
|
24
|
+
In particular there are some very specific combinations of Metrics and Dimensions that
|
25
|
+
you are restricted to and those are explained in this document:
|
26
|
+
|
27
|
+
http://code.google.com/apis/analytics/docs/gdata/gdataReferenceDimensionsMetrics.html
|
28
|
+
|
29
|
+
= Usage
|
30
|
+
This library does all three. A typical transaction will look like this:
|
31
|
+
|
32
|
+
gs = Gattica.new('johndoe@google.com','password',123456)
|
33
|
+
results = gs.get({ :start_date => '2008-01-01',
|
34
|
+
:end_date => '2008-02-01',
|
35
|
+
:dimensions => 'browser',
|
36
|
+
:metrics => 'pageviews',
|
37
|
+
:sort => '-pageviews'})
|
38
|
+
|
39
|
+
So we instantiate a copy of Gattica and pass it a Google Account email address and password.
|
40
|
+
The third parameter is the profile_id that we want to access data for.
|
41
|
+
|
42
|
+
Then we call +get+ with the parameters we want to shape our data with. In this case we want
|
43
|
+
total page views, broken down by browser, from Jan 1 2008 to Feb 1 2008, sorted by descending
|
44
|
+
page views.
|
45
|
+
|
46
|
+
If you don't know the profile_id you want to get data for, call +accounts+
|
47
|
+
|
48
|
+
gs = Gattica.new('johndoe@google.com','password')
|
49
|
+
accounts = gs.accounts
|
50
|
+
|
51
|
+
This returns all of the accounts and profiles that the user has access to. Note that if you
|
52
|
+
use this method to get profiles, you need to manually set the profile before you can call +get+
|
53
|
+
|
54
|
+
gs.profile_id = 123456
|
55
|
+
results = gs.get({ :start_date => '2008-01-01',
|
56
|
+
:end_date => '2008-02-01',
|
57
|
+
:dimensions => 'browser',
|
58
|
+
:metrics => 'pageviews',
|
59
|
+
:sort => '-pageviews'})
|
60
|
+
|
61
|
+
When you put in the names for the dimensions and metrics you want, refer to this doc for the
|
62
|
+
available names:
|
63
|
+
|
64
|
+
http://code.google.com/apis/analytics/docs/gdata/gdataReferenceDimensionsMetrics.html
|
65
|
+
|
66
|
+
Note that you do *not* use the 'ga:' prefix when you tell Gattica which ones you want. Gattica
|
67
|
+
adds that for you automatically.
|
68
|
+
|
69
|
+
If you want to search on more than one dimension or metric, pass them in as an array (you can
|
70
|
+
also pass in single values as arrays too, if you wish):
|
71
|
+
|
72
|
+
results = gs.get({ :start_date => '2008-01-01',
|
73
|
+
:end_date => '2008-02-01',
|
74
|
+
:dimensions => ['browser','browserVersion'],
|
75
|
+
:metrics => ['pageviews','visits'],
|
76
|
+
:sort => ['-pageviews']})
|
77
|
+
|
78
|
+
= Output
|
79
|
+
When Gattica was originally created it was intended to take the data returned and put it into
|
80
|
+
Excel for someone else to crunch through the numbers. Thus, Gattica has great built-in support
|
81
|
+
for CSV output. Once you have your data simply:
|
82
|
+
|
83
|
+
results.to_csv
|
84
|
+
|
85
|
+
A couple example rows of what that looks like:
|
86
|
+
|
87
|
+
"id","updated","title","browser","pageviews"
|
88
|
+
"http://www.google.com/analytics/feeds/data?ids=ga:12345&ga:browser=Internet%20Explorer&start-date=2009-01-01&end-date=2009-01-31","2009-01-30T16:00:00-08:00","ga:browser=Internet Explorer","Internet Explorer","53303"
|
89
|
+
"http://www.google.com/analytics/feeds/data?ids=ga:12345&ga:browser=Firefox&start-date=2009-01-01&end-date=2009-01-31","2009-01-30T16:00:00-08:00","ga:browser=Firefox","Firefox","20323"
|
90
|
+
|
91
|
+
Data is comma-separated and double-quote delimited. In most cases, people don't care
|
92
|
+
about the id, updated, or title attributes of this data. They just want the dimensions and
|
93
|
+
metrics. In that case, pass the symbol +:short+ to +to_csv+ and receive get back only the
|
94
|
+
the good stuff:
|
95
|
+
|
96
|
+
results.to_csv(:short)
|
97
|
+
|
98
|
+
Which returns:
|
99
|
+
|
100
|
+
"browser","pageviews"
|
101
|
+
"Internet Explorer","53303"
|
102
|
+
"Firefox","20323"
|
103
|
+
|
104
|
+
You can also just output the results as a string and you'll get the standard inspect syntax:
|
105
|
+
|
106
|
+
results.to_s
|
107
|
+
|
108
|
+
Gives you:
|
109
|
+
|
110
|
+
{ "end_date"=>#<Date: 4909725/2,0,2299161>,
|
111
|
+
"start_date"=>#<Date: 4909665/2,0,2299161>,
|
112
|
+
"points"=>[
|
113
|
+
{ "title"=>"ga:browser=Internet Explorer",
|
114
|
+
"dimensions"=>[{:browser=>"Internet Explorer"}],
|
115
|
+
"id"=>"http://www.google.com/analytics/feeds/data?ids=ga:12345&ga:browser=Internet%20Explorer&start-date=2009-01-01&end-date=2009-01-31",
|
116
|
+
"metrics"=>[{:pageviews=>53303}],
|
117
|
+
"updated"=>#<DateTime: 212100120000001/86400000,-1/3,2299161>}]}
|
118
|
+
|
119
|
+
= Limitations
|
120
|
+
The GA API limits each call to 1000 results per "page." If you want more, you need to tell
|
121
|
+
the API what number to begin at and it will return the next 1000. Gattica does not currently
|
122
|
+
support this, but it's in the plan for the very next version.
|
123
|
+
|
124
|
+
The GA API support filtering, so you can say things like "only show me the pageviews for pages
|
125
|
+
whose URL meets the regular expression ^/saf.*?/$". Gattica can pass in filters, but you'll need
|
126
|
+
to know how to format them correctly (refer to the GA API), it won't let you do any kind of pretty
|
127
|
+
syntax and convert it for you. I plan to add this soon.
|
128
|
+
|
129
|
+
= The Future
|
130
|
+
A couple of things I have planned:
|
131
|
+
|
132
|
+
1. Tests!
|
133
|
+
2. The option to use a custom delimiter for output
|
134
|
+
3. Automatically handle paging (the API only returns 1000 results at a time). Gattica will request
|
135
|
+
one result set, see how many pages there are, then do several calls until all pages are retrieved
|
136
|
+
or it hits the limit of the number of results you want and return all that data as one big block.
|
137
|
+
4. Create a nice syntax for filters
|
data/VERSION.yml
ADDED
data/examples/example.rb
ADDED
@@ -0,0 +1,23 @@
|
|
1
|
+
require '../lib/gattica'
|
2
|
+
|
3
|
+
# authenticate with the API
|
4
|
+
ga = Gattica.new('username@gmail.com','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
|
data/lib/gattica.rb
ADDED
@@ -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('johndoe@google.com','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('johndoe@google.com','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.4'
|
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('johndoe@google.com','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('johndoe@google.com','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('johndoe@google.com','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,0 +1,26 @@
|
|
1
|
+
require 'rubygems'
|
2
|
+
require 'hpricot'
|
3
|
+
|
4
|
+
module Gattica
|
5
|
+
|
6
|
+
# Represents an account that an authenticated user has access to
|
7
|
+
|
8
|
+
class Account
|
9
|
+
|
10
|
+
include Convertible
|
11
|
+
|
12
|
+
attr_reader :id, :updated, :title, :table_id, :account_id, :account_name, :profile_id, :web_property_id
|
13
|
+
|
14
|
+
def initialize(xml)
|
15
|
+
@id = xml.at(:id).inner_html
|
16
|
+
@updated = DateTime.parse(xml.at(:updated).inner_html)
|
17
|
+
@title = xml.at(:title).inner_html
|
18
|
+
@table_id = xml.at('dxp:tableid').inner_html
|
19
|
+
@account_id = xml.at("dxp:property[@name='ga:accountId']").attributes['value'].to_i
|
20
|
+
@account_name = xml.at("dxp:property[@name='ga:accountName']").attributes['value']
|
21
|
+
@profile_id = xml.at("dxp:property[@name='ga:profileId']").attributes['value'].to_i
|
22
|
+
@web_property_id = xml.at("dxp:property[@name='ga:webPropertyId']").attributes['value']
|
23
|
+
end
|
24
|
+
|
25
|
+
end
|
26
|
+
end
|
data/lib/gattica/auth.rb
ADDED
@@ -0,0 +1,39 @@
|
|
1
|
+
require 'net/http'
|
2
|
+
require 'net/https'
|
3
|
+
|
4
|
+
module Gattica
|
5
|
+
|
6
|
+
# Authenticates a user against the Google Client Login system
|
7
|
+
|
8
|
+
class Auth
|
9
|
+
|
10
|
+
include Convertible
|
11
|
+
|
12
|
+
SCRIPT_NAME = '/accounts/ClientLogin'
|
13
|
+
HEADERS = { 'Content-Type' => 'application/x-www-form-urlencoded' }
|
14
|
+
OPTIONS = { :source => '', :service => 'analytics' }
|
15
|
+
|
16
|
+
attr_reader :response, :data, :tokens, :token
|
17
|
+
|
18
|
+
# Prepare the user info along with options and header
|
19
|
+
def initialize(http, user, options={}, headers={})
|
20
|
+
data = OPTIONS.merge(options)
|
21
|
+
data = data.merge(user.to_h)
|
22
|
+
headers = HEADERS.merge(headers)
|
23
|
+
|
24
|
+
@response, @data = http.post(SCRIPT_NAME, data.to_query, headers)
|
25
|
+
@tokens = parse_tokens(@data)
|
26
|
+
end
|
27
|
+
|
28
|
+
private
|
29
|
+
# Parse the authentication tokens out of the response
|
30
|
+
def parse_tokens(data)
|
31
|
+
tokens = {}
|
32
|
+
data.split("\n").each do |t|
|
33
|
+
tokens.merge!({ t.split('=').first.downcase.to_sym => t.split('=').last })
|
34
|
+
end
|
35
|
+
return tokens
|
36
|
+
end
|
37
|
+
|
38
|
+
end
|
39
|
+
end
|
@@ -0,0 +1,28 @@
|
|
1
|
+
module Gattica
|
2
|
+
|
3
|
+
# Common output methods that are sharable
|
4
|
+
|
5
|
+
module Convertible
|
6
|
+
|
7
|
+
# output as hash
|
8
|
+
def to_h
|
9
|
+
output = {}
|
10
|
+
instance_variables.each do |var|
|
11
|
+
output.merge!({ var[1..-1] => instance_variable_get(var) }) unless var == '@xml' # exclude the whole XML dump
|
12
|
+
end
|
13
|
+
output
|
14
|
+
end
|
15
|
+
|
16
|
+
# output nice inspect syntax
|
17
|
+
def to_s
|
18
|
+
to_h.inspect
|
19
|
+
end
|
20
|
+
|
21
|
+
alias inspect to_s
|
22
|
+
|
23
|
+
def to_query
|
24
|
+
to_h.to_query
|
25
|
+
end
|
26
|
+
|
27
|
+
end
|
28
|
+
end
|
@@ -0,0 +1,18 @@
|
|
1
|
+
class Hash
|
2
|
+
|
3
|
+
def to_query
|
4
|
+
require 'cgi' unless defined?(CGI) && defined?(CGI::escape)
|
5
|
+
self.collect do |key, value|
|
6
|
+
"#{CGI.escape(key.to_s)}=#{CGI.escape(value.to_s)}"
|
7
|
+
end.sort * '&'
|
8
|
+
end
|
9
|
+
|
10
|
+
def key
|
11
|
+
self.keys.first if self.length == 1
|
12
|
+
end
|
13
|
+
|
14
|
+
def value
|
15
|
+
self.values.first if self.length == 1
|
16
|
+
end
|
17
|
+
|
18
|
+
end
|
@@ -0,0 +1,52 @@
|
|
1
|
+
module Gattica
|
2
|
+
|
3
|
+
# Represents a single "row" of data containing any number of dimensions, metrics
|
4
|
+
|
5
|
+
class DataPoint
|
6
|
+
|
7
|
+
include Convertible
|
8
|
+
|
9
|
+
attr_reader :id, :updated, :title, :dimensions, :metrics, :xml
|
10
|
+
|
11
|
+
# Parses the XML <entry> element
|
12
|
+
def initialize(xml)
|
13
|
+
@xml = xml.to_s
|
14
|
+
@id = xml.at('id').inner_html
|
15
|
+
@updated = DateTime.parse(xml.at('updated').inner_html)
|
16
|
+
@title = xml.at('title').inner_html
|
17
|
+
@dimensions = xml.search('dxp:dimension').collect do |dimension|
|
18
|
+
{ dimension.attributes['name'].split(':').last.to_sym => dimension.attributes['value'].split(':').last }
|
19
|
+
end
|
20
|
+
@metrics = xml.search('dxp:metric').collect do |metric|
|
21
|
+
{ metric.attributes['name'].split(':').last.to_sym => metric.attributes['value'].split(':').last.to_i }
|
22
|
+
end
|
23
|
+
end
|
24
|
+
|
25
|
+
|
26
|
+
# Outputs in Comma Seperated Values format
|
27
|
+
def to_csv(format = :long)
|
28
|
+
output = ''
|
29
|
+
|
30
|
+
# only output
|
31
|
+
case format
|
32
|
+
when :long
|
33
|
+
output = "\"#{@id}\",\"#{@updated.to_s}\",\"#{@title}\","
|
34
|
+
end
|
35
|
+
|
36
|
+
# output all dimensions
|
37
|
+
output += @dimensions.collect do |dimension|
|
38
|
+
"\"#{dimension.value}\""
|
39
|
+
end.join(',')
|
40
|
+
output += ','
|
41
|
+
|
42
|
+
# output all metrics
|
43
|
+
output += @metrics.collect do |metric|
|
44
|
+
"\"#{metric.value}\""
|
45
|
+
end.join(',')
|
46
|
+
|
47
|
+
return output
|
48
|
+
end
|
49
|
+
|
50
|
+
end
|
51
|
+
|
52
|
+
end
|
@@ -0,0 +1,54 @@
|
|
1
|
+
module Gattica
|
2
|
+
|
3
|
+
# Encapsulates the data returned by the GA API
|
4
|
+
|
5
|
+
class DataSet
|
6
|
+
|
7
|
+
include Convertible
|
8
|
+
|
9
|
+
attr_reader :total_results, :start_index, :items_per_page, :start_date, :end_date, :points, :xml
|
10
|
+
|
11
|
+
def initialize(xml)
|
12
|
+
@xml = xml.to_s
|
13
|
+
@total_results = xml.at('openSearch:totalResults').inner_html.to_i
|
14
|
+
@start_index = xml.at('openSearch:startIndex').inner_html.to_i
|
15
|
+
@items_per_page = xml.at('openSearch:itemsPerPage').inner_html.to_i
|
16
|
+
@start_date = Date.parse(xml.at('dxp:startDate').inner_html)
|
17
|
+
@end_date = Date.parse(xml.at('dxp:endDate').inner_html)
|
18
|
+
@points = xml.search(:entry).collect { |entry| DataPoint.new(entry) }
|
19
|
+
end
|
20
|
+
|
21
|
+
|
22
|
+
# output important data to CSV, ignoring all the specific data about this dataset
|
23
|
+
# (total_results, start_date) and just output the data from the points
|
24
|
+
|
25
|
+
def to_csv(format = :long)
|
26
|
+
# build the headers
|
27
|
+
output = ''
|
28
|
+
|
29
|
+
# only show the nitty gritty details of id, updated_at and title if requested
|
30
|
+
case format
|
31
|
+
when :long
|
32
|
+
output = '"id","updated","title",'
|
33
|
+
end
|
34
|
+
|
35
|
+
output += @points.first.dimensions.collect do |dimension|
|
36
|
+
"\"#{dimension.key.to_s}\""
|
37
|
+
end.join(',')
|
38
|
+
output += ','
|
39
|
+
output += @points.first.metrics.collect do |metric|
|
40
|
+
"\"#{metric.key.to_s}\""
|
41
|
+
end.join(',')
|
42
|
+
output += "\n"
|
43
|
+
|
44
|
+
# get the data from each point
|
45
|
+
@points.each do |point|
|
46
|
+
output += point.to_csv(format) + "\n"
|
47
|
+
end
|
48
|
+
|
49
|
+
return output
|
50
|
+
end
|
51
|
+
|
52
|
+
end
|
53
|
+
|
54
|
+
end
|
@@ -0,0 +1,18 @@
|
|
1
|
+
module GatticaError
|
2
|
+
# user errors
|
3
|
+
class InvalidEmail < StandardError; end;
|
4
|
+
class InvalidPassword < StandardError; end;
|
5
|
+
# authentication errors
|
6
|
+
class CouldNotAuthenticate < StandardError; end;
|
7
|
+
# profile errors
|
8
|
+
class InvalidProfileId < StandardError; end;
|
9
|
+
# search errors
|
10
|
+
class TooManyDimensions < StandardError; end;
|
11
|
+
class TooManyMetrics < StandardError; end;
|
12
|
+
class InvalidSort < StandardError; end;
|
13
|
+
class InvalidFilter < StandardError; end;
|
14
|
+
class MissingStartDate < StandardError; end;
|
15
|
+
class MissingEndDate < StandardError; end;
|
16
|
+
# errors from Analytics
|
17
|
+
class AnalyticsError < StandardError; end;
|
18
|
+
end
|
data/lib/gattica/user.rb
ADDED
@@ -0,0 +1,32 @@
|
|
1
|
+
module Gattica
|
2
|
+
|
3
|
+
# Represents a user to be authenticated by GA
|
4
|
+
|
5
|
+
class User
|
6
|
+
|
7
|
+
include Convertible
|
8
|
+
|
9
|
+
SERVICE = 'analytics'
|
10
|
+
attr_accessor :email, :password
|
11
|
+
|
12
|
+
def initialize(email,password,source='')
|
13
|
+
@email = email
|
14
|
+
@password = password
|
15
|
+
validate
|
16
|
+
end
|
17
|
+
|
18
|
+
# User gets a special +to_h+ because Google expects +Email+ and +Passwd+ instead of our nicer internal names
|
19
|
+
def to_h
|
20
|
+
{ :Email => @email,
|
21
|
+
:Passwd => @password }
|
22
|
+
end
|
23
|
+
|
24
|
+
private
|
25
|
+
# Determine whether or not this is a valid user
|
26
|
+
def validate
|
27
|
+
raise GatticaError::InvalidEmail, "The email address '#{@email}' is not valid" if not @email.match(/^(?:[_a-z0-9-]+)(\.[_a-z0-9-]+)*@([a-z0-9-]+)(\.[a-zA-Z0-9\-\.]+)*(\.[a-z]{2,4})$/i)
|
28
|
+
raise GatticaError::InvalidPassword, "The password cannot be blank" if @password.empty? || @password.nil?
|
29
|
+
end
|
30
|
+
|
31
|
+
end
|
32
|
+
end
|
data/test/helper.rb
ADDED
@@ -0,0 +1,15 @@
|
|
1
|
+
require File.join(File.dirname(__FILE__), *%w[.. lib gattica])
|
2
|
+
|
3
|
+
require 'rubygems'
|
4
|
+
require 'test/unit'
|
5
|
+
require 'mocha'
|
6
|
+
|
7
|
+
include Gattica
|
8
|
+
|
9
|
+
def fixture(name)
|
10
|
+
File.read(File.join(File.dirname(__FILE__), 'fixtures', name))
|
11
|
+
end
|
12
|
+
|
13
|
+
def absolute_project_path
|
14
|
+
File.expand_path(File.join(File.dirname(__FILE__), '..'))
|
15
|
+
end
|
data/test/suite.rb
ADDED
data/test/test_sample.rb
ADDED
metadata
ADDED
@@ -0,0 +1,81 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: cannikin-gattica
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.1.4
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Rob Cameron
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
|
12
|
+
date: 2009-04-22 00:00:00 -07:00
|
13
|
+
default_executable:
|
14
|
+
dependencies:
|
15
|
+
- !ruby/object:Gem::Dependency
|
16
|
+
name: hpricot
|
17
|
+
type: :runtime
|
18
|
+
version_requirement:
|
19
|
+
version_requirements: !ruby/object:Gem::Requirement
|
20
|
+
requirements:
|
21
|
+
- - ">="
|
22
|
+
- !ruby/object:Gem::Version
|
23
|
+
version: 0.8.1
|
24
|
+
version:
|
25
|
+
description: Gattica is a Ruby library for extracting data from the Google Analytics API.
|
26
|
+
email: cannikinn@gmail.com
|
27
|
+
executables: []
|
28
|
+
|
29
|
+
extensions: []
|
30
|
+
|
31
|
+
extra_rdoc_files: []
|
32
|
+
|
33
|
+
files:
|
34
|
+
- History.txt
|
35
|
+
- README.rdoc
|
36
|
+
- LICENSE
|
37
|
+
- VERSION.yml
|
38
|
+
- examples/example.rb
|
39
|
+
- lib/gattica
|
40
|
+
- lib/gattica.rb
|
41
|
+
- lib/gattica/account.rb
|
42
|
+
- lib/gattica/auth.rb
|
43
|
+
- lib/gattica/convertible.rb
|
44
|
+
- lib/gattica/core_extensions.rb
|
45
|
+
- lib/gattica/data_point.rb
|
46
|
+
- lib/gattica/data_set.rb
|
47
|
+
- lib/gattica/exceptions.rb
|
48
|
+
- lib/gattica/user.rb
|
49
|
+
- test/helper.rb
|
50
|
+
- test/suite.rb
|
51
|
+
- test/test_sample.rb
|
52
|
+
has_rdoc: true
|
53
|
+
homepage: http://github.com/cannikin/gattica
|
54
|
+
post_install_message:
|
55
|
+
rdoc_options:
|
56
|
+
- --inline-source
|
57
|
+
- --charset=UTF-8
|
58
|
+
require_paths:
|
59
|
+
- lib
|
60
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
61
|
+
requirements:
|
62
|
+
- - ">="
|
63
|
+
- !ruby/object:Gem::Version
|
64
|
+
version: "0"
|
65
|
+
version:
|
66
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
67
|
+
requirements:
|
68
|
+
- - ">="
|
69
|
+
- !ruby/object:Gem::Version
|
70
|
+
version: "0"
|
71
|
+
version:
|
72
|
+
requirements:
|
73
|
+
- A Google Analytics Account
|
74
|
+
- One or more Profiles that are being tracked in your GA account
|
75
|
+
rubyforge_project:
|
76
|
+
rubygems_version: 1.2.0
|
77
|
+
signing_key:
|
78
|
+
specification_version: 2
|
79
|
+
summary: Gattica is a Ruby library for extracting data from the Google Analytics API.
|
80
|
+
test_files: []
|
81
|
+
|