lazy_google_analytics 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
data/.DS_Store ADDED
Binary file
data/.gitignore ADDED
@@ -0,0 +1,19 @@
1
+ *.gem
2
+ *.rbc
3
+ .bundle
4
+ .config
5
+ .yardoc
6
+ Gemfile.lock
7
+ InstalledFiles
8
+ _yardoc
9
+ coverage
10
+ doc/
11
+ lib/bundler/man
12
+ pkg
13
+ rdoc
14
+ spec/reports
15
+ test/tmp
16
+ test/version_tmp
17
+ tmp
18
+ spec/fixtures/configs/account_config.yml
19
+ spec/fixtures/keys/
data/Gemfile ADDED
@@ -0,0 +1,8 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in lazy_google_analytics.gemspec
4
+ gemspec
5
+
6
+ gem "debugger"
7
+ gem "rspec"
8
+ gem "pry"
data/LICENSE.txt ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2013 miguel michelson
2
+
3
+ MIT License
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
19
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,71 @@
1
+ # LazyGoogleAnalytics
2
+
3
+ Lazy google analytics for the lazy programmer. it´s an abstraction around google-analytics-client gem.
4
+
5
+ ## Motivation
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 , so i come around with a simple way to implement it. thats all. Hope you like it.
8
+
9
+
10
+ ## Installation
11
+
12
+ Add this line to your application's Gemfile:
13
+
14
+ gem 'lazy_google_analytics'
15
+
16
+ And then execute:
17
+
18
+ $ bundle
19
+
20
+ Or install it yourself as:
21
+
22
+ $ gem install lazy_google_analytics
23
+
24
+ ## Simple Usage
25
+
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"
32
+
33
+ @config = LazyGoogleAnalytics::Config.new(config_options)
34
+
35
+ @auth = LazyGoogleAnalytics::Auth.new(@config)
36
+
37
+ @auth.authorize
38
+
39
+ @client = LazyGoogleAnalytics::Client.new({:config=>@config,
40
+ :auth=>@auth,
41
+ :client_options => {}
42
+ })
43
+
44
+ ## GA How to:
45
+
46
+ If you follow this simple steps , you can´t fail.
47
+
48
+ + First, you have to register your api access in: [google api console](https://code.google.com/apis/console/) and create a server key.
49
+ + Download the p12 key.
50
+ + Add the created @developer.gserviceaccount.com to your users list in the analytics user panel.
51
+ + Configure options based on server key and analytics profile id, not the (UA-something) account id!
52
+
53
+
54
+ ## Contributing
55
+
56
+ 1. Fork it
57
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
58
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
59
+ 4. Push to the branch (`git push origin my-new-feature`)
60
+ 5. Create new Pull Request
61
+
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 ADDED
@@ -0,0 +1 @@
1
+ require "bundler/gem_tasks"
@@ -0,0 +1,25 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'lazy_google_analytics/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "lazy_google_analytics"
8
+ spec.version = LazyGoogleAnalytics::VERSION
9
+ spec.authors = ["miguel michelson"]
10
+ spec.email = ["miguelmichelson@gmail.com"]
11
+ spec.description = %q{google analytics api access for the lazy ones}
12
+ spec.summary = %q{google analytics api access for the lazy ones}
13
+ spec.homepage = ""
14
+ spec.license = "MIT"
15
+
16
+ spec.files = `git ls-files`.split($/)
17
+ spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
18
+ spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
19
+ spec.require_paths = ["lib"]
20
+
21
+ spec.add_runtime_dependency("google-api-client")
22
+
23
+ spec.add_development_dependency "bundler", "~> 1.3"
24
+ spec.add_development_dependency "rake"
25
+ end
@@ -0,0 +1,19 @@
1
+ module LazyGoogleAnalytics
2
+ class Auth
3
+
4
+ attr_accessor :analytics, :client
5
+
6
+ def initialize(config)
7
+ @key = Google::APIClient::PKCS12.load_key(config.key_file, config.pass_phrase)
8
+ @asserter = Google::APIClient::JWTAsserter.new( config.email, config.scope, @key)
9
+ end
10
+
11
+ def authorize
12
+ @client = Google::APIClient.new()
13
+ @client.authorization = @asserter.authorize()
14
+ @analytics = @client.discovered_api("analytics",'v3')
15
+ end
16
+
17
+ end
18
+ end
19
+
@@ -0,0 +1,52 @@
1
+ module LazyGoogleAnalytics
2
+ class Client
3
+
4
+ def initialize(opts = {})
5
+ @config = opts[:config]
6
+ @auth = opts[:auth]
7
+
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"
15
+
16
+ end
17
+
18
+
19
+ def results
20
+
21
+ startDate = DateTime.now.prev_month.strftime("%Y-%m-%d")
22
+ endDate = DateTime.now.strftime("%Y-%m-%d")
23
+
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
+ }
32
+
33
+ @results = @auth.client.execute( options)
34
+
35
+ end
36
+
37
+ def formatted_columns
38
+ (@results || self.results).data.column_headers.map { |c|
39
+ c.name
40
+ }.join("\t")
41
+ end
42
+
43
+ def formatted_rows
44
+ (@results || self.results).data.rows.each do |r|
45
+ print r.join("\t"), "\n"
46
+ end
47
+ end
48
+
49
+ end
50
+ end
51
+
52
+
@@ -0,0 +1,43 @@
1
+ module LazyGoogleAnalytics
2
+ class Config
3
+
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?
40
+ end
41
+
42
+ end
43
+ end
@@ -0,0 +1,3 @@
1
+ module LazyGoogleAnalytics
2
+ VERSION = "0.0.1"
3
+ end
@@ -0,0 +1,13 @@
1
+
2
+ $:.unshift(File.dirname(__FILE__)) unless $:.include?(File.dirname(__FILE__)) || $:.include?(File.expand_path(File.dirname(__FILE__)))
3
+
4
+ require "google/api_client"
5
+
6
+ module LazyGoogleAnalytics
7
+
8
+ autoload :VERSION, 'lazy_google_analytics/version.rb'
9
+ autoload :Config, 'lazy_google_analytics/config.rb'
10
+ autoload :Auth, 'lazy_google_analytics/auth.rb'
11
+ autoload :Client, 'lazy_google_analytics/client.rb'
12
+
13
+ end
data/spec/.DS_Store ADDED
Binary file
Binary file
@@ -0,0 +1,20 @@
1
+ require File.expand_path(File.dirname(__FILE__) + '../../spec_helper')
2
+
3
+ describe "Config" do
4
+ before(:all) do
5
+ @config = LazyGoogleAnalytics::Config.new(config_options)
6
+ @auth = LazyGoogleAnalytics::Auth.new(@config)
7
+ end
8
+
9
+ it "authorization object" do
10
+ @auth.authorize
11
+ @auth.analytics.class.should be Google::APIClient::API
12
+ end
13
+
14
+ end
15
+
16
+
17
+
18
+
19
+
20
+
@@ -0,0 +1,20 @@
1
+ require File.expand_path(File.dirname(__FILE__) + '../../spec_helper')
2
+
3
+ describe "Client" do
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
+ })
13
+ end
14
+
15
+ it "find objects object" do
16
+ #binding.pry
17
+ @client.results
18
+ end
19
+
20
+ end
@@ -0,0 +1,20 @@
1
+ require File.expand_path(File.dirname(__FILE__) + '../../spec_helper')
2
+
3
+ describe "Config" do
4
+ before(:all) do
5
+ @config = LazyGoogleAnalytics::Config.new(config_options)
6
+
7
+ @config2 = LazyGoogleAnalytics::Config.new({
8
+ :pass_phrase => "notasecret"
9
+ })
10
+ end
11
+
12
+ 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
18
+ end
19
+
20
+ end
@@ -0,0 +1,21 @@
1
+ $LOAD_PATH.unshift(File.dirname(__FILE__))
2
+ require 'rspec'
3
+ require 'debugger'
4
+ require File.join(File.dirname(__FILE__), '../lib', 'lazy_google_analytics')
5
+ require 'stringio'
6
+ require "pry"
7
+
8
+ RSpec.configure do |config|
9
+
10
+ def fixture_key(type, filename)
11
+ dir_name = type.to_s + "s"
12
+ File.dirname(__FILE__) + "/fixtures/#{dir_name}/#{filename}"
13
+ end
14
+
15
+ def config_options
16
+ config = YAML.load( File.open(fixture_key("config", "account_config.yml")) )
17
+ config["key_file"] = fixture_key("key", config["pem"])
18
+ return config
19
+ end
20
+
21
+ end
metadata ADDED
@@ -0,0 +1,124 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: lazy_google_analytics
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - miguel michelson
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2013-03-22 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: google-api-client
16
+ requirement: !ruby/object:Gem::Requirement
17
+ none: false
18
+ requirements:
19
+ - - ! '>='
20
+ - !ruby/object:Gem::Version
21
+ version: '0'
22
+ type: :runtime
23
+ prerelease: false
24
+ version_requirements: !ruby/object:Gem::Requirement
25
+ none: false
26
+ requirements:
27
+ - - ! '>='
28
+ - !ruby/object:Gem::Version
29
+ version: '0'
30
+ - !ruby/object:Gem::Dependency
31
+ name: bundler
32
+ requirement: !ruby/object:Gem::Requirement
33
+ none: false
34
+ requirements:
35
+ - - ~>
36
+ - !ruby/object:Gem::Version
37
+ version: '1.3'
38
+ type: :development
39
+ prerelease: false
40
+ version_requirements: !ruby/object:Gem::Requirement
41
+ none: false
42
+ requirements:
43
+ - - ~>
44
+ - !ruby/object:Gem::Version
45
+ version: '1.3'
46
+ - !ruby/object:Gem::Dependency
47
+ name: rake
48
+ requirement: !ruby/object:Gem::Requirement
49
+ none: false
50
+ requirements:
51
+ - - ! '>='
52
+ - !ruby/object:Gem::Version
53
+ version: '0'
54
+ type: :development
55
+ prerelease: false
56
+ version_requirements: !ruby/object:Gem::Requirement
57
+ none: false
58
+ requirements:
59
+ - - ! '>='
60
+ - !ruby/object:Gem::Version
61
+ version: '0'
62
+ description: google analytics api access for the lazy ones
63
+ email:
64
+ - miguelmichelson@gmail.com
65
+ executables: []
66
+ extensions: []
67
+ extra_rdoc_files: []
68
+ files:
69
+ - .DS_Store
70
+ - .gitignore
71
+ - Gemfile
72
+ - LICENSE.txt
73
+ - README.md
74
+ - Rakefile
75
+ - lazy_google_analytics.gemspec
76
+ - lib/lazy_google_analytics.rb
77
+ - lib/lazy_google_analytics/auth.rb
78
+ - lib/lazy_google_analytics/client.rb
79
+ - lib/lazy_google_analytics/config.rb
80
+ - lib/lazy_google_analytics/version.rb
81
+ - spec/.DS_Store
82
+ - spec/fixtures/.DS_Store
83
+ - spec/functional/auth_spec.rb
84
+ - spec/functional/client_spec.rb
85
+ - spec/functional/config_spec.rb
86
+ - spec/spec_helper.rb
87
+ homepage: ''
88
+ licenses:
89
+ - MIT
90
+ post_install_message:
91
+ rdoc_options: []
92
+ require_paths:
93
+ - lib
94
+ required_ruby_version: !ruby/object:Gem::Requirement
95
+ none: false
96
+ requirements:
97
+ - - ! '>='
98
+ - !ruby/object:Gem::Version
99
+ version: '0'
100
+ segments:
101
+ - 0
102
+ hash: 3802011328619793831
103
+ required_rubygems_version: !ruby/object:Gem::Requirement
104
+ none: false
105
+ requirements:
106
+ - - ! '>='
107
+ - !ruby/object:Gem::Version
108
+ version: '0'
109
+ segments:
110
+ - 0
111
+ hash: 3802011328619793831
112
+ requirements: []
113
+ rubyforge_project:
114
+ rubygems_version: 1.8.25
115
+ signing_key:
116
+ specification_version: 3
117
+ summary: google analytics api access for the lazy ones
118
+ test_files:
119
+ - spec/.DS_Store
120
+ - spec/fixtures/.DS_Store
121
+ - spec/functional/auth_spec.rb
122
+ - spec/functional/client_spec.rb
123
+ - spec/functional/config_spec.rb
124
+ - spec/spec_helper.rb