gawk 0.0.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/.gitignore +15 -0
- data/.rspec +3 -0
- data/.travis.yml +5 -0
- data/CODE_OF_CONDUCT.md +74 -0
- data/Gemfile +6 -0
- data/Gemfile.lock +93 -0
- data/LICENSE.txt +21 -0
- data/README.md +136 -0
- data/Rakefile +6 -0
- data/bin/console +14 -0
- data/bin/setup +8 -0
- data/exe/gawkat +5 -0
- data/gawk.gemspec +40 -0
- data/lib/gawk/cli.rb +114 -0
- data/lib/gawk/engine.rb +86 -0
- data/lib/gawk/table.rb +61 -0
- data/lib/gawk/version.rb +3 -0
- data/lib/gawk.rb +8 -0
- metadata +148 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA256:
|
3
|
+
metadata.gz: 3cacd5f6665362a411d802890daf178824cf51173a6afa551a49801a2b9da39a
|
4
|
+
data.tar.gz: 4fbc789bfab4bec406d400db941c1f2143070d0fc8a722c59d8b17d10b94418c
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 2d4f55a7692599c0bc4159f326ace6cafaf057d35dad2215747232eee46842cd9ba2fd219b0cba330fb680338ae73d46d479c49d537cec4c0ce1a156f0a9fb48
|
7
|
+
data.tar.gz: ff3d0b6db1a58a4eb28696c083ba739d0c4e441eccbb3e29b9350f6e03344aaeb49539c59baec5af842a8470c596f72d2e6db2ac5fe0f54f43a648ec6b44c6fa
|
data/.gitignore
ADDED
data/.rspec
ADDED
data/.travis.yml
ADDED
data/CODE_OF_CONDUCT.md
ADDED
@@ -0,0 +1,74 @@
|
|
1
|
+
# Contributor Covenant Code of Conduct
|
2
|
+
|
3
|
+
## Our Pledge
|
4
|
+
|
5
|
+
In the interest of fostering an open and welcoming environment, we as
|
6
|
+
contributors and maintainers pledge to making participation in our project and
|
7
|
+
our community a harassment-free experience for everyone, regardless of age, body
|
8
|
+
size, disability, ethnicity, gender identity and expression, level of experience,
|
9
|
+
nationality, personal appearance, race, religion, or sexual identity and
|
10
|
+
orientation.
|
11
|
+
|
12
|
+
## Our Standards
|
13
|
+
|
14
|
+
Examples of behavior that contributes to creating a positive environment
|
15
|
+
include:
|
16
|
+
|
17
|
+
* Using welcoming and inclusive language
|
18
|
+
* Being respectful of differing viewpoints and experiences
|
19
|
+
* Gracefully accepting constructive criticism
|
20
|
+
* Focusing on what is best for the community
|
21
|
+
* Showing empathy towards other community members
|
22
|
+
|
23
|
+
Examples of unacceptable behavior by participants include:
|
24
|
+
|
25
|
+
* The use of sexualized language or imagery and unwelcome sexual attention or
|
26
|
+
advances
|
27
|
+
* Trolling, insulting/derogatory comments, and personal or political attacks
|
28
|
+
* Public or private harassment
|
29
|
+
* Publishing others' private information, such as a physical or electronic
|
30
|
+
address, without explicit permission
|
31
|
+
* Other conduct which could reasonably be considered inappropriate in a
|
32
|
+
professional setting
|
33
|
+
|
34
|
+
## Our Responsibilities
|
35
|
+
|
36
|
+
Project maintainers are responsible for clarifying the standards of acceptable
|
37
|
+
behavior and are expected to take appropriate and fair corrective action in
|
38
|
+
response to any instances of unacceptable behavior.
|
39
|
+
|
40
|
+
Project maintainers have the right and responsibility to remove, edit, or
|
41
|
+
reject comments, commits, code, wiki edits, issues, and other contributions
|
42
|
+
that are not aligned to this Code of Conduct, or to ban temporarily or
|
43
|
+
permanently any contributor for other behaviors that they deem inappropriate,
|
44
|
+
threatening, offensive, or harmful.
|
45
|
+
|
46
|
+
## Scope
|
47
|
+
|
48
|
+
This Code of Conduct applies both within project spaces and in public spaces
|
49
|
+
when an individual is representing the project or its community. Examples of
|
50
|
+
representing a project or community include using an official project e-mail
|
51
|
+
address, posting via an official social media account, or acting as an appointed
|
52
|
+
representative at an online or offline event. Representation of a project may be
|
53
|
+
further defined and clarified by project maintainers.
|
54
|
+
|
55
|
+
## Enforcement
|
56
|
+
|
57
|
+
Instances of abusive, harassing, or otherwise unacceptable behavior may be
|
58
|
+
reported by contacting the project team at jonas-kay@outlook.com. All
|
59
|
+
complaints will be reviewed and investigated and will result in a response that
|
60
|
+
is deemed necessary and appropriate to the circumstances. The project team is
|
61
|
+
obligated to maintain confidentiality with regard to the reporter of an incident.
|
62
|
+
Further details of specific enforcement policies may be posted separately.
|
63
|
+
|
64
|
+
Project maintainers who do not follow or enforce the Code of Conduct in good
|
65
|
+
faith may face temporary or permanent repercussions as determined by other
|
66
|
+
members of the project's leadership.
|
67
|
+
|
68
|
+
## Attribution
|
69
|
+
|
70
|
+
This Code of Conduct is adapted from the [Contributor Covenant][homepage], version 1.4,
|
71
|
+
available at [http://contributor-covenant.org/version/1/4][version]
|
72
|
+
|
73
|
+
[homepage]: http://contributor-covenant.org
|
74
|
+
[version]: http://contributor-covenant.org/version/1/4/
|
data/Gemfile
ADDED
data/Gemfile.lock
ADDED
@@ -0,0 +1,93 @@
|
|
1
|
+
PATH
|
2
|
+
remote: .
|
3
|
+
specs:
|
4
|
+
gawk (0.0.1)
|
5
|
+
|
6
|
+
GEM
|
7
|
+
remote: https://rubygems.org/
|
8
|
+
specs:
|
9
|
+
addressable (2.5.2)
|
10
|
+
public_suffix (>= 2.0.2, < 4.0)
|
11
|
+
crack (0.4.3)
|
12
|
+
safe_yaml (~> 1.0.0)
|
13
|
+
declarative (0.0.10)
|
14
|
+
declarative-option (0.1.0)
|
15
|
+
diff-lcs (1.3)
|
16
|
+
faraday (0.15.2)
|
17
|
+
multipart-post (>= 1.2, < 3)
|
18
|
+
google-api-client (0.23.0)
|
19
|
+
addressable (~> 2.5, >= 2.5.1)
|
20
|
+
googleauth (>= 0.5, < 0.7.0)
|
21
|
+
httpclient (>= 2.8.1, < 3.0)
|
22
|
+
mime-types (~> 3.0)
|
23
|
+
representable (~> 3.0)
|
24
|
+
retriable (>= 2.0, < 4.0)
|
25
|
+
googleauth (0.6.2)
|
26
|
+
faraday (~> 0.12)
|
27
|
+
jwt (>= 1.4, < 3.0)
|
28
|
+
logging (~> 2.0)
|
29
|
+
memoist (~> 0.12)
|
30
|
+
multi_json (~> 1.11)
|
31
|
+
os (~> 0.9)
|
32
|
+
signet (~> 0.7)
|
33
|
+
hashdiff (0.3.7)
|
34
|
+
httpclient (2.8.3)
|
35
|
+
jwt (2.1.0)
|
36
|
+
little-plugger (1.1.4)
|
37
|
+
logging (2.2.2)
|
38
|
+
little-plugger (~> 1.1)
|
39
|
+
multi_json (~> 1.10)
|
40
|
+
memoist (0.16.0)
|
41
|
+
mime-types (3.1)
|
42
|
+
mime-types-data (~> 3.2015)
|
43
|
+
mime-types-data (3.2016.0521)
|
44
|
+
multi_json (1.13.1)
|
45
|
+
multipart-post (2.0.0)
|
46
|
+
os (0.9.6)
|
47
|
+
public_suffix (3.0.2)
|
48
|
+
rake (10.5.0)
|
49
|
+
representable (3.0.4)
|
50
|
+
declarative (< 0.1.0)
|
51
|
+
declarative-option (< 0.2.0)
|
52
|
+
uber (< 0.2.0)
|
53
|
+
retriable (3.1.2)
|
54
|
+
rspec (3.7.0)
|
55
|
+
rspec-core (~> 3.7.0)
|
56
|
+
rspec-expectations (~> 3.7.0)
|
57
|
+
rspec-mocks (~> 3.7.0)
|
58
|
+
rspec-core (3.7.1)
|
59
|
+
rspec-support (~> 3.7.0)
|
60
|
+
rspec-expectations (3.7.0)
|
61
|
+
diff-lcs (>= 1.2.0, < 2.0)
|
62
|
+
rspec-support (~> 3.7.0)
|
63
|
+
rspec-mocks (3.7.0)
|
64
|
+
diff-lcs (>= 1.2.0, < 2.0)
|
65
|
+
rspec-support (~> 3.7.0)
|
66
|
+
rspec-support (3.7.1)
|
67
|
+
safe_yaml (1.0.4)
|
68
|
+
signet (0.8.1)
|
69
|
+
addressable (~> 2.3)
|
70
|
+
faraday (~> 0.9)
|
71
|
+
jwt (>= 1.5, < 3.0)
|
72
|
+
multi_json (~> 1.10)
|
73
|
+
uber (0.1.0)
|
74
|
+
vcr (4.0.0)
|
75
|
+
webmock (3.4.2)
|
76
|
+
addressable (>= 2.3.6)
|
77
|
+
crack (>= 0.3.2)
|
78
|
+
hashdiff
|
79
|
+
|
80
|
+
PLATFORMS
|
81
|
+
ruby
|
82
|
+
|
83
|
+
DEPENDENCIES
|
84
|
+
bundler (~> 1.16)
|
85
|
+
gawk!
|
86
|
+
google-api-client (~> 0.11)
|
87
|
+
rake (~> 10.0)
|
88
|
+
rspec (~> 3.0)
|
89
|
+
vcr
|
90
|
+
webmock
|
91
|
+
|
92
|
+
BUNDLED WITH
|
93
|
+
1.16.2
|
data/LICENSE.txt
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
The MIT License (MIT)
|
2
|
+
|
3
|
+
Copyright (c) 2018 Jonas Kay
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
7
|
+
in the Software without restriction, including without limitation the rights
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
10
|
+
furnished to do so, subject to the following conditions:
|
11
|
+
|
12
|
+
The above copyright notice and this permission notice shall be included in
|
13
|
+
all copies or substantial portions of the Software.
|
14
|
+
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
21
|
+
THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,136 @@
|
|
1
|
+
# Gawk
|
2
|
+
|
3
|
+
Print Google Analytics reports inside your terminal.
|
4
|
+
|
5
|
+
## Getting Started
|
6
|
+
|
7
|
+
### Installation
|
8
|
+
|
9
|
+
Add this line to your application's Gemfile:
|
10
|
+
|
11
|
+
```ruby
|
12
|
+
gem 'gawk'
|
13
|
+
```
|
14
|
+
|
15
|
+
And then execute:
|
16
|
+
|
17
|
+
$ bundle
|
18
|
+
|
19
|
+
Or install it yourself as:
|
20
|
+
|
21
|
+
$ gem install gawk
|
22
|
+
|
23
|
+
### Enable Google Analytics Reporting API
|
24
|
+
|
25
|
+
Gawk uses the [Google Analytics Reporting API v4](https://developers.google.com/analytics/devguides/reporting/core/v4/) to retrieve the reports. You need to enable the Reporting API in Google's API Console with the following steps:
|
26
|
+
|
27
|
+
1. Open the [Library page](https://console.developers.google.com/apis/library) in the API Console.
|
28
|
+
2. Select the project associated with your application. Create a project if you do not have one already.
|
29
|
+
3. Use the **Library page** to find the Google Analytics Reporting API. Click on the API and enable it for your project.
|
30
|
+
|
31
|
+
### Create Authorization Credentials
|
32
|
+
|
33
|
+
Gawk uses OAuth 2.0 to access the Google Analytics Reporting API. You need to create the required credentials with the following steps:
|
34
|
+
|
35
|
+
1. Open the [Credentials page](https://console.developers.google.com/apis/credentials) in the API Console.
|
36
|
+
2. Click **Create credentials > OAuth client ID**.
|
37
|
+
3. Complete the form.
|
38
|
+
4. Use the **Credentials page** to download the credentials as JSON.
|
39
|
+
5. Save the downloaded credentials as `client_secrets.json` in your project's root.
|
40
|
+
|
41
|
+
**Attention**: Gawk stores OAuth 2.0 tokens inside `tokens.yaml`. You might want to add both `client_secrets.json` and `tokens.yaml` into your project's `.gitignore`.
|
42
|
+
|
43
|
+
## Usage
|
44
|
+
|
45
|
+
To start using Gawk, you need to add the Gawk config file `.gawk.yaml` into your project root.
|
46
|
+
|
47
|
+
After that, you can print all your reports:
|
48
|
+
|
49
|
+
$ bundle exec gawkat
|
50
|
+
|
51
|
+
Or, print a specific report using the report identifier:
|
52
|
+
|
53
|
+
$ bundle exec gawkat mobile_sessions
|
54
|
+
|
55
|
+
### .gawk.yaml
|
56
|
+
|
57
|
+
You should use the following format when constructing a `.gawk.yaml` file:
|
58
|
+
|
59
|
+
```yaml
|
60
|
+
# Unique identifier for your report
|
61
|
+
foobar:
|
62
|
+
table:
|
63
|
+
# Name of your report table
|
64
|
+
name: STRING
|
65
|
+
request:
|
66
|
+
# The View ID you want to use
|
67
|
+
view_id: STRING
|
68
|
+
# List of date ranges (max. 2 ranges)
|
69
|
+
date_ranges:
|
70
|
+
- start_date: DATE
|
71
|
+
end_date: DATE
|
72
|
+
- start_date: DATE
|
73
|
+
end_date: DATE
|
74
|
+
# List of reporting dimensions
|
75
|
+
dimensions:
|
76
|
+
- STRING
|
77
|
+
# List of reporting metrics
|
78
|
+
metrics:
|
79
|
+
- STRING
|
80
|
+
# Optional filters
|
81
|
+
filters_expression: STRING
|
82
|
+
```
|
83
|
+
|
84
|
+
All reports should have the same date ranges. If the reports don't share the same date ranges, you should only print them individually using the report identifer.
|
85
|
+
|
86
|
+
Here is a `.gawk.yaml` example that you can try out:
|
87
|
+
|
88
|
+
```yaml
|
89
|
+
hello:
|
90
|
+
table:
|
91
|
+
name: Hello, world!
|
92
|
+
request:
|
93
|
+
view_id: XXXX # Replace this with your View ID
|
94
|
+
date_ranges:
|
95
|
+
- start_date: 2018/06/01
|
96
|
+
end_date: 2018/06/30
|
97
|
+
dimensions:
|
98
|
+
- ga:browser
|
99
|
+
metrics:
|
100
|
+
- ga:sessions
|
101
|
+
filters_expression: ga:deviceCategory==mobile
|
102
|
+
```
|
103
|
+
|
104
|
+
|
105
|
+
If you don't know your View ID, you can find it here: https://ga-dev-tools.appspot.com/account-explorer/.
|
106
|
+
|
107
|
+
You can find a list of all the possible dimensions and metrics here: https://developers.google.com/analytics/devguides/reporting/core/dimsmets
|
108
|
+
|
109
|
+
|
110
|
+
##
|
111
|
+
|
112
|
+
## Development
|
113
|
+
|
114
|
+
After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake spec` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
|
115
|
+
|
116
|
+
To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
|
117
|
+
|
118
|
+
## Testing
|
119
|
+
|
120
|
+
Run the tests:
|
121
|
+
|
122
|
+
```
|
123
|
+
rspec spec/gawk
|
124
|
+
```
|
125
|
+
|
126
|
+
## Contributing
|
127
|
+
|
128
|
+
Bug reports and pull requests are welcome on GitHub at https://github.com/jonaskay/gawk. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [Contributor Covenant](http://contributor-covenant.org) code of conduct.
|
129
|
+
|
130
|
+
## License
|
131
|
+
|
132
|
+
The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
|
133
|
+
|
134
|
+
## Code of Conduct
|
135
|
+
|
136
|
+
Everyone interacting in the Gawk project’s codebases, issue trackers, chat rooms and mailing lists is expected to follow the [code of conduct](https://github.com/[USERNAME]/gawk/blob/master/CODE_OF_CONDUCT.md).
|
data/Rakefile
ADDED
data/bin/console
ADDED
@@ -0,0 +1,14 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
|
3
|
+
require "bundler/setup"
|
4
|
+
require "gawk"
|
5
|
+
|
6
|
+
# You can add fixtures and/or initialization code here to make experimenting
|
7
|
+
# with your gem easier. You can also use a different console, if you like.
|
8
|
+
|
9
|
+
# (If you use this, don't forget to add pry to your Gemfile!)
|
10
|
+
# require "pry"
|
11
|
+
# Pry.start
|
12
|
+
|
13
|
+
require "irb"
|
14
|
+
IRB.start(__FILE__)
|
data/bin/setup
ADDED
data/exe/gawkat
ADDED
data/gawk.gemspec
ADDED
@@ -0,0 +1,40 @@
|
|
1
|
+
|
2
|
+
lib = File.expand_path("../lib", __FILE__)
|
3
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
|
+
require "gawk/version"
|
5
|
+
|
6
|
+
Gem::Specification.new do |spec|
|
7
|
+
spec.name = "gawk"
|
8
|
+
spec.version = Gawk::VERSION
|
9
|
+
spec.authors = ["Jonas Kay"]
|
10
|
+
spec.email = ["jonas-kay@outlook.com"]
|
11
|
+
|
12
|
+
spec.summary = "Print Google Analytics reports inside your terminal"
|
13
|
+
spec.homepage = "https://github.com/jonaskay/gawk"
|
14
|
+
spec.license = "MIT"
|
15
|
+
|
16
|
+
# Prevent pushing this gem to RubyGems.org. To allow pushes either set the 'allowed_push_host'
|
17
|
+
# to allow pushing to a single host or delete this section to allow pushing to any host.
|
18
|
+
if spec.respond_to?(:metadata)
|
19
|
+
spec.metadata["allowed_push_host"] = "https://rubygems.org"
|
20
|
+
else
|
21
|
+
raise "RubyGems 2.0 or newer is required to protect against " \
|
22
|
+
"public gem pushes."
|
23
|
+
end
|
24
|
+
|
25
|
+
# Specify which files should be added to the gem when it is released.
|
26
|
+
# The `git ls-files -z` loads the files in the RubyGem that have been added into git.
|
27
|
+
spec.files = Dir.chdir(File.expand_path('..', __FILE__)) do
|
28
|
+
`git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
|
29
|
+
end
|
30
|
+
spec.bindir = "exe"
|
31
|
+
spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
|
32
|
+
spec.require_paths = ["lib"]
|
33
|
+
|
34
|
+
spec.add_development_dependency "bundler", "~> 1.16"
|
35
|
+
spec.add_development_dependency "rake", "~> 10.0"
|
36
|
+
spec.add_development_dependency "rspec", "~> 3.0"
|
37
|
+
spec.add_development_dependency "webmock"
|
38
|
+
spec.add_development_dependency "vcr"
|
39
|
+
spec.add_development_dependency "google-api-client", "~> 0.11"
|
40
|
+
end
|
data/lib/gawk/cli.rb
ADDED
@@ -0,0 +1,114 @@
|
|
1
|
+
require 'yaml'
|
2
|
+
require 'erb'
|
3
|
+
|
4
|
+
module Gawk
|
5
|
+
class CLI
|
6
|
+
def initialize(config_file: '.gawk.yaml', report_key: nil, engine: Gawk::Engine.new)
|
7
|
+
@config_file = config_file
|
8
|
+
@report_key = report_key
|
9
|
+
@engine = engine
|
10
|
+
end
|
11
|
+
|
12
|
+
def output
|
13
|
+
requests = get_data_from_config_file('request')
|
14
|
+
tables = get_data_from_config_file('table')
|
15
|
+
@engine.get_reports(requests).each.with_index do |report, i|
|
16
|
+
puts generate_table(tables[i], requests, report).to_s
|
17
|
+
end
|
18
|
+
end
|
19
|
+
|
20
|
+
def get_data_from_config_file(data_type)
|
21
|
+
data = []
|
22
|
+
if @report_key
|
23
|
+
data << load_config_file.dig(@report_key, data_type)
|
24
|
+
else
|
25
|
+
load_config_file.each {|k, v| data << v.fetch(data_type) }
|
26
|
+
end
|
27
|
+
data
|
28
|
+
end
|
29
|
+
|
30
|
+
private
|
31
|
+
|
32
|
+
def load_config_file
|
33
|
+
erb = ERB.new(File.read(@config_file))
|
34
|
+
YAML.load(erb.result(binding))
|
35
|
+
end
|
36
|
+
|
37
|
+
def generate_table(config, requests, report)
|
38
|
+
table = Gawk::Table.new
|
39
|
+
table.name = config['name']
|
40
|
+
table.headings = generate_table_headings(report)
|
41
|
+
table.rows = generate_table_rows(requests, report)
|
42
|
+
table
|
43
|
+
end
|
44
|
+
|
45
|
+
def generate_table_headings(report)
|
46
|
+
headings = []
|
47
|
+
headings << report.column_header.dimensions.join('; ')
|
48
|
+
report.column_header.metric_header.metric_header_entries.each do |entry|
|
49
|
+
headings << entry.name
|
50
|
+
end
|
51
|
+
headings
|
52
|
+
end
|
53
|
+
|
54
|
+
def generate_table_rows(requests, report)
|
55
|
+
rows = []
|
56
|
+
if requests[0]['date_ranges'].length > 1
|
57
|
+
# Add a row for total changes.
|
58
|
+
rows << [
|
59
|
+
"Total",
|
60
|
+
report.data.totals[0].values.map.with_index do |x, i|
|
61
|
+
get_change_in_percentage(x, report.data.totals[1].values[i])
|
62
|
+
end
|
63
|
+
].flatten
|
64
|
+
# Add data rows.
|
65
|
+
report.data.rows.each.with_index do |row, i|
|
66
|
+
# Add a row for dimension values.
|
67
|
+
rows << ["#{i+1}. #{row.dimensions.join('; ')}"]
|
68
|
+
# Add a data row for each date range.
|
69
|
+
for j in (0..1) do
|
70
|
+
rows << [
|
71
|
+
get_date_range_as_string(requests[0]['date_ranges'][j]),
|
72
|
+
row.metrics[j].values
|
73
|
+
].flatten
|
74
|
+
end
|
75
|
+
# Add a row for data changes.
|
76
|
+
rows << [
|
77
|
+
"% Change",
|
78
|
+
report.column_header.metric_header.metric_header_entries.map.with_index do |entry, j|
|
79
|
+
get_change_in_percentage(row.metrics[0].values[j], row.metrics[1].values[j])
|
80
|
+
end
|
81
|
+
].flatten
|
82
|
+
end
|
83
|
+
else
|
84
|
+
# Add a row for total changes.
|
85
|
+
rows << ["Total", report.data.totals[0].values].flatten
|
86
|
+
# Add data rows.
|
87
|
+
report.data.rows.each.with_index do |row, i|
|
88
|
+
rows << ["#{i+1}. #{row.dimensions.join('; ')}", row.metrics.map(&:values)].flatten
|
89
|
+
end
|
90
|
+
end
|
91
|
+
rows
|
92
|
+
end
|
93
|
+
|
94
|
+
def get_change_in_percentage(first_value, second_value)
|
95
|
+
if second_value == 0
|
96
|
+
if first_value == second_value
|
97
|
+
return "#{second_value.to_f}%"
|
98
|
+
else
|
99
|
+
return "∞"
|
100
|
+
end
|
101
|
+
else
|
102
|
+
return "#{((first_value.to_f / second_value.to_f - 1) * 100).round(2)}%"
|
103
|
+
end
|
104
|
+
end
|
105
|
+
|
106
|
+
def get_date_range_as_string(date_range)
|
107
|
+
"#{format_date(date_range['start_date'])} - #{format_date(date_range['end_date'])}"
|
108
|
+
end
|
109
|
+
|
110
|
+
def format_date(date)
|
111
|
+
Date.parse(date.to_s).strftime("%b %-d, %Y")
|
112
|
+
end
|
113
|
+
end
|
114
|
+
end
|
data/lib/gawk/engine.rb
ADDED
@@ -0,0 +1,86 @@
|
|
1
|
+
require 'google/apis/analyticsreporting_v4'
|
2
|
+
require 'googleauth/stores/file_token_store'
|
3
|
+
|
4
|
+
module Gawk
|
5
|
+
class Engine
|
6
|
+
USER_ID = 'gawk'
|
7
|
+
OOB_URI = 'urn:ietf:wg:oauth:2.0:oob'
|
8
|
+
|
9
|
+
def initialize(scope: 'https://www.googleapis.com/auth/analytics.readonly',
|
10
|
+
secrets_file: 'client_secrets.json', token_file: 'tokens.yaml')
|
11
|
+
|
12
|
+
@secrets_file = secrets_file
|
13
|
+
@token_file = token_file
|
14
|
+
@authorizer = Google::Auth::UserAuthorizer.new(client_id, scope, token_store)
|
15
|
+
@service = Google::Apis::AnalyticsreportingV4::AnalyticsReportingService.new
|
16
|
+
@service.authorization = auth_client
|
17
|
+
end
|
18
|
+
|
19
|
+
def get_reports(requests)
|
20
|
+
report_requests = []
|
21
|
+
requests.each do |r|
|
22
|
+
report_request = Google::Apis::AnalyticsreportingV4::ReportRequest.new
|
23
|
+
|
24
|
+
report_request.view_id = r['view_id'].to_s
|
25
|
+
report_request.date_ranges = r['date_ranges'].map {|h| generate_date_range(h)}
|
26
|
+
report_request.dimensions = r['dimensions'].map {|s| generate_dimension(s)}
|
27
|
+
report_request.metrics = r['metrics'].map {|s| generate_metric(s)}
|
28
|
+
report_request.filters_expression = r['filters_expression']
|
29
|
+
|
30
|
+
report_requests << report_request
|
31
|
+
|
32
|
+
end
|
33
|
+
|
34
|
+
get_reports_request = \
|
35
|
+
Google::Apis::AnalyticsreportingV4::GetReportsRequest.new(report_requests: report_requests)
|
36
|
+
|
37
|
+
response = @service.batch_get_reports(get_reports_request)
|
38
|
+
|
39
|
+
response.reports
|
40
|
+
end
|
41
|
+
|
42
|
+
private
|
43
|
+
|
44
|
+
def client_id
|
45
|
+
Google::Auth::ClientId.from_file(@secrets_file)
|
46
|
+
end
|
47
|
+
|
48
|
+
def token_store
|
49
|
+
Google::Auth::Stores::FileTokenStore.new(file: @token_file)
|
50
|
+
end
|
51
|
+
|
52
|
+
def auth_client
|
53
|
+
credentials = @authorizer.get_credentials(USER_ID)
|
54
|
+
if credentials.nil?
|
55
|
+
url = @authorizer.get_authorization_url(base_url: OOB_URI )
|
56
|
+
puts "Open #{url} in your browser and enter the resulting code:"
|
57
|
+
code = gets
|
58
|
+
credentials = @authorizer.get_and_store_credentials_from_code(
|
59
|
+
user_id: USER_ID,
|
60
|
+
code: code,
|
61
|
+
base_url: OOB_URI
|
62
|
+
)
|
63
|
+
end
|
64
|
+
credentials
|
65
|
+
end
|
66
|
+
|
67
|
+
def generate_date_range(hash)
|
68
|
+
Google::Apis::AnalyticsreportingV4::DateRange.new(
|
69
|
+
start_date: format_date(hash['start_date']),
|
70
|
+
end_date: format_date(hash['end_date'])
|
71
|
+
)
|
72
|
+
end
|
73
|
+
|
74
|
+
def format_date(date)
|
75
|
+
Date.parse(date.to_s).strftime
|
76
|
+
end
|
77
|
+
|
78
|
+
def generate_dimension(name)
|
79
|
+
Google::Apis::AnalyticsreportingV4::Dimension.new(name: name)
|
80
|
+
end
|
81
|
+
|
82
|
+
def generate_metric(expression)
|
83
|
+
Google::Apis::AnalyticsreportingV4::Metric.new(expression: expression)
|
84
|
+
end
|
85
|
+
end
|
86
|
+
end
|
data/lib/gawk/table.rb
ADDED
@@ -0,0 +1,61 @@
|
|
1
|
+
module Gawk
|
2
|
+
class Table
|
3
|
+
def initialize(name: "", headings: [], rows: [])
|
4
|
+
@name = name
|
5
|
+
@headings = headings
|
6
|
+
@rows = rows
|
7
|
+
@widths = get_widths
|
8
|
+
end
|
9
|
+
|
10
|
+
def name=(str)
|
11
|
+
@name = str
|
12
|
+
end
|
13
|
+
|
14
|
+
def headings=(arr)
|
15
|
+
@headings = arr
|
16
|
+
@widths = get_widths
|
17
|
+
end
|
18
|
+
|
19
|
+
def rows=(arr)
|
20
|
+
@rows = arr
|
21
|
+
@widths = get_widths
|
22
|
+
end
|
23
|
+
|
24
|
+
def to_s
|
25
|
+
buffer = []
|
26
|
+
buffer << ""
|
27
|
+
buffer << "=" * width
|
28
|
+
buffer << @name
|
29
|
+
buffer << "-" * width
|
30
|
+
buffer << buffer(@headings)
|
31
|
+
buffer << ""
|
32
|
+
@rows.each {|row| buffer << buffer(row)}
|
33
|
+
buffer << ""
|
34
|
+
buffer.join("\n")
|
35
|
+
end
|
36
|
+
|
37
|
+
def width
|
38
|
+
(@widths.inject(0){|sum,x| sum + x } + (@widths.length - 1) * 6)
|
39
|
+
end
|
40
|
+
|
41
|
+
private
|
42
|
+
|
43
|
+
def buffer(arr)
|
44
|
+
str = ""
|
45
|
+
arr.each.with_index do |e, i|
|
46
|
+
str += " #{e} " + " " * (@widths[i].to_i - e.length)
|
47
|
+
end
|
48
|
+
str.strip
|
49
|
+
end
|
50
|
+
|
51
|
+
def get_widths
|
52
|
+
widths = @headings.map {|s| s.length}
|
53
|
+
@rows.each do |row|
|
54
|
+
row.each.with_index do |element, i|
|
55
|
+
widths[i] = element.length if element.length > widths[i]
|
56
|
+
end
|
57
|
+
end
|
58
|
+
widths.flatten
|
59
|
+
end
|
60
|
+
end
|
61
|
+
end
|
data/lib/gawk/version.rb
ADDED
data/lib/gawk.rb
ADDED
metadata
ADDED
@@ -0,0 +1,148 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: gawk
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.1
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Jonas Kay
|
8
|
+
autorequire:
|
9
|
+
bindir: exe
|
10
|
+
cert_chain: []
|
11
|
+
date: 2018-07-05 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.16'
|
20
|
+
type: :development
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - "~>"
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '1.16'
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: rake
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - "~>"
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '10.0'
|
34
|
+
type: :development
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - "~>"
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '10.0'
|
41
|
+
- !ruby/object:Gem::Dependency
|
42
|
+
name: rspec
|
43
|
+
requirement: !ruby/object:Gem::Requirement
|
44
|
+
requirements:
|
45
|
+
- - "~>"
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: '3.0'
|
48
|
+
type: :development
|
49
|
+
prerelease: false
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - "~>"
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: '3.0'
|
55
|
+
- !ruby/object:Gem::Dependency
|
56
|
+
name: webmock
|
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: vcr
|
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: google-api-client
|
85
|
+
requirement: !ruby/object:Gem::Requirement
|
86
|
+
requirements:
|
87
|
+
- - "~>"
|
88
|
+
- !ruby/object:Gem::Version
|
89
|
+
version: '0.11'
|
90
|
+
type: :development
|
91
|
+
prerelease: false
|
92
|
+
version_requirements: !ruby/object:Gem::Requirement
|
93
|
+
requirements:
|
94
|
+
- - "~>"
|
95
|
+
- !ruby/object:Gem::Version
|
96
|
+
version: '0.11'
|
97
|
+
description:
|
98
|
+
email:
|
99
|
+
- jonas-kay@outlook.com
|
100
|
+
executables:
|
101
|
+
- gawkat
|
102
|
+
extensions: []
|
103
|
+
extra_rdoc_files: []
|
104
|
+
files:
|
105
|
+
- ".gitignore"
|
106
|
+
- ".rspec"
|
107
|
+
- ".travis.yml"
|
108
|
+
- CODE_OF_CONDUCT.md
|
109
|
+
- Gemfile
|
110
|
+
- Gemfile.lock
|
111
|
+
- LICENSE.txt
|
112
|
+
- README.md
|
113
|
+
- Rakefile
|
114
|
+
- bin/console
|
115
|
+
- bin/setup
|
116
|
+
- exe/gawkat
|
117
|
+
- gawk.gemspec
|
118
|
+
- lib/gawk.rb
|
119
|
+
- lib/gawk/cli.rb
|
120
|
+
- lib/gawk/engine.rb
|
121
|
+
- lib/gawk/table.rb
|
122
|
+
- lib/gawk/version.rb
|
123
|
+
homepage: https://github.com/jonaskay/gawk
|
124
|
+
licenses:
|
125
|
+
- MIT
|
126
|
+
metadata:
|
127
|
+
allowed_push_host: https://rubygems.org
|
128
|
+
post_install_message:
|
129
|
+
rdoc_options: []
|
130
|
+
require_paths:
|
131
|
+
- lib
|
132
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
133
|
+
requirements:
|
134
|
+
- - ">="
|
135
|
+
- !ruby/object:Gem::Version
|
136
|
+
version: '0'
|
137
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
138
|
+
requirements:
|
139
|
+
- - ">="
|
140
|
+
- !ruby/object:Gem::Version
|
141
|
+
version: '0'
|
142
|
+
requirements: []
|
143
|
+
rubyforge_project:
|
144
|
+
rubygems_version: 2.7.6
|
145
|
+
signing_key:
|
146
|
+
specification_version: 4
|
147
|
+
summary: Print Google Analytics reports inside your terminal
|
148
|
+
test_files: []
|