lazy_google_analytics 0.0.1 → 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
data/.DS_Store CHANGED
Binary file
data/README.md CHANGED
@@ -6,7 +6,6 @@ Lazy google analytics for the lazy programmer. it´s an abstraction around googl
6
6
 
7
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. thats all. Hope you like it.
8
8
 
9
-
10
9
  ## Installation
11
10
 
12
11
  Add this line to your application's Gemfile:
@@ -23,23 +22,38 @@ Or install it yourself as:
23
22
 
24
23
  ## Simple Usage
25
24
 
26
- config_options = pass_phrase: "notasecret",
27
- key_file: /location/to_your/key_file.p12,
28
- client_id: "XXXXX.apps.googleusercontent.com",
29
- scope: "https://www.googleapis.com/auth/analytics.readonly",
30
- profile_id: "XXXXX",
31
- email: "XXXXXX@developer.gserviceaccount.com"
25
+ setup options:
26
+
27
+ LazyGoogleAnalytics::Config.setup do |config|
28
+ config.pass_phrase = "notasecret"
29
+ config.key_file = /location/to_your/key_file.p12
30
+ config.client_id = "XXXXX.apps.googleusercontent.com"
31
+ config.scope = "https://www.googleapis.com/auth/analytics.readonly"
32
+ config.profile_id = "XXXXX"
33
+ config.email = "XXXXXX@developer.gserviceaccount.com"
34
+ end
35
+
36
+ api calls:
37
+
38
+ @client = LazyGoogleAnalytics::Client.new()
32
39
 
33
- @config = LazyGoogleAnalytics::Config.new(config_options)
40
+ By default LazyGoogleAnalytics::Client is going to make a call for visits within 1 month timeline sorted by month & day, using the profile in config block.
34
41
 
35
- @auth = LazyGoogleAnalytics::Auth.new(@config)
42
+ But you can extend that behavior passing and entire options_hash with specific parameters:
36
43
 
37
- @auth.authorize
44
+ @client = LazyGoogleAnalytics::Client.new()
45
+ @client.parameters({'ids' => "ga:XXXXX",
46
+ 'start-date' => "2013-03-12",
47
+ 'end-date' => "2013-04-12",
48
+ 'dimensions' => "ga:day,ga:month",
49
+ 'metrics' => "ga:visits",
50
+ 'sort' => "ga:month,ga:day" })
38
51
 
39
- @client = LazyGoogleAnalytics::Client.new({:config=>@config,
40
- :auth=>@auth,
41
- :client_options => {}
42
- })
52
+ ## Rails 3
53
+
54
+ installs configuration initializer
55
+
56
+ rails g lazy_google_analytics:install
43
57
 
44
58
  ## GA How to:
45
59
 
@@ -50,7 +64,6 @@ If you follow this simple steps , you can´t fail.
50
64
  + Add the created @developer.gserviceaccount.com to your users list in the analytics user panel.
51
65
  + Configure options based on server key and analytics profile id, not the (UA-something) account id!
52
66
 
53
-
54
67
  ## Contributing
55
68
 
56
69
  1. Fork it
@@ -59,13 +72,3 @@ If you follow this simple steps , you can´t fail.
59
72
  4. Push to the branch (`git push origin my-new-feature`)
60
73
  5. Create new Pull Request
61
74
 
62
-
63
- ### TODO
64
-
65
- + Needs some refactor on client options.
66
- + Needs more specs on client options.
67
- + Config Generators for ruby on rails for the laziest ones.
68
-
69
-
70
-
71
-
data/Rakefile CHANGED
@@ -1 +1,10 @@
1
1
  require "bundler/gem_tasks"
2
+ require 'rspec/core/rake_task'
3
+
4
+ desc 'Default: run unit specs.'
5
+ task :default => :spec
6
+
7
+ desc 'Test the lazy_high_charts plugin.'
8
+ RSpec::Core::RakeTask.new('spec') do |t|
9
+ t.pattern = FileList['spec/**/*_spec.rb']
10
+ end
@@ -22,4 +22,7 @@ Gem::Specification.new do |spec|
22
22
 
23
23
  spec.add_development_dependency "bundler", "~> 1.3"
