hisui 0.1.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/MIT-LICENSE +20 -0
- data/README.md +72 -0
- data/Rakefile +62 -0
- data/lib/hisui.rb +26 -0
- data/lib/hisui/management/account.rb +51 -0
- data/lib/hisui/management/account_summary.rb +28 -0
- data/lib/hisui/management/base.rb +16 -0
- data/lib/hisui/management/goal.rb +47 -0
- data/lib/hisui/management/profile.rb +52 -0
- data/lib/hisui/management/segment.rb +18 -0
- data/lib/hisui/management/web_property.rb +56 -0
- data/lib/hisui/model.rb +41 -0
- data/lib/hisui/request.rb +41 -0
- data/lib/hisui/response.rb +72 -0
- data/lib/hisui/user.rb +33 -0
- data/lib/hisui/version.rb +3 -0
- metadata +200 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: 1020a01dd1a5f3b6aa38c32115d62e640445ce1e
|
4
|
+
data.tar.gz: 3594dc12b39b223e3fb865edf336800f842ffcf1
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 85cb0d5f217b557933c7eb26ff82ab01fbe96e2fbd90822682515473835efec78bc425da35466b0af55f88f17801cf455b83291fef2297dddea323d35589ca43
|
7
|
+
data.tar.gz: 4103a42346735e9faf18cdb5e07c43bda310e4958bf80e913658eac1046a03077a0b711bc9bfb378bb2af8cc85ec536d6b25bab44634bcc4b1b4f181fa80365f
|
data/MIT-LICENSE
ADDED
@@ -0,0 +1,20 @@
|
|
1
|
+
Copyright 2017 ikepon
|
2
|
+
|
3
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
4
|
+
a copy of this software and associated documentation files (the
|
5
|
+
"Software"), to deal in the Software without restriction, including
|
6
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
7
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
8
|
+
permit persons to whom the Software is furnished to do so, subject to
|
9
|
+
the following conditions:
|
10
|
+
|
11
|
+
The above copyright notice and this permission notice shall be
|
12
|
+
included in all copies or substantial portions of the Software.
|
13
|
+
|
14
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
15
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
16
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
17
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
18
|
+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
19
|
+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
20
|
+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,72 @@
|
|
1
|
+
# Hisui [![Build Status](https://travis-ci.org/ikepon/hisui.png)](https://travis-ci.org/ikepon/hisui)
|
2
|
+
|
3
|
+
Google Analytics Reporting API v4 Client on Rails application
|
4
|
+
|
5
|
+
Hisui referred to [Legato](https://github.com/tpitale/legato) which is Google Analytics Reporting API Client for Ruby.
|
6
|
+
|
7
|
+
Legato uses Core Reporting API V3.
|
8
|
+
Hisui uses Reporting API v4.
|
9
|
+
|
10
|
+
But, usage is similar to Legato.
|
11
|
+
|
12
|
+
## Installation
|
13
|
+
Add this line to your application's Gemfile:
|
14
|
+
|
15
|
+
```ruby
|
16
|
+
gem 'hisui'
|
17
|
+
```
|
18
|
+
|
19
|
+
And then execute:
|
20
|
+
```bash
|
21
|
+
$ bundle install
|
22
|
+
```
|
23
|
+
|
24
|
+
## Usage
|
25
|
+
### Google Analytics Management API Version 3.0
|
26
|
+
Usage is the same as [Legato](https://github.com/tpitale/legato/blob/master/README.md#google-analytics-management).
|
27
|
+
|
28
|
+
### Google Analytics Reporting API v4
|
29
|
+
1. Get profile.
|
30
|
+
```ruby
|
31
|
+
user = Hisui::User.new(access_token)
|
32
|
+
profile = user.profiles.first # Select profile that you want to get Google Analytics data.
|
33
|
+
```
|
34
|
+
|
35
|
+
2. Create class extended `Hisui::Model`. And Set `metrics` and `dimensions`.
|
36
|
+
```ruby
|
37
|
+
class DailySummary
|
38
|
+
extend Hisui::Model
|
39
|
+
|
40
|
+
metrics %i[pageviews sessions users new_users bounce_rate pageviews_per_session avg_session_duration]
|
41
|
+
dimensions %i[date]
|
42
|
+
end
|
43
|
+
```
|
44
|
+
|
45
|
+
3. Get Google Analytics API response with `results` methods.
|
46
|
+
Set `start_date` and `end_date` if you need.(defult period is past one month)
|
47
|
+
```ruby
|
48
|
+
response = DailySummary.results(profile: profile, start_date: Date.current - 7.days, end_date: Date.current)
|
49
|
+
```
|
50
|
+
|
51
|
+
4. Use data.
|
52
|
+
```ruby
|
53
|
+
response.raw_attributes
|
54
|
+
|
55
|
+
#=> [#<OpenStruct date="20171122", pageviews="137", sessions="73", users="51", newUsers="43", bounceRate="69.56521739130434", pageviewsPerSession="2.608695652173913", avgSessionDuration="87.69565217391305">,
|
56
|
+
# ...
|
57
|
+
# #<OpenStruct date="20171129", pageviews="95", sessions="67", users="44", newUsers="32", bounceRate="80.0", pageviewsPerSession="2.25", avgSessionDuration="42.0">]
|
58
|
+
|
59
|
+
response.total_values
|
60
|
+
|
61
|
+
#=> #<OpenStruct pageviews="646", sessions="308", users="223", newUsers="144", bounceRate="77.77777777777779", pageviewsPerSession="2.3518518518518519", avgSessionDuration="62.148148148148145">
|
62
|
+
|
63
|
+
response.date?
|
64
|
+
|
65
|
+
#=> true
|
66
|
+
```
|
67
|
+
|
68
|
+
## Contributing
|
69
|
+
Fork it, fix me, and send me your pull requests.
|
70
|
+
|
71
|
+
## License
|
72
|
+
The gem is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT).
|
data/Rakefile
ADDED
@@ -0,0 +1,62 @@
|
|
1
|
+
begin
|
2
|
+
require 'bundler/setup'
|
3
|
+
rescue LoadError
|
4
|
+
puts 'You must `gem install bundler` and `bundle install` to run rake tasks'
|
5
|
+
end
|
6
|
+
|
7
|
+
require 'bundler/gem_tasks'
|
8
|
+
require 'rspec/core/rake_task'
|
9
|
+
require 'oauth2'
|
10
|
+
|
11
|
+
task default: :spec
|
12
|
+
|
13
|
+
RSpec::Core::RakeTask.new
|
14
|
+
|
15
|
+
module OAuth2Helpers
|
16
|
+
def build_client(id, secret)
|
17
|
+
opts = {
|
18
|
+
:authorize_url => 'https://accounts.google.com/o/oauth2/auth',
|
19
|
+
:token_url => 'https://accounts.google.com/o/oauth2/token'
|
20
|
+
}
|
21
|
+
|
22
|
+
OAuth2::Client.new(id, secret, opts)
|
23
|
+
end
|
24
|
+
|
25
|
+
def auth_url(client)
|
26
|
+
client.auth_code.authorize_url({
|
27
|
+
:scope => 'https://www.googleapis.com/auth/analytics.readonly',
|
28
|
+
:redirect_uri => 'http://localhost'
|
29
|
+
})
|
30
|
+
end
|
31
|
+
|
32
|
+
extend self
|
33
|
+
end
|
34
|
+
|
35
|
+
namespace :oauth2 do
|
36
|
+
desc 'Get a new OAuth2 Token'
|
37
|
+
task :token do
|
38
|
+
puts 'Get your OAuth2 id and secret from the API Console: https://code.google.com/apis/console#access'
|
39
|
+
|
40
|
+
puts
|
41
|
+
print 'Your OAuth2 id: '
|
42
|
+
oauth_id = $stdin.gets.strip
|
43
|
+
print 'Your OAuth2 secret: '
|
44
|
+
oauth_secret = $stdin.gets.strip
|
45
|
+
|
46
|
+
client = OAuth2Helpers.build_client(oauth_id, oauth_secret)
|
47
|
+
|
48
|
+
puts
|
49
|
+
puts "Opening the OAuth2 auth url: #{OAuth2Helpers.auth_url(client)} ..."
|
50
|
+
`open "#{OAuth2Helpers.auth_url(client)}"`
|
51
|
+
|
52
|
+
puts
|
53
|
+
print 'OAuth2 Code (in the url): '
|
54
|
+
code = $stdin.gets.strip
|
55
|
+
|
56
|
+
access_token = client.auth_code.get_token(code, :redirect_uri => 'http://localhost')
|
57
|
+
|
58
|
+
puts
|
59
|
+
puts "Here's your access token: "
|
60
|
+
puts access_token.token
|
61
|
+
end
|
62
|
+
end
|
data/lib/hisui.rb
ADDED
@@ -0,0 +1,26 @@
|
|
1
|
+
require 'hisui/version'
|
2
|
+
|
3
|
+
require 'hisui/user'
|
4
|
+
require 'hisui/model'
|
5
|
+
require 'hisui/request'
|
6
|
+
require 'hisui/response'
|
7
|
+
|
8
|
+
require 'hisui/management/base'
|
9
|
+
require 'hisui/management/account'
|
10
|
+
require 'hisui/management/account_summary'
|
11
|
+
require 'hisui/management/goal'
|
12
|
+
require 'hisui/management/profile'
|
13
|
+
require 'hisui/management/segment'
|
14
|
+
require 'hisui/management/web_property'
|
15
|
+
|
16
|
+
module Hisui
|
17
|
+
class << self
|
18
|
+
def from_ga_string(str)
|
19
|
+
str.gsub(/ga:|mcf:|rt:/, '')
|
20
|
+
end
|
21
|
+
|
22
|
+
def to_ga_string(str, tracking_scope = "ga")
|
23
|
+
"#{$1}#{tracking_scope}:#{$2}" if str.to_s.camelize(:lower) =~ /^(-)?(.*)$/
|
24
|
+
end
|
25
|
+
end
|
26
|
+
end
|
@@ -0,0 +1,51 @@
|
|
1
|
+
module Hisui
|
2
|
+
module Management
|
3
|
+
class Account < Base
|
4
|
+
attr_accessor :id, :name, :user, :web_properties
|
5
|
+
|
6
|
+
def initialize(attributes, user, web_properties = nil)
|
7
|
+
@id = attributes['id']
|
8
|
+
@name = attributes['name']
|
9
|
+
@user = user
|
10
|
+
@web_properties = web_properties
|
11
|
+
end
|
12
|
+
|
13
|
+
class << self
|
14
|
+
def default_path
|
15
|
+
'/accounts'
|
16
|
+
end
|
17
|
+
|
18
|
+
def build_from_summary(attributes, user)
|
19
|
+
web_properties_attributes = attributes[:webProperties]
|
20
|
+
|
21
|
+
summary_properties = web_properties_attributes.inject([]) { |props, web_property_attributes|
|
22
|
+
web_property_attributes[:accountId] = attributes[:id]
|
23
|
+
props << WebProperty.build_from_summary(web_property_attributes, user)
|
24
|
+
}
|
25
|
+
|
26
|
+
Account.new(attributes, user, summary_properties)
|
27
|
+
end
|
28
|
+
|
29
|
+
def from_child(child)
|
30
|
+
all(child.user).detect { |a| a.id == child.account_id }
|
31
|
+
end
|
32
|
+
end
|
33
|
+
|
34
|
+
def path
|
35
|
+
"/accounts/#{id}"
|
36
|
+
end
|
37
|
+
|
38
|
+
def web_properties
|
39
|
+
@web_properties ||= WebProperty.for_account(self)
|
40
|
+
end
|
41
|
+
|
42
|
+
def profiles
|
43
|
+
if web_properties
|
44
|
+
web_properties.inject([]) { |profiles, prop| profiles.concat(prop.profiles) }
|
45
|
+
else
|
46
|
+
Profile.for_account(self)
|
47
|
+
end
|
48
|
+
end
|
49
|
+
end
|
50
|
+
end
|
51
|
+
end
|
@@ -0,0 +1,28 @@
|
|
1
|
+
module Hisui
|
2
|
+
module Management
|
3
|
+
class AccountSummary < Base
|
4
|
+
attr_accessor :user, :account
|
5
|
+
|
6
|
+
def initialize(attributes, user)
|
7
|
+
@account = Account.build_from_summary(attributes, user)
|
8
|
+
@user = user
|
9
|
+
end
|
10
|
+
|
11
|
+
def self.default_path
|
12
|
+
'/accountSummaries'
|
13
|
+
end
|
14
|
+
|
15
|
+
def path
|
16
|
+
''
|
17
|
+
end
|
18
|
+
|
19
|
+
def profiles
|
20
|
+
account.profiles
|
21
|
+
end
|
22
|
+
|
23
|
+
def web_properties
|
24
|
+
account.web_properties
|
25
|
+
end
|
26
|
+
end
|
27
|
+
end
|
28
|
+
end
|
@@ -0,0 +1,16 @@
|
|
1
|
+
module Hisui
|
2
|
+
module Management
|
3
|
+
class Base
|
4
|
+
class << self
|
5
|
+
def base_uri
|
6
|
+
'https://www.googleapis.com/analytics/v3/management'
|
7
|
+
end
|
8
|
+
|
9
|
+
def all(user, path = default_path)
|
10
|
+
json = user.access_token.get(base_uri + path).body
|
11
|
+
JSON.parse(json)['items'].map { |item| new(item.with_indifferent_access, user) }
|
12
|
+
end
|
13
|
+
end
|
14
|
+
end
|
15
|
+
end
|
16
|
+
end
|
@@ -0,0 +1,47 @@
|
|
1
|
+
module Hisui
|
2
|
+
module Management
|
3
|
+
class Goal < Base
|
4
|
+
GA_ATTRIBUTES = {
|
5
|
+
:id => 'id',
|
6
|
+
:name => 'name',
|
7
|
+
:account_id => 'accountId',
|
8
|
+
:web_property_id => 'webPropertyId',
|
9
|
+
:profile_id => 'profileId'
|
10
|
+
}
|
11
|
+
|
12
|
+
attr_accessor *GA_ATTRIBUTES.keys
|
13
|
+
attr_accessor :attributes, :user
|
14
|
+
|
15
|
+
def initialize(attributes, user)
|
16
|
+
GA_ATTRIBUTES.each do |key, string_key|
|
17
|
+
self.send("#{key}=", attributes.delete(string_key) || attributes.delete(key))
|
18
|
+
end
|
19
|
+
|
20
|
+
@attributes = attributes
|
21
|
+
@user = user
|
22
|
+
end
|
23
|
+
|
24
|
+
class << self
|
25
|
+
def default_path
|
26
|
+
'/accounts/~all/webproperties/~all/profiles/~all/goals'
|
27
|
+
end
|
28
|
+
|
29
|
+
def for_account(account)
|
30
|
+
all(account.user, account.path + '/webproperties/~all/profiles/~all/goals')
|
31
|
+
end
|
32
|
+
|
33
|
+
def for_web_property(web_property)
|
34
|
+
all(web_property.user, web_property.path + '/profiles/~all/goals')
|
35
|
+
end
|
36
|
+
|
37
|
+
def for_profile(profile)
|
38
|
+
all(profile.user, profile.path + '/goals')
|
39
|
+
end
|
40
|
+
end
|
41
|
+
|
42
|
+
def path
|
43
|
+
self.class.default_path + '/' + id.to_s
|
44
|
+
end
|
45
|
+
end
|
46
|
+
end
|
47
|
+
end
|
@@ -0,0 +1,52 @@
|
|
1
|
+
module Hisui
|
2
|
+
module Management
|
3
|
+
class Profile < Base
|
4
|
+
GA_ATTRIBUTES = {
|
5
|
+
:id => 'id',
|
6
|
+
:name => 'name',
|
7
|
+
:account_id => 'accountId',
|
8
|
+
:web_property_id => 'webPropertyId'
|
9
|
+
}
|
10
|
+
|
11
|
+
attr_accessor *GA_ATTRIBUTES.keys
|
12
|
+
attr_accessor :attributes, :user
|
13
|
+
|
14
|
+
def initialize(attributes, user)
|
15
|
+
GA_ATTRIBUTES.each do |key, string_key|
|
16
|
+
self.send("#{key}=", attributes.delete(string_key) || attributes.delete(key))
|
17
|
+
end
|
18
|
+
|
19
|
+
@attributes = attributes
|
20
|
+
@user = user
|
21
|
+
end
|
22
|
+
|
23
|
+
class << self
|
24
|
+
def default_path
|
25
|
+
'/accounts/~all/webproperties/~all/profiles'
|
26
|
+
end
|
27
|
+
|
28
|
+
def build_from_summary(attributes, user)
|
29
|
+
Profile.new(attributes, user)
|
30
|
+
end
|
31
|
+
|
32
|
+
def for_account(account)
|
33
|
+
all(account.user, account.path + '/webproperties/~all/profiles')
|
34
|
+
end
|
35
|
+
|
36
|
+
def for_web_property(web_property)
|
37
|
+
all(web_property.user, web_property.path + '/profiles')
|
38
|
+
end
|
39
|
+
end
|
40
|
+
|
41
|
+
def path
|
42
|
+
"/accounts/#{account_id}/webproperties/#{web_property_id}/profiles/#{id}"
|
43
|
+
end
|
44
|
+
|
45
|
+
# XXX: Only use `user.profiles.#{profile}.goals
|
46
|
+
# Cannot use `web_properties.#{web_property}.profiles.#{profile}.goals
|
47
|
+
def goals
|
48
|
+
Goal.for_profile(self)
|
49
|
+
end
|
50
|
+
end
|
51
|
+
end
|
52
|
+
end
|
@@ -0,0 +1,18 @@
|
|
1
|
+
module Hisui
|
2
|
+
module Management
|
3
|
+
class Segment < Base
|
4
|
+
attr_accessor :id, :name, :definition, :user
|
5
|
+
|
6
|
+
def initialize(attributes, user)
|
7
|
+
@id = attributes['id']
|
8
|
+
@name = attributes['name']
|
9
|
+
@definition = attributes['definition']
|
10
|
+
@user = user
|
11
|
+
end
|
12
|
+
|
13
|
+
def self.default_path
|
14
|
+
"/segments"
|
15
|
+
end
|
16
|
+
end
|
17
|
+
end
|
18
|
+
end
|
@@ -0,0 +1,56 @@
|
|
1
|
+
module Hisui
|
2
|
+
module Management
|
3
|
+
class WebProperty < Base
|
4
|
+
GA_ATTRIBUTES = {
|
5
|
+
:id => 'id',
|
6
|
+
:name => 'name',
|
7
|
+
:account_id => 'accountId',
|
8
|
+
:website_url => 'websiteUrl'
|
9
|
+
}
|
10
|
+
|
11
|
+
attr_accessor *GA_ATTRIBUTES.keys
|
12
|
+
attr_accessor :attributes, :user, :profiles
|
13
|
+
|
14
|
+
def initialize(attributes, user, profiles = nil)
|
15
|
+
GA_ATTRIBUTES.each do |key, string_key|
|
16
|
+
self.send("#{key}=", attributes.delete(string_key) || attributes.delete(key))
|
17
|
+
end
|
18
|
+
|
19
|
+
@attributes = attributes
|
20
|
+
@user = user
|
21
|
+
@profiles = profiles
|
22
|
+
end
|
23
|
+
|
24
|
+
class << self
|
25
|
+
def default_path
|
26
|
+
'/accounts/~all/webproperties'
|
27
|
+
end
|
28
|
+
|
29
|
+
def build_from_summary(attributes, user)
|
30
|
+
summary_profiles = attributes[:profiles].inject([]) { |summary_profiles, profile|
|
31
|
+
profile[:webPropertyId] = attributes[:id]
|
32
|
+
summary_profiles << Profile.build_from_summary(profile, user)
|
33
|
+
}
|
34
|
+
|
35
|
+
new(attributes, user, summary_profiles)
|
36
|
+
end
|
37
|
+
|
38
|
+
def for_account(account)
|
39
|
+
all(account.user, account.path + '/webproperties')
|
40
|
+
end
|
41
|
+
end
|
42
|
+
|
43
|
+
def path
|
44
|
+
"/accounts/#{account_id}/webproperties/#{id}"
|
45
|
+
end
|
46
|
+
|
47
|
+
def profiles
|
48
|
+
@profiles ||= Profile.for_web_property(self)
|
49
|
+
end
|
50
|
+
|
51
|
+
def account
|
52
|
+
Account.from_child(self)
|
53
|
+
end
|
54
|
+
end
|
55
|
+
end
|
56
|
+
end
|
data/lib/hisui/model.rb
ADDED
@@ -0,0 +1,41 @@
|
|
1
|
+
module Hisui
|
2
|
+
module Model
|
3
|
+
# Adds metrics to the class for retrieval from GA
|
4
|
+
#
|
5
|
+
# @param fields [Symbol] the names of the fields to retrieve
|
6
|
+
# @return [ListParameter] the set of all metrics
|
7
|
+
def metrics(field = nil)
|
8
|
+
@metrics ||= Set.new([])
|
9
|
+
field.try(:each) { |f| @metrics << { expression: Hisui.to_ga_string(f) } }
|
10
|
+
@metrics
|
11
|
+
end
|
12
|
+
|
13
|
+
# Adds dimensions to the class for retrieval from GA
|
14
|
+
#
|
15
|
+
# @param fields [Symbol] the names of the fields to retrieve
|
16
|
+
# @return [ListParameter] the set of all dimensions
|
17
|
+
def dimensions(field = nil)
|
18
|
+
@dimensions ||= Set.new([])
|
19
|
+
field.try(:each) { |f| @dimensions << { name: Hisui.to_ga_string(f) } }
|
20
|
+
@dimensions
|
21
|
+
end
|
22
|
+
|
23
|
+
# Adds order_bys to the class for retrieval from GA
|
24
|
+
#
|
25
|
+
# @param field [Hash] options:
|
26
|
+
# * field_name
|
27
|
+
# * order_type
|
28
|
+
# * sort_order
|
29
|
+
#
|
30
|
+
# cf. https://developers.google.com/analytics/devguides/reporting/core/v4/rest/v4/reports/batchGet#OrderBy
|
31
|
+
def order_bys(field = nil)
|
32
|
+
@order_bys ||= Set.new([])
|
33
|
+
@order_bys << field if field
|
34
|
+
@order_bys
|
35
|
+
end
|
36
|
+
|
37
|
+
def results(profile:, **options)
|
38
|
+
Hisui::Request.new(profile: profile, model: self, **options).fetch_data
|
39
|
+
end
|
40
|
+
end
|
41
|
+
end
|
@@ -0,0 +1,41 @@
|
|
1
|
+
require 'google/apis/analyticsreporting_v4'
|
2
|
+
|
3
|
+
module Hisui
|
4
|
+
class Request
|
5
|
+
BASIC_OPTION_KEYS = [:start_date, :end_date, :limit]
|
6
|
+
|
7
|
+
attr_accessor :profile, :model, :start_date, :end_date, :limit
|
8
|
+
|
9
|
+
def initialize(profile:, model:, **options)
|
10
|
+
@profile = profile
|
11
|
+
@model = model
|
12
|
+
@start_date = Time.current.advance(months: -1)
|
13
|
+
@end_date = Time.current
|
14
|
+
|
15
|
+
BASIC_OPTION_KEYS.each do |key|
|
16
|
+
self.send("#{key}=".to_sym, options[key]) if options.try(:has_key?, key)
|
17
|
+
end
|
18
|
+
end
|
19
|
+
|
20
|
+
def fetch_data
|
21
|
+
reporting_service = Google::Apis::AnalyticsreportingV4::AnalyticsReportingService.new
|
22
|
+
reporting_service.authorization = profile.user.access_token.token
|
23
|
+
|
24
|
+
date_range = Google::Apis::AnalyticsreportingV4::DateRange.new(start_date: start_date.to_s, end_date: end_date.to_s)
|
25
|
+
|
26
|
+
request = Google::Apis::AnalyticsreportingV4::GetReportsRequest.new(
|
27
|
+
report_requests: [Google::Apis::AnalyticsreportingV4::ReportRequest.new(
|
28
|
+
view_id: profile.id,
|
29
|
+
metrics: model.metrics,
|
30
|
+
dimensions: model.dimensions,
|
31
|
+
date_ranges: [date_range],
|
32
|
+
order_bys: model.order_bys,
|
33
|
+
page_size: limit
|
34
|
+
)]
|
35
|
+
)
|
36
|
+
|
37
|
+
response = reporting_service.batch_get_reports(request)
|
38
|
+
Hisui::Response.new(response)
|
39
|
+
end
|
40
|
+
end
|
41
|
+
end
|
@@ -0,0 +1,72 @@
|
|
1
|
+
module Hisui
|
2
|
+
class Response
|
3
|
+
attr_reader :response
|
4
|
+
|
5
|
+
def initialize(response)
|
6
|
+
@response = response
|
7
|
+
end
|
8
|
+
|
9
|
+
def raw_attributes
|
10
|
+
@raw_attributes ||= begin
|
11
|
+
raw_attributes = []
|
12
|
+
|
13
|
+
data.rows.each do |row|
|
14
|
+
row_data = []
|
15
|
+
row.dimensions.each do |dimension|
|
16
|
+
row_data << dimension
|
17
|
+
end
|
18
|
+
|
19
|
+
row.metrics.first.values.each do |value|
|
20
|
+
row_data << value
|
21
|
+
end
|
22
|
+
|
23
|
+
raw_attributes << Hash[fields.zip(row_data)]
|
24
|
+
end
|
25
|
+
|
26
|
+
raw_attributes.map { |attributes| OpenStruct.new(attributes) }
|
27
|
+
end
|
28
|
+
end
|
29
|
+
|
30
|
+
def total_values
|
31
|
+
@total_values ||= OpenStruct.new(Hash[metrics.zip(data.totals.first.values)])
|
32
|
+
end
|
33
|
+
|
34
|
+
def data?
|
35
|
+
data.row_count.to_i > 0
|
36
|
+
end
|
37
|
+
|
38
|
+
private
|
39
|
+
|
40
|
+
def column_header
|
41
|
+
@column_header = response.reports.first.column_header
|
42
|
+
end
|
43
|
+
|
44
|
+
def metrics
|
45
|
+
@metrics ||= begin
|
46
|
+
column_header.metric_header.metric_header_entries.each_with_object([]) do |metric, arr|
|
47
|
+
arr << Hisui.from_ga_string(metric.name)
|
48
|
+
end
|
49
|
+
end
|
50
|
+
end
|
51
|
+
|
52
|
+
def fields
|
53
|
+
@fields ||= begin
|
54
|
+
fields = []
|
55
|
+
|
56
|
+
column_header.dimensions.each do |dimension|
|
57
|
+
fields << Hisui.from_ga_string(dimension)
|
58
|
+
end
|
59
|
+
|
60
|
+
column_header.metric_header.metric_header_entries.each do |metric|
|
61
|
+
fields << Hisui.from_ga_string(metric.name)
|
62
|
+
end
|
63
|
+
|
64
|
+
fields
|
65
|
+
end
|
66
|
+
end
|
67
|
+
|
68
|
+
def data
|
69
|
+
@data = response.reports.first.data
|
70
|
+
end
|
71
|
+
end
|
72
|
+
end
|
data/lib/hisui/user.rb
ADDED
@@ -0,0 +1,33 @@
|
|
1
|
+
module Hisui
|
2
|
+
class User
|
3
|
+
attr_accessor :access_token
|
4
|
+
|
5
|
+
def initialize(access_token)
|
6
|
+
@access_token = access_token
|
7
|
+
end
|
8
|
+
|
9
|
+
# All the `Account` records available to this user
|
10
|
+
def accounts
|
11
|
+
Management::Account.all(self)
|
12
|
+
end
|
13
|
+
|
14
|
+
def account_summaries
|
15
|
+
Management::AccountSummary.all(self)
|
16
|
+
end
|
17
|
+
|
18
|
+
# All the `Profile` records available to this user
|
19
|
+
def profiles
|
20
|
+
Management::Profile.all(self)
|
21
|
+
end
|
22
|
+
|
23
|
+
# All the `Segment` records available to this user
|
24
|
+
def segments
|
25
|
+
Management::Segment.all(self)
|
26
|
+
end
|
27
|
+
|
28
|
+
# All the `WebProperty` records available to this user
|
29
|
+
def web_properties
|
30
|
+
Management::WebProperty.all(self)
|
31
|
+
end
|
32
|
+
end
|
33
|
+
end
|
metadata
ADDED
@@ -0,0 +1,200 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: hisui
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.1.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- ikepon
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2017-11-29 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: rails
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - ">="
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: 4.1.0
|
20
|
+
type: :runtime
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - ">="
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: 4.1.0
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: oauth2
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - ">="
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '0'
|
34
|
+
type: :runtime
|
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: :runtime
|
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: sqlite3
|
57
|
+
requirement: !ruby/object:Gem::Requirement
|
58
|
+
requirements:
|
59
|
+
- - ">="
|
60
|
+
- !ruby/object:Gem::Version
|
61
|
+
version: '0'
|
62
|
+
type: :development
|
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: dotenv
|
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: rspec-rails
|
85
|
+
requirement: !ruby/object:Gem::Requirement
|
86
|
+
requirements:
|
87
|
+
- - ">="
|
88
|
+
- !ruby/object:Gem::Version
|
89
|
+
version: '0'
|
90
|
+
type: :development
|
91
|
+
prerelease: false
|
92
|
+
version_requirements: !ruby/object:Gem::Requirement
|
93
|
+
requirements:
|
94
|
+
- - ">="
|
95
|
+
- !ruby/object:Gem::Version
|
96
|
+
version: '0'
|
97
|
+
- !ruby/object:Gem::Dependency
|
98
|
+
name: vcr
|
99
|
+
requirement: !ruby/object:Gem::Requirement
|
100
|
+
requirements:
|
101
|
+
- - ">="
|
102
|
+
- !ruby/object:Gem::Version
|
103
|
+
version: '0'
|
104
|
+
type: :development
|
105
|
+
prerelease: false
|
106
|
+
version_requirements: !ruby/object:Gem::Requirement
|
107
|
+
requirements:
|
108
|
+
- - ">="
|
109
|
+
- !ruby/object:Gem::Version
|
110
|
+
version: '0'
|
111
|
+
- !ruby/object:Gem::Dependency
|
112
|
+
name: webmock
|
113
|
+
requirement: !ruby/object:Gem::Requirement
|
114
|
+
requirements:
|
115
|
+
- - ">="
|
116
|
+
- !ruby/object:Gem::Version
|
117
|
+
version: '0'
|
118
|
+
type: :development
|
119
|
+
prerelease: false
|
120
|
+
version_requirements: !ruby/object:Gem::Requirement
|
121
|
+
requirements:
|
122
|
+
- - ">="
|
123
|
+
- !ruby/object:Gem::Version
|
124
|
+
version: '0'
|
125
|
+
- !ruby/object:Gem::Dependency
|
126
|
+
name: pry-byebug
|
127
|
+
requirement: !ruby/object:Gem::Requirement
|
128
|
+
requirements:
|
129
|
+
- - ">="
|
130
|
+
- !ruby/object:Gem::Version
|
131
|
+
version: '0'
|
132
|
+
type: :development
|
133
|
+
prerelease: false
|
134
|
+
version_requirements: !ruby/object:Gem::Requirement
|
135
|
+
requirements:
|
136
|
+
- - ">="
|
137
|
+
- !ruby/object:Gem::Version
|
138
|
+
version: '0'
|
139
|
+
- !ruby/object:Gem::Dependency
|
140
|
+
name: simplecov
|
141
|
+
requirement: !ruby/object:Gem::Requirement
|
142
|
+
requirements:
|
143
|
+
- - ">="
|
144
|
+
- !ruby/object:Gem::Version
|
145
|
+
version: '0'
|
146
|
+
type: :development
|
147
|
+
prerelease: false
|
148
|
+
version_requirements: !ruby/object:Gem::Requirement
|
149
|
+
requirements:
|
150
|
+
- - ">="
|
151
|
+
- !ruby/object:Gem::Version
|
152
|
+
version: '0'
|
153
|
+
description: Access the Google Analytics Reporting API v4 On Rails
|
154
|
+
email:
|
155
|
+
- tatsuyanoheya@gmail.com
|
156
|
+
executables: []
|
157
|
+
extensions: []
|
158
|
+
extra_rdoc_files: []
|
159
|
+
files:
|
160
|
+
- MIT-LICENSE
|
161
|
+
- README.md
|
162
|
+
- Rakefile
|
163
|
+
- lib/hisui.rb
|
164
|
+
- lib/hisui/management/account.rb
|
165
|
+
- lib/hisui/management/account_summary.rb
|
166
|
+
- lib/hisui/management/base.rb
|
167
|
+
- lib/hisui/management/goal.rb
|
168
|
+
- lib/hisui/management/profile.rb
|
169
|
+
- lib/hisui/management/segment.rb
|
170
|
+
- lib/hisui/management/web_property.rb
|
171
|
+
- lib/hisui/model.rb
|
172
|
+
- lib/hisui/request.rb
|
173
|
+
- lib/hisui/response.rb
|
174
|
+
- lib/hisui/user.rb
|
175
|
+
- lib/hisui/version.rb
|
176
|
+
homepage: https://github.com/ikepon/hisui
|
177
|
+
licenses:
|
178
|
+
- MIT
|
179
|
+
metadata: {}
|
180
|
+
post_install_message:
|
181
|
+
rdoc_options: []
|
182
|
+
require_paths:
|
183
|
+
- lib
|
184
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
185
|
+
requirements:
|
186
|
+
- - ">="
|
187
|
+
- !ruby/object:Gem::Version
|
188
|
+
version: '0'
|
189
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
190
|
+
requirements:
|
191
|
+
- - ">="
|
192
|
+
- !ruby/object:Gem::Version
|
193
|
+
version: '0'
|
194
|
+
requirements: []
|
195
|
+
rubyforge_project:
|
196
|
+
rubygems_version: 2.6.11
|
197
|
+
signing_key:
|
198
|
+
specification_version: 4
|
199
|
+
summary: Access the Google Analytics Reporting API v4 On Rails
|
200
|
+
test_files: []
|