simple_ga_reporting 1.0.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: 2e88b845d1465d10bcc1ca4933003986f08a4dc3feaba25a134773e91dfb4ed4
4
+ data.tar.gz: 95f7c6475a6c0ef348114fd924760ff5e88a83e645d8d5aadd0c9977f15e92f0
5
+ SHA512:
6
+ metadata.gz: '062738c24349613449247506a27c93da82d44239e5895937ef3b4f5ad19baf69dd8dce1fe6dc4021d83daea8f530e7bd77e10b233fb8aae7a3858a8facc827df'
7
+ data.tar.gz: 8c21dc84064814dfe06f1b23cc7521965b8d7655b0b83b8adac23ad02b6a0004abfbcfcba41b901b645131c6f89a3375ce1d353d9350f5fb6d89d616897bba5b
@@ -0,0 +1,61 @@
1
+ # Ruby CircleCI 2.0 configuration file
2
+ #
3
+ # Check https://circleci.com/docs/2.0/language-ruby/ for more details
4
+ #
5
+ version: 2
6
+ jobs:
7
+ build:
8
+ docker:
9
+ # specify the version you desire here
10
+ - image: circleci/ruby:2.5.0-node-browsers
11
+
12
+ # Specify service dependencies here if necessary
13
+ # CircleCI maintains a library of pre-built images
14
+ # documented at https://circleci.com/docs/2.0/circleci-images/
15
+ # - image: circleci/postgres:9.4
16
+
17
+ working_directory: ~/repo
18
+
19
+ steps:
20
+ - checkout
21
+
22
+ # Download and cache dependencies
23
+ # - restore_cache:
24
+ # keys:
25
+ # - v1-dependencies-{{ checksum "Gemfile.lock" }}
26
+ # # fallback to using the latest cache if no exact match is found
27
+ # - v1-dependencies-
28
+
29
+ - run:
30
+ name: install dependencies
31
+ command: |
32
+ bundle install --jobs=4 --retry=3 --path vendor/bundle
33
+
34
+ # - save_cache:
35
+ # paths:
36
+ # - ./vendor/bundle
37
+ # key: v1-dependencies-{{ checksum "Gemfile.lock" }}
38
+
39
+ # Database setup
40
+ # - run: bundle exec rake db:create
41
+ # - run: bundle exec rake db:schema:load
42
+
43
+ # run tests!
44
+ - run:
45
+ name: run tests
46
+ command: |
47
+ mkdir /tmp/test-results
48
+ TEST_FILES="$(circleci tests glob "spec/**/*_spec.rb" | circleci tests split --split-by=timings)"
49
+
50
+ bundle exec rspec --format progress \
51
+ --format RspecJunitFormatter \
52
+ --out /tmp/test-results/rspec.xml \
53
+ --format progress \
54
+ $TEST_FILES
55
+
56
+ # collect reports
57
+ - store_test_results:
58
+ path: /tmp/test-results
59
+ - store_artifacts:
60
+ path: /tmp/test-results
61
+ destination: test-results
data/.gitignore ADDED
@@ -0,0 +1,56 @@
1
+ *.gem
2
+ *.rbc
3
+ /.config
4
+ /coverage/
5
+ /InstalledFiles
6
+ /pkg/
7
+ /spec/reports/
8
+ /spec/examples.txt
9
+ /test/tmp/
10
+ /test/version_tmp/
11
+ /tmp/
12
+
13
+ # Used by dotenv library to load environment variables.
14
+ # .env
15
+
16
+ ## Specific to RubyMotion:
17
+ .dat*
18
+ .repl_history
19
+ build/
20
+ *.bridgesupport
21
+ build-iPhoneOS/
22
+ build-iPhoneSimulator/
23
+
24
+ ## Specific to RubyMotion (use of CocoaPods):
25
+ #
26
+ # We recommend against adding the Pods directory to your .gitignore. However
27
+ # you should judge for yourself, the pros and cons are mentioned at:
28
+ # https://guides.cocoapods.org/using/using-cocoapods.html#should-i-check-the-pods-directory-into-source-control
29
+ #
30
+ # vendor/Pods/
31
+
32
+ ## Documentation cache and generated files:
33
+ /.yardoc/
34
+ /_yardoc/
35
+ /doc/
36
+ /rdoc/
37
+
38
+ ## Environment normalization:
39
+ /.bundle/
40
+ /vendor/bundle
41
+ /lib/bundler/man/
42
+
43
+ # for a library or gem, you might want to ignore these files since the code is
44
+ # intended to run in multiple environments; otherwise, check them in:
45
+ # Gemfile.lock
46
+ # .ruby-version
47
+ # .ruby-gemset
48
+
49
+ # unless supporting rvm < 1.11.0 or doing something fancy, ignore this:
50
+ .rvmrc
51
+
52
+ # rspec failure tracking
53
+ .rspec_status
54
+
55
+ Gemfile.lock
56
+ Guardfile
data/.rspec ADDED
@@ -0,0 +1,3 @@
1
+ --format documentation
2
+ --color
3
+ --require spec_helper
@@ -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 takiya@toran.sakura.ne.jp. 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
@@ -0,0 +1,6 @@
1
+ source "https://rubygems.org"
2
+
3
+ git_source(:github) {|repo_name| "https://github.com/#{repo_name}" }
4
+
5
+ # Specify your gem's dependencies in simple_ga_reporting.gemspec
6
+ gemspec
data/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2018 Osamu Takiya
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 all
13
+ 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 THE
21
+ SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,427 @@
1
+ [![CircleCI](https://circleci.com/gh/corselia/simple-ga-reporting.svg?style=svg)](https://circleci.com/gh/corselia/simple-ga-reporting)
2
+
3
+ # What's this?
4
+ This gem helps you to obtain `Google Analytics` reporting data with ease.
5
+
6
+ First of all, you create configuration YAML file as Google Analytics API. Then execute the simple Ruby code, and you obtain Google Analytics reporting data.
7
+
8
+ Yeah, very simple :)
9
+
10
+ ## Wrapper gem of Legato
11
+ This gem is a wrapper of [Legato gem](https://github.com/tpitale/legato). `Legato` is very coooooooool and full-stack gem, so complicated as a beginner (me!).
12
+
13
+ Therefore I developed this wrapper gem, `simple-ga-reporting`.
14
+
15
+ # Get Started
16
+
17
+ ## 1. Installation
18
+ ```bash
19
+ $ gem install simple-ga-reporting
20
+ ```
21
+
22
+ ## 2. Create API configuration file
23
+ - You must create `config/key_and_email.yml`
24
+ - This YAML file must contain two Key-Value sets
25
+ - `private_key` and its value
26
+ - `client_email` and its value
27
+ - How to get above two Key-Value sets is written later
28
+
29
+ ## 3. Create Google Analytics reporting configuration file
30
+ - You must create the YAML file which contains Google Analytics reporting configuration such as `start-date`, `end-date`, `metrics`, `demensions`, `filters` and etc.
31
+ - filepath is `config/ga_reporting_config.yml`
32
+ - This YAML file format is shown later
33
+
34
+ ## 4. Create filters file (Optional)
35
+ - If you wanna use filters function, please create `config/filters.rb`
36
+ - the filters configuration on GUI is below
37
+
38
+ ![filter configuration on GUI](images/config_filter_by_gui.png)
39
+
40
+ ## 5. Write your app
41
+ - Write your application used this gem
42
+ - The sample code is below
43
+ - Use only two class methods
44
+ - `SimpleGaReports.configure`
45
+ - `SimpleGaReports.filtered_results`
46
+ - Only three lines :-)
47
+ - Note
48
+ - If you don't use filters, use `SimpleGaReports.raw_results`
49
+ - when no filter is in configure file, `SimpleGaReports.raw_results` is the same as `SimpleGaReports.filtered_results`
50
+
51
+ ```ruby
52
+ require 'simple_ga_reporting'
53
+
54
+ SimpleGaReports.configure
55
+ SimpleGaReports.filtered_results #=> Legato::Query
56
+ ```
57
+
58
+ # Configuration files
59
+
60
+ ## API configuration file
61
+ - You must create Google Analytics API configuration file
62
+ - Default filepath is `config/key_and_email.yml`
63
+ - You can change filepath by argument with `create_ga_user` method
64
+ - Sample is below
65
+
66
+ ```yaml
67
+ private_key: "-----BEGIN PRIVATE KEY-----\nMIIE..........Eqw==\n-----END PRIVATE KEY-----\n"
68
+ client_email: "your_account_name@foobar.iam.gserviceaccount.com"
69
+ ```
70
+
71
+ ## Google Analytics reporting configuration file
72
+ - You must create the YAML file which contains Google Analytics reporting configuration
73
+ - Default filepath is `config/ga_reporting_config.yml`
74
+ - You can change filepath by argument with `configure` method
75
+ - Sample is below
76
+
77
+ ```yaml
78
+ profile_name: my_profile_name
79
+ start_date: 2018-04-01
80
+ end_date: 2018-04-05
81
+ metrics:
82
+ - users
83
+ - pageviews
84
+ dimensions:
85
+ - pagePath
86
+ - pageTitle
87
+ sort:
88
+ - -pageviews
89
+ - -users
90
+ filters:
91
+ - my_awesome_filter
92
+ - his_awesome_filter
93
+ - her_awesome_filter
94
+ limit: 20
95
+ ```
96
+
97
+ - Please refer the official documents as configuration Key-Value sets
98
+ - https://developers.google.com/analytics/devguides/reporting/core/v3/reference
99
+
100
+ ![ga_query_parametes_summary.png](images/ga_query_parametes_summary.png)
101
+
102
+ - Though that documents show many Key-Value sets, you can config only several sets as below
103
+ - start-date
104
+ - end-date
105
+ - metrics
106
+ - dimensions
107
+ - sort
108
+ - filters
109
+ - max-results
110
+ - samplingLevel
111
+ - Some Key names in configure file are different from ones in official document
112
+ - `start_date` in configure file (not `start-date`)
113
+ - `end_date` in configure file (not `end-date`)
114
+ - `limit` in configure file (instead of `max-results`)
115
+ - `sampling_level` in configure file (not `samplingLevel`)
116
+ - The type of `metrics`, `dimensions`, `sort` and `filters` is `Array`
117
+ - The order of elements doesn't matter
118
+
119
+ ## Key-Value detail
120
+
121
+ #### profile_name
122
+ - REQUIRED
123
+ - It was called `view (view name)` before
124
+ - but in Japanese page, still `ビュー名`
125
+
126
+ ![View or Profile](images/ga_profile(view).png)
127
+
128
+ #### start_date
129
+ - REQUIRED
130
+ - Original Key name is `start-date`
131
+ - Value examples
132
+ - `2018-04-01`
133
+ - `today` or `yesterday`
134
+ - `14daysAgo`
135
+ - use `daysAgo` phrase even if `1day`
136
+
137
+ #### end_date
138
+ - REQUIRED
139
+ - Original Key name is `end-date`
140
+ - Value examples are the same as `start-date`
141
+
142
+ #### metrics
143
+ - REQUIRED
144
+ - `指標` in Japanese
145
+ - Official documents are below (very useful!)
146
+ - [Dimensions & Metrics Explorer](https://developers.google.com/analytics/devguides/reporting/core/dimsmets)
147
+ - Value examples
148
+ - `users`
149
+ - `uniquePageviews`
150
+
151
+ ![Dimensions & Metrics Explorer](images/dimensions_and_metrics_explorer.png)
152
+
153
+
154
+ #### dimensions
155
+ - optional
156
+ - Official documents are below (very useful!)
157
+ - [Dimensions & Metrics Explorer](https://developers.google.com/analytics/devguides/reporting/core/dimsmets)
158
+ - Value examples
159
+ - `pagePath`
160
+ - `sessionCount`
161
+
162
+ #### sort
163
+ - optional
164
+ - prefix
165
+ - `-` sign means descending
166
+ - no sign means ascending
167
+ - Don't use `+` sign
168
+ - Value examples
169
+ - `-pageviews`
170
+ - `users`
171
+
172
+ #### filters
173
+ - optional
174
+ - Value is method name you named in `filters.rb`
175
+
176
+ #### limit
177
+ - optional
178
+ - Original Key name is `max-results`
179
+ - Value is max amount of returned rows
180
+ - Default value is `100`
181
+
182
+ #### sampling_level
183
+ - optional
184
+ - Original Key name is `samplingLevel`
185
+ - Default value is `HIGHER_PRECISION`
186
+
187
+ ## filters file
188
+ - You use filters function using `filters.rb` file
189
+ - the filename `filters.rb` is determined
190
+ - only filepath is optional
191
+ - the module name `Filters` is determined
192
+ - Default filepath is `config/filters.rb`
193
+ - You can change it by argument with `configure` method
194
+ - Examples
195
+
196
+ ```ruby
197
+ module Filters
198
+ def chrome_or_fx
199
+ filter :chrome_or_fx, &lambda { contains(:browser, 'Chrome|Firefox') }
200
+ end
201
+
202
+ def awesome_page
203
+ filter :awesome_page, &lambda { contains(:pagePath, '\A.*awesome.*\z') }
204
+ end
205
+ end
206
+ ```
207
+
208
+ - `chrome_or_fx` method filters the results
209
+ - pick up data which contains 'Google Chrome' or 'Firefox' as browser
210
+ - the convention of filter method is below
211
+ - define method name
212
+ - define filter name by symbol which is the same as method name
213
+ - define filter behavior by block object
214
+ - Usage of multiple filters
215
+ - If you want to apply AND-chain, only specify each filter in Google Analytics reporting configuration file
216
+ - If you want to apply OR-chain, use regexp in block object as `chrome_or_fx` method
217
+ - Adding refer [Legato's Documents](https://github.com/tpitale/legato#google-analytics-supported-filtering-methods)
218
+
219
+ # Arguments of method
220
+
221
+ ## `SimpleGaReports.filtered_results` method
222
+ - take one argument
223
+ - If you don't use filters, use `SimpleGaReports.raw_results` method
224
+ - when no filter is in configure file, `SimpleGaReports.raw_results` is the same as `SimpleGaReports.filtered_results`
225
+
226
+ 1. `key_and_email_file`
227
+ - default is `config/key_and_email.yml`
228
+
229
+ ## `SimpleGaReports.configure` method
230
+ - take three arguments
231
+
232
+ 1. `yaml_file`
233
+ - first argument
234
+ - default is `config/ga_reporting_config.yml`
235
+
236
+ 1. `filters_file`
237
+ - second argument
238
+ - default is `config/filters.rb`
239
+
240
+ 1. `**options`
241
+ - third argument
242
+ - default is nil
243
+ - if you set this argument, you can overwrite values in Google Analytics reporting configuration file
244
+ - example
245
+ - `start_date: '90daysAgo', end_date: '60daysAgo', limit: 100`
246
+
247
+ # How to get `private_key` and `client_email`
248
+
249
+ #### 1. Access to [Google Cloud Platform Console](https://console.cloud.google.com/)
250
+
251
+ #### 2. Create new project
252
+ - name new project as you like
253
+
254
+ #### 3. Create service account for Google Analytics API and select `API and Service` from menu
255
+
256
+ ![Google Cloud Platform API menu](images/gcp_api_menu.png)
257
+
258
+ #### 4. Create authentication information
259
+ - Select `Service Account Key`
260
+
261
+ ![Google Cloud Platform API Auth Info](images/gcp_api_auth_info.png)
262
+
263
+ #### 5. Select Service Account and download JSON Key
264
+
265
+ ![Google Cloud Platform JSON Key](images/gcp_download_json_key.png)
266
+
267
+ #### 6. Open JSON Key file by editor and pick up `"private_key"` and `"client_email"`
268
+
269
+ ![Google Cloud Platform private_key and client_email](images/gcp_private_key_and_client_email.png)
270
+
271
+ #### 7. They are the very Key-Value sets you want
272
+ - Deal with them carefully
273
+
274
+ #### 8. They are the very Key-Value sets you want
275
+ - Deal with them carefully
276
+
277
+ #### 9. Activate Analytics API
278
+ - NOT `Google Analytics Reporting API` BUT `Analytics API`
279
+
280
+ ![GCP Activate API 01](images/gcp_activate_api_01.png)
281
+
282
+ ![GCP Activate API 02](images/gcp_activate_api_02.png)
283
+
284
+ ![GCP Activate API 03](images/gcp_activate_api_03.png)
285
+
286
+ #### 10. Add API user to Google Analytics
287
+ - In Google Analytics, add mail address of API user
288
+ - Only `Display and Analytics` authority needs
289
+
290
+ ![ga_add_api_user.png](images/ga_add_api_user.png)
291
+
292
+ # Sample App
293
+
294
+ ## Prepare files
295
+
296
+ 1. `./my_sample_app.rb`
297
+ 1. `./foo/my_key_and_email.yml`
298
+ 1. `./bar/my_ga_reporting_config.yml`
299
+ 1. `./foobar/filters.rb`
300
+
301
+ #### 1. `./my_sample_app.rb`
302
+ ```ruby
303
+ require 'simple_ga_reporting'
304
+
305
+ SimpleGaReports.configure('./bar/my_ga_reporting_config.yml','./foobar/filters.rb', start_date:
306
+ '2daysAgo', limit: 100)
307
+ results = SimpleGaReports.filtered_results('./foo/my_key_and_email.yml')
308
+
309
+ results.each do |result|
310
+ puts '==================================='
311
+ puts result['pagePath']
312
+ puts result['pageTitle']
313
+ puts result['pageviews']
314
+ puts result['users']
315
+ end
316
+ ```
317
+
318
+ #### 2. `./foo/my_key_and_email.yml`
319
+ ```yaml
320
+ private_key: "-----BEGIN PRIVATE KEY-----\nMIIE..........Eqw==\n-----END PRIVATE KEY-----\n"
321
+ client_email: "your_account_name@foobar.iam.gserviceaccount.com"
322
+ ```
323
+
324
+ #### 3. `./bar/my_ga_reporting_config.yml`
325
+ ```yaml
326
+ profile_name: my_profile_name
327
+ start_date: 2018-04-01
328
+ end_date: 2018-04-05
329
+ metrics:
330
+ - users
331
+ - pageviews
332
+ dimensions:
333
+ - pagePath
334
+ - pageTitle
335
+ sort:
336
+ - -pageviews
337
+ - -users
338
+ filters:
339
+ - chrome_or_fx
340
+ - happy_page
341
+ limit: 20
342
+ ```
343
+
344
+ #### 4. `./foobar/filters.rb`
345
+ ```ruby
346
+ module Filters
347
+ def chrome_or_fx
348
+ filter :chrome_or_fx, &lambda { contains(:browser, 'Chrome|Firefox') }
349
+ end
350
+
351
+ def happy_page
352
+ filter :happy_page, &lambda { contains(:pagePath, '\A.*happy.*\z') }
353
+ end
354
+ end
355
+ ```
356
+
357
+ ## execute script
358
+ ```bash
359
+ $ ruby ./my_sample_app.rb
360
+ ===================================
361
+ /i_am_happy.html
362
+ I am HAPPY!
363
+ 10000
364
+ 1000
365
+ ===================================
366
+ /happy/index.html
367
+ HAPPY TOP
368
+ 9000
369
+ 900
370
+ ===================================
371
+ /path/to/happy.html
372
+ Are You Happy?
373
+ 8000
374
+ 800
375
+ ===================================
376
+ /happy.php
377
+ HAPPY CHECK!
378
+ 7000
379
+ 700
380
+ ===================================
381
+ ......
382
+ ```
383
+
384
+ # Very Awesome Reference Book (Recommended)
385
+ - [わかばちゃんと学ぶ Googleアナリティクス (Let's study Google Analytics with Wakaba-Chan)](http://www.c-r.com/book/detail/1217)
386
+
387
+ ![わかばちゃんと学ぶ Googleアナリティクス](http://www.c-r.com/book/images/x/86354-232-7_x.jpg)
388
+
389
+ - Created by [Ai Minatogawa](https://llminatoll.github.io/)
390
+ - She is an energetic creator!
391
+ - Japanese version only
392
+ - English version will...?
393
+
394
+ # Official documents
395
+ ## Core Reporting API - Reference Guide
396
+ - https://developers.google.com/analytics/devguides/reporting/core/v3/reference
397
+
398
+ ## Dimensions & Metrics Explorer
399
+ - https://developers.google.com/analytics/devguides/reporting/core/dimsmets
400
+
401
+ ## Query Explorer
402
+ - https://ga-dev-tools.appspot.com/query-explorer/
403
+
404
+ ## API Rate Limits
405
+ - https://developers.google.com/analytics/devguides/reporting/core/v3/limits-quotas
406
+
407
+ # TODO
408
+ - can input not only `profile_name` but also `profile id (view id)`
409
+ - implement following query parametes
410
+ - offset
411
+ - quota_user
412
+ - segment_id
413
+
414
+ # Development
415
+ 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.
416
+
417
+ 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).
418
+
419
+ # Contributing
420
+ Bug reports and pull requests are welcome on GitHub at https://github.com/corselia/simple-ga-reporting. 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.
421
+
422
+ # License
423
+ The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
424
+
425
+ # Code of Conduct
426
+ Everyone interacting in the SimpleGaReporting project’s codebases, issue trackers, chat rooms and mailing lists is
427
+ expected to follow the [code of conduct](https://github.com/corselia/simple-ga-reporting/blob/master/CODE_OF_CONDUCT.md).
data/Rakefile ADDED
@@ -0,0 +1,6 @@
1
+ require "bundler/gem_tasks"
2
+ require "rspec/core/rake_task"
3
+
4
+ RSpec::Core::RakeTask.new(:spec)
5
+
6
+ task :default => :spec
data/bin/console ADDED
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "bundler/setup"
4
+ require "simple_ga_reporting"
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
@@ -0,0 +1,8 @@
1
+ #!/usr/bin/env bash
2
+ set -euo pipefail
3
+ IFS=$'\n\t'
4
+ set -vx
5
+
6
+ bundle install
7
+
8
+ # Do any other automated setup that you need to do here
data/config/filters.rb ADDED
@@ -0,0 +1,19 @@
1
+ module Filters
2
+ def my_awesome_filter
3
+ end
4
+
5
+ def his_awesome_filter
6
+ end
7
+
8
+ def her_awesome_filter
9
+ end
10
+
11
+ def chrome_or_fx
12
+ filter :chrome_or_fx, &lambda { contains(:browser, 'Chrome|Firefox') }
13
+ # filter :chrome_or_fx, &lambda { ['Chrome', 'Firefox'].map { |browser| matches(:browser, browser) } }
14
+ end
15
+
16
+ def over_one_thousand_search_sessions
17
+ filter :over_one_thousand_search_sessions, &lambda { gte(:searchSessions, 1000) }
18
+ end
19
+ end
@@ -0,0 +1,17 @@
1
+ profile_name: my_profile_name
2
+ start_date: 2018-04-01
3
+ end_date: 2018-04-05
4
+ metrics:
5
+ - users
6
+ - pageviews
7
+ dimensions:
8
+ - pagePath
9
+ - pageTitle
10
+ sort:
11
+ - -pageviews
12
+ - -users
13
+ filters:
14
+ - my_awesome_filter
15
+ - his_awesome_filter
16
+ - her_awesome_filter
17
+ limit: 20
@@ -0,0 +1,2 @@
1
+ private_key: "-----BEGIN PRIVATE KEY-----\nMIIE..........Eqw==\n-----END PRIVATE KEY-----\n"
2
+ client_email: "your_account_name@foobar.iam.gserviceaccount.com"
Binary file
Binary file
Binary file
Binary file
Binary file
Binary file
Binary file
Binary file
Binary file
@@ -0,0 +1,2 @@
1
+ require "simple_ga_reporting/version"
2
+ require 'simple_ga_reporting/simple_ga_reports'
@@ -0,0 +1,15 @@
1
+ require $filters_file
2
+
3
+ class LegatoGaModel
4
+ extend Legato::Model
5
+ extend Filters
6
+
7
+ # TODO: oh...global variable...
8
+ dimensions(*$model_config['dimensions'])
9
+ metrics(*$model_config['metrics'])
10
+
11
+ # TODO: 'Filters' is hard coding
12
+ Filters.instance_methods.each do |symbol_method_name|
13
+ send(symbol_method_name.to_s)
14
+ end
15
+ end
@@ -0,0 +1,49 @@
1
+ require 'legato'
2
+ require 'oauth2'
3
+ require 'signet/oauth_2/client'
4
+
5
+ module LegatoGaUser
6
+ SCOPE = 'https://www.googleapis.com/auth/analytics.readonly'.freeze
7
+ TOKEN_CREDENTIAL_URI = 'https://accounts.google.com/o/oauth2/token'.freeze
8
+ AUDIENCE = 'https://accounts.google.com/o/oauth2/token'.freeze
9
+ AUTHORIZE_URL = 'https://accounts.google.com/o/oauth2/auth'.freeze
10
+ TOKEN_URL = 'https://accounts.google.com/o/oauth2/token'.freeze
11
+
12
+ # private
13
+ def create_ga_user(key_and_email_file='config/key_and_email.yml')
14
+ signing_key = OpenSSL::PKey::RSA.new(private_key(key_and_email_file))
15
+ auth_client = Signet::OAuth2::Client.new(
16
+ token_credential_uri: TOKEN_CREDENTIAL_URI,
17
+ audience: AUDIENCE,
18
+ scope: SCOPE,
19
+ issuer: client_email(key_and_email_file),
20
+ signing_key: signing_key,
21
+ sub: client_email(key_and_email_file),
22
+ )
23
+ access_token = auth_client.fetch_access_token!
24
+
25
+ oauth_client = OAuth2::Client.new('', '', {
26
+ authorize_url: AUTHORIZE_URL,
27
+ token_url: TOKEN_URL,
28
+ })
29
+
30
+ token = OAuth2::AccessToken.new(
31
+ oauth_client,
32
+ access_token['access_token'],
33
+ expires_in: access_token['expires_in'],
34
+ )
35
+
36
+ ga_user = Legato::User.new(token)
37
+ ga_user.access_token.expired?
38
+
39
+ ga_user
40
+ end
41
+
42
+ def private_key(key_and_email_file)
43
+ YAML.load_file(key_and_email_file)['private_key']
44
+ end
45
+
46
+ def client_email(key_and_email_file)
47
+ YAML.load_file(key_and_email_file)['client_email']
48
+ end
49
+ end
@@ -0,0 +1,84 @@
1
+ require 'simple_ga_reporting/legato_ga_user'
2
+
3
+ class SimpleGaReports
4
+ extend LegatoGaUser
5
+ end
6
+
7
+ class << SimpleGaReports
8
+ def configure(yaml_file='config/ga_reporting_config.yml', filters_file='config/filters.rb', **options)
9
+ # TODO: oh... global variable...
10
+ $model_config = YAML.load_file(yaml_file)
11
+ $filters_file = filters_file
12
+
13
+ require 'simple_ga_reporting/legato_ga_model'
14
+ query_parameters($model_config, options)
15
+ end
16
+
17
+ def filtered_results(key_and_email_file)
18
+ raw_results = raw_results(key_and_email_file)
19
+ filtering(raw_results)
20
+ end
21
+
22
+ def raw_results(key_and_email_file)
23
+ # 'legato_ga_model' method is generated by Model class automatically
24
+ query = ga_profile(key_and_email_file).legato_ga_model
25
+
26
+ query.results(
27
+ start_date: @start_date,
28
+ end_date: @end_date,
29
+ limit: @limit,
30
+ # offset: @offset,
31
+ sort: @sort,
32
+ # quota_user: @quota_user,
33
+ sampling_level: @sampling_level,
34
+ # segment_id: @segment_id,
35
+ )
36
+ end
37
+
38
+ def filtering(results)
39
+ if @filters
40
+ @filters.each do |filter_symbol_name|
41
+ results = results.send(filter_symbol_name) # TODO: oh... send method...
42
+ end
43
+ end
44
+
45
+ results
46
+ end
47
+
48
+ # TODO: should specify by profile id, too
49
+ def ga_profile(key_and_email_file)
50
+ ga_user = create_ga_user(key_and_email_file)
51
+
52
+ ga_user.profiles.each do |profile|
53
+ return profile if profile.name === @profile_name
54
+ end
55
+ end
56
+
57
+ # HACK: not cool descriptions
58
+ def query_parameters(model_config, **options)
59
+ @start_date = options[:start_date] ? options[:start_date] : model_config['start_date']
60
+ @end_date = options[:end_date] ? options[:end_date] : model_config['end_date']
61
+ @limit = options[:limit] ? options[:limit] : model_config['limit']
62
+ # @offset = options[:offset] ? options[:offset] : model_config['offset']
63
+ @sort = options[:sort] ? options[:sort] : model_config['sort'] # Array
64
+ # @quota_user = options[:quota_user] ? options[:quota_user] : model_config['quota_user']
65
+ @sampling_level = options[:sampling_level] ? options[:sampling_level] : 'HIGHER_PRECISION'
66
+ # @segment_id = options[:segment_id] ? options[:segment_id] : model_config['segment_id']
67
+ @filters = options[:filters] ? options[:filters] : model_config['filters'] # Array
68
+ @profile_name = options[:profile_name] ? options[:profile_name] : model_config['profile_name']
69
+
70
+ # for RSpec (ummm... not good)
71
+ {
72
+ start_date: @start_date,
73
+ end_date: @end_date,
74
+ limit: @limit,
75
+ offset: @offset,
76
+ sort: @sort,
77
+ quota_user: @quota_user,
78
+ sampling_level: @sampling_level,
79
+ segment_id: @segment_id,
80
+ filters: @filters,
81
+ profile_name: @profile_name,
82
+ }
83
+ end
84
+ end
@@ -0,0 +1,3 @@
1
+ module SimpleGaReporting
2
+ VERSION = '1.0.0'
3
+ end
@@ -0,0 +1,32 @@
1
+ lib = File.expand_path("../lib", __FILE__)
2
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
3
+ require "simple_ga_reporting/version"
4
+
5
+ Gem::Specification.new do |spec|
6
+ spec.name = "simple_ga_reporting"
7
+ spec.version = SimpleGaReporting::VERSION
8
+ spec.authors = ["Osamu Takiya"]
9
+ spec.email = ["takiya@toran.sakura.ne.jp"]
10
+
11
+ spec.summary = %q{With simple configuration (YAML), you can obtain Google Analytics reporting data}
12
+ spec.description = %q{This gem is powered by Legato gem. First of all, you create configuration YAML file as Google Analytics API. Then execute the simple Ruby code, and you obtain Google Analytics Reporting Data. Very simple.}
13
+ spec.homepage = "https://github.com/corselia/simple-ga-reporting"
14
+ spec.license = "MIT"
15
+
16
+ spec.files = `git ls-files -z`.split("\x0").reject do |f|
17
+ f.match(%r{^(test|spec|features)/})
18
+ end
19
+ spec.bindir = "exe"
20
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
21
+ spec.require_paths = ["lib"]
22
+
23
+ spec.add_development_dependency "bundler"
24
+ spec.add_development_dependency "rake"
25
+ spec.add_development_dependency "rspec"
26
+ spec.add_development_dependency "guard-rspec"
27
+ spec.add_development_dependency "rspec_junit_formatter" # 'cannot load such file -- rspec_junit_formatter'
28
+
29
+ spec.add_dependency "legato"
30
+ spec.add_dependency "oauth2"
31
+ spec.add_dependency "signet"
32
+ end
metadata ADDED
@@ -0,0 +1,190 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: simple_ga_reporting
3
+ version: !ruby/object:Gem::Version
4
+ version: 1.0.0
5
+ platform: ruby
6
+ authors:
7
+ - Osamu Takiya
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2018-04-06 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: '0'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ">="
25
+ - !ruby/object:Gem::Version
26
+ version: '0'
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: rspec
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: guard-rspec
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: rspec_junit_formatter
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: legato
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
+ - !ruby/object:Gem::Dependency
98
+ name: oauth2
99
+ requirement: !ruby/object:Gem::Requirement
100
+ requirements:
101
+ - - ">="
102
+ - !ruby/object:Gem::Version
103
+ version: '0'
104
+ type: :runtime
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: signet
113
+ requirement: !ruby/object:Gem::Requirement
114
+ requirements:
115
+ - - ">="
116
+ - !ruby/object:Gem::Version
117
+ version: '0'
118
+ type: :runtime
119
+ prerelease: false
120
+ version_requirements: !ruby/object:Gem::Requirement
121
+ requirements:
122
+ - - ">="
123
+ - !ruby/object:Gem::Version
124
+ version: '0'
125
+ description: This gem is powered by Legato gem. First of all, you create configuration
126
+ YAML file as Google Analytics API. Then execute the simple Ruby code, and you obtain
127
+ Google Analytics Reporting Data. Very simple.
128
+ email:
129
+ - takiya@toran.sakura.ne.jp
130
+ executables: []
131
+ extensions: []
132
+ extra_rdoc_files: []
133
+ files:
134
+ - ".circleci/config.yml"
135
+ - ".gitignore"
136
+ - ".rspec"
137
+ - CODE_OF_CONDUCT.md
138
+ - Gemfile
139
+ - LICENSE
140
+ - README.md
141
+ - Rakefile
142
+ - bin/console
143
+ - bin/setup
144
+ - config/filters.rb
145
+ - config/sample_ga_reporting_config.yml
146
+ - config/sample_key_and_email.yml
147
+ - images/config_filter_by_gui.png
148
+ - images/dimensions_and_metrics_explorer.png
149
+ - images/ga_add_api_user.png
150
+ - images/ga_profile(view).png
151
+ - images/ga_query_parametes_summary.png
152
+ - images/gcp_activate_api_01.png
153
+ - images/gcp_activate_api_02.png
154
+ - images/gcp_activate_api_03.png
155
+ - images/gcp_api_auth_info.png
156
+ - images/gcp_api_menu.png
157
+ - images/gcp_download_json_key.png
158
+ - images/gcp_private_key_and_client_email.png
159
+ - lib/simple_ga_reporting.rb
160
+ - lib/simple_ga_reporting/legato_ga_model.rb
161
+ - lib/simple_ga_reporting/legato_ga_user.rb
162
+ - lib/simple_ga_reporting/simple_ga_reports.rb
163
+ - lib/simple_ga_reporting/version.rb
164
+ - simple_ga_reporting.gemspec
165
+ homepage: https://github.com/corselia/simple-ga-reporting
166
+ licenses:
167
+ - MIT
168
+ metadata: {}
169
+ post_install_message:
170
+ rdoc_options: []
171
+ require_paths:
172
+ - lib
173
+ required_ruby_version: !ruby/object:Gem::Requirement
174
+ requirements:
175
+ - - ">="
176
+ - !ruby/object:Gem::Version
177
+ version: '0'
178
+ required_rubygems_version: !ruby/object:Gem::Requirement
179
+ requirements:
180
+ - - ">="
181
+ - !ruby/object:Gem::Version
182
+ version: '0'
183
+ requirements: []
184
+ rubyforge_project:
185
+ rubygems_version: 2.7.3
186
+ signing_key:
187
+ specification_version: 4
188
+ summary: With simple configuration (YAML), you can obtain Google Analytics reporting
189
+ data
190
+ test_files: []