24
24
  spec.add_development_dependency "rake"
25
+ spec.add_dependency "hash-deep-merge"
26
+ spec.add_dependency "activesupport"
27
+
25
28
  end
data/lib/.DS_Store ADDED
Binary file
Binary file
@@ -0,0 +1,21 @@
1
+ # encoding: UTF-8
2
+
3
+ module LazyGoogleAnalytics
4
+ class InstallGenerator < ::Rails::Generators::Base
5
+
6
+ source_root File.expand_path("../../../templates", __FILE__)
7
+
8
+ desc "Creates a Lazy-Google-Analytics initializer."
9
+ #class_option :orm
10
+
11
+ def copy_initializer
12
+ say_status("installing", "Lazy google analytics", :green)
13
+ copy_file "lazy_google_analytics.rb.erb", "config/initializers/lazy_google_analytics.rb"
14
+ end
15
+
16
+ def show_readme
17
+ readme "README" if behavior == :invoke
18
+ end
19
+
20
+ end
21
+ end
@@ -0,0 +1,13 @@
1
+
2
+ Hey, Thanks for install Lazy Google Analytics!
3
+ In order to make Lazy GA work you will need to follow these steps:
4
+
5
+ 1. register your api access in: google api console at https://code.google.com/apis/console/) and create a server key.
6
+
7
+ 2. Download the p12 key. and save it in your app ie:. config/keys/xxx.p12
8
+
9
+ 3. In order give permissions to the generated user to your GA profile you need to add the created @developer.gserviceaccount.com to your users list in the analytics profile user panel.
10
+
11
+ 4. Configure options in config/initializers/lazy_google_analytics.rb
12
+
13
+ Enjoy :)
@@ -0,0 +1,8 @@
1
+ LazyGoogleAnalytics::Config.setup do |config|
2
+ #config.pass_phrase = "notasecret"
3
+ #config.key_file = Rails.root + "/config/XXXXkey_file.p12"
4
+ #config.client_id = "XXXXX.apps.googleusercontent.com"
5
+ #config.scope = "https://www.googleapis.com/auth/analytics.readonly"
6
+ #config.profile_id = "XXXXX" # note that this number corresponds to profile id , not analytics UA-XXXXX number
7
+ #config.email = "XXXXXX@developer.gserviceaccount.com" # you have to add email dev account to users list on analytics profile
8
+ end
Binary file
@@ -3,7 +3,8 @@ module LazyGoogleAnalytics
3
3
 
4
4
  attr_accessor :analytics, :client
5
5
 
6
- def initialize(config)
6
+ def initialize
7
+ config = LazyGoogleAnalytics::Config
7
8
  @key = Google::APIClient::PKCS12.load_key(config.key_file, config.pass_phrase)
8
9
  @asserter = Google::APIClient::JWTAsserter.new( config.email, config.scope, @key)
9
10
  end
@@ -1,37 +1,39 @@
1
1
  module LazyGoogleAnalytics
2
2
  class Client
3
3
 
4
+ CLIENT_OPTIONS = %w(api_method parameters ids start_date end_date dimensions metrics sort filters)
5
+ attr_accessor :options, :auth
6
+
4
7
  def initialize(opts = {})
5
- @config = opts[:config]
6
- @auth = opts[:auth]
7
8
 
8
- @api_method = opts[:client_options][:api_method] ||= @auth.analytics.data.ga.get
9
- @ids = opts[:client_options][:ids] ||= "ga:#{@config.profile_id}"
10
- @start_date = opts[:client_options][:start_date] ||= DateTime.now.prev_month.strftime("%Y-%m-%d")
11
- @end_date = opts[:client_options][:end_date] ||= DateTime.now.strftime("%Y-%m-%d")
12
- @dimensions = opts[:client_options][:dimensions] ||= "ga:day,ga:month"
13
- @metrics = opts[:client_options][:metrics] ||= "ga:visits"
14
- @sort = opts[:client_options][:sort] ||= "ga:month,ga:day"
9
+ @auth ||= LazyGoogleAnalytics::Auth.new
10
+ @auth.authorize # check expiration and cache ?
15
11
 
