ranalytics 0.1.071029125738
Sign up to get free protection for your applications and to get access to all the features.
- data/README +69 -0
- data/lib/ranalytics.rb +165 -0
- metadata +47 -0
data/README
ADDED
@@ -0,0 +1,69 @@
|
|
1
|
+
= ranalytics -- Ruby Google Analytics interface, inspired by Burnalytics
|
2
|
+
|
3
|
+
Author:: Mirek Rusin
|
4
|
+
|
5
|
+
Copyright (c) 2007 Mirek Rusin, parts of code stolen from Burnalitics
|
6
|
+
|
7
|
+
You can redistribute it and/or modify it under the same terms as Ruby.
|
8
|
+
|
9
|
+
= ranalytics -- Ruby Google Analytics interface, inspired by Burnalytics
|
10
|
+
|
11
|
+
== Examples
|
12
|
+
|
13
|
+
To iterate all profiles (websites) inside your google analytics account
|
14
|
+
|
15
|
+
require 'ranalytics'
|
16
|
+
analytics = Analytics.new 'YOURLOGIN@gmail.com', 'YOURPASSWORD'
|
17
|
+
analytics.profiles.each do |profile|
|
18
|
+
puts profile.name
|
19
|
+
end
|
20
|
+
|
21
|
+
To retrieve hits from the first profile
|
22
|
+
|
23
|
+
require 'ranalytics'
|
24
|
+
analytics = Analytics.new 'YOURLOGIN@gmail.com', 'YOURPASSWORD'
|
25
|
+
profile = analytics.profiles.first
|
26
|
+
puts profile.visits(Date.today) # prints visits for specific day (Date instance)
|
27
|
+
puts profile.visits('20070101-20070131) # prints visits for the range (String)
|
28
|
+
Or you can use predefined Symbols:
|
29
|
+
puts profile.visits(:yesterday)
|
30
|
+
puts profile.visits(:last_7_days)
|
31
|
+
puts profile.visits(:last_week)
|
32
|
+
|
33
|
+
To get stats for each day, you can:
|
34
|
+
|
35
|
+
require 'ranalytics'
|
36
|
+
analytics = Analytics.new 'YOURLOGIN@gmail.com', 'YOURPASSWORD'
|
37
|
+
profile = analytics.profiles.first
|
38
|
+
for date in Date.parse('2007-01-01')..Date.today
|
39
|
+
puts profile.visits(date)
|
40
|
+
end
|
41
|
+
|
42
|
+
== Download
|
43
|
+
|
44
|
+
The latest version of ranalytics can be found at
|
45
|
+
|
46
|
+
* http://mirekrusin.com/ruby/ranalytics
|
47
|
+
|
48
|
+
Documentation can be found at
|
49
|
+
|
50
|
+
* http://mirekrusin.com/ruby/ranalytics
|
51
|
+
|
52
|
+
== Installation
|
53
|
+
|
54
|
+
The preferred method of installing ranalytics is through its GEM file. You'll need to have
|
55
|
+
RubyGems[http://rubygems.rubyforge.org/wiki/wiki.pl] installed for that, though. If you have it,
|
56
|
+
then use:
|
57
|
+
|
58
|
+
% [sudo] gem install ranalytics.gem
|
59
|
+
|
60
|
+
== License
|
61
|
+
|
62
|
+
ranalytics is released under the LGPL license.
|
63
|
+
|
64
|
+
== Support
|
65
|
+
|
66
|
+
The ranalytics homepage is http://mirekrusin.com/ruby/ranalytics
|
67
|
+
|
68
|
+
For other information, feel free to ask on the ruby-talk mailing list
|
69
|
+
(which is mirrored to comp.lang.ruby) or contact mailto:ruby@mirekrusin.com
|
data/lib/ranalytics.rb
ADDED
@@ -0,0 +1,165 @@
|
|
1
|
+
# = ranalytics -- Ruby Google Analytics interface, inspired by Burnalytics
|
2
|
+
#
|
3
|
+
# Author:: Mirek Rusin
|
4
|
+
#
|
5
|
+
# Copyright (c) 2007 Mirek Rusin, parts of code stolen from Burnalitics
|
6
|
+
#
|
7
|
+
# You can redistribute it and/or modify it under the same terms as Ruby.
|
8
|
+
|
9
|
+
# = ranalytics -- Ruby Google Analytics interface, inspired by Burnalytics
|
10
|
+
#
|
11
|
+
# == Examples
|
12
|
+
#
|
13
|
+
# To iterate all profiles (websites) inside your google analytics account
|
14
|
+
#
|
15
|
+
# require 'ranalytics'
|
16
|
+
# analytics = Analytics.new 'YOURLOGIN@gmail.com', 'YOURPASSWORD'
|
17
|
+
# analytics.profiles.each do |profile|
|
18
|
+
# puts profile.name
|
19
|
+
# end
|
20
|
+
#
|
21
|
+
# To retrieve hits from the first profile
|
22
|
+
#
|
23
|
+
# require 'ranalytics'
|
24
|
+
# analytics = Analytics.new 'YOURLOGIN@gmail.com', 'YOURPASSWORD'
|
25
|
+
# profile = analytics.profiles.first
|
26
|
+
# puts profile.visits(Date.today) # prints visits for specific day (Date instance)
|
27
|
+
# puts profile.visits('20070101-20070131) # prints visits for the range (String)
|
28
|
+
# # Or you can use predefined Symbols:
|
29
|
+
# puts profile.visits(:yesterday)
|
30
|
+
# puts profile.visits(:last_7_days)
|
31
|
+
# puts profile.visits(:last_week)
|
32
|
+
#
|
33
|
+
# To get stats for each day, you can:
|
34
|
+
#
|
35
|
+
# require 'ranalytics'
|
36
|
+
# analytics = Analytics.new 'YOURLOGIN@gmail.com', 'YOURPASSWORD'
|
37
|
+
# profile = analytics.profiles.first
|
38
|
+
# for date in Date.parse('2007-01-01')..Date.today
|
39
|
+
# puts profile.visits(date)
|
40
|
+
# end
|
41
|
+
|
42
|
+
require 'net/http'
|
43
|
+
require 'net/https'
|
44
|
+
require 'uri'
|
45
|
+
require 'rubygems'
|
46
|
+
require 'hpricot'
|
47
|
+
require 'pp'
|
48
|
+
require 'time'
|
49
|
+
require 'date'
|
50
|
+
|
51
|
+
class RAnalytics
|
52
|
+
|
53
|
+
def initialize(email, password)
|
54
|
+
@email = email
|
55
|
+
@password = password
|
56
|
+
@profiles = []
|
57
|
+
@cookies = ''
|
58
|
+
login
|
59
|
+
end
|
60
|
+
|
61
|
+
def login
|
62
|
+
data = {
|
63
|
+
'continue' => 'https://www.google.com/analytics/home/?et=reset&hl=en-GB',
|
64
|
+
'service' => 'analytics',
|
65
|
+
'nui' => 'hidden',
|
66
|
+
'hl' => 'en-GB',
|
67
|
+
'GA3T' => 'ouVrvynQwUs',
|
68
|
+
'Email' => @email,
|
69
|
+
'PersistentCookie' => 'yes',
|
70
|
+
'Passwd' => @password }
|
71
|
+
request('/accounts/ServiceLoginBoxAuth', data).code.to_i == 200
|
72
|
+
end
|
73
|
+
|
74
|
+
def request(address, data=nil)
|
75
|
+
raise ScriptError, "Not logged in" unless @cookies
|
76
|
+
headers = {
|
77
|
+
'Cookie' => @cookies,
|
78
|
+
'User-Agent' => 'Ruby, RAnalytics, Mirek Rusin'
|
79
|
+
#'User-Agent' => 'Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.8.0.1) Gecko/20060111 Firefox/1.5.0.1'
|
80
|
+
}
|
81
|
+
response = nil
|
82
|
+
http = Net::HTTP.new("www.google.com", 443)
|
83
|
+
http.use_ssl = true
|
84
|
+
http.verify_mode = OpenSSL::SSL::VERIFY_NONE
|
85
|
+
http.start do |http|
|
86
|
+
if data
|
87
|
+
req = Net::HTTP::Post.new(address, headers)
|
88
|
+
req.set_form_data(data)
|
89
|
+
else
|
90
|
+
req = Net::HTTP::Get.new(address, headers)
|
91
|
+
end
|
92
|
+
response = http.request(req)
|
93
|
+
# cookies are set only once on login
|
94
|
+
@cookies = response.response['set-cookie'].split('; ')[0] if response.response['set-cookie'] && @cookies == ''
|
95
|
+
end
|
96
|
+
response
|
97
|
+
end
|
98
|
+
|
99
|
+
def profiles
|
100
|
+
return @profiles unless @profiles.size == 0
|
101
|
+
hpricot = Hpricot(request('/analytics/home/?et=reset&hl=em-GB').body)
|
102
|
+
@profiles = hpricot.search("select[@name='profile_list']/option").reject { |e| e['value'] == "0" }.map do |e|
|
103
|
+
RAnalyticsProfile.new(self, e['value'], e.inner_html)
|
104
|
+
end
|
105
|
+
end
|
106
|
+
|
107
|
+
def self.to_daterange(args)
|
108
|
+
a, b = Time.now, Time.now
|
109
|
+
case args.class.to_s
|
110
|
+
when 'String' # && args =~ /\d{8}-\d{8}/
|
111
|
+
return args # XXX
|
112
|
+
|
113
|
+
when 'Date'
|
114
|
+
a = b = args
|
115
|
+
|
116
|
+
when 'Symbol'
|
117
|
+
case args
|
118
|
+
when :yesterday then a = Time.now - 60*60*24
|
119
|
+
when :last_7_days then a = Time.now - 60*60*24*7; b = Time.now - 60*60*24
|
120
|
+
when :last_week
|
121
|
+
a = Time.now - 60*60*24 * (7 + (Time.now - 60*60*24).strftime('%w').to_i)
|
122
|
+
b = a + 60*60*24*6
|
123
|
+
end
|
124
|
+
|
125
|
+
end
|
126
|
+
"#{a.strftime('%Y%m%d')}-#{b.strftime('%Y%m%d')}"
|
127
|
+
end
|
128
|
+
|
129
|
+
end
|
130
|
+
|
131
|
+
class RAnalyticsProfile
|
132
|
+
|
133
|
+
attr_accessor :id, :name, :reports, :ranalytics
|
134
|
+
|
135
|
+
def initialize(ranalytics, profile_id, profile_name=nil)
|
136
|
+
@ranalytics = ranalytics
|
137
|
+
@id = profile_id
|
138
|
+
@name = profile_name
|
139
|
+
@reports = {}
|
140
|
+
end
|
141
|
+
|
142
|
+
# name - ie. DashboardReport
|
143
|
+
# range - ie. 20070510-20070609
|
144
|
+
def report(name, daterange)
|
145
|
+
daterange = RAnalytics.to_daterange(daterange)
|
146
|
+
unless defined?(@reports[daterange][name])
|
147
|
+
r = @ranalytics.request("/analytics/reporting/export?fmt=1&id=#{@id}&pdr=#{daterange}&cmp=average&rpt=#{name}")
|
148
|
+
if r.code.to_i == 302
|
149
|
+
r = request( Hpricot(r.body).at('html/body/a')['href'] ) # The document has moved [here]
|
150
|
+
end
|
151
|
+
@reports[daterange] = {}
|
152
|
+
@reports[daterange][name] = r.body
|
153
|
+
end
|
154
|
+
@reports[daterange][name]
|
155
|
+
end
|
156
|
+
|
157
|
+
def visits(daterange)
|
158
|
+
Hpricot(report('DashboardReport', daterange)).at('#VisitsSummary/SummaryValue').inner_html.to_i
|
159
|
+
end
|
160
|
+
|
161
|
+
def to_s
|
162
|
+
"RAnalyticsProfile #{@id} #{@name||'Untitled'}"
|
163
|
+
end
|
164
|
+
|
165
|
+
end
|
metadata
ADDED
@@ -0,0 +1,47 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
rubygems_version: 0.9.2
|
3
|
+
specification_version: 1
|
4
|
+
name: ranalytics
|
5
|
+
version: !ruby/object:Gem::Version
|
6
|
+
version: 0.1.071029125738
|
7
|
+
date: 2007-10-29 00:00:00 +00:00
|
8
|
+
summary: Ruby Google Analytics interface
|
9
|
+
require_paths:
|
10
|
+
- lib
|
11
|
+
email: ruby@mirekrusin.com
|
12
|
+
homepage: http://mirekrusin.com/ruby/ranalytics
|
13
|
+
rubyforge_project:
|
14
|
+
description:
|
15
|
+
autorequire: ranalytics
|
16
|
+
default_executable:
|
17
|
+
bindir: bin
|
18
|
+
has_rdoc: true
|
19
|
+
required_ruby_version: !ruby/object:Gem::Version::Requirement
|
20
|
+
requirements:
|
21
|
+
- - ">"
|
22
|
+
- !ruby/object:Gem::Version
|
23
|
+
version: 0.0.0
|
24
|
+
version:
|
25
|
+
platform: ruby
|
26
|
+
signing_key:
|
27
|
+
cert_chain:
|
28
|
+
post_install_message:
|
29
|
+
authors:
|
30
|
+
- Mirek Rusin
|
31
|
+
files:
|
32
|
+
- lib/ranalytics.rb
|
33
|
+
- README
|
34
|
+
test_files: []
|
35
|
+
|
36
|
+
rdoc_options: []
|
37
|
+
|
38
|
+
extra_rdoc_files:
|
39
|
+
- README
|
40
|
+
executables: []
|
41
|
+
|
42
|
+
extensions: []
|
43
|
+
|
44
|
+
requirements: []
|
45
|
+
|
46
|
+
dependencies: []
|
47
|
+
|