lazy_google_analytics 0.1.2 → 0.1.5
Sign up to get free protection for your applications and to get access to all the features.
- data/CHANGELOG.md +0 -0
- data/README.md +36 -10
- data/lazy_google_analytics.gemspec +1 -1
- data/lib/lazy_google_analytics/client.rb +12 -11
- data/lib/lazy_google_analytics/version.rb +1 -1
- data/spec/functional/client_spec.rb +69 -25
- metadata +6 -5
data/CHANGELOG.md
ADDED
File without changes
|
data/README.md
CHANGED
@@ -1,10 +1,12 @@
|
|
1
1
|
# LazyGoogleAnalytics
|
2
2
|
|
3
|
-
Lazy google analytics
|
3
|
+
Lazy google analytics it´s an abstraction around google-analytics-client gem, to make server to server api calls.
|
4
4
|
|
5
5
|
## Motivation
|
6
6
|
|
7
|
-
google-analytics-client gem is a very powerfull tool to access the api resources on google. but for me it was not very straightforward
|
7
|
+
google-analytics-client gem is a very powerfull tool to access the api resources on google. but for me it was not very straightforward, so I come around with a simple way to implement it. that's all.
|
8
|
+
|
9
|
+
Hope you like it.
|
8
10
|
|
9
11
|
## Installation
|
10
12
|
|
@@ -22,7 +24,7 @@ Or install it yourself as:
|
|
22
24
|
|
23
25
|
## Simple Usage
|
24
26
|
|
25
|
-
|
27
|
+
### Configuration setup:
|
26
28
|
|
27
29
|
LazyGoogleAnalytics::Config.setup do |config|
|
28
30
|
config.pass_phrase = "notasecret"
|
@@ -33,13 +35,17 @@ Or install it yourself as:
|
|
33
35
|
config.email = "XXXXXX@developer.gserviceaccount.com"
|
34
36
|
end
|
35
37
|
|
36
|
-
|
38
|
+
### Api calls:
|
37
39
|
|
38
40
|
@client = LazyGoogleAnalytics::Client.new()
|
39
41
|
|
40
|
-
By default LazyGoogleAnalytics::Client is going to make a call for visits within 1 month timeline sorted by month & day, using the
|
42
|
+
By default LazyGoogleAnalytics::Client is going to make a call for visits within 1 month timeline sorted by month & day, using the profile_id in config block.
|
43
|
+
|
44
|
+
But you can override specific parameters like this:
|
45
|
+
|
46
|
+
@client = LazyGoogleAnalytics::Client.new({:ids => "ga:123456"})
|
41
47
|
|
42
|
-
|
48
|
+
Also you can extend the options after initialization, passing and entire options_hash overriding all default parameters:
|
43
49
|
|
44
50
|
@client = LazyGoogleAnalytics::Client.new()
|
45
51
|
@client.parameters({'ids' => "ga:XXXXX",
|
@@ -49,20 +55,40 @@ Or install it yourself as:
|
|
49
55
|
'metrics' => "ga:visits",
|
50
56
|
'sort' => "ga:month,ga:day" })
|
51
57
|
|
58
|
+
#### Results
|
59
|
+
|
60
|
+
To get the results of the initialized client you have to call .results method
|
61
|
+
|
62
|
+
@results = @client.results
|
63
|
+
@results.columns
|
64
|
+
@results.rows
|
65
|
+
|
66
|
+
Also this class have 2 convenience methods to output data:
|
67
|
+
|
68
|
+
To get the queried columns:
|
69
|
+
|
70
|
+
@results.formatted_columns
|
71
|
+
|
72
|
+
To get the rows:
|
73
|
+
|
74
|
+
@results.formatted_rows
|
75
|
+
|
76
|
+
|
52
77
|
## Rails 3
|
53
78
|
|
54
|
-
|
79
|
+
Installs configuration initializer
|
55
80
|
|
56
81
|
rails g lazy_google_analytics:install
|
57
82
|
|
58
|
-
##
|
83
|
+
## Google Analytics How to:
|
59
84
|
|
60
85
|
If you follow this simple steps , you can´t fail.
|
61
86
|
|
62
87
|
+ First, you have to register your api access in: [google api console](https://code.google.com/apis/console/) and create a server key.
|
63
88
|
+ Download the p12 key.
|
64
|
-
+ Add the created @developer.gserviceaccount.com to your users list in the analytics user panel.
|
65
|
-
+
|
89
|
+
+ Add the created @developer.gserviceaccount.com to your users list in the analytics user panel of the profile you want to retrieve the data, otherwise it wont work.
|
90
|
+
+ Note that the profile_id of the configuration object is not the UA-XXXXX number , is the Google Analytics profile number.
|
91
|
+
+ To play with the api to tests different options (without write any code) you can use [Google Analytics Query Explorer 2](https://ga-dev-tools.appspot.com/explorer/)
|
66
92
|
|
67
93
|
## Contributing
|
68
94
|
|
@@ -9,7 +9,7 @@ Gem::Specification.new do |spec|
|
|
9
9
|
spec.authors = ["miguel michelson"]
|
10
10
|
spec.email = ["miguelmichelson@gmail.com"]
|
11
11
|
spec.description = %q{google analytics api access for the lazy ones}
|
12
|
-
spec.summary = %q{google analytics api access for the lazy ones}
|
12
|
+
spec.summary = %q{google analytics api access server 2 server for the lazy ones}
|
13
13
|
spec.homepage = ""
|
14
14
|
spec.license = "MIT"
|
15
15
|
|
@@ -19,22 +19,27 @@ module LazyGoogleAnalytics
|
|
19
19
|
|
20
20
|
def defaults_options(opts)
|
21
21
|
|
22
|
+
api_method = opts[:api_method] ||= @auth.analytics.data.ga.get
|
22
23
|
start_date = opts[:start_date] ||= DateTime.now.prev_month.strftime("%Y-%m-%d")
|
23
24
|
end_date = opts[:end_date] ||= DateTime.now.strftime("%Y-%m-%d")
|
25
|
+
ids = opts[:ids] ||= "ga:#{LazyGoogleAnalytics::Config.profile_id}"
|
26
|
+
dimensions = opts[:dimensions] ||= "ga:day,ga:month"
|
27
|
+
metrics = opts[:metrics] ||= "ga:visits"
|
28
|
+
sort = opts[:sort] ||= "ga:month,ga:day"
|
24
29
|
|
25
|
-
self.api_method(
|
26
|
-
self.parameters({'ids' =>
|
30
|
+
self.api_method(api_method)
|
31
|
+
self.parameters({'ids' => ids,
|
27
32
|
'start-date' => start_date,
|
28
33
|
'end-date' => end_date,
|
29
|
-
'dimensions' =>
|
30
|
-
'metrics' =>
|
31
|
-
'sort' =>
|
34
|
+
'dimensions' => dimensions,
|
35
|
+
'metrics' => metrics,
|
36
|
+
'sort' => sort })
|
32
37
|
end
|
33
38
|
|
34
39
|
|
35
40
|
def results
|
36
41
|
@results = @auth.client.execute(@options)
|
37
|
-
raise_detected_errors
|
42
|
+
raise_detected_errors if @results.status > 200
|
38
43
|
end
|
39
44
|
|
40
45
|
def formatted_columns
|
@@ -61,11 +66,7 @@ module LazyGoogleAnalytics
|
|
61
66
|
|
62
67
|
def raise_detected_errors
|
63
68
|
body = JSON.parse(@results.body)
|
64
|
-
if body.keys.include?("error")
|
65
|
-
raise body["error"]["errors"].collect{|e| e["reason"] + e["message"] }.join(", ")
|
66
|
-
else
|
67
|
-
@results
|
68
|
-
end
|
69
|
+
raise body["error"]["errors"].collect{|e| "#{e["reason"]}: #{e["message"]}" }.join(", ") if body.keys.include?("error")
|
69
70
|
end
|
70
71
|
|
71
72
|
end
|
@@ -1,43 +1,87 @@
|
|
1
1
|
require File.expand_path(File.dirname(__FILE__) + '../../spec_helper')
|
2
2
|
|
3
3
|
|
4
|
-
describe "
|
4
|
+
describe "Client" do
|
5
5
|
before :each do
|
6
6
|
config_setup
|
7
|
-
@error_report = LazyGoogleAnalytics::Client.new()
|
8
|
-
@error_report.parameters({'ids' => "ga:#{LazyGoogleAnalytics::Config.profile_id}",
|
9
|
-
'start-date' => DateTime.now.prev_month.strftime("%Y-%m-%d"),
|
10
|
-
'end-date' => DateTime.now.strftime("%Y-%m-%d"),
|
11
|
-
'dimensions' => "ga:year,ga:month",
|
12
|
-
'metrics' => "ga:visits,ga:bounces,ga:entranceBounceRate",
|
13
|
-
'sort' => "ga:month,ga:day" })
|
14
|
-
|
15
7
|
end
|
16
8
|
|
17
|
-
it "should raise and error" do
|
18
|
-
lambda { @error_report.results }.should raise_error
|
19
|
-
end
|
20
|
-
end
|
21
9
|
|
22
|
-
describe "
|
23
|
-
|
24
|
-
|
25
|
-
|
10
|
+
describe "errors" do
|
11
|
+
before :each do
|
12
|
+
@error_report = LazyGoogleAnalytics::Client.new()
|
13
|
+
@error_report.parameters({'ids' => "ga:#{LazyGoogleAnalytics::Config.profile_id}",
|
14
|
+
'start-date' => DateTime.now.prev_month.strftime("%Y-%m-%d"),
|
15
|
+
'end-date' => DateTime.now.strftime("%Y-%m-%d"),
|
16
|
+
'dimensions' => "ga:year,ga:month",
|
17
|
+
'metrics' => "ga:visits,ga:bounces,ga:entranceBounceRate",
|
18
|
+
'sort' => "ga:month,ga:day" })
|
19
|
+
|
20
|
+
end
|
21
|
+
|
22
|
+
it "should raise and error" do
|
23
|
+
lambda { @error_report.results }.should raise_error
|
24
|
+
end
|
26
25
|
end
|
27
26
|
|
28
|
-
|
29
|
-
|
27
|
+
describe "override entire options" do
|
28
|
+
before :each do
|
29
|
+
@report = LazyGoogleAnalytics::Client.new()
|
30
|
+
@report.parameters({'ids' => "ga:123456",
|
31
|
+
'dimensions' => "ga:year,ga:month",
|
32
|
+
'sort' => "ga:month,ga:day" })
|
33
|
+
|
34
|
+
end
|
35
|
+
|
36
|
+
it "options should have new options" do
|
37
|
+
@report.options[:parameters].keys.should == ["ids", "dimensions", "sort"]
|
38
|
+
@report.options[:parameters]["ids"].should == "ga:123456"
|
39
|
+
end
|
40
|
+
|
30
41
|
end
|
31
42
|
|
32
|
-
|
33
|
-
|
43
|
+
describe "initialize with specific options without overriding defaults" do
|
44
|
+
before :each do
|
45
|
+
@report = LazyGoogleAnalytics::Client.new({:ids => "ga:123"})
|
46
|
+
end
|
47
|
+
|
48
|
+
it "options should have new options" do
|
49
|
+
@report.options[:parameters].keys.should == ["ids", "start-date", "end-date", "dimensions", "metrics", "sort"]
|
50
|
+
@report.options[:parameters]["ids"].should == "ga:123"
|
51
|
+
end
|
52
|
+
|
34
53
|
end
|
35
54
|
|
36
|
-
|
37
|
-
|
38
|
-
|
55
|
+
describe "block initialization" do
|
56
|
+
before :each do
|
57
|
+
@report = LazyGoogleAnalytics::Client.new do |client|
|
58
|
+
client.parameters({ "ids" => "ga:123456"})
|
59
|
+
end
|
60
|
+
end
|
61
|
+
it "options should have new options" do
|
62
|
+
@report.options[:parameters].keys.should == ["ids"]
|
63
|
+
@report.options[:parameters]["ids"].should == "ga:123456"
|
64
|
+
end
|
39
65
|
end
|
40
66
|
|
41
|
-
|
67
|
+
describe "Client" do
|
68
|
+
before(:all) do
|
69
|
+
@client = LazyGoogleAnalytics::Client.new()
|
70
|
+
end
|
71
|
+
|
72
|
+
it "find objects object" do
|
73
|
+
@client.results
|
74
|
+
end
|
42
75
|
|
76
|
+
it "headers" do
|
77
|
+
@client.formatted_columns.should == "ga:day\tga:month\tga:visits"
|
78
|
+
end
|
43
79
|
|
80
|
+
it "rows" do
|
81
|
+
@client.formatted_rows.class.should == Array
|
82
|
+
@client.formatted_rows.should_not be_empty
|
83
|
+
end
|
84
|
+
|
85
|
+
end
|
86
|
+
|
87
|
+
end
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: lazy_google_analytics
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.1.
|
4
|
+
version: 0.1.5
|
5
5
|
prerelease:
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
@@ -9,7 +9,7 @@ authors:
|
|
9
9
|
autorequire:
|
10
10
|
bindir: bin
|
11
11
|
cert_chain: []
|
12
|
-
date: 2013-03-
|
12
|
+
date: 2013-03-24 00:00:00.000000000 Z
|
13
13
|
dependencies:
|
14
14
|
- !ruby/object:Gem::Dependency
|
15
15
|
name: google-api-client
|
@@ -100,6 +100,7 @@ extra_rdoc_files: []
|
|
100
100
|
files:
|
101
101
|
- .DS_Store
|
102
102
|
- .gitignore
|
103
|
+
- CHANGELOG.md
|
103
104
|
- Gemfile
|
104
105
|
- LICENSE.txt
|
105
106
|
- README.md
|
@@ -140,7 +141,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
|
|
140
141
|
version: '0'
|
141
142
|
segments:
|
142
143
|
- 0
|
143
|
-
hash:
|
144
|
+
hash: 3806553411161090089
|
144
145
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
145
146
|
none: false
|
146
147
|
requirements:
|
@@ -149,13 +150,13 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
149
150
|
version: '0'
|
150
151
|
segments:
|
151
152
|
- 0
|
152
|
-
hash:
|
153
|
+
hash: 3806553411161090089
|
153
154
|
requirements: []
|
154
155
|
rubyforge_project:
|
155
156
|
rubygems_version: 1.8.25
|
156
157
|
signing_key:
|
157
158
|
specification_version: 3
|
158
|
-
summary: google analytics api access for the lazy ones
|
159
|
+
summary: google analytics api access server 2 server for the lazy ones
|
159
160
|
test_files:
|
160
161
|
- spec/.DS_Store
|
161
162
|
- spec/fixtures/.DS_Store
|