12
+ self.tap do |client|
13
+ client.options ||= {}
14
+ client.defaults_options(opts)
15
+ client.options ||= opts
16
+ yield client if block_given?
17
+ end
16
18
  end
17
19
 
20
+ def defaults_options(opts)
18
21
 
19
- def results
20
-
21
- startDate = DateTime.now.prev_month.strftime("%Y-%m-%d")
22
- endDate = DateTime.now.strftime("%Y-%m-%d")
22
+ start_date = opts[:start_date] ||= DateTime.now.prev_month.strftime("%Y-%m-%d")
23
+ end_date = opts[:end_date] ||= DateTime.now.strftime("%Y-%m-%d")
23
24
 
24
- options = { :api_method => @api_method,
25
- :parameters => {'ids' => @ids,
26
- 'start-date' => @start_date,
27
- 'end-date' => @end_date,
28
- 'dimensions' => @dimensions,
29
- 'metrics' => @metrics,
30
- 'sort' => @sort }
31
- }
25
+ self.api_method(@auth.analytics.data.ga.get)
26
+ self.parameters({'ids' => "ga:#{LazyGoogleAnalytics::Config.profile_id}",
27
+ 'start-date' => start_date,
28
+ 'end-date' => end_date,
29
+ 'dimensions' => "ga:day,ga:month",
30
+ 'metrics' => "ga:visits",
31
+ 'sort' => "ga:month,ga:day" })
32
+ end
32
33
 
33
- @results = @auth.client.execute( options)
34
34
 
35
+ def results
36
+ @results = @auth.client.execute(@options)
35
37
  end
36
38
 
37
39
  def formatted_columns
@@ -46,6 +48,16 @@ module LazyGoogleAnalytics
46
48
  end
47
49
  end
48
50
 
51
+ def method_missing(meth, opts = {})
52
+ merge_options meth, opts
53
+ end
54
+
55
+ private
56
+
57
+ def merge_options(name, opts)
58
+ @options.merge! name => opts
59
+ end
60
+
49
61
  end
50
62
  end
51
63
 
@@ -1,43 +1,17 @@
1
+ require "active_support/core_ext/module/attribute_accessors"
2
+
1
3
  module LazyGoogleAnalytics
2
4
  class Config
3
5
 
4
- class WrongConfig < StandardError; end
5
-
6
- def initialize(data={})
7
- @data = {}
8
- update!(data)
9
- end
10
-
11
- def update!(data)
12
- data.each do |key, value|
13
- self[key] = value
14
- end
15
- end
16
-
17
- def [](key)
18
- @data[key.to_sym]
19
- end
20
-
21
- def []=(key, value)
22
- if value.class == Hash
23
- @data[key.to_sym] = LazyGoogleAnalytics::Config.new(value)
24
- else
25
- @data[key.to_sym] = value
26
- end
27
- end
28
-
29
- def method_missing(sym, *args)
30
- if sym.to_s =~ /(.+)=$/
31
- self[$1] = args.first
32
- else
33
- self[sym]
34
- end
35
- end
36
-
37
- def has_required_keys?
38
- res = @data.keys - [:pass_phrase, :key_file, :client_id, :scope, :profile_id, :email]
39
- raise LazyGoogleAnalytics::Config::WrongConfig unless res.empty?
6
+ def self.setup
7
+ yield self
40
8
  end
41
9
 
10
+ mattr_accessor :pass_phrase,
11
+ :key_file,
12
+ :client_id,
13
+ :scope,
14
+ :profile_id,
15
+ :email
42
16
  end
43
17
  end
@@ -0,0 +1,8 @@
1
+ # encoding: UTF-8
2
+
3
+ module LazyGoogleAnalytics
4
+ module Rails
5
+ class Engine < ::Rails::Engine
6
+ end
7
+ end
8
+ end
@@ -0,0 +1,8 @@
1
+ # encoding: UTF-8
2
+ module LazyGoogleAnalytics
3
+ class Railtie < ::Rails::Railtie
4
+ config.before_configuration do
5
+ #put some configuration here
6
+ end
7
+ end
8
+ end
@@ -1,3 +1,3 @@
1
1
  module LazyGoogleAnalytics
