gareadonly 0.1.2
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/Gemfile +5 -0
- data/LICENSE.txt +22 -0
- data/README.md +47 -0
- data/Rakefile +1 -0
- data/gareadonly.gemspec +27 -0
- data/lib/gareadonly/configuration.rb +33 -0
- data/lib/gareadonly/connection.rb +20 -0
- data/lib/gareadonly/debug.rb +7 -0
- data/lib/gareadonly/version.rb +3 -0
- data/lib/gareadonly.rb +47 -0
- data/lib/generators/gareadonly/gareadonly_generator.rb +12 -0
- data/lib/generators/templates/gareadonly_initializer.rb +9 -0
- metadata +140 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: 55a1743ecea3acf4037cb39f654b8f29321f9492
|
4
|
+
data.tar.gz: eae62a04cb8eb27ae531f59eda3ff19e3db721da
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 03362b01db04512e257c4b5b117679cf0551f943785a5b038847a7ceca76f08348f1aee546703487a31740ecc99568382f18b3b692d2ef3e41a58299b1638cc1
|
7
|
+
data.tar.gz: 335c566b63439414ddc3f34264c5c6ad40f1c04c81ebc3c90ce386e1c0dbb1fa138756fa8b71f2137a873440b013768f35b32e06174264e05baaba5e9459cda6
|
data/Gemfile
ADDED
data/LICENSE.txt
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
Copyright (c) 2014 theo
|
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,47 @@
|
|
1
|
+
# Google Analytics API Read-only
|
2
|
+
|
3
|
+
GAReadOnly is a simple wrapper to get the data out of google analytics using the API.
|
4
|
+
|
5
|
+
In order to make the gem works, you'll few configuration steps to do in your Google API Console.
|
6
|
+
|
7
|
+
Currently only one method works(The only one I really needed so bad) but I'll add more method). Eventually the gem might became a full wrapper with write/delete/modify actions
|
8
|
+
|
9
|
+
## Installation
|
10
|
+
|
11
|
+
Add this line to your application's Gemfile:
|
12
|
+
|
13
|
+
gem 'gareadonly'
|
14
|
+
|
15
|
+
And then execute:
|
16
|
+
|
17
|
+
$ bundle
|
18
|
+
|
19
|
+
Or install it yourself as:
|
20
|
+
|
21
|
+
$ gem install gareadonly
|
22
|
+
|
23
|
+
## Configuration
|
24
|
+
|
25
|
+
To install the initializer file execute:
|
26
|
+
|
27
|
+
$ rails g gareadonly
|
28
|
+
|
29
|
+
## Usage
|
30
|
+
|
31
|
+
GAReadonly.connect
|
32
|
+
pageviews = GAReadonly.get_analytics_data({'start-date' => '2014-01-01', 'end-date' => 'yesterday', 'metrics' => 'ga:pageviews', 'dimensions' => 'ga:date'})
|
33
|
+
puts "We had #{pageviews.totalsForAllResults['ga:pageviews']} since the 2014-01-01'
|
34
|
+
|
35
|
+
To connect to the Google API, you'll need to run `GAReadonly.connect`. In therory you only need to run it ounce. The connection is persistant.
|
36
|
+
|
37
|
+
Currently the only query available is `get_analytics_data`. It takes an hash as argument. The following hash will retrieve all the page views since the begining of the year 2014 `GAReadonly.get_analytics_data({'start-date' => '2014-01-01', 'end-date' => 'yesterday', 'metrics' => 'ga:pageviews', 'dimensions' => 'ga:date'})`
|
38
|
+
|
39
|
+
## Contributing
|
40
|
+
|
41
|
+
If You want more features, please let me know. Thanks.
|
42
|
+
|
43
|
+
1. Fork it
|
44
|
+
2. Create your feature branch (`git checkout -b my-new-feature`)
|
45
|
+
3. Commit your changes (`git commit -am 'Add some feature'`)
|
46
|
+
4. Push to the branch (`git push origin my-new-feature`)
|
47
|
+
5. Create new Pull Request
|
data/Rakefile
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
require "bundler/gem_tasks"
|
data/gareadonly.gemspec
ADDED
@@ -0,0 +1,27 @@
|
|
1
|
+
# coding: utf-8
|
2
|
+
lib = File.expand_path('../lib', __FILE__)
|
3
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
|
+
require 'GAReadonly/version'
|
5
|
+
|
6
|
+
Gem::Specification.new do |spec|
|
7
|
+
spec.name = "gareadonly"
|
8
|
+
spec.version = GAReadonly::VERSION
|
9
|
+
spec.authors = ["theo negri"]
|
10
|
+
spec.email = ["theo.negri@gmail.com"]
|
11
|
+
spec.description = %q{Simple read-only wrapper for the Google Analytics API}
|
12
|
+
spec.summary = %q{Simple read-only wrapper for the Google Analytics API}
|
13
|
+
spec.homepage = "http://github.com/theonegri/gareadonly"
|
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_development_dependency "bundler", "~> 1.3"
|
22
|
+
spec.add_development_dependency "rake"
|
23
|
+
spec.add_development_dependency 'google-api-client'
|
24
|
+
spec.add_dependency('google-api-client')
|
25
|
+
spec.add_development_dependency 'oauth2'
|
26
|
+
spec.add_dependency('oauth2')
|
27
|
+
end
|
@@ -0,0 +1,33 @@
|
|
1
|
+
module GAReadonly
|
2
|
+
class Configuration
|
3
|
+
attr_accessor :api_scope
|
4
|
+
attr_accessor :application_name
|
5
|
+
attr_accessor :application_version
|
6
|
+
attr_accessor :key
|
7
|
+
attr_accessor :signing_key
|
8
|
+
attr_accessor :issuer
|
9
|
+
attr_accessor :id
|
10
|
+
attr_accessor :client
|
11
|
+
attr_accessor :analytics
|
12
|
+
attr_accessor :discovered_api_name
|
13
|
+
attr_accessor :discovered_api_version
|
14
|
+
attr_accessor :google_url
|
15
|
+
attr_accessor :debug
|
16
|
+
|
17
|
+
API_SCOPE = "https://www.googleapis.com/auth/analytics.readonly"
|
18
|
+
ANALYTICS = 'analytics'
|
19
|
+
VERSION = 'v3'
|
20
|
+
GOOGLE_URL = "https://accounts.google.com/o/oauth2/token"
|
21
|
+
|
22
|
+
def initialize
|
23
|
+
# Set up the defaults
|
24
|
+
self.api_scope = API_SCOPE
|
25
|
+
self.discovered_api_name = ANALYTICS
|
26
|
+
self.discovered_api_version = VERSION
|
27
|
+
self.analytics = ""
|
28
|
+
self.client = ""
|
29
|
+
self.google_url = GOOGLE_URL
|
30
|
+
self.signing_key = ""
|
31
|
+
end
|
32
|
+
end
|
33
|
+
end
|
@@ -0,0 +1,20 @@
|
|
1
|
+
require "google/api_client"
|
2
|
+
require "google/api_client/client_secrets"
|
3
|
+
require "google/api_client/auth/installed_app"
|
4
|
+
require "OAuth2"
|
5
|
+
|
6
|
+
module GAReadonly
|
7
|
+
class Connection
|
8
|
+
def initialize
|
9
|
+
GAReadonly.configuration.client = Google::APIClient.new(:application_name => GAReadonly.configuration.application_name,:application_version => GAReadonly.configuration.application_version)
|
10
|
+
GAReadonly.configuration.signing_key = Google::APIClient::KeyUtils.load_from_pkcs12(GAReadonly.configuration.key, 'notasecret')
|
11
|
+
GAReadonly.configuration.client.authorization = Signet::OAuth2::Client.new(:token_credential_uri => GAReadonly.configuration.google_url,
|
12
|
+
:audience => GAReadonly.configuration.google_url,
|
13
|
+
:scope => GAReadonly.configuration.api_scope,
|
14
|
+
issuer: GAReadonly.configuration.issuer,
|
15
|
+
signing_key: GAReadonly.configuration.signing_key)
|
16
|
+
GAReadonly.configuration.client.authorization.fetch_access_token!
|
17
|
+
GAReadonly.configuration.analytics = GAReadonly.configuration.client.discovered_api(GAReadonly.configuration.discovered_api_name,GAReadonly.configuration.discovered_api_version)
|
18
|
+
end
|
19
|
+
end
|
20
|
+
end
|
data/lib/gareadonly.rb
ADDED
@@ -0,0 +1,47 @@
|
|
1
|
+
require "rubygems"
|
2
|
+
require 'json'
|
3
|
+
require 'ostruct'
|
4
|
+
|
5
|
+
require "GAReadonly/version"
|
6
|
+
require "GAReadonly/configuration"
|
7
|
+
require "GAReadonly/connection"
|
8
|
+
require "GAReadonly/debug"
|
9
|
+
|
10
|
+
module GAReadonly
|
11
|
+
LOG_PREFIX = "** [GAReadonly] "
|
12
|
+
|
13
|
+
class << self
|
14
|
+
# Configure the GAReadonly notifier application-wide settings.
|
15
|
+
def configure(config_hash=nil)
|
16
|
+
if config_hash
|
17
|
+
config_hash.each do |k,v|
|
18
|
+
configuration.send("#{k}=", v) rescue nil if configuration.respond_to?("#{k}=")
|
19
|
+
end
|
20
|
+
end
|
21
|
+
|
22
|
+
yield(configuration) if block_given?
|
23
|
+
end
|
24
|
+
|
25
|
+
# Connect to the GA API
|
26
|
+
def connect
|
27
|
+
Connection.new
|
28
|
+
end
|
29
|
+
|
30
|
+
# Debug logs
|
31
|
+
def debug(message)
|
32
|
+
Debug.new("#{LOG_PREFIX}#{message}") if configuration.debug
|
33
|
+
end
|
34
|
+
|
35
|
+
# Configuration getters
|
36
|
+
def configuration
|
37
|
+
@configuration ||= GAReadonly::Configuration.new
|
38
|
+
end
|
39
|
+
|
40
|
+
def get_analytics_data(options = {})
|
41
|
+
#TODO: check the return co and use the debug when return code says so
|
42
|
+
options = options.merge({'ids' => GAReadonly.configuration.id })
|
43
|
+
data = GAReadonly.configuration.client.execute(:api_method => GAReadonly.configuration.analytics.data.ga.get,:parameters => options)
|
44
|
+
return OpenStruct.new(JSON.parse data.body)
|
45
|
+
end
|
46
|
+
end
|
47
|
+
end
|
@@ -0,0 +1,12 @@
|
|
1
|
+
require 'rails/generators'
|
2
|
+
|
3
|
+
class GareadonlyGenerator < Rails::Generators::Base
|
4
|
+
source_root File.expand_path("../../templates", __FILE__)
|
5
|
+
desc "Creates GAReadonly initializer for your application"
|
6
|
+
|
7
|
+
def copy_initializer
|
8
|
+
template "gareadonly_initializer.rb", "config/initializers/gareadonly.rb"
|
9
|
+
|
10
|
+
puts "Install complete! Thanks!"
|
11
|
+
end
|
12
|
+
end
|
@@ -0,0 +1,9 @@
|
|
1
|
+
GAReadonly.configure do |config|
|
2
|
+
# Set this options to what makes sense for you
|
3
|
+
# config.application_name = 'My App v1'
|
4
|
+
# config.application_version = '1.0'
|
5
|
+
# config.key = 'path_to_your_p12_key'
|
6
|
+
# config.issuer = 'someone@developer.gserviceaccount.com'
|
7
|
+
# config.id = 'ga:XXXX'
|
8
|
+
|
9
|
+
end
|
metadata
ADDED
@@ -0,0 +1,140 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: gareadonly
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.1.2
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- theo negri
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2014-05-09 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: bundler
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - ~>
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: '1.3'
|
20
|
+
type: :development
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - ~>
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '1.3'
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: rake
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - '>='
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '0'
|
34
|
+
type: :development
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - '>='
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '0'
|
41
|
+
- !ruby/object:Gem::Dependency
|
42
|
+
name: google-api-client
|
43
|
+
requirement: !ruby/object:Gem::Requirement
|
44
|
+
requirements:
|
45
|
+
- - '>='
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: '0'
|
48
|
+
type: :development
|
49
|
+
prerelease: false
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - '>='
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: '0'
|
55
|
+
- !ruby/object:Gem::Dependency
|
56
|
+
name: google-api-client
|
57
|
+
requirement: !ruby/object:Gem::Requirement
|
58
|
+
requirements:
|
59
|
+
- - '>='
|
60
|
+
- !ruby/object:Gem::Version
|
61
|
+
version: '0'
|
62
|
+
type: :runtime
|
63
|
+
prerelease: false
|
64
|
+
version_requirements: !ruby/object:Gem::Requirement
|
65
|
+
requirements:
|
66
|
+
- - '>='
|
67
|
+
- !ruby/object:Gem::Version
|
68
|
+
version: '0'
|
69
|
+
- !ruby/object:Gem::Dependency
|
70
|
+
name: oauth2
|
71
|
+
requirement: !ruby/object:Gem::Requirement
|
72
|
+
requirements:
|
73
|
+
- - '>='
|
74
|
+
- !ruby/object:Gem::Version
|
75
|
+
version: '0'
|
76
|
+
type: :development
|
77
|
+
prerelease: false
|
78
|
+
version_requirements: !ruby/object:Gem::Requirement
|
79
|
+
requirements:
|
80
|
+
- - '>='
|
81
|
+
- !ruby/object:Gem::Version
|
82
|
+
version: '0'
|
83
|
+
- !ruby/object:Gem::Dependency
|
84
|
+
name: oauth2
|
85
|
+
requirement: !ruby/object:Gem::Requirement
|
86
|
+
requirements:
|
87
|
+
- - '>='
|
88
|
+
- !ruby/object:Gem::Version
|
89
|
+
version: '0'
|
90
|
+
type: :runtime
|
91
|
+
prerelease: false
|
92
|
+
version_requirements: !ruby/object:Gem::Requirement
|
93
|
+
requirements:
|
94
|
+
- - '>='
|
95
|
+
- !ruby/object:Gem::Version
|
96
|
+
version: '0'
|
97
|
+
description: Simple read-only wrapper for the Google Analytics API
|
98
|
+
email:
|
99
|
+
- theo.negri@gmail.com
|
100
|
+
executables: []
|
101
|
+
extensions: []
|
102
|
+
extra_rdoc_files: []
|
103
|
+
files:
|
104
|
+
- Gemfile
|
105
|
+
- LICENSE.txt
|
106
|
+
- README.md
|
107
|
+
- Rakefile
|
108
|
+
- gareadonly.gemspec
|
109
|
+
- lib/gareadonly.rb
|
110
|
+
- lib/gareadonly/configuration.rb
|
111
|
+
- lib/gareadonly/connection.rb
|
112
|
+
- lib/gareadonly/debug.rb
|
113
|
+
- lib/gareadonly/version.rb
|
114
|
+
- lib/generators/gareadonly/gareadonly_generator.rb
|
115
|
+
- lib/generators/templates/gareadonly_initializer.rb
|
116
|
+
homepage: http://github.com/theonegri/gareadonly
|
117
|
+
licenses:
|
118
|
+
- MIT
|
119
|
+
metadata: {}
|
120
|
+
post_install_message:
|
121
|
+
rdoc_options: []
|
122
|
+
require_paths:
|
123
|
+
- lib
|
124
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
125
|
+
requirements:
|
126
|
+
- - '>='
|
127
|
+
- !ruby/object:Gem::Version
|
128
|
+
version: '0'
|
129
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
130
|
+
requirements:
|
131
|
+
- - '>='
|
132
|
+
- !ruby/object:Gem::Version
|
133
|
+
version: '0'
|
134
|
+
requirements: []
|
135
|
+
rubyforge_project:
|
136
|
+
rubygems_version: 2.1.10
|
137
|
+
signing_key:
|
138
|
+
specification_version: 4
|
139
|
+
summary: Simple read-only wrapper for the Google Analytics API
|
140
|
+
test_files: []
|