2
- VERSION = "0.0.1"
2
+ VERSION = "0.1.0"
3
3
  end
@@ -2,6 +2,7 @@
2
2
  $:.unshift(File.dirname(__FILE__)) unless $:.include?(File.dirname(__FILE__)) || $:.include?(File.expand_path(File.dirname(__FILE__)))
3
3
 
4
4
  require "google/api_client"
5
+ require "active_support"
5
6
 
6
7
  module LazyGoogleAnalytics
7
8
 
@@ -10,4 +11,9 @@ module LazyGoogleAnalytics
10
11
  autoload :Auth, 'lazy_google_analytics/auth.rb'
11
12
  autoload :Client, 'lazy_google_analytics/client.rb'
12
13
 
14
+ if defined?(::Rails::Railtie)
15
+ autoload :Railtie, 'lazy_google_analytics/railtie.rb'
16
+ autoload :Rails, 'lazy_google_analytics/engine.rb' if ::Rails.version >= '3.1'
17
+ end
18
+
13
19
  end
data/spec/.DS_Store CHANGED
Binary file
Binary file
@@ -2,8 +2,8 @@ require File.expand_path(File.dirname(__FILE__) + '../../spec_helper')
2
2
 
3
3
  describe "Config" do
4
4
  before(:all) do
5
- @config = LazyGoogleAnalytics::Config.new(config_options)
6
- @auth = LazyGoogleAnalytics::Auth.new(@config)
5
+ config_setup
6
+ @auth = LazyGoogleAnalytics::Auth.new
7
7
  end
8
8
 
9
9
  it "authorization object" do
@@ -2,19 +2,21 @@ require File.expand_path(File.dirname(__FILE__) + '../../spec_helper')
2
2
 
3
3
  describe "Client" do
4
4
  before(:all) do
5
- @config = LazyGoogleAnalytics::Config.new(config_options)
6
- @auth = LazyGoogleAnalytics::Auth.new(@config)
7
- @auth.authorize
8
- @client = LazyGoogleAnalytics::Client.new({
9
- :config=>@config,
10
- :auth=>@auth,
11
- :client_options => {}
12
- })
5
+ config_setup
6
+ @client = LazyGoogleAnalytics::Client.new()
13
7
  end
14
8
 
15
9
  it "find objects object" do
16
- #binding.pry
17
10
  @client.results
18
11
  end
19
12
 
13
+ it "headers" do
14
+ @client.formatted_columns.should == "ga:day\tga:month\tga:visits"
15
+ end
16
+
17
+ it "rows" do
18
+ @client.formatted_rows.class.should == Array
19
+ @client.formatted_rows.should_not be_empty
20
+ end
21
+
20
22
  end
@@ -1,20 +1,24 @@
1
1
  require File.expand_path(File.dirname(__FILE__) + '../../spec_helper')
2
2
 
3
- describe "Config" do
3
+ describe "Config class" do
4
4
  before(:all) do
5
- @config = LazyGoogleAnalytics::Config.new(config_options)
6
5
 
7
- @config2 = LazyGoogleAnalytics::Config.new({
8
- :pass_phrase => "notasecret"
9
- })
6
+ LazyGoogleAnalytics::Config.setup do |config|
7
+ config.pass_phrase = config_options["pass_phrase"]
8
+ config.key_file = config_options["key_file"]
9
+ config.client_id = config_options["client_id"]
10
+ config.scope = config_options["scope"]
11
+ config.profile_id = config_options["profile_id"]
12
+ config.email = config_options["email"]
13
+ end
10
14
  end
11
15
 
12
16
  it "has all the keys required" do
13
- @config.pass_phrase.should_not be_empty
14
- @config.key_file.should_not be_empty
15
- @config.scope.should_not be_empty
16
- @config.profile_id.should_not be_empty
17
- @config.email.should_not be_empty
17
+ LazyGoogleAnalytics::Config.pass_phrase.should_not be_empty
18
+ LazyGoogleAnalytics::Config.key_file.should_not be_empty
19
+ LazyGoogleAnalytics::Config.scope.should_not be_empty
20
+ LazyGoogleAnalytics::Config.profile_id.should_not be_empty
21
+ LazyGoogleAnalytics::Config.email.should_not be_empty
18
22
  end
19
23
 
20
24
  end
data/spec/spec_helper.rb CHANGED
@@ -18,4 +18,16 @@ RSpec.configure do |config|
18
18
  return config
19
19
  end
20
20
 
21
+ def config_setup
22
+ LazyGoogleAnalytics::Config.setup do |config|
23
+ config.pass_phrase = config_options["pass_phrase"]
24
+ config.key_file = config_options["key_file"]
25
+ config.client_id = config_options["client_id"]
26
+ config.scope = config_options["scope"]
27
+ config.profile_id = config_options["profile_id"]
28
+ config.email = config_options["email"]
29
+ end
30
+ LazyGoogleAnalytics::Config
31
+ end
32
+
21
33
  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.0.1
4
+ version: 0.1.0
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-22 00:00:00.000000000 Z
12
+ date: 2013-03-23 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: google-api-client
@@ -59,6 +59,38 @@ dependencies:
59
59
  - - ! '>='
60
60
  - !ruby/object:Gem::Version
61
61
  version: '0'
62
+ - !ruby/object:Gem::Dependency
63
+ name: hash-deep-merge
64
+ requirement: !ruby/object:Gem::Requirement
65
+ none: false
66
+ requirements:
67
+ - - ! '>='
68
+ - !ruby/object:Gem::Version
69
+ version: '0'
70
+ type: :runtime
71
+ prerelease: false
72
+ version_requirements: !ruby/object:Gem::Requirement
73
+ none: false
74
+ requirements:
75
+ - - ! '>='
76
+ - !ruby/object:Gem::Version
77
+ version: '0'
78
+ - !ruby/object:Gem::Dependency
79
+ name: activesupport
80
+ requirement: !ruby/object:Gem::Requirement
81
+ none: false
82
+ requirements:
83
+ - - ! '>='
84
+ - !ruby/object:Gem::Version
85
+ version: '0'
86
+ type: :runtime
87
+ prerelease: false
88
+ version_requirements: !ruby/object:Gem::Requirement
89
+ none: false
90
+ requirements:
91
+ - - ! '>='
92
+ - !ruby/object:Gem::Version
93
+ version: '0'
62
94
  description: google analytics api access for the lazy ones
63
95
  email:
64
96
  - miguelmichelson@gmail.com
@@ -73,10 +105,19 @@ files:
73
105
  - README.md
74
106
  - Rakefile
75
107
  - lazy_google_analytics.gemspec
108
+ - lib/.DS_Store
109
+ - lib/generators/.DS_Store
110
+ - lib/generators/lazy_google_analytics/.DS_Store
111
+ - lib/generators/lazy_google_analytics/install/install_generator.rb
112
+ - lib/generators/templates/README
113
+ - lib/generators/templates/lazy_google_analytics.rb.erb
76
114
  - lib/lazy_google_analytics.rb
115
+ - lib/lazy_google_analytics/.DS_Store
77
116
  - lib/lazy_google_analytics/auth.rb
78
117
  - lib/lazy_google_analytics/client.rb
79
118
  - lib/lazy_google_analytics/config.rb
119
+ - lib/lazy_google_analytics/engine.rb
120
+ - lib/lazy_google_analytics/railtie.rb
80
121
  - lib/lazy_google_analytics/version.rb
81
122
  - spec/.DS_Store
82
123
  - spec/fixtures/.DS_Store
@@ -99,7 +140,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
99
140
  version: '0'
100
141
  segments:
101
142
  - 0
102
- hash: 3802011328619793831
143
+ hash: -2207734988759702426
103
144
  required_rubygems_version: !ruby/object:Gem::Requirement
104
145
  none: false
105
146
  requirements:
@@ -108,7 +149,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
108
149
  version: '0'
109
150
  segments:
110
151
  - 0
111
- hash: 3802011328619793831
152
+ hash: -2207734988759702426
112
153
  requirements: []
113
154
  rubyforge_project:
114
155
  rubygems_version: 1.